koel/app/Models/User.php

132 lines
3.1 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Models;
2017-08-05 17:44:38 +00:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2015-12-27 09:12:10 +00:00
use Illuminate\Foundation\Auth\User as Authenticatable;
2016-09-26 06:30:00 +00:00
use Illuminate\Notifications\Notifiable;
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
{
2016-09-26 06:30:00 +00:00
use Notifiable;
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
*/
2018-08-24 15:27:19 +00:00
private const HIDDEN_PREFERENCES = ['lastfm_session_key'];
2015-12-20 12:17:35 +00:00
2015-12-13 04:42:28 +00:00
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',
];
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
2018-08-24 15:27:19 +00:00
public function playlists(): HasMany
2015-12-13 04:42:28 +00:00
{
return $this->hasMany(Playlist::class);
}
2018-08-24 15:27:19 +00:00
public function interactions(): HasMany
2015-12-13 04:42:28 +00:00
{
return $this->hasMany(Interaction::class);
}
2015-12-20 12:17:35 +00:00
/**
2018-08-24 15:27:19 +00:00
* @return mixed|null
2015-12-20 12:17:35 +00:00
*/
2018-08-24 15:27:19 +00:00
public function getPreference(string $key)
2015-12-20 12:17:35 +00:00
{
// We can't use $this->preferences directly, since the data has been tampered
// by getPreferencesAttribute().
return array_get((array) unserialize($this->attributes['preferences']), $key);
}
/**
2018-08-24 15:27:19 +00:00
* @param mixed $val
2015-12-20 12:17:35 +00:00
*/
2018-08-24 15:27:19 +00:00
public function savePreference(string $key, $val): void
2015-12-20 12:17:35 +00:00
{
$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
*
2018-08-24 15:27:19 +00:00
* @param mixed $val
2018-08-29 07:07:44 +00:00
*
2018-08-24 15:27:19 +00:00
* @see self::savePreference
2015-12-20 12:17:35 +00:00
*/
2018-08-24 15:27:19 +00:00
public function setPreference(string $key, $val): void
2015-12-20 12:17:35 +00:00
{
2018-08-24 15:27:19 +00:00
$this->savePreference($key, $val);
2015-12-20 12:17:35 +00:00
}
2018-08-24 15:27:19 +00:00
public function deletePreference(string $key): void
2015-12-20 12:17:35 +00:00
{
$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.
*/
2018-08-24 15:27:19 +00:00
public function connectedToLastfm(): bool
2015-12-21 13:49:00 +00:00
{
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
*/
2018-08-24 15:27:19 +00:00
public function getLastfmSessionKeyAttribute(): ?string
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.
*
2018-08-24 15:27:19 +00:00
* @param mixed[] $value
2015-12-20 12:17:35 +00:00
*/
2018-08-24 15:27:19 +00:00
public function setPreferencesAttribute(array $value): void
2015-12-20 12:17:35 +00:00
{
$this->attributes['preferences'] = serialize($value);
}
/**
* Unserialize the user preferences back to an array before returning.
*
2018-08-24 15:27:19 +00:00
* @return mixed[]
2015-12-20 12:17:35 +00:00
*/
2018-08-24 15:27:19 +00:00
public function getPreferencesAttribute(string $value): array
2015-12-20 12:17:35 +00:00
{
2017-06-04 08:34:21 +00:00
$preferences = unserialize($value) ?: [];
// Hide sensitive data from returned preferences.
2018-08-24 15:27:19 +00:00
foreach (self::HIDDEN_PREFERENCES as $key) {
2017-06-04 08:34:21 +00:00
if (array_key_exists($key, $preferences)) {
$preferences[$key] = 'hidden';
2015-12-20 12:17:35 +00:00
}
2017-06-04 08:34:21 +00:00
}
return $preferences;
2015-12-20 12:17:35 +00:00
}
2015-12-13 04:42:28 +00:00
}