koel/app/Models/User.php

164 lines
3.5 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Models;
2015-12-27 09:12:10 +00:00
use Illuminate\Foundation\Auth\User as Authenticatable;
2015-12-13 04:42:28 +00:00
2016-02-03 15:39:15 +00:00
/**
2016-06-04 14:17:24 +00:00
* @property array preferences
* @property int id
* @property bool is_admin
* @property string lastfm_session_key
2016-02-03 15:39:15 +00:00
*/
2015-12-27 09:12:10 +00:00
class User extends Authenticatable
2015-12-13 04:42:28 +00:00
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
2015-12-20 12:17:35 +00:00
/**
* The preferences that we don't want to show to the client.
2016-03-06 04:11:28 +00:00
*
2015-12-20 12:17:35 +00:00
* @var array
*/
protected $hiddenPreferences = ['lastfm_session_key'];
2015-12-13 04:42:28 +00:00
/**
* The attributes that are protected from mass assign.
*
* @var array
*/
protected $guarded = ['id'];
protected $casts = [
2015-12-15 10:32:41 +00:00
'id' => 'int',
2015-12-13 04:42:28 +00:00
'is_admin' => 'bool',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
2015-12-14 13:22:39 +00:00
protected $hidden = ['password', 'remember_token', 'created_at', 'updated_at'];
2015-12-13 04:42:28 +00:00
public function playlists()
{
return $this->hasMany(Playlist::class);
}
public function interactions()
{
return $this->hasMany(Interaction::class);
}
2015-12-20 12:17:35 +00:00
/**
* Get a preference item of the current user.
2016-03-06 04:11:28 +00:00
*
* @param string $key
*
2015-12-20 12:17:35 +00:00
* @return string|null
*/
public function getPreference($key)
{
// We can't use $this->preferences directly, since the data has been tampered
// by getPreferencesAttribute().
return array_get((array) unserialize($this->attributes['preferences']), $key);
}
/**
* Save a user preference.
2016-03-06 04:11:28 +00:00
*
2015-12-20 12:17:35 +00:00
* @param string $key
* @param string $val
*/
public function savePreference($key, $val)
{
$preferences = $this->preferences;
$preferences[$key] = $val;
$this->preferences = $preferences;
2015-12-20 12:30:28 +00:00
2015-12-20 12:17:35 +00:00
$this->save();
}
/**
* An alias to savePreference().
2016-02-03 15:39:15 +00:00
*
2015-12-20 12:17:35 +00:00
* @see $this::savePreference
2016-02-03 15:39:15 +00:00
*
* @param $key
* @param $val
2015-12-20 12:17:35 +00:00
*/
public function setPreference($key, $val)
{
return $this->savePreference($key, $val);
}
/**
* Delete a preference.
2016-03-06 04:11:28 +00:00
*
2015-12-20 12:17:35 +00:00
* @param string $key
*/
public function deletePreference($key)
{
$preferences = $this->preferences;
array_forget($preferences, $key);
$this->update(compact('preferences'));
}
2015-12-21 13:49:00 +00:00
/**
* Determine if the user is connected to Last.fm.
2016-03-06 04:11:28 +00:00
*
2015-12-21 13:49:00 +00:00
* @return bool
*/
public function connectedToLastfm()
{
return (bool) $this->lastfm_session_key;
2015-12-21 13:49:00 +00:00
}
/**
* Get the user's Last.fm session key.
2016-03-06 04:11:28 +00:00
*
2015-12-21 13:49:00 +00:00
* @return string|null The key if found, or null if user isn't connected to Last.fm
*/
public function getLastfmSessionKeyAttribute()
2015-12-21 13:49:00 +00:00
{
return $this->getPreference('lastfm_session_key');
}
2015-12-20 12:17:35 +00:00
/**
* User preferences are stored as a serialized associative array.
*
* @param array $value
*/
public function setPreferencesAttribute($value)
{
$this->attributes['preferences'] = serialize($value);
}
/**
* Unserialize the user preferences back to an array before returning.
*
* @param string $value
*
* @return array
*/
public function getPreferencesAttribute($value)
{
$preferences = unserialize($value) ?: [];
// Hide the user's secrets away!
foreach ($this->hiddenPreferences as $key) {
if (isset($preferences[$key])) {
$preferences[$key] = 'hidden';
}
}
return $preferences;
}
2015-12-13 04:42:28 +00:00
}