2015-08-30 13:22:00 +02:00
|
|
|
<?php
|
|
|
|
|
2015-10-25 06:17:45 +01:00
|
|
|
/**
|
|
|
|
* Pony.fm - A community for pony fan music.
|
|
|
|
* Copyright (C) 2015 Peter Deltchev
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-01-01 01:12:30 +01:00
|
|
|
namespace Poniverse\Ponyfm\Models;
|
2015-08-31 14:35:47 +02:00
|
|
|
|
|
|
|
use Gravatar;
|
2015-08-30 15:01:12 +02:00
|
|
|
use Illuminate\Auth\Authenticatable;
|
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword;
|
2015-08-31 13:19:29 +02:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
|
2015-08-30 13:22:00 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2015-11-24 06:19:23 +01:00
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable;
|
2015-12-25 03:08:49 +01:00
|
|
|
use Auth;
|
2015-08-30 14:29:12 +02:00
|
|
|
use Illuminate\Support\Str;
|
2016-01-16 05:10:20 +01:00
|
|
|
use Poniverse\Ponyfm\Traits\IndexedInElasticsearchTrait;
|
2015-11-24 12:07:43 +01:00
|
|
|
use Venturecraft\Revisionable\RevisionableTrait;
|
2015-08-30 13:22:00 +02:00
|
|
|
|
2016-01-01 01:36:08 +01:00
|
|
|
/**
|
|
|
|
* Poniverse\Ponyfm\Models\User
|
|
|
|
*
|
|
|
|
* @property integer $id
|
|
|
|
* @property string $display_name
|
|
|
|
* @property string $username
|
|
|
|
* @property boolean $sync_names
|
|
|
|
* @property string $email
|
|
|
|
* @property string $gravatar
|
|
|
|
* @property string $slug
|
|
|
|
* @property boolean $uses_gravatar
|
|
|
|
* @property boolean $can_see_explicit_content
|
|
|
|
* @property string $bio
|
|
|
|
* @property integer $track_count
|
|
|
|
* @property integer $comment_count
|
|
|
|
* @property \Carbon\Carbon $created_at
|
|
|
|
* @property \Carbon\Carbon $updated_at
|
|
|
|
* @property integer $avatar_id
|
|
|
|
* @property string $remember_token
|
|
|
|
* @property boolean $is_archived
|
|
|
|
* @property \Carbon\Carbon $disabled_at
|
|
|
|
* @property-read \Poniverse\Ponyfm\Models\Image $avatar
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Poniverse\Ponyfm\Models\ResourceUser[] $users
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Poniverse\Ponyfm\Models\Role[] $roles
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Poniverse\Ponyfm\Models\Comment[] $comments
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Poniverse\Ponyfm\Models\Track[] $tracks
|
|
|
|
* @property-read mixed $url
|
|
|
|
* @property-read mixed $message_url
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Venturecraft\Revisionable\Revision[] $revisionHistory
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\Poniverse\Ponyfm\Models\User userDetails()
|
|
|
|
*/
|
2015-11-24 06:19:23 +01:00
|
|
|
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, \Illuminate\Contracts\Auth\Access\Authorizable
|
2015-08-30 13:22:00 +02:00
|
|
|
{
|
2016-01-16 05:10:20 +01:00
|
|
|
use Authenticatable, CanResetPassword, Authorizable, RevisionableTrait, IndexedInElasticsearchTrait;
|
2016-01-07 19:16:37 +01:00
|
|
|
|
|
|
|
protected $elasticsearchType = 'user';
|
2015-08-30 15:01:12 +02:00
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
protected $table = 'users';
|
2015-11-09 20:35:30 +01:00
|
|
|
protected $casts = [
|
|
|
|
'id' => 'integer',
|
|
|
|
'sync_names' => 'boolean',
|
|
|
|
'uses_gravatar' => 'boolean',
|
|
|
|
'can_see_explicit_content' => 'boolean',
|
|
|
|
'track_count' => 'integer',
|
|
|
|
'comment_count' => 'integer',
|
|
|
|
'avatar_id' => 'integer',
|
|
|
|
'is_archived' => 'boolean',
|
|
|
|
];
|
2015-12-29 17:48:35 +01:00
|
|
|
protected $dates = ['created_at', 'updated_at', 'disabled_at'];
|
|
|
|
protected $hidden = ['disabled_at'];
|
2015-08-30 14:29:12 +02:00
|
|
|
|
|
|
|
public function scopeUserDetails($query)
|
|
|
|
{
|
|
|
|
if (Auth::check()) {
|
|
|
|
$query->with([
|
|
|
|
'users' => function ($query) {
|
|
|
|
$query->whereUserId(Auth::user()->id);
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !$query;
|
|
|
|
}
|
|
|
|
|
2015-11-09 20:35:30 +01:00
|
|
|
/**
|
|
|
|
* @param string $username
|
|
|
|
* @param string $displayName
|
|
|
|
* @param string $email
|
|
|
|
* @return User
|
|
|
|
*/
|
|
|
|
public static function findOrCreate(string $username, string $displayName, string $email) {
|
|
|
|
$user = static::where('username', $username)
|
|
|
|
->where('is_archived', false)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if (null !== $user) {
|
|
|
|
return $user;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$user = new User;
|
|
|
|
|
|
|
|
$user->username = $username;
|
|
|
|
$user->display_name = $displayName;
|
|
|
|
$user->email = $email;
|
|
|
|
$user->uses_gravatar = true;
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
public function avatar()
|
|
|
|
{
|
2016-01-01 01:12:30 +01:00
|
|
|
return $this->belongsTo('Poniverse\Ponyfm\Models\Image');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function users()
|
|
|
|
{
|
2016-01-01 01:12:30 +01:00
|
|
|
return $this->hasMany('Poniverse\Ponyfm\Models\ResourceUser', 'artist_id');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2015-11-24 06:19:23 +01:00
|
|
|
public function roles()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Role::class, 'role_user');
|
|
|
|
}
|
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
public function comments()
|
|
|
|
{
|
2016-01-01 01:12:30 +01:00
|
|
|
return $this->hasMany('Poniverse\Ponyfm\Models\Comment', 'profile_id')->orderBy('created_at', 'desc');
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
2015-11-06 12:26:23 +01:00
|
|
|
public function tracks()
|
|
|
|
{
|
2016-01-01 01:12:30 +01:00
|
|
|
return $this->hasMany('Poniverse\Ponyfm\Models\Track', 'user_id');
|
2015-11-06 12:26:23 +01:00
|
|
|
}
|
|
|
|
|
2015-08-30 14:29:12 +02:00
|
|
|
public function getIsArchivedAttribute()
|
|
|
|
{
|
|
|
|
return (bool)$this->attributes['is_archived'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUrlAttribute()
|
|
|
|
{
|
2015-12-20 16:07:36 +01:00
|
|
|
return action('ArtistsController@getProfile', $this->slug);
|
2015-08-30 14:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getMessageUrlAttribute()
|
|
|
|
{
|
|
|
|
return 'http://mlpforums.com/index.php?app=members&module=messaging§ion=send&do=form&fromMemberID=' . $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAuthIdentifier()
|
|
|
|
{
|
|
|
|
return $this->getKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAuthPassword()
|
|
|
|
{
|
|
|
|
return $this->password_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getReminderEmail()
|
|
|
|
{
|
|
|
|
return $this->email;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setDisplayNameAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['display_name'] = $value;
|
|
|
|
$this->attributes['slug'] = Str::slug($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAvatarUrl($type = Image::NORMAL)
|
|
|
|
{
|
|
|
|
if (!$this->uses_gravatar) {
|
|
|
|
return $this->avatar->getUrl($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->email == "redacted@example.net") {
|
|
|
|
return Gravatar::getUrl($this->id . "", Image::$ImageTypes[$type]['width'], "identicon");
|
|
|
|
}
|
|
|
|
|
|
|
|
$email = $this->gravatar;
|
|
|
|
|
|
|
|
if (!strlen($email)) {
|
|
|
|
$email = $this->email;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Gravatar::getUrl($email, Image::$ImageTypes[$type]['width']);
|
|
|
|
}
|
|
|
|
|
2015-08-30 13:22:00 +02:00
|
|
|
/**
|
2015-08-30 14:29:12 +02:00
|
|
|
* Get the token value for the "remember me" session.
|
2015-08-30 13:22:00 +02:00
|
|
|
*
|
2015-08-30 14:29:12 +02:00
|
|
|
* @return string
|
2015-08-30 13:22:00 +02:00
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function getRememberToken()
|
|
|
|
{
|
|
|
|
return $this->remember_token;
|
|
|
|
}
|
2015-08-30 13:22:00 +02:00
|
|
|
|
|
|
|
/**
|
2015-08-30 14:29:12 +02:00
|
|
|
* Set the token value for the "remember me" session.
|
2015-08-30 13:22:00 +02:00
|
|
|
*
|
2015-08-30 14:29:12 +02:00
|
|
|
* @param string $value
|
|
|
|
* @return void
|
2015-08-30 13:22:00 +02:00
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function setRememberToken($value)
|
|
|
|
{
|
|
|
|
$this->remember_token = $value;
|
|
|
|
}
|
2015-08-30 13:22:00 +02:00
|
|
|
|
|
|
|
/**
|
2015-08-30 14:29:12 +02:00
|
|
|
* Get the column name for the "remember me" token.
|
2015-08-30 13:22:00 +02:00
|
|
|
*
|
2015-08-30 14:29:12 +02:00
|
|
|
* @return string
|
2015-08-30 13:22:00 +02:00
|
|
|
*/
|
2015-08-30 14:29:12 +02:00
|
|
|
public function getRememberTokenName()
|
|
|
|
{
|
|
|
|
return "remember_token";
|
|
|
|
}
|
2015-11-24 06:19:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this user has the given role.
|
|
|
|
*
|
|
|
|
* @param $roleName
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasRole($roleName)
|
|
|
|
{
|
|
|
|
foreach ($this->roles as $role) {
|
|
|
|
if ($role->name === $roleName) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-07 19:16:37 +01:00
|
|
|
|
2016-01-17 08:35:43 +01:00
|
|
|
public static function mapPublicUserSummary(User $user) {
|
|
|
|
return [
|
|
|
|
'id' => $user->id,
|
|
|
|
'name' => $user->display_name,
|
|
|
|
'slug' => $user->slug,
|
|
|
|
'url' => $user->url,
|
|
|
|
'is_archived' => $user->is_archived,
|
|
|
|
'avatars' => [
|
|
|
|
'small' => $user->getAvatarUrl(Image::SMALL),
|
|
|
|
'normal' => $user->getAvatarUrl(Image::NORMAL)
|
|
|
|
],
|
|
|
|
'created_at' => $user->created_at
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-01-07 19:16:37 +01:00
|
|
|
/**
|
|
|
|
* Returns this model in Elasticsearch-friendly form. The array returned by
|
|
|
|
* this method should match the current mapping for this model's ES type.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function toElasticsearch() {
|
2016-01-16 05:10:20 +01:00
|
|
|
return [
|
|
|
|
'username' => $this->username,
|
|
|
|
'display_name' => $this->display_name,
|
|
|
|
'tracks' => $this->tracks->pluck('title'),
|
|
|
|
];
|
2016-01-07 19:16:37 +01:00
|
|
|
}
|
2016-01-17 11:33:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function shouldBeIndexed():bool {
|
|
|
|
return $this->disabled_at === null;
|
|
|
|
}
|
2015-10-25 06:17:45 +01:00
|
|
|
}
|