41 lines
1.2 KiB
Go
41 lines
1.2 KiB
Go
|
|
package models
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
"gorm.io/gorm"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// 角色预设权限关联表
|
|||
|
|
type MgtLinkRolePmn struct {
|
|||
|
|
RoleId uint `gorm:"column:role_id;primaryKey" json:"role_id"` // 角色ID
|
|||
|
|
AppId uint `gorm:"column:app_id;primaryKey" json:"app_id"` // 应用ID
|
|||
|
|
PmnId uint `gorm:"column:pmn_id;primaryKey" json:"pmn_id"` // 权限ID
|
|||
|
|
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime" json:"created_at,omitempty"` // 创建时间
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (MgtLinkRolePmn) TableName() string {
|
|||
|
|
return "mgt_link_role_pmn"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// func init() {
|
|||
|
|
// database.AppendMigrate(&MgtLinkRolePmn{})
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// AfterDelete钩子:仅当该角色在该应用下不再拥有任何权限时,移除角色-应用关联
|
|||
|
|
func (l *MgtLinkRolePmn) AfterDelete(tx *gorm.DB) error {
|
|||
|
|
var count int64
|
|||
|
|
if err := tx.Model(&MgtLinkRolePmn{}).
|
|||
|
|
Where("app_id = ? AND role_id = ?", l.AppId, l.RoleId).
|
|||
|
|
Count(&count).Error; err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
if count == 0 {
|
|||
|
|
if err := tx.Where("app_id = ? AND role_id = ?", l.AppId, l.RoleId).
|
|||
|
|
Delete(&MgtLinkRoleApp{}).Error; err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return nil
|
|||
|
|
}
|