59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package entity
|
|
|
|
// Role 角色實體
|
|
type Role struct {
|
|
ID int64 `gorm:"column:id;primaryKey" json:"id"`
|
|
ClientID int `gorm:"column:client_id;index" json:"client_id"`
|
|
UID string `gorm:"column:uid;uniqueIndex;size:32" json:"uid"`
|
|
Name string `gorm:"column:name;size:100" json:"name"`
|
|
Status Status `gorm:"column:status;index" json:"status"`
|
|
|
|
// 關聯權限 (不存資料庫)
|
|
Permissions Permissions `gorm:"-" json:"permissions,omitempty"`
|
|
|
|
TimeStamp
|
|
}
|
|
|
|
// TableName 指定表名
|
|
func (Role) TableName() string {
|
|
return "role"
|
|
}
|
|
|
|
// IsActive 是否啟用
|
|
func (r *Role) IsActive() bool {
|
|
return r.Status.IsActive()
|
|
}
|
|
|
|
// IsAdmin 是否為管理員角色 (需要傳入 adminUID)
|
|
func (r *Role) IsAdmin(adminUID string) bool {
|
|
return r.UID == adminUID
|
|
}
|
|
|
|
// Validate 驗證角色資料
|
|
func (r *Role) Validate() error {
|
|
if r.UID == "" {
|
|
return ErrInvalidData("role uid is required")
|
|
}
|
|
if r.Name == "" {
|
|
return ErrInvalidData("role name is required")
|
|
}
|
|
if r.ClientID <= 0 {
|
|
return ErrInvalidData("role client_id must be positive")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ErrInvalidData 無效資料錯誤
|
|
func ErrInvalidData(msg string) error {
|
|
return &ValidationError{Message: msg}
|
|
}
|
|
|
|
// ValidationError 驗證錯誤
|
|
type ValidationError struct {
|
|
Message string
|
|
}
|
|
|
|
func (e *ValidationError) Error() string {
|
|
return e.Message
|
|
}
|