liujiandao
2024-03-30 1e65185f0ecb8d4f8f1fd9ea822137e0c841a47f
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package v1
 
import (
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "github.com/gin-gonic/gin"
)
 
type AuthorityApi struct{}
 
// CreateAuthority
//
//    @Tags        Authority
//    @Summary    创建角色
//    @Security    ApiKeyAuth
//    @accept        application/json
//    @Produce    application/json
//    @Param        data    body        request.AddAuthority    true    "查询参数"
//    @Success    200        {object}    contextx.Response{}
//    @Router        /api/authority/add [post]
func (a *AuthorityApi) CreateAuthority(c *gin.Context) {
    var params request.AddAuthority
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
 
    errCode, auth := checkAuthorityParams(&params)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
 
    errCode, auth.Id = authorityService.CreateAuthority(auth)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
    }
 
    casbinInfos := append(request.DefaultCasbin(), params.CasbinInfos...)
 
    //_ = menuService.AddMenuAuthority(systemReq.DefaultMenu(), authority.Id)
    _ = casbinService.UpdateCasbin(auth.Id, casbinInfos)
 
    ctx.Ok()
}
 
//DeleteAuthority
//@Tags      Authority
//@Summary   删除角色
//@Security  ApiKeyAuth
//@accept    application/json
//@Produce   application/json
//@Param     data  body      request.AddAuthority            true  "删除角色"
//@Router    /authority/deleteAuthority [post]
//func (a *AuthorityApi) DeleteAuthority(c *gin.Context) {
//    var authority system.SysAuthority
//    err := c.ShouldBindJSON(&authority)
//    if err != nil {
//        response.FailWithMessage(err.Error(), c)
//        return
//    }
//    err = utils.Verify(authority, utils.AuthorityIdVerify)
//    if err != nil {
//        response.FailWithMessage(err.Error(), c)
//        return
//    }
//    err = authorityService.DeleteAuthority(&authority)
//    if err != nil { // 删除角色之前需要判断是否有用户正在使用此角色
//        global.GVA_LOG.Error("删除失败!", zap.Error(err))
//        response.FailWithMessage("删除失败"+err.Error(), c)
//        return
//    }
//    response.OkWithMessage("删除成功", c)
//}
 
// UpdateAuthority
//    @Tags        Authority
//    @Summary    更新角色信息
//    @Security    ApiKeyAuth
//    @accept        application/json
//    @Produce    application/json
//    @Param        data    body    request.AddAuthority    true    "权限id, 权限名, 父角色id"
//    @Router        /authority/updateAuthority [post]
//func (a *AuthorityApi) UpdateAuthority(c *gin.Context) {
//    var auth system.SysAuthority
//    err := c.ShouldBindJSON(&auth)
//    if err != nil {
//        response.FailWithMessage(err.Error(), c)
//        return
//    }
//    err = utils.Verify(auth, utils.AuthorityVerify)
//    if err != nil {
//        response.FailWithMessage(err.Error(), c)
//        return
//    }
//    authority, err := authorityService.UpdateAuthority(auth)
//    if err != nil {
//        global.GVA_LOG.Error("更新失败!", zap.Error(err))
//        response.FailWithMessage("更新失败"+err.Error(), c)
//        return
//    }
//    response.OkWithDetailed(systemRes.SysAuthorityResponse{Authority: authority}, "更新成功", c)
//}
 
// GetAuthorityList
//    @Tags        Authority
//    @Summary    分页获取角色列表
//    @Security    ApiKeyAuth
//    @accept        application/json
//    @Produce    application/json
//    @Param        data    body    request.AddAuthority    true    "页码, 每页大小"
//    @Router        /authority/getAuthorityList [post]
//func (a *AuthorityApi) GetAuthorityList(c *gin.Context) {
//    var pageInfo request.PageInfo
//    err := c.ShouldBindJSON(&pageInfo)
//    if err != nil {
//        response.FailWithMessage(err.Error(), c)
//        return
//    }
//    err = utils.Verify(pageInfo, utils.PageInfoVerify)
//    if err != nil {
//        response.FailWithMessage(err.Error(), c)
//        return
//    }
//    list, total, err := authorityService.GetAuthorityInfoList(pageInfo)
//    if err != nil {
//        global.GVA_LOG.Error("获取失败!", zap.Error(err))
//        response.FailWithMessage("获取失败"+err.Error(), c)
//        return
//    }
//    response.OkWithDetailed(response.PageResult{
//        List:     list,
//        Total:    total,
//        Page:     pageInfo.Page,
//        PageSize: pageInfo.PageSize,
//    }, "获取成功", c)
//}
 
// SetMenuAuthority
//
//    @Tags        Authority
//    @Summary    设置角色菜单
//    @Security    ApiKeyAuth
//    @accept        application/json
//    @Produce    application/json
//    @Param        data    body        request.SetAuthorityMenu    true    "设置角色资源权限"
//    @Success    200        {object}    contextx.Response{}
//    @Router        /api/authority/setMenuAuthority [post]
func (a *AuthorityApi) SetMenuAuthority(c *gin.Context) {
    var params request.SetAuthorityMenu
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
 
    menus, errCode := menuService.GetMenusByIds(params.Menus)
    if errCode != ecode.OK {
        ctx.Fail(ecode.MenuListErr)
        return
    }
 
    auth := model.Authority{
        Id:    params.AuthorityId,
        Menus: menus,
    }
 
    err := authorityService.SetMenuAuthority(&auth)
    if err != nil {
        ctx.Fail(ecode.SetMenuAuthorityErr)
        return
    }
 
    ctx.Ok()
}
 
func checkAuthorityParams(params *request.AddAuthority) (int, model.Authority) {
    return ecode.OK, model.Authority{
        AuthorityName: params.AuthorityName,
    }
}