koel/app/Models/User.php

166 lines
3.9 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
2015-12-14 13:22:39 +00:00
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
2015-12-13 04:42:28 +00:00
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
2015-12-14 13:22:39 +00:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\Access\Authorizable;
2015-12-13 04:42:28 +00:00
2015-12-14 13:22:39 +00:00
class User extends Model implements
AuthenticatableContract,
2015-12-13 04:42:28 +00:00
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
/**
* 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.
*
* @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.
*
* @param string $key
*
* @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.
*
* @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().
*
* @see $this::savePreference
*/
public function setPreference($key, $val)
{
return $this->savePreference($key, $val);
}
/**
* Delete a preference.
*
* @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.
*
* @return bool
*/
public function connectedToLastfm()
{
2015-12-21 13:50:26 +00:00
return (bool) $this->getLastfmSessionKey();
2015-12-21 13:49:00 +00:00
}
/**
* Get the user's Last.fm session key.
*
* @return string|null The key if found, or null if user isn't connected to Last.fm
*/
public function getLastfmSessionKey()
{
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
}