2016-07-21 22:01:57 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2019-08-08 11:55:22 +08:00
|
|
|
use Illuminate\Support\Arr;
|
2019-04-25 23:24:24 +08:00
|
|
|
use Laravel\Passport\HasApiTokens;
|
2019-07-30 15:12:31 +08:00
|
|
|
use App\Models\Concerns\HasPassword;
|
2019-04-23 10:05:58 +08:00
|
|
|
use Tymon\JWTAuth\Contracts\JWTSubject;
|
2019-07-03 16:19:13 +08:00
|
|
|
use Illuminate\Notifications\Notifiable;
|
2019-08-08 11:55:22 +08:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2018-07-20 14:42:43 +08:00
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
2016-07-29 12:46:19 +08:00
|
|
|
|
2019-04-23 10:05:58 +08:00
|
|
|
class User extends Authenticatable implements JWTSubject
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2019-07-03 16:19:13 +08:00
|
|
|
use Notifiable;
|
2019-07-30 15:12:31 +08:00
|
|
|
use HasPassword;
|
2019-04-25 23:24:24 +08:00
|
|
|
use HasApiTokens;
|
|
|
|
|
2019-03-02 22:58:37 +08:00
|
|
|
const BANNED = -1;
|
|
|
|
const NORMAL = 0;
|
|
|
|
const ADMIN = 1;
|
2016-10-23 11:41:52 +08:00
|
|
|
const SUPER_ADMIN = 2;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2019-08-08 11:55:22 +08:00
|
|
|
protected $primaryKey = 'uid';
|
2019-03-02 22:58:37 +08:00
|
|
|
public $timestamps = false;
|
2016-10-23 11:41:52 +08:00
|
|
|
protected $fillable = ['email', 'nickname', 'permission'];
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2018-02-23 09:51:23 +08:00
|
|
|
protected $casts = [
|
|
|
|
'uid' => 'integer',
|
|
|
|
'score' => 'integer',
|
|
|
|
'avatar' => 'integer',
|
|
|
|
'permission' => 'integer',
|
2018-08-17 12:32:44 +08:00
|
|
|
'verified' => 'bool',
|
2018-02-23 09:51:23 +08:00
|
|
|
];
|
|
|
|
|
2019-03-23 17:40:02 +08:00
|
|
|
protected $hidden = ['password', 'remember_token'];
|
|
|
|
|
2019-08-08 11:55:22 +08:00
|
|
|
protected static $mappings = [];
|
|
|
|
|
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
$columns = [
|
|
|
|
'uid',
|
|
|
|
'email',
|
|
|
|
'nickname',
|
|
|
|
'score',
|
|
|
|
'avatar',
|
|
|
|
'password',
|
|
|
|
'ip',
|
|
|
|
'permission',
|
|
|
|
'last_sign_at',
|
|
|
|
'register_at',
|
|
|
|
'verified',
|
|
|
|
];
|
|
|
|
array_walk($columns, function ($column) {
|
|
|
|
static::$mappings[$column] = Arr::get(static::$mappings, $column, $column);
|
|
|
|
});
|
|
|
|
|
|
|
|
static::addGlobalScope(function (Builder $builder) {
|
|
|
|
$query = $builder->getQuery();
|
|
|
|
|
|
|
|
$mapItem = function ($item) {
|
|
|
|
if (Arr::has(static::$mappings, $item['column'])) {
|
|
|
|
$item['column'] = static::$mappings[$item['column']];
|
|
|
|
}
|
|
|
|
return $item;
|
|
|
|
};
|
|
|
|
$mapColumn = function ($column) {
|
|
|
|
if (Arr::has(static::$mappings, $column)) {
|
|
|
|
return static::$mappings[$column];
|
|
|
|
}
|
|
|
|
return $column;
|
|
|
|
};
|
|
|
|
|
|
|
|
$query->wheres = array_map($mapItem, $query->wheres);
|
|
|
|
if ($query->columns) {
|
|
|
|
$query->columns = array_map($mapColumn, $query->columns);
|
|
|
|
}
|
|
|
|
if ($query->orders) {
|
|
|
|
$query->orders = array_map($mapItem, $query->orders);
|
|
|
|
}
|
|
|
|
if ($query->groups) {
|
|
|
|
$query->groups = array_map($mapColumn, $query->groups);
|
|
|
|
}
|
|
|
|
if ($query->havings) {
|
|
|
|
$query->havings = array_map($mapItem, $query->havings);
|
|
|
|
}
|
|
|
|
|
|
|
|
$builder->setQuery($query);
|
|
|
|
return $builder;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-23 11:41:52 +08:00
|
|
|
public function isAdmin()
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2019-03-02 22:58:37 +08:00
|
|
|
return $this->permission >= static::ADMIN;
|
2016-10-23 11:41:52 +08:00
|
|
|
}
|
2016-10-17 17:51:51 +08:00
|
|
|
|
2019-03-14 23:55:49 +08:00
|
|
|
public function closet()
|
2016-10-23 11:41:52 +08:00
|
|
|
{
|
2019-03-14 23:55:49 +08:00
|
|
|
return $this->belongsToMany(Texture::class, 'user_closet')->withPivot('item_name');
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2019-08-08 11:55:22 +08:00
|
|
|
public function getUidAttribute()
|
|
|
|
{
|
2019-08-08 15:23:37 +08:00
|
|
|
return intval($this->attributes[$this->primaryKey]);
|
2019-08-08 11:55:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getEmailAttribute()
|
|
|
|
{
|
|
|
|
return $this->attributes[static::$mappings['email']];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setEmailAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes[static::$mappings['email']] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNicknameAttribute()
|
|
|
|
{
|
|
|
|
return $this->attributes[static::$mappings['nickname']];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setNicknameAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes[static::$mappings['nickname']] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getScoreAttribute()
|
|
|
|
{
|
2019-08-08 15:23:37 +08:00
|
|
|
return intval($this->attributes[static::$mappings['score']]);
|
2019-08-08 11:55:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setScoreAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes[static::$mappings['score']] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAvatarAttribute()
|
|
|
|
{
|
2019-08-08 15:23:37 +08:00
|
|
|
return intval($this->attributes[static::$mappings['avatar']]);
|
2019-08-08 11:55:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setAvatarAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes[static::$mappings['avatar']] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPasswordAttribute()
|
|
|
|
{
|
|
|
|
return $this->attributes[static::$mappings['password']];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPasswordAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes[static::$mappings['password']] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIpAttribute()
|
|
|
|
{
|
|
|
|
return $this->attributes[static::$mappings['ip']];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setIpAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes[static::$mappings['ip']] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPermissionAttribute()
|
|
|
|
{
|
2019-08-08 15:23:37 +08:00
|
|
|
return intval($this->attributes[static::$mappings['permission']]);
|
2019-08-08 11:55:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setPermissionAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes[static::$mappings['permission']] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLastSignAtAttribute()
|
|
|
|
{
|
|
|
|
return $this->attributes[static::$mappings['last_sign_at']];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLastSignAtAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes[static::$mappings['last_sign_at']] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRegisterAtAttribute()
|
|
|
|
{
|
|
|
|
return $this->attributes[static::$mappings['register_at']];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setRegisterAtAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes[static::$mappings['register_at']] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getVerifiedAttribute()
|
|
|
|
{
|
2019-08-08 15:23:37 +08:00
|
|
|
return boolval($this->attributes[static::$mappings['verified']]);
|
2019-08-08 11:55:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setVerifiedAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes[static::$mappings['verified']] = $value;
|
|
|
|
}
|
|
|
|
|
2019-03-22 21:40:12 +08:00
|
|
|
public function getPlayerNameAttribute()
|
|
|
|
{
|
|
|
|
$player = $this->players->first();
|
2019-04-19 19:36:36 +08:00
|
|
|
|
2019-03-22 21:40:12 +08:00
|
|
|
return $player ? $player->name : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPlayerNameAttribute($value)
|
|
|
|
{
|
|
|
|
$player = $this->players->first();
|
|
|
|
if ($player) {
|
|
|
|
$player->name = $value;
|
|
|
|
$player->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
public function delete()
|
|
|
|
{
|
2016-10-16 18:16:15 +08:00
|
|
|
Player::where('uid', $this->uid)->delete();
|
2019-03-15 00:03:54 +08:00
|
|
|
|
2016-10-23 11:41:52 +08:00
|
|
|
return parent::delete();
|
|
|
|
}
|
2016-07-22 19:36:24 +08:00
|
|
|
|
2016-10-23 11:41:52 +08:00
|
|
|
public function players()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Player', 'uid');
|
|
|
|
}
|
2016-07-23 15:20:10 +08:00
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
public function getAuthIdentifier()
|
|
|
|
{
|
|
|
|
return $this->uid;
|
|
|
|
}
|
2019-04-23 10:05:58 +08:00
|
|
|
|
|
|
|
public function getJWTIdentifier()
|
|
|
|
{
|
|
|
|
return $this->getKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getJWTCustomClaims()
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|