Files
full/module/base/mgt/internal/models/link_role_pmn.go

41 lines
1.2 KiB
Go
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.
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
}