From 7cf78436d4fcd8e8172d5e28e9375ae48f9199c7 Mon Sep 17 00:00:00 2001 From: pixel <303176530@qq.com> Date: Thu, 26 Dec 2019 13:21:37 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=92=E8=89=B2=E7=BA=A7=E5=88=AB=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QMPlusServer/model/sysModel/sys_authority.go | 32 +++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/QMPlusServer/model/sysModel/sys_authority.go b/QMPlusServer/model/sysModel/sys_authority.go index 0ac73e41..1dbec00f 100644 --- a/QMPlusServer/model/sysModel/sys_authority.go +++ b/QMPlusServer/model/sysModel/sys_authority.go @@ -10,8 +10,10 @@ import ( type SysAuthority struct { gorm.Model - AuthorityId string `json:"authorityId" gorm:"not null;unique"` - AuthorityName string `json:"authorityName"` + AuthorityId string `json:"authorityId" gorm:"not null;unique"` + AuthorityName string `json:"authorityName"` + ParentId string `json:"parentId"` + Children []SysAuthority `json:"children"` } // 创建角色 @@ -24,8 +26,13 @@ func (a *SysAuthority) CreateAuthority() (err error, authority *SysAuthority) { func (a *SysAuthority) DeleteAuthority() (err error) { err = qmsql.DEFAULTDB.Where("authority_id = ?", a.AuthorityId).Find(&SysUser{}).Error if err != nil { - err = qmsql.DEFAULTDB.Where("authority_id = ?", a.AuthorityId).First(a).Unscoped().Delete(a).Error - new(CasbinModel).clearCasbin(0, a.AuthorityId) + err = qmsql.DEFAULTDB.Where("parentId = ?", a.AuthorityId).Find(&SysAuthority{}).Error + if err != nil { + err = qmsql.DEFAULTDB.Where("authority_id = ?", a.AuthorityId).First(a).Unscoped().Delete(a).Error + new(CasbinModel).clearCasbin(0, a.AuthorityId) + } else { + err = errors.New("此角色存在子角色不允许删除") + } } else { err = errors.New("此角色有用户正在使用禁止删除") } @@ -40,7 +47,22 @@ func (a *SysAuthority) GetInfoList(info modelInterface.PageInfo) (err error, lis return } else { 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 } } + +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 +}