123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- package system
- import (
- "errors"
- "strconv"
- "gorm.io/gorm"
- "lcfns/global"
- "lcfns/model/common/request"
- "lcfns/model/system"
- "lcfns/model/system/response"
- )
- var ErrRoleExistence = errors.New("存在相同角色id")
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: CreateAuthority
- //@description: 创建一个角色
- //@param: auth model.SysAuthority
- //@return: authority system.SysAuthority, err error
- type AuthorityService struct{}
- var AuthorityServiceApp = new(AuthorityService)
- func (authorityService *AuthorityService) CreateAuthority(auth system.SysAuthority) (authority system.SysAuthority, err error) {
- var authorityBox system.SysAuthority
- if !errors.Is(global.Db.Where("authority_id = ?", auth.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) {
- return auth, ErrRoleExistence
- }
- err = global.Db.Create(&auth).Error
- return auth, err
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: CopyAuthority
- //@description: 复制一个角色
- //@param: copyInfo response.SysAuthorityCopyResponse
- //@return: authority system.SysAuthority, err error
- func (authorityService *AuthorityService) CopyAuthority(copyInfo response.SysAuthorityCopyResponse) (authority system.SysAuthority, err error) {
- var authorityBox system.SysAuthority
- if !errors.Is(global.Db.Where("authority_id = ?", copyInfo.Authority.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) {
- return authority, ErrRoleExistence
- }
- copyInfo.Authority.Children = []system.SysAuthority{}
- menus, err := MenuServiceApp.GetMenuAuthority(&request.GetAuthorityId{AuthorityId: copyInfo.OldAuthorityId})
- if err != nil {
- return
- }
- var baseMenu []system.SysBaseMenu
- for _, v := range menus {
- intNum, _ := strconv.Atoi(v.MenuId)
- v.SysBaseMenu.ID = uint(intNum)
- baseMenu = append(baseMenu, v.SysBaseMenu)
- }
- copyInfo.Authority.SysBaseMenus = baseMenu
- err = global.Db.Create(©Info.Authority).Error
- if err != nil {
- return
- }
- var btns []system.SysAuthorityBtn
- err = global.Db.Find(&btns, "authority_id = ?", copyInfo.OldAuthorityId).Error
- if err != nil {
- return
- }
- if len(btns) > 0 {
- for i := range btns {
- btns[i].AuthorityId = copyInfo.Authority.AuthorityId
- }
- err = global.Db.Create(&btns).Error
- if err != nil {
- return
- }
- }
- paths := CasbinServiceApp.GetPolicyPathByAuthorityId(copyInfo.OldAuthorityId)
- err = CasbinServiceApp.UpdateCasbin(copyInfo.Authority.AuthorityId, paths)
- if err != nil {
- _ = authorityService.DeleteAuthority(©Info.Authority)
- }
- return copyInfo.Authority, err
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: UpdateAuthority
- //@description: 更改一个角色
- //@param: auth model.SysAuthority
- //@return: authority system.SysAuthority, err error
- func (authorityService *AuthorityService) UpdateAuthority(auth system.SysAuthority) (authority system.SysAuthority, err error) {
- err = global.Db.Where("authority_id = ?", auth.AuthorityId).First(&system.SysAuthority{}).Updates(&auth).Error
- return auth, err
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: DeleteAuthority
- //@description: 删除角色
- //@param: auth *model.SysAuthority
- //@return: err error
- func (authorityService *AuthorityService) DeleteAuthority(auth *system.SysAuthority) (err error) {
- if errors.Is(global.Db.Debug().Preload("Users").First(&auth).Error, gorm.ErrRecordNotFound) {
- return errors.New("该角色不存在")
- }
- if len(auth.Users) != 0 {
- return errors.New("此角色有用户正在使用禁止删除")
- }
- if !errors.Is(global.Db.Where("authority_id = ?", auth.AuthorityId).First(&system.SysUser{}).Error, gorm.ErrRecordNotFound) {
- return errors.New("此角色有用户正在使用禁止删除")
- }
- if !errors.Is(global.Db.Where("parent_id = ?", auth.AuthorityId).First(&system.SysAuthority{}).Error, gorm.ErrRecordNotFound) {
- return errors.New("此角色存在子角色不允许删除")
- }
- db := global.Db.Preload("SysBaseMenus").Preload("DataAuthorityId").Where("authority_id = ?", auth.AuthorityId).First(auth)
- err = db.Unscoped().Delete(auth).Error
- if err != nil {
- return
- }
- if len(auth.SysBaseMenus) > 0 {
- err = global.Db.Model(auth).Association("SysBaseMenus").Delete(auth.SysBaseMenus)
- if err != nil {
- return
- }
- // err = db.Association("SysBaseMenus").Delete(&auth)
- }
- if len(auth.DataAuthorityId) > 0 {
- err = global.Db.Model(auth).Association("DataAuthorityId").Delete(auth.DataAuthorityId)
- if err != nil {
- return
- }
- }
- err = global.Db.Delete(&[]system.SysUserAuthority{}, "sys_authority_authority_id = ?", auth.AuthorityId).Error
- if err != nil {
- return
- }
- err = global.Db.Delete(&[]system.SysAuthorityBtn{}, "authority_id = ?", auth.AuthorityId).Error
- if err != nil {
- return
- }
- authorityId := strconv.Itoa(int(auth.AuthorityId))
- CasbinServiceApp.ClearCasbin(0, authorityId)
- return err
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: GetAuthorityInfoList
- //@description: 分页获取数据
- //@param: info request.PageInfo
- //@return: list interface{}, total int64, err error
- func (authorityService *AuthorityService) GetAuthorityInfoList(info request.PageInfo) (list interface{}, total int64, err error) {
- limit := info.PageSize
- offset := info.PageSize * (info.Page - 1)
- db := global.Db.Model(&system.SysAuthority{})
- if err = db.Where("parent_id = ?", "0").Count(&total).Error; total == 0 || err != nil {
- return
- }
- var authority []system.SysAuthority
- err = db.Limit(limit).Offset(offset).Preload("DataAuthorityId").Where("parent_id = ?", "0").Find(&authority).Error
- for k := range authority {
- err = authorityService.findChildrenAuthority(&authority[k])
- }
- return authority, total, err
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: GetAuthorityInfo
- //@description: 获取所有角色信息
- //@param: auth model.SysAuthority
- //@return: sa system.SysAuthority, err error
- func (authorityService *AuthorityService) GetAuthorityInfo(auth system.SysAuthority) (sa system.SysAuthority, err error) {
- err = global.Db.Preload("DataAuthorityId").Where("authority_id = ?", auth.AuthorityId).First(&sa).Error
- return sa, err
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: SetDataAuthority
- //@description: 设置角色资源权限
- //@param: auth model.SysAuthority
- //@return: error
- func (authorityService *AuthorityService) SetDataAuthority(auth system.SysAuthority) error {
- var s system.SysAuthority
- global.Db.Preload("DataAuthorityId").First(&s, "authority_id = ?", auth.AuthorityId)
- err := global.Db.Model(&s).Association("DataAuthorityId").Replace(&auth.DataAuthorityId)
- return err
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: SetMenuAuthority
- //@description: 菜单与角色绑定
- //@param: auth *model.SysAuthority
- //@return: error
- func (authorityService *AuthorityService) SetMenuAuthority(auth *system.SysAuthority) error {
- var s system.SysAuthority
- global.Db.Preload("SysBaseMenus").First(&s, "authority_id = ?", auth.AuthorityId)
- err := global.Db.Model(&s).Association("SysBaseMenus").Replace(&auth.SysBaseMenus)
- return err
- }
- //@author: [piexlmax](https://github.com/piexlmax)
- //@function: findChildrenAuthority
- //@description: 查询子角色
- //@param: authority *model.SysAuthority
- //@return: err error
- func (authorityService *AuthorityService) findChildrenAuthority(authority *system.SysAuthority) (err error) {
- err = global.Db.Preload("DataAuthorityId").Where("parent_id = ?", authority.AuthorityId).Find(&authority.Children).Error
- if len(authority.Children) > 0 {
- for k := range authority.Children {
- err = authorityService.findChildrenAuthority(&authority.Children[k])
- }
- }
- return err
- }
|