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

122 lines
2.8 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
namespace App\Models;
2019-07-30 15:12:31 +08:00
use App\Models\Concerns\HasPassword;
2020-04-19 19:36:39 +08:00
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Auth\User as Authenticatable;
2019-12-14 11:10:37 +08:00
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
use Lorisleiva\LaravelSearchString\Concerns\SearchString;
2019-12-14 11:10:37 +08:00
use Tymon\JWTAuth\Contracts\JWTSubject;
2016-07-29 12:46:19 +08:00
2020-04-19 19:36:39 +08:00
/**
2020-06-28 16:14:56 +08:00
* @property int $uid
* @property string $email
* @property string $password
* @property string $nickname
* @property string|null $locale
* @property int $avatar
* @property int $score
* @property int $permission
* @property string $ip
* @property string $last_sign_at
* @property string $register_at
* @property bool $verified
* @property string $player_name
* @property Collection $players
* @property Collection $closet
2020-04-19 19:36:39 +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;
use SearchString;
2019-04-25 23:24:24 +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
protected $primaryKey = 'uid';
public $timestamps = false;
2020-06-06 15:48:43 +08:00
protected $fillable = [
'email', 'nickname', 'avatar', 'score', 'permission', 'last_sign_at',
];
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'];
protected $searchStringColumns = [
'uid',
'email' => ['searchable' => true],
'nickname' => ['searchable' => true],
'avatar', 'score', 'permission', 'ip',
'last_sign_at' => ['date' => true],
'register_at' => ['date' => true],
'verified' => ['boolean' => true],
];
2020-03-10 15:12:03 +08:00
public function isAdmin(): bool
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()
{
2020-04-19 19:36:39 +08:00
return $this->hasMany(Player::class, 'uid');
2016-10-23 11:41:52 +08:00
}
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
}