Browse Source

角色级别功能完成

main
pixel 5 years ago
parent
commit
7cf78436d4
  1. 24
      QMPlusServer/model/sysModel/sys_authority.go

24
QMPlusServer/model/sysModel/sys_authority.go

@ -12,6 +12,8 @@ type SysAuthority struct {
gorm.Model gorm.Model
AuthorityId string `json:"authorityId" gorm:"not null;unique"` AuthorityId string `json:"authorityId" gorm:"not null;unique"`
AuthorityName string `json:"authorityName"` AuthorityName string `json:"authorityName"`
ParentId string `json:"parentId"`
Children []SysAuthority `json:"children"`
} }
// 创建角色 // 创建角色
@ -23,9 +25,14 @@ func (a *SysAuthority) CreateAuthority() (err error, authority *SysAuthority) {
// 删除角色 // 删除角色
func (a *SysAuthority) DeleteAuthority() (err error) { func (a *SysAuthority) DeleteAuthority() (err error) {
err = qmsql.DEFAULTDB.Where("authority_id = ?", a.AuthorityId).Find(&SysUser{}).Error err = qmsql.DEFAULTDB.Where("authority_id = ?", a.AuthorityId).Find(&SysUser{}).Error
if err != nil {
err = qmsql.DEFAULTDB.Where("parentId = ?", a.AuthorityId).Find(&SysAuthority{}).Error
if err != nil { if err != nil {
err = qmsql.DEFAULTDB.Where("authority_id = ?", a.AuthorityId).First(a).Unscoped().Delete(a).Error err = qmsql.DEFAULTDB.Where("authority_id = ?", a.AuthorityId).First(a).Unscoped().Delete(a).Error
new(CasbinModel).clearCasbin(0, a.AuthorityId) new(CasbinModel).clearCasbin(0, a.AuthorityId)
} else {
err = errors.New("此角色存在子角色不允许删除")
}
} else { } else {
err = errors.New("此角色有用户正在使用禁止删除") err = errors.New("此角色有用户正在使用禁止删除")
} }
@ -40,7 +47,22 @@ func (a *SysAuthority) GetInfoList(info modelInterface.PageInfo) (err error, lis
return return
} else { } else {
var authority []SysAuthority var authority []SysAuthority
err = db.Find(&authority).Error
err = db.Where("parent_id = 0").Find(&authority).Error
if len(authority) > 0 {
for k, _ := range authority {
err = findChildrenAuthority(&authority[k])
}
}
return err, authority, total return err, authority, total
} }
} }
func findChildrenAuthority(authority *SysAuthority) (err error) {
err = qmsql.DEFAULTDB.Where("parent_id = ?", authority.AuthorityId).Find(&authority.Children).Error
if len(authority.Children) > 0 {
for k, _ := range authority.Children {
err = findChildrenAuthority(&authority.Children[k])
}
}
return err
}
Loading…
Cancel
Save