74 lines
2.1 KiB
Go
74 lines
2.1 KiB
Go
package entity
|
|
|
|
import "go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
// Permission 權限實體 (MongoDB)
|
|
type Permission struct {
|
|
ID primitive.ObjectID `bson:"_id,omitempty" json:"id"`
|
|
ParentID primitive.ObjectID `bson:"parent_id,omitempty" json:"parent_id"`
|
|
Name string `bson:"name" json:"name"`
|
|
HTTPMethod string `bson:"http_method,omitempty" json:"http_method,omitempty"`
|
|
HTTPPath string `bson:"http_path,omitempty" json:"http_path,omitempty"`
|
|
Status Status `bson:"status" json:"status"`
|
|
Type PermissionType `bson:"type" json:"type"`
|
|
|
|
TimeStamp `bson:",inline"`
|
|
}
|
|
|
|
// CollectionName 集合名稱
|
|
func (Permission) CollectionName() string {
|
|
return "permission"
|
|
}
|
|
|
|
// IsActive 是否啟用
|
|
func (p *Permission) IsActive() bool {
|
|
return p.Status.IsActive()
|
|
}
|
|
|
|
// IsParent 是否為父權限
|
|
func (p *Permission) IsParent() bool {
|
|
return p.ParentID.IsZero()
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
// 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 角色權限關聯實體 (MongoDB)
|
|
type RolePermission struct {
|
|
ID primitive.ObjectID `bson:"_id,omitempty" json:"id"`
|
|
RoleID primitive.ObjectID `bson:"role_id" json:"role_id"`
|
|
PermissionID primitive.ObjectID `bson:"permission_id" json:"permission_id"`
|
|
|
|
TimeStamp `bson:",inline"`
|
|
}
|
|
|
|
// CollectionName 集合名稱
|
|
func (RolePermission) CollectionName() string {
|
|
return "role_permission"
|
|
}
|
|
|
|
// Validate 驗證資料
|
|
func (rp *RolePermission) Validate() error {
|
|
if rp.RoleID.IsZero() {
|
|
return ErrInvalidData("role_id is required")
|
|
}
|
|
if rp.PermissionID.IsZero() {
|
|
return ErrInvalidData("permission_id is required")
|
|
}
|
|
return nil
|
|
}
|