add user model
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Model;
|
namespace App\Model;
|
||||||
|
|
||||||
|
use Hyperf\Database\Model\Relations\BelongsTo;
|
||||||
use Hyperf\Database\Model\Relations\HasMany;
|
use Hyperf\Database\Model\Relations\HasMany;
|
||||||
use Hyperf\DbConnection\Model\Model;
|
use Hyperf\DbConnection\Model\Model;
|
||||||
use OpenApi\Attributes as OA;
|
use OpenApi\Attributes as OA;
|
||||||
@@ -15,6 +16,8 @@ use Qbhy\HyperfAuth\Authenticatable;
|
|||||||
* @property string $password
|
* @property string $password
|
||||||
* @property string $email
|
* @property string $email
|
||||||
* @property int $status
|
* @property int $status
|
||||||
|
* @property int $role_id
|
||||||
|
* @property bool $api_key_enabled
|
||||||
* @property array $ext
|
* @property array $ext
|
||||||
* @property string $refresh_token
|
* @property string $refresh_token
|
||||||
* @property \Carbon\Carbon $refresh_token_expires_at
|
* @property \Carbon\Carbon $refresh_token_expires_at
|
||||||
@@ -29,6 +32,8 @@ use Qbhy\HyperfAuth\Authenticatable;
|
|||||||
new OA\Property(property: 'username', type: 'string', example: 'user_1234'),
|
new OA\Property(property: 'username', type: 'string', example: 'user_1234'),
|
||||||
new OA\Property(property: 'email', type: 'string', example: 'user@example.com'),
|
new OA\Property(property: 'email', type: 'string', example: 'user@example.com'),
|
||||||
new OA\Property(property: 'status', type: 'integer', example: 1),
|
new OA\Property(property: 'status', type: 'integer', example: 1),
|
||||||
|
new OA\Property(property: 'role_id', type: 'integer', nullable: true, example: 1),
|
||||||
|
new OA\Property(property: 'api_key_enabled', type: 'boolean', example: false),
|
||||||
new OA\Property(property: 'ext', type: 'object', nullable: true, example: ['nickname' => 'user']),
|
new OA\Property(property: 'ext', type: 'object', nullable: true, example: ['nickname' => 'user']),
|
||||||
new OA\Property(property: 'refresh_token_expires_at', type: 'string', format: 'date-time', nullable: true),
|
new OA\Property(property: 'refresh_token_expires_at', type: 'string', format: 'date-time', nullable: true),
|
||||||
new OA\Property(property: 'created_at', type: 'string', format: 'date-time'),
|
new OA\Property(property: 'created_at', type: 'string', format: 'date-time'),
|
||||||
@@ -50,6 +55,8 @@ class User extends Model implements Authenticatable
|
|||||||
'password',
|
'password',
|
||||||
'email',
|
'email',
|
||||||
'status',
|
'status',
|
||||||
|
'role_id',
|
||||||
|
'api_key_enabled',
|
||||||
'ext',
|
'ext',
|
||||||
'refresh_token',
|
'refresh_token',
|
||||||
'refresh_token_expires_at',
|
'refresh_token_expires_at',
|
||||||
@@ -69,6 +76,8 @@ class User extends Model implements Authenticatable
|
|||||||
protected array $casts = [
|
protected array $casts = [
|
||||||
'id' => 'integer',
|
'id' => 'integer',
|
||||||
'status' => 'integer',
|
'status' => 'integer',
|
||||||
|
'role_id' => 'integer',
|
||||||
|
'api_key_enabled' => 'boolean',
|
||||||
'ext' => 'array',
|
'ext' => 'array',
|
||||||
'refresh_token_expires_at' => 'datetime',
|
'refresh_token_expires_at' => 'datetime',
|
||||||
'created_at' => 'datetime',
|
'created_at' => 'datetime',
|
||||||
@@ -107,6 +116,37 @@ class User extends Model implements Authenticatable
|
|||||||
return password_verify($password, $this->password);
|
return password_verify($password, $this->password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 角色关联
|
||||||
|
*/
|
||||||
|
public function role(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Role::class, 'role_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户的 API Keys
|
||||||
|
*/
|
||||||
|
public function apiKeys(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(ApiKey::class, 'user_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isAdministrator(): bool
|
||||||
|
{
|
||||||
|
return $this->role?->name === 'administrator';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isDeveloper(): bool
|
||||||
|
{
|
||||||
|
return $this->role?->name === 'developer';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isAccessor(): bool
|
||||||
|
{
|
||||||
|
return $this->role?->name === 'accessor';
|
||||||
|
}
|
||||||
|
|
||||||
public function developedPlatforms(): HasMany
|
public function developedPlatforms(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(Platform::class, 'developer_id');
|
return $this->hasMany(Platform::class, 'developer_id');
|
||||||
|
|||||||
Reference in New Issue
Block a user