Files
nexusphp/app/Models/User.php

122 lines
3.5 KiB
PHP
Raw Normal View History

2021-04-02 19:48:41 +08:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
2021-04-22 03:24:59 +08:00
use Laravel\Sanctum\HasApiTokens;
2021-04-02 19:48:41 +08:00
class User extends Authenticatable
{
2021-04-22 03:24:59 +08:00
use HasFactory, Notifiable, HasApiTokens;
2021-04-02 19:48:41 +08:00
2021-04-17 19:01:33 +08:00
public $timestamps = false;
const STATUS_CONFIRMED = 'confirmed';
const STATUS_PENDING = 'pending';
2021-04-25 21:28:58 +08:00
const ENABLED_YES = 'yes';
const ENABLED_NO = 'no';
2021-04-19 20:13:21 +08:00
const CLASS_PEASANT = "0";
const CLASS_USER = "1";
const CLASS_POWER_USER = "2";
const CLASS_ELITE_USER = "3";
const CLASS_CRAZY_USER = "4";
const CLASS_INSANE_USER = "5";
const CLASS_VETERAN_USER = "6";
const CLASS_EXTREME_USER = "7";
const CLASS_ULTIMATE_USER = "8";
const CLASS_NEXUS_MASTER = "9";
const CLASS_VIP = "10";
const CLASS_RETIREE = "11";
const CLASS_UPLOADER = "12";
const CLASS_MODERATOR = "13";
const CLASS_ADMINISTRATOR = "14";
const CLASS_SYSOP = "15";
2021-04-22 03:24:59 +08:00
const CLASS_STAFF_LEADER = "16";
2021-04-19 20:13:21 +08:00
public static $classes = [
self::CLASS_PEASANT => ['text' => 'Peasant'],
self::CLASS_USER => ['text' => 'User'],
self::CLASS_POWER_USER => ['text' => 'Power User'],
self::CLASS_ELITE_USER => ['text' => 'Elite User'],
self::CLASS_CRAZY_USER => ['text' => 'Crazy User'],
self::CLASS_INSANE_USER => ['text' => 'Insane User'],
self::CLASS_VETERAN_USER => ['text' => 'Veteran User'],
self::CLASS_EXTREME_USER => ['text' => 'Extreme User'],
2021-04-22 03:24:59 +08:00
self::CLASS_ULTIMATE_USER => ['text' => 'Ultimate User'],
2021-04-19 20:13:21 +08:00
self::CLASS_NEXUS_MASTER => ['text' => 'Nexus Master'],
self::CLASS_VIP => ['text' => 'Vip'],
self::CLASS_RETIREE => ['text' => 'Retiree'],
self::CLASS_UPLOADER => ['text' => 'Uploader'],
self::CLASS_MODERATOR => ['text' => 'Moderator'],
self::CLASS_ADMINISTRATOR => ['text' => 'Administrator'],
self::CLASS_SYSOP => ['text' => 'Sysop'],
2021-04-22 03:24:59 +08:00
self::CLASS_STAFF_LEADER => ['text' => 'Staff Leader'],
2021-04-19 20:13:21 +08:00
];
2021-04-22 03:24:59 +08:00
public function getClassTextAttribute(): string
2021-04-19 20:13:21 +08:00
{
return self::$classes[$this->class]['text'] ?? '';
}
2021-04-17 19:01:33 +08:00
/**
* 为数组 / JSON 序列化准备日期。
*
* @param \DateTimeInterface $date
* @return string
*/
2021-04-22 03:24:59 +08:00
protected function serializeDate(\DateTimeInterface $date): string
2021-04-17 19:01:33 +08:00
{
return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
}
2021-04-02 19:48:41 +08:00
/**
* The attributes that are mass assignable.
*
* @var array
*/
2021-04-20 20:18:02 +08:00
protected $fillable = ['username', 'email', 'passhash', 'secret', 'editsecret', 'added'];
2021-04-02 19:48:41 +08:00
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
2021-04-23 01:28:41 +08:00
'secret', 'passhash',
2021-04-02 19:48:41 +08:00
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
2021-04-25 21:28:58 +08:00
'added' => 'datetime',
2021-04-02 19:48:41 +08:00
];
2021-04-25 02:12:14 +08:00
2021-04-25 21:28:58 +08:00
public function checkIsNormal(array $fields = ['status', 'enabled'])
{
if (in_array('visible', $fields) && $this->getAttribute('status') != self::STATUS_CONFIRMED) {
throw new \InvalidArgumentException(sprintf('User: %s is not confirmed.', $this->id));
}
if (in_array('enabled', $fields) && $this->getAttribute('enabled') != self::ENABLED_YES) {
throw new \InvalidArgumentException(sprintf('User: %s is not enabled.', $this->id));
}
return true;
}
2021-04-25 02:12:14 +08:00
2021-04-26 20:37:17 +08:00
public function exams()
2021-04-25 02:12:14 +08:00
{
2021-04-25 21:28:58 +08:00
return $this->hasMany(ExamUser::class, 'uid');
2021-04-25 02:12:14 +08:00
}
2021-04-02 19:48:41 +08:00
}