package entity // Permission 權限實體 type Permission struct { ID int64 `gorm:"column:id;primaryKey" json:"id"` ParentID int64 `gorm:"column:parent;index" json:"parent_id"` Name string `gorm:"column:name;size:100;index" json:"name"` HTTPMethod string `gorm:"column:http_method;size:10" json:"http_method,omitempty"` HTTPPath string `gorm:"column:http_path;size:255" json:"http_path,omitempty"` Status Status `gorm:"column:status;index" json:"status"` Type PermissionType `gorm:"column:type" json:"type"` TimeStamp } // TableName 指定表名 func (Permission) TableName() string { return "permission" } // IsActive 是否啟用 func (p *Permission) IsActive() bool { return p.Status.IsActive() } // IsParent 是否為父權限 func (p *Permission) IsParent() bool { return p.ParentID == 0 } // IsAPIPermission 是否為 API 權限 func (p *Permission) IsAPIPermission() bool { return p.HTTPPath != "" && p.HTTPMethod != "" } // Validate 驗證資料 func (p *Permission) Validate() error { if p.Name == "" { return ErrInvalidData("permission name is required") } if p.ParentID < 0 { return ErrInvalidData("permission parent_id cannot be negative") } // API 權限必須有 path 和 method if (p.HTTPPath != "" && p.HTTPMethod == "") || (p.HTTPPath == "" && p.HTTPMethod != "") { return ErrInvalidData("permission http_path and http_method must be both set or both empty") } return nil } // RolePermission 角色權限關聯實體 type RolePermission struct { ID int64 `gorm:"column:id;primaryKey" json:"id"` RoleID int64 `gorm:"column:role_id;index:idx_role_permission" json:"role_id"` PermissionID int64 `gorm:"column:permission_id;index:idx_role_permission" json:"permission_id"` TimeStamp } // TableName 指定表名 func (RolePermission) TableName() string { return "role_permission" } // Validate 驗證資料 func (rp *RolePermission) Validate() error { if rp.RoleID <= 0 { return ErrInvalidData("role_id must be positive") } if rp.PermissionID <= 0 { return ErrInvalidData("permission_id must be positive") } return nil }