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
|
|
|
/**
|
2018-09-03 12:41:49 +00:00
|
|
|
* @property array $preferences
|
|
|
|
* @property int $id
|
|
|
|
* @property bool $is_admin
|
2018-09-04 06:25:24 +00:00
|
|
|
* @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-09-04 06:25:24 +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-09-04 06:25:24 +00:00
|
|
|
/**
|
|
|
|
* @return mixed|null
|
|
|
|
*/
|
|
|
|
public function getPreference(string $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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $val
|
|
|
|
*/
|
|
|
|
public function savePreference(string $key, $val): void
|
|
|
|
{
|
|
|
|
$preferences = $this->preferences;
|
|
|
|
$preferences[$key] = $val;
|
|
|
|
$this->preferences = $preferences;
|
|
|
|
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An alias to savePreference().
|
|
|
|
*
|
|
|
|
* @param mixed $val
|
|
|
|
*
|
|
|
|
* @see self::savePreference
|
|
|
|
*/
|
|
|
|
public function setPreference(string $key, $val): void
|
|
|
|
{
|
|
|
|
$this->savePreference($key, $val);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function deletePreference(string $key): void
|
|
|
|
{
|
|
|
|
$preferences = $this->preferences;
|
|
|
|
array_forget($preferences, $key);
|
|
|
|
|
|
|
|
$this->update(compact('preferences'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the user is connected to Last.fm.
|
|
|
|
*/
|
|
|
|
public function connectedToLastfm(): bool
|
|
|
|
{
|
|
|
|
return (bool) $this->lastfm_session_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 getLastfmSessionKeyAttribute(): ?string
|
|
|
|
{
|
|
|
|
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-29 08:01:53 +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
|
|
|
}
|