liujiandao
2024-03-26 1c17ff16fd13e4d8bbab75d8a728cf18465b20e0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package service
 
import (
    "aps_crm/model"
    "aps_crm/pkg/ecode"
    "github.com/flipped-aurora/gin-vue-admin/server/global"
    "github.com/flipped-aurora/gin-vue-admin/server/model/system"
    "strconv"
)
 
type AuthorityService struct{}
 
var AuthorityServiceApp = new(AuthorityService)
 
// CreateAuthority create authority and return authority id and error code
func (authorityService *AuthorityService) CreateAuthority(auth model.Authority) (int, uint) {
    // check if authority name exists
    err := model.NewSysAuthoritySearch().Create(&auth)
    if err != nil {
        return ecode.RoleExist, 0
    }
 
    return ecode.OK, auth.Id
}
 
func (authorityService *AuthorityService) UpdateAuthority(auth model.Authority) int {
    _, err := model.NewSysAuthoritySearch().SetAuthorityId(auth.Id).Find()
    if err != nil {
        return ecode.RoleNotExist
    }
 
    err = model.NewSysAuthoritySearch().SetAuthorityId(auth.Id).Update(&auth)
 
    return ecode.OK
}
 
func (authorityService *AuthorityService) DeleteAuthority(auth *model.Authority) int {
    _, err := model.NewSysAuthoritySearch().SetAuthorityId(auth.Id).Find()
    if err != nil {
        return ecode.RoleNotExist
    }
 
    if len(auth.Users) != 0 {
        return ecode.RoleDeleteErr1
    }
 
    authorityId := strconv.Itoa(int(auth.Id))
    CasbinServiceApp.ClearCasbin(0, authorityId)
    return ecode.OK
}
 
//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetAuthorityInfo
//@description: 获取所有角色信息
//@param: auth model.Authority
//@return: sa system.Authority, err error
 
func (authorityService *AuthorityService) GetAuthorityInfo(auth system.SysAuthority) (sa system.SysAuthority, err error) {
    err = global.GVA_DB.Preload("DataAuthorityId").Where("authority_id = ?", auth.AuthorityId).First(&sa).Error
    return sa, err
}
 
//@author: [piexlmax](https://github.com/piexlmax)
//@function: SetMenuAuthority
//@description: 设置角色资源权限
//@param: auth model.Authority
//@return: error
 
func (authorityService *AuthorityService) SetDataAuthority(auth system.SysAuthority) error {
    var s system.SysAuthority
    global.GVA_DB.Preload("DataAuthorityId").First(&s, "authority_id = ?", auth.AuthorityId)
    err := global.GVA_DB.Model(&s).Association("DataAuthorityId").Replace(&auth.DataAuthorityId)
    return err
}
 
//@author: [piexlmax](https://github.com/piexlmax)
//@function: SetMenuAuthority
//@description: 菜单与角色绑定
//@param: auth *model.Authority
//@return: error
 
func (authorityService *AuthorityService) SetMenuAuthority(auth *model.Authority) error {
    return model.NewSysAuthoritySearch().SetMenuAuthority(auth)
}