add user model

This commit is contained in:
2026-03-09 10:12:35 +08:00
parent e41f2a62f2
commit f792c04002
+40
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Model;
use Hyperf\Database\Model\Relations\BelongsTo;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\DbConnection\Model\Model;
use OpenApi\Attributes as OA;
@@ -15,6 +16,8 @@ use Qbhy\HyperfAuth\Authenticatable;
* @property string $password
* @property string $email
* @property int $status
* @property int $role_id
* @property bool $api_key_enabled
* @property array $ext
* @property string $refresh_token
* @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: 'email', type: 'string', example: 'user@example.com'),
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: 'refresh_token_expires_at', type: 'string', format: 'date-time', nullable: true),
new OA\Property(property: 'created_at', type: 'string', format: 'date-time'),
@@ -50,6 +55,8 @@ class User extends Model implements Authenticatable
'password',
'email',
'status',
'role_id',
'api_key_enabled',
'ext',
'refresh_token',
'refresh_token_expires_at',
@@ -69,6 +76,8 @@ class User extends Model implements Authenticatable
protected array $casts = [
'id' => 'integer',
'status' => 'integer',
'role_id' => 'integer',
'api_key_enabled' => 'boolean',
'ext' => 'array',
'refresh_token_expires_at' => 'datetime',
'created_at' => 'datetime',
@@ -107,6 +116,37 @@ class User extends Model implements Authenticatable
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
{
return $this->hasMany(Platform::class, 'developer_id');