Fine tune preferences to handle multi-administration options.

This commit is contained in:
James Cole
2024-04-01 19:59:21 +02:00
parent 75ea035630
commit f6e28dc88f
5 changed files with 134 additions and 174 deletions

View File

@@ -83,8 +83,23 @@ class Preference extends Model
/** @var User $user */
$user = auth()->user();
/** @var null|Preference $preference */
// some preferences do not have an administration ID.
// some need it, to make sure the correct one is selected.
$userGroupId = (int)$user->user_group_id;
$userGroupId = $userGroupId === 0 ? null : $userGroupId;
/** @var Preference|null $preference */
$preference = null;
$items = config('firefly.admin_specific_prefs');
if (null !== $userGroupId && in_array($value, $items, true)) {
// find a preference with a specific user_group_id
$preference = $user->preferences()->where('user_group_id', $userGroupId)->where('name', $value)->first();
}
if (!in_array($value, $items, true)) {
// find any one.
$preference = $user->preferences()->where('name', $value)->first();
}
// try again with ID, but this time don't care about the preferred user_group_id
if (null === $preference) {
$preference = $user->preferences()->where('id', (int)$value)->first();
}
@@ -97,6 +112,7 @@ class Preference extends Model
$preference->name = $value;
$preference->data = $default[$value];
$preference->user_id = (int)$user->id;
$preference->user_group_id = in_array($value, $items, true) ? $userGroupId : null;
$preference->save();
return $preference;

View File

@@ -28,6 +28,8 @@ use FireflyIII\Models\Preference;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Session;
/**
* Class Preferences.
@@ -42,24 +44,15 @@ class Preferences
}
return Preference::where('user_id', $user->id)
->where(function(Builder $q) use($user) {
->where(function (Builder $q) use ($user) {
$q->whereNull('user_group_id');
$q->orWhere('user_group_id', $user->user_group_id);
})
->get();
}
/**
* @param mixed $default
*
* @throws FireflyException
*/
public function get(string $name, $default = null): ?Preference
public function get(string $name, null | array | bool | int | string $default = null): ?Preference
{
if ('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
/** @var null|User $user */
$user = auth()->user();
if (null === $user) {
@@ -72,20 +65,12 @@ class Preferences
return $this->getForUser($user, $name, $default);
}
/**
* @throws FireflyException
*/
public function getForUser(User $user, string $name, null|array|bool|int|string $default = null): ?Preference
public function getForUser(User $user, string $name, null | array | bool | int | string $default = null): ?Preference
{
if ('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
$preference = Preference::
where(function(Builder $q) use($user) {
$q->whereNull('user_group_id');
$q->orWhere('user_group_id', $user->user_group_id);
})
->where('user_id', $user->id)->where('name', $name)->first(['id', 'user_id', 'name', 'data', 'updated_at', 'created_at']);
// don't care about user group ID, except for some specific preferences.
$userGroupId = $this->getUserGroupId($user, $name);
$preference = Preference::where('user_group_id', $userGroupId)->where('user_id', $user->id)->where('name', $name)->first(['id', 'user_id', 'name', 'data', 'updated_at', 'created_at']);
if (null !== $preference && null === $preference->data) {
$preference->delete();
$preference = null;
@@ -103,17 +88,11 @@ class Preferences
return $this->setForUser($user, $name, $default);
}
/**
* @throws FireflyException
*/
public function delete(string $name): bool
{
if ('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
$fullName = sprintf('preference%s%s', auth()->user()->id, $name);
if (\Cache::has($fullName)) {
\Cache::forget($fullName);
if (Cache::has($fullName)) {
Cache::forget($fullName);
}
Preference::where('user_id', auth()->user()->id)->where('name', $name)->delete();
@@ -122,32 +101,16 @@ class Preferences
public function forget(User $user, string $name): void
{
if ('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
$key = sprintf('preference%s%s', $user->id, $name);
\Cache::forget($key);
\Cache::put($key, '', 5);
Cache::forget($key);
Cache::put($key, '', 5);
}
/**
* @param mixed $value
*
* @throws FireflyException
*/
public function setForUser(User $user, string $name, $value): Preference
public function setForUser(User $user, string $name, null | array | bool | int | string $value): Preference
{
if ('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
$fullName = sprintf('preference%s%s', $user->id, $name);
\Cache::forget($fullName);
$groupId = null;
$items = config('firefly.admin_specific_prefs');
if(in_array($name, $items, true)) {
$groupId = (int)$user->user_group_id;
}
$groupId = $this->getUserGroupId($user, $name);
Cache::forget($fullName);
/** @var null|Preference $pref */
$pref = Preference::where('user_group_id', $groupId)->where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']);
@@ -167,35 +130,36 @@ class Preferences
$pref->name = $name;
}
$pref->data = $value;
try {
$pref->save();
} catch (\PDOException $e) {
throw new FireflyException(sprintf('Could not save preference: %s', $e->getMessage()), 0, $e);
}
\Cache::forever($fullName, $pref);
Cache::forever($fullName, $pref);
return $pref;
}
public function beginsWith(User $user, string $search): Collection
{
return Preference::where('user_id', $user->id)->where('name', 'LIKE', $search.'%')->get();
return Preference::where('user_id', $user->id)->where('name', 'LIKE', $search . '%')->get();
}
/**
* Find by name, has no user ID in it, because the method is called from an unauthenticated route any way.
*/
public function findByName(string $name): Collection
{
if ('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
return Preference::where('name', $name)->get();
}
public function getArrayForUser(User $user, array $list): array
{
$result = [];
$preferences = Preference::where('user_id', $user->id)->whereIn('name', $list)->get(['id', 'name', 'data']);
$preferences = Preference
::where('user_id', $user->id)
->where(function (Builder $q) use ($user) {
$q->whereNull('user_group_id');
$q->orWhere('user_group_id', $user->user_group_id);
})
->whereIn('name', $list)
->get(['id', 'name', 'data']);
/** @var Preference $preference */
foreach ($preferences as $preference) {
@@ -210,17 +174,8 @@ class Preferences
return $result;
}
/**
* @param mixed $default
*
* @throws FireflyException
*/
public function getFresh(string $name, $default = null): ?Preference
public function getFresh(string $name, null | array | bool | int | string $default = null): ?Preference
{
if ('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
/** @var null|User $user */
$user = auth()->user();
if (null === $user) {
@@ -230,22 +185,6 @@ class Preferences
return $preference;
}
return $this->getFreshForUser($user, $name, $default);
}
/**
* TODO remove me.
*
* @param null $default
*
* @throws FireflyException
*/
public function getFreshForUser(User $user, string $name, $default = null): ?Preference
{
if ('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
return $this->getForUser($user, $name, $default);
}
@@ -270,19 +209,12 @@ class Preferences
public function mark(): void
{
$this->set('lastActivity', microtime());
\Session::forget('first');
Session::forget('first');
}
/**
* @param mixed $value
*
* @throws FireflyException
*/
public function set(string $name, $value): Preference
public function set(string $name, null | array | bool | int | string $value): Preference
{
if ('currencyPreference' === $name) {
throw new FireflyException('No longer supports "currencyPreference", please refactor me.');
}
/** @var User $user */
$user = auth()->user();
if (null === $user) {
// make new preference, return it:
@@ -295,4 +227,14 @@ class Preferences
return $this->setForUser($user, $name, $value);
}
private function getUserGroupId(User $user, string $preferenceName): ?int
{
$groupId = null;
$items = config('firefly.admin_specific_prefs');
if (in_array($preferenceName, $items, true)) {
$groupId = (int)$user->user_group_id;
}
return $groupId;
}
}

View File

@@ -35,10 +35,12 @@ class PreferenceTransformer extends AbstractTransformer
*/
public function transform(Preference $preference): array
{
$userGroupId = $preference->user_group_id === 0 ? null : $preference->user_group_id;
return [
'id' => $preference->id,
'created_at' => $preference->created_at->toAtomString(),
'updated_at' => $preference->updated_at->toAtomString(),
'user_group_id' => $userGroupId,
'name' => $preference->name,
'data' => $preference->data,
];

View File

@@ -216,7 +216,7 @@ return [
],
// administration specific preferences
'admin_specific_prefs' => ['frontpageAccounts'],
'admin_specific_prefs' => ['frontPageAccounts', 'lastActivity'],
// default user-related values
'darkMode' => 'browser',