Skip to content

Commit

Permalink
Allow to get/set settings for another user by id
Browse files Browse the repository at this point in the history
  • Loading branch information
leonjza committed Dec 2, 2015
1 parent 71504e9 commit 9a2fe0f
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,26 @@ abstract class Settings
/**
* Retreive a setting by name.
*
* @param $name
* @param $name
* @param null $for_id
*
* @return mixed
* @throws \Seat\Services\Exceptions\SettingException
*/
public static function get($name)
public static function get($name, $for_id = null)
{

return Cache::rememberForever(
self::get_key_prefix($name), function () use ($name) {
self::get_key_prefix($name), function () use ($name, $for_id) {

// Init a new MODEL
$value = (new static::$model);

// If we are not in the global scope, add a constraint
// to be user specific.
if (static::$scope != 'global')
$value = $value->where('user_id', auth()->user()->id);
$value = $value->where('user_id',
is_null($for_id) ? auth()->user()->id : $for_id);

// Retreive the value
$value = $value->where('name', $name)
Expand All @@ -99,12 +102,13 @@ public static function get($name)
}

/**
* @param $name
* @param $value
* @param $name
* @param $value
* @param null $for_id
*
* @throws \Seat\Services\Exceptions\SettingException
*/
public static function set($name, $value)
public static function set($name, $value, $for_id = null)
{

// Init a new MODEL
Expand All @@ -113,7 +117,8 @@ public static function set($name, $value)
// If we are not in the global scope, add a constraint
// to be user specific.
if (static::$scope != 'global')
$db = $db->where('user_id', auth()->user()->id);
$db = $db->where('user_id',
is_null($for_id) ? auth()->user()->id : $for_id);

// Retreive the value
$db = $db->where('name', $name)
Expand All @@ -132,7 +137,7 @@ public static function set($name, $value)
// Again, if we are not in the global context, then
// we need to constrain this setting to a user.
if (static::$scope != 'global')
$db->user_id = auth()->user()->id;
$db->user_id = is_null($for_id) ? auth()->user()->id : $for_id;

$db->save();

Expand Down

0 comments on commit 9a2fe0f

Please sign in to comment.