blessing-skin-server/app/Models/User.php

89 lines
1.8 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
namespace App\Models;
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;
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;
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
public $primaryKey = 'uid';
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'];
2016-10-23 11:41:52 +08:00
public function isAdmin()
2016-07-21 22:01:57 +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-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();
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
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
}