Fix various issues with new features, nullpointers and missing checks.

This commit is contained in:
James Cole
2024-12-31 08:05:25 +01:00
parent e4aa218b5f
commit 9e34314dbc
9 changed files with 55 additions and 51 deletions

View File

@@ -47,16 +47,15 @@ class Preferences
}
return Preference::where('user_id', $user->id)
->where('name', '!=', 'currencyPreference')
->where(function (Builder $q) use ($user): void {
$q->whereNull('user_group_id');
$q->orWhere('user_group_id', $user->user_group_id);
})
->get()
;
->where('name', '!=', 'currencyPreference')
->where(function (Builder $q) use ($user): void {
$q->whereNull('user_group_id');
$q->orWhere('user_group_id', $user->user_group_id);
})
->get();
}
public function get(string $name, null|array|bool|int|string $default = null): ?Preference
public function get(string $name, null | array | bool | int | string $default = null): ?Preference
{
/** @var null|User $user */
$user = auth()->user();
@@ -70,7 +69,7 @@ class Preferences
return $this->getForUser($user, $name, $default);
}
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
{
// don't care about user group ID, except for some specific preferences.
$userGroupId = $this->getUserGroupId($user, $name);
@@ -122,16 +121,16 @@ class Preferences
Cache::put($key, '', 5);
}
public function setForUser(User $user, string $name, null|array|bool|int|string $value): Preference
public function setForUser(User $user, string $name, null | array | bool | int | string $value): Preference
{
$fullName = sprintf('preference%s%s', $user->id, $name);
$groupId = $this->getUserGroupId($user, $name);
$groupId = 0 === (int) $groupId ? null : (int) $groupId;
$fullName = sprintf('preference%s%s', $user->id, $name);
$groupId = $this->getUserGroupId($user, $name);
$groupId = 0 === (int) $groupId ? null : (int) $groupId;
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']);
$pref = Preference::where('user_group_id', $groupId)->where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']);
if (null !== $pref && null === $value) {
$pref->delete();
@@ -174,13 +173,12 @@ class Preferences
{
$result = [];
$preferences = Preference::where('user_id', $user->id)
->where(function (Builder $q) use ($user): void {
$q->whereNull('user_group_id');
$q->orWhere('user_group_id', $user->user_group_id);
})
->whereIn('name', $list)
->get(['id', 'name', 'data'])
;
->where(function (Builder $q) use ($user): void {
$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,6 +208,10 @@ class Preferences
try {
$result->data = decrypt($result->data);
} catch (DecryptException $e) {
if ('The MAC is invalid.' === $e->getMessage()) {
Log::debug('Set data to NULL');
$result->data = null;
}
Log::error(sprintf('Could not decrypt preference "%s": %s', $name, $e->getMessage()));
return $result;
@@ -218,7 +220,7 @@ class Preferences
return $result;
}
public function getEncryptedForUser(User $user, string $name, null|array|bool|int|string $default = null): ?Preference
public function getEncryptedForUser(User $user, string $name, null | array | bool | int | string $default = null): ?Preference
{
$result = $this->getForUser($user, $name, $default);
if ('' === $result->data) {
@@ -230,6 +232,10 @@ class Preferences
try {
$result->data = decrypt($result->data);
} catch (DecryptException $e) {
if ('The MAC is invalid.' === $e->getMessage()) {
Log::debug('Set data to NULL');
$result->data = null;
}
Log::error(sprintf('Could not decrypt preference "%s": %s', $name, $e->getMessage()));
return $result;
@@ -239,7 +245,7 @@ class Preferences
return $result;
}
public function getFresh(string $name, null|array|bool|int|string $default = null): ?Preference
public function getFresh(string $name, null | array | bool | int | string $default = null): ?Preference
{
/** @var null|User $user */
$user = auth()->user();
@@ -277,7 +283,7 @@ class Preferences
Session::forget('first');
}
public function set(string $name, null|array|bool|int|string $value): Preference
public function set(string $name, null | array | bool | int | string $value): Preference
{
/** @var null|User $user */
$user = auth()->user();