sys_authority.go 6.1 KB
Newer Older
1 2 3 4 5 6 7
package service

import (
	"errors"
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
8
	"gin-vue-admin/model/response"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
9
	"gorm.io/gorm"
10
	"strconv"
11 12 13 14
)

// @title    CreateAuthority
// @description   创建一个角色
15 16
// @auth                     (2020/04/05  20:22)
// @param     auth            model.SysAuthority
17
// @return                    error
18
// @return    authority       model.SysAuthority
Mr.奇淼('s avatar
Mr.奇淼( 已提交
19

20
func CreateAuthority(auth model.SysAuthority) (err error, authority model.SysAuthority) {
21
	var authorityBox model.SysAuthority
22
	if !errors.Is(global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) {
23 24
		return errors.New("存在相同角色id"), auth
	}
25 26
	err = global.GVA_DB.Create(&auth).Error
	return err, auth
27 28
}

29 30 31 32 33 34 35 36
// @title    CopyAuthority
// @description   复制一个角色
// @auth                     (2020/04/05  20:22)
// @param     copyInfo        response.SysAuthorityCopyResponse
// @return                    error
// @return    authority       model.SysAuthority

func CopyAuthority(copyInfo response.SysAuthorityCopyResponse) (err error, authority model.SysAuthority) {
37
	var authorityBox model.SysAuthority
38
	if !errors.Is(global.GVA_DB.Where("authority_id = ?", copyInfo.Authority.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) {
39 40
		return errors.New("存在相同角色id"), authority
	}
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	copyInfo.Authority.Children = []model.SysAuthority{}
	err, menus := GetMenuAuthority(copyInfo.OldAuthorityId)
	var baseMenu []model.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.GVA_DB.Create(&copyInfo.Authority).Error

	paths := GetPolicyPathByAuthorityId(copyInfo.OldAuthorityId)
	err = UpdateCasbin(copyInfo.Authority.AuthorityId, paths)
	if err != nil {
		_ = DeleteAuthority(&copyInfo.Authority)
	}
	return err, copyInfo.Authority
}

60 61 62 63 64 65 66 67 68 69 70 71
// @title    UpdateAuthority
// @description   更改一个角色
// @auth                     (2020/04/05  20:22)
// @param     auth            model.SysAuthority
// @return                    error
// @return    authority       model.SysAuthority

func UpdateAuthority(auth model.SysAuthority) (err error, authority model.SysAuthority) {
	err = global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&model.SysAuthority{}).Updates(&auth).Error
	return err, auth
}

72 73
// @title    DeleteAuthority
// @description   删除角色
74 75
// @auth                     (2020/04/05  20:22)
// @param     auth            model.SysAuthority
76 77
// @return                    error
// 删除角色
Mr.奇淼('s avatar
Mr.奇淼( 已提交
78

79
func DeleteAuthority(auth *model.SysAuthority) (err error) {
80
	if !errors.Is(global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&model.SysUser{}).Error, gorm.ErrRecordNotFound) {
81
		return errors.New("此角色有用户正在使用禁止删除")
82
	}
83
	if !errors.Is(global.GVA_DB.Where("parent_id = ?", auth.AuthorityId).First(&model.SysAuthority{}).Error, gorm.ErrRecordNotFound) {
84
		return errors.New("此角色存在子角色不允许删除")
85
	}
86 87
	db := global.GVA_DB.Preload("SysBaseMenus").Where("authority_id = ?", auth.AuthorityId).First(auth)
	err = db.Unscoped().Delete(auth).Error
88
	if len(auth.SysBaseMenus) > 0 {
89 90
		err = global.GVA_DB.Model(auth).Association("SysBaseMenus").Delete(auth.SysBaseMenus)
		//err = db.Association("SysBaseMenus").Delete(&auth)
91 92 93
	} else {
		err = db.Error
	}
94
	ClearCasbin(0, auth.AuthorityId)
95 96 97 98 99
	return err
}

// @title    GetInfoList
// @description   删除文件切片记录
100 101
// @auth                     (2020/04/05  20:22)
// @param     info            request.PaveInfo
102 103
// @return                    error
// 分页获取数据
Mr.奇淼('s avatar
Mr.奇淼( 已提交
104

Mr.奇淼('s avatar
Mr.奇淼( 已提交
105
func GetAuthorityInfoList(info request.PageInfo) (err error, list interface{}, total int64) {
106 107 108
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.GVA_DB
109 110 111
	var authority []model.SysAuthority
	err = db.Limit(limit).Offset(offset).Preload("DataAuthorityId").Where("parent_id = 0").Find(&authority).Error
	if len(authority) > 0 {
112
		for k := range authority {
113
			err = findChildrenAuthority(&authority[k])
114 115
		}
	}
116
	return err, authority, total
117 118 119 120
}

// @title    GetAuthorityInfo
// @description   获取所有角色信息
121 122
// @auth                     (2020/04/05  20:22)
// @param     auth            model.SysAuthority
123
// @return                    error
124
// @param     authority       model.SysAuthority
Mr.奇淼('s avatar
Mr.奇淼( 已提交
125

126 127
func GetAuthorityInfo(auth model.SysAuthority) (err error, sa model.SysAuthority) {
	err = global.GVA_DB.Preload("DataAuthorityId").Where("authority_id = ?", auth.AuthorityId).First(&sa).Error
128 129 130 131 132
	return err, sa
}

// @title    SetDataAuthority
// @description   设置角色资源权限
133 134
// @auth                     (2020/04/05  20:22)
// @param     auth            model.SysAuthority
135
// @return                    error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
136

137
func SetDataAuthority(auth model.SysAuthority) error {
138
	var s model.SysAuthority
139
	global.GVA_DB.Preload("DataAuthorityId").First(&s, "authority_id = ?", auth.AuthorityId)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
140
	err := global.GVA_DB.Model(&s).Association("DataAuthorityId").Replace(&auth.DataAuthorityId)
141 142 143 144 145
	return err
}

// @title    SetMenuAuthority
// @description   菜单与角色绑定
146 147
// @auth                     (2020/04/05  20:22)
// @param     auth            *model.SysAuthority
148
// @return                    error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
149

150
func SetMenuAuthority(auth *model.SysAuthority) error {
151
	var s model.SysAuthority
152
	global.GVA_DB.Preload("SysBaseMenus").First(&s, "authority_id = ?", auth.AuthorityId)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
153
	err := global.GVA_DB.Model(&s).Association("SysBaseMenus").Replace(&auth.SysBaseMenus)
154 155 156 157 158
	return err
}

// @title    findChildrenAuthority
// @description   查询子角色
159 160
// @auth                     (2020/04/05  20:22)
// @param     auth            *model.SysAuthority
161
// @return                    error
Mr.奇淼('s avatar
Mr.奇淼( 已提交
162

163 164 165
func findChildrenAuthority(authority *model.SysAuthority) (err error) {
	err = global.GVA_DB.Preload("DataAuthorityId").Where("parent_id = ?", authority.AuthorityId).Find(&authority.Children).Error
	if len(authority.Children) > 0 {
166
		for k := range authority.Children {
167 168 169 170 171
			err = findChildrenAuthority(&authority.Children[k])
		}
	}
	return err
}