mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-01-08 07:03:23 -06:00
Expand Preferences to handle not logged in users.
This commit is contained in:
parent
1059c7e2be
commit
08b8bd27f9
@ -6,6 +6,7 @@ namespace FireflyIII\Support;
|
|||||||
use Auth;
|
use Auth;
|
||||||
use Cache;
|
use Cache;
|
||||||
use FireflyIII\Models\Preference;
|
use FireflyIII\Models\Preference;
|
||||||
|
use FireflyIII\User;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Preferences
|
* Class Preferences
|
||||||
@ -91,16 +92,34 @@ class Preferences
|
|||||||
*/
|
*/
|
||||||
public function set($name, $value)
|
public function set($name, $value)
|
||||||
{
|
{
|
||||||
$fullName = 'preference' . Auth::user()->id . $name;
|
$user = Auth::user();
|
||||||
|
if (is_null($user)) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->setForUser(Auth::user(), $name, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \FireflyIII\User $user
|
||||||
|
* @param $name
|
||||||
|
* @param string $value
|
||||||
|
*
|
||||||
|
* @return Preference
|
||||||
|
*/
|
||||||
|
public function setForUser(User $user, $name, $value)
|
||||||
|
{
|
||||||
|
$fullName = 'preference' . $user->id . $name;
|
||||||
Cache::forget($fullName);
|
Cache::forget($fullName);
|
||||||
$pref = Preference::where('user_id', Auth::user()->id)->where('name', $name)->first(['id', 'name', 'data_encrypted']);
|
$pref = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data_encrypted']);
|
||||||
if ($pref) {
|
|
||||||
|
if (!is_null($pref)) {
|
||||||
$pref->data = $value;
|
$pref->data = $value;
|
||||||
} else {
|
} else {
|
||||||
$pref = new Preference;
|
$pref = new Preference;
|
||||||
$pref->name = $name;
|
$pref->name = $name;
|
||||||
$pref->data = $value;
|
$pref->data = $value;
|
||||||
$pref->user()->associate(Auth::user());
|
$pref->user()->associate($user);
|
||||||
|
|
||||||
}
|
}
|
||||||
$pref->save();
|
$pref->save();
|
||||||
|
Loading…
Reference in New Issue
Block a user