Files
firefly-iii/app/Support/Preferences.php

330 lines
11 KiB
PHP
Raw Normal View History

2015-02-06 20:43:19 +01:00
<?php
2022-12-29 19:42:26 +01:00
/**
* Preferences.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-02-06 20:43:19 +01:00
namespace FireflyIII\Support;
2021-04-06 17:00:16 +02:00
use FireflyIII\Exceptions\FireflyException;
2015-02-11 07:35:10 +01:00
use FireflyIII\Models\Preference;
use FireflyIII\User;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Encryption\EncryptException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;
2015-06-03 21:25:11 +02:00
2015-02-06 20:43:19 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class Preferences.
2015-02-06 20:43:19 +01:00
*/
class Preferences
{
public function all(): Collection
{
$user = auth()->user();
if (null === $user) {
2022-10-30 14:24:37 +01:00
return new Collection();
}
return Preference::where('user_id', $user->id)
2025-02-20 07:28:24 +01:00
->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();
}
2025-02-20 07:28:24 +01:00
public function get(string $name, null | array | bool | int | string $default = null): ?Preference
2015-02-06 20:43:19 +01:00
{
2023-12-20 19:35:52 +01:00
/** @var null|User $user */
2023-06-21 12:34:58 +02:00
$user = auth()->user();
if (null === $user) {
$preference = new Preference();
$preference->data = $default;
return $preference;
}
return $this->getForUser($user, $name, $default);
2020-10-24 07:18:37 +02:00
}
2025-02-20 07:28:24 +01:00
public function getForUser(User $user, string $name, null | array | bool | int | string $default = null): ?Preference
2023-06-21 12:34:58 +02:00
{
2025-02-20 07:28:24 +01:00
Log::debug(sprintf('getForUser(#%d, "%s")', $user->id, $name));
// don't care about user group ID, except for some specific preferences.
$userGroupId = $this->getUserGroupId($user, $name);
2025-02-20 07:28:24 +01:00
$query = Preference::where('user_id', $user->id)->where('name', $name);
if (null !== $userGroupId) {
Log::debug('Include user group ID in query');
$query->where('user_group_id', $userGroupId);
}
$preference = $query->first(['id', 'user_id', 'user_group_id', 'name', 'data', 'updated_at', 'created_at']);
2023-06-21 12:34:58 +02:00
if (null !== $preference && null === $preference->data) {
$preference->delete();
$preference = null;
2025-02-20 07:28:24 +01:00
Log::debug('Removed empty preference.');
2023-06-21 12:34:58 +02:00
}
if (null !== $preference) {
2025-02-20 07:28:24 +01:00
Log::debug(sprintf('Found preference #%d for user #%d: %s', $preference->id, $user->id, $name));
2023-06-21 12:34:58 +02:00
return $preference;
}
// no preference found and default is null:
if (null === $default) {
2025-02-20 07:28:24 +01:00
Log::debug('Return NULL, create no preference.');
2023-06-21 12:34:58 +02:00
// return NULL
return null;
}
return $this->setForUser($user, $name, $default);
}
private function getUserGroupId(User $user, string $preferenceName): ?int
{
$groupId = null;
$items = config('firefly.admin_specific_prefs') ?? [];
if (in_array($preferenceName, $items, true)) {
2024-12-22 08:43:12 +01:00
$groupId = (int) $user->user_group_id;
}
return $groupId;
}
2022-12-29 19:42:26 +01:00
public function delete(string $name): bool
{
$fullName = sprintf('preference%s%s', auth()->user()->id, $name);
if (Cache::has($fullName)) {
Cache::forget($fullName);
2022-12-29 19:42:26 +01:00
}
2022-12-30 20:38:54 +01:00
Preference::where('user_id', auth()->user()->id)->where('name', $name)->delete();
2022-12-29 19:42:26 +01:00
return true;
}
public function forget(User $user, string $name): void
{
$key = sprintf('preference%s%s', $user->id, $name);
Cache::forget($key);
Cache::put($key, '', 5);
2022-12-29 19:42:26 +01:00
}
2025-02-20 07:28:24 +01:00
public function setForUser(User $user, string $name, null | array | bool | int | string $value): Preference
2020-10-24 07:18:37 +02:00
{
2025-02-20 07:28:24 +01:00
$fullName = sprintf('preference%s%s', $user->id, $name);
$userGroupId = $this->getUserGroupId($user, $name);
$userGroupId = 0 === (int) $userGroupId ? null : (int) $userGroupId;
2024-12-22 13:19:23 +01:00
Cache::forget($fullName);
2025-02-20 07:28:24 +01:00
$query = Preference::where('user_id', $user->id)->where('name', $name);
if (null !== $userGroupId) {
Log::debug('Include user group ID in query');
$query->where('user_group_id', $userGroupId);
}
2025-02-20 07:28:24 +01:00
$preference = $query->first(['id', 'user_id', 'user_group_id', 'name', 'data', 'updated_at', 'created_at']);
if (null !== $preference && null === $value) {
$preference->delete();
2023-06-21 12:34:58 +02:00
return new Preference();
}
if (null === $value) {
return new Preference();
}
2025-02-20 07:28:24 +01:00
if (null === $preference) {
$preference = new Preference();
$preference->user_id = (int) $user->id;
$preference->user_group_id = $userGroupId;
$preference->name = $name;
2024-12-22 13:19:23 +01:00
2023-06-21 12:34:58 +02:00
}
2025-02-20 07:28:24 +01:00
$preference->data = $value;
$preference->save();
Cache::forever($fullName, $preference);
2015-02-06 20:43:19 +01:00
2025-02-20 07:28:24 +01:00
return $preference;
2023-06-21 12:34:58 +02:00
}
public function beginsWith(User $user, string $search): Collection
{
$value = sprintf('%s%%', $search);
return Preference::where('user_id', $user->id)->whereLike('name', $value)->get();
2022-12-29 19:42:26 +01:00
}
/**
* Find by name, has no user ID in it, because the method is called from an unauthenticated route any way.
*/
2023-06-21 12:34:58 +02:00
public function findByName(string $name): Collection
{
return Preference::where('name', $name)->get();
}
2022-12-29 19:42:26 +01:00
public function getArrayForUser(User $user, array $list): array
{
$result = [];
$preferences = Preference::where('user_id', $user->id)
2025-02-20 07:28:24 +01:00
->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']);
2023-12-20 19:35:52 +01:00
2022-12-29 19:42:26 +01:00
/** @var Preference $preference */
foreach ($preferences as $preference) {
$result[$preference->name] = $preference->data;
2017-03-02 19:57:46 +01:00
}
2022-12-29 19:42:26 +01:00
foreach ($list as $name) {
if (!array_key_exists($name, $result)) {
$result[$name] = null;
}
2018-03-05 19:35:58 +01:00
}
2018-08-06 19:14:30 +02:00
2022-12-29 19:42:26 +01:00
return $result;
2016-01-20 15:23:36 +01:00
}
2025-01-04 15:16:11 +01:00
public function getEncrypted(string $name, mixed $default = null): ?Preference
2024-12-22 08:43:12 +01:00
{
$result = $this->get($name, $default);
if (null === $result) {
return null;
}
if ('' === $result->data) {
// Log::warning(sprintf('Empty encrypted preference found: "%s"', $name));
2024-12-22 08:43:12 +01:00
return $result;
}
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()));
2024-12-22 08:43:12 +01:00
return $result;
}
return $result;
}
2025-02-20 07:28:24 +01:00
public function getEncryptedForUser(User $user, string $name, null | array | bool | int | string $default = null): ?Preference
2024-12-22 08:43:12 +01:00
{
$result = $this->getForUser($user, $name, $default);
if ('' === $result->data) {
// Log::warning(sprintf('Empty encrypted preference found: "%s"', $name));
2024-12-22 08:43:12 +01:00
return $result;
}
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()));
2024-12-22 08:43:12 +01:00
return $result;
}
return $result;
}
2025-02-20 07:28:24 +01:00
public function getFresh(string $name, null | array | bool | int | string $default = null): ?Preference
2015-02-06 20:43:19 +01:00
{
2023-12-20 19:35:52 +01:00
/** @var null|User $user */
2016-09-16 12:15:58 +02:00
$user = auth()->user();
2017-11-15 12:25:49 +01:00
if (null === $user) {
2022-10-30 14:24:37 +01:00
$preference = new Preference();
$preference->data = $default;
2016-04-25 18:43:09 +02:00
return $preference;
}
return $this->getForUser($user, $name, $default);
}
2022-12-29 19:42:26 +01:00
/**
* @throws FireflyException
*/
public function lastActivity(): string
{
$lastActivity = microtime();
$preference = $this->get('lastActivity', microtime());
if (null !== $preference && null !== $preference->data) {
$lastActivity = $preference->data;
}
if (is_array($lastActivity)) {
$lastActivity = implode(',', $lastActivity);
}
2024-12-22 08:43:12 +01:00
return hash('sha256', (string) $lastActivity);
2022-12-29 19:42:26 +01:00
}
public function mark(): void
2020-10-24 07:18:37 +02:00
{
$this->set('lastActivity', microtime());
Session::forget('first');
2020-10-24 07:18:37 +02:00
}
2025-02-20 07:28:24 +01:00
public function set(string $name, null | array | bool | int | string $value): Preference
{
/** @var null|User $user */
$user = auth()->user();
if (null === $user) {
// make new preference, return it:
2022-10-30 14:24:37 +01:00
$pref = new Preference();
2021-04-06 18:48:02 +02:00
$pref->name = $name;
$pref->data = $value;
return $pref;
2021-04-06 18:36:37 +02:00
}
2015-06-03 22:11:50 +02:00
return $this->setForUser($user, $name, $value);
2015-02-06 20:43:19 +01:00
}
2024-12-22 20:32:58 +01:00
public function setEncrypted(string $name, mixed $value): Preference
{
try {
$encrypted = encrypt($value);
} catch (EncryptException $e) {
Log::error(sprintf('Could not encrypt preference "%s": %s', $name, $e->getMessage()));
throw new FireflyException(sprintf('Could not encrypt preference "%s". Cowardly refuse to continue.', $name));
}
return $this->set($name, $encrypted);
}
2015-03-29 08:14:32 +02:00
}