Files
FendxPHP/app/Dto/UserDto.php
Lawson 2782d765fb feat(database): 添加用户角色权限系统及相关监控功能
- 创建用户表(users)包含基本信息和认证字段
- 创建角色表(roles)用于权限控制
- 创建权限表(permissions)定义系统权限
- 创建用户角色关联表(user_roles)建立用户与角色关系
- 创建角色权限关联表(role_permissions)建立角色与权限关系
- 创建迁移记录表(migrations)追踪数据库变更
- 添加AdminController提供管理员面板功能
- 实现系统监控、配置管理、缓存清理等功能
- 添加AOP切面编程支持的各种通知类型
- 实现告警管理AlertManager支持多渠道告警
- 添加文档注解接口规范
2026-04-08 17:00:28 +08:00

325 lines
6.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace App\Dto;
/**
* 用户数据传输对象
*/
class UserDto extends BaseDto
{
#[Required]
private ?int $id = null;
#[Required]
#[Length(min: 2, max: 50)]
private string $username = '';
#[Required]
#[Length(min: 6, max: 100)]
private string $email = '';
#[Required]
#[Length(min: 6, max: 255)]
private string $password = '';
#[Length(max: 100)]
private string $nickname = '';
#[Length(max: 20)]
private string $phone = '';
#[Length(max: 255)]
private string $avatar = '';
private ?int $status = null;
private ?int $roleId = null;
private ?string $roleName = '';
private ?\DateTime $createdAt = null;
private ?\DateTime $updatedAt = null;
private ?\DateTime $lastLoginAt = null;
private array $permissions = [];
private array $roles = [];
public function getId(): ?int
{
return $this->id;
}
public function setId(int $id): self
{
$this->id = $id;
return $this;
}
public function getUsername(): string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getEmail(): string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getNickname(): string
{
return $this->nickname;
}
public function setNickname(string $nickname): self
{
$this->nickname = $nickname;
return $this;
}
public function getPhone(): string
{
return $this->phone;
}
public function setPhone(string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getAvatar(): string
{
return $this->avatar;
}
public function setAvatar(string $avatar): self
{
$this->avatar = $avatar;
return $this;
}
public function getStatus(): ?int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->status = $status;
return $this;
}
public function getRoleId(): ?int
{
return $this->roleId;
}
public function setRoleId(int $roleId): self
{
$this->roleId = $roleId;
return $this;
}
public function getRoleName(): ?string
{
return $this->roleName;
}
public function setRoleName(string $roleName): self
{
$this->roleName = $roleName;
return $this;
}
public function getCreatedAt(): ?\DateTime
{
return $this->createdAt;
}
public function setCreatedAt(\DateTime $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTime
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTime $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getLastLoginAt(): ?\DateTime
{
return $this->lastLoginAt;
}
public function setLastLoginAt(\DateTime $lastLoginAt): self
{
$this->lastLoginAt = $lastLoginAt;
return $this;
}
public function getPermissions(): array
{
return $this->permissions;
}
public function setPermissions(array $permissions): self
{
$this->permissions = $permissions;
return $this;
}
public function getRoles(): array
{
return $this->roles;
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* 添加权限
*/
public function addPermission(string $permission): self
{
if (!in_array($permission, $this->permissions)) {
$this->permissions[] = $permission;
}
return $this;
}
/**
* 添加角色
*/
public function addRole(string $role): self
{
if (!in_array($role, $this->roles)) {
$this->roles[] = $role;
}
return $this;
}
/**
* 检查是否有指定权限
*/
public function hasPermission(string $permission): bool
{
return in_array($permission, $this->permissions);
}
/**
* 检查是否有指定角色
*/
public function hasRole(string $role): bool
{
return in_array($role, $this->roles);
}
/**
* 获取用于API响应的数据隐藏敏感信息
*/
public function toApiResponse(): array
{
$data = $this->toArray();
// 移除敏感信息
unset($data['password']);
// 格式化日期
if ($this->createdAt) {
$data['created_at'] = $this->createdAt->format('Y-m-d H:i:s');
}
if ($this->updatedAt) {
$data['updated_at'] = $this->updatedAt->format('Y-m-d H:i:s');
}
if ($this->lastLoginAt) {
$data['last_login_at'] = $this->lastLoginAt->format('Y-m-d H:i:s');
}
return $data;
}
/**
* 创建用于登录的用户DTO
*/
public static function forLogin(string $username, string $password): self
{
return (new self())
->setUsername($username)
->setPassword($password);
}
/**
* 创建用于注册的用户DTO
*/
public static function forRegister(string $username, string $email, string $password): self
{
return (new self())
->setUsername($username)
->setEmail($email)
->setPassword($password);
}
/**
* 验证邮箱格式
*/
public function validateEmail(): bool
{
return filter_var($this->email, FILTER_VALIDATE_EMAIL) !== false;
}
/**
* 验证手机号格式
*/
public function validatePhone(): bool
{
return preg_match('/^1[3-9]\d{9}$/', $this->phone) === 1;
}
/**
* 验证用户名格式
*/
public function validateUsername(): bool
{
return preg_match('/^[a-zA-Z0-9_]{2,50}$/', $this->username) === 1;
}
}