Repository navigation
8000
More file actions
Expand file tree
/
Copy pathsys_role.go
More file actions
156 lines (143 loc) · 4.05 KB
/
Copy pathsys_role.go
File metadata and controls
156 lines (143 loc) · 4.05 KB
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
package v1
import (
"gin-web/models"
"gin-web/pkg/cache_service"
"gin-web/pkg/request"
"gin-web/pkg/response"
"gin-web/pkg/service"
"github.com/gin-gonic/gin"
"github.com/piupuer/go-helper/pkg/req"
"github.com/piupuer/go-helper/pkg/resp"
"github.com/piupuer/go-helper/pkg/tracing"
"github.com/piupuer/go-helper/pkg/utils"
)
// FindRoleByIds
// @Security Bearer
// @Accept json
// @Produce json
// @Success 201 {object} resp.Resp "success"
// @Tags Role
// @Description FindRoleByIds
// @Param ids path string true "ids"
// @Router /role/list/{ids} [GET]
func FindRoleByIds(c *gin.Context) {
ctx := tracing.RealCtx(c)
_, span := tracer.Start(ctx, tracing.Name(tracing.Rest, "FindRoleByIds"))
defer span.End()
ids := req.UintIds(c)
cs := cache_service.New(c)
list := cs.FindRoleByIds(ids)
resp.SuccessWithData(list, &[]response.Role{})
}
// FindRole
// @Security Bearer
// @Accept json
// @Produce json
// @Success 201 {object} resp.Resp "success"
// @Tags Role
// @Description FindRole
// @Param params query request.Role true "params"
// @Router /role/list [GET]
func FindRole(c *gin.Context) {
ctx := tracing.RealCtx(c)
_, span := tracer.Start(ctx, tracing.Name(tracing.Rest, "FindRole"))
defer span.End()
var r request.Role
req.ShouldBind(c, &r)
user := GetCurrentUser(c)
// bind current user role sort(low level cannot view high level)
r.CurrentRoleSort = *user.Role.Sort
cs := cache_service.New(c)
list := cs.FindRole(&r)
resp.SuccessWithPageData(list, &[]response.Role{}, r.Page)
}
// CreateRole
// @Security Bearer
// @Accept json
// @Produce json
// @Success 201 {object} resp.Resp "success"
// @Tags Role
// @Description CreateRole
// @Param params body request.CreateRole true "params"
// @Router /role/create [POST]
func CreateRole(c *gin.Context) {
ctx := tracing.RealCtx(c)
_, span := tracer.Start(ctx, tracing.Name(tracing.Rest, "CreateRole"))
defer span.End()
user := GetCurrentUser(c)
var r request.CreateRole
req.ShouldBind(c, &r)
req.Validate(c, r, r.FieldTrans())
if r.Sort != nil && *user.Role.Sort > uint(*r.Sort) {
resp.CheckErr("sort must >= %d", *user.Role.Sort)
}
my := service.New(c)
err := my.Q.Create(r, new(models.SysRole))
resp.CheckErr(err)
resp.Success()
}
// UpdateRoleById
// @Security Bearer
// @Accept json
// @Produce json
// @Success 201 {object} resp.Resp "success"
// @Tags Role
// @Description UpdateRoleById
// @Param id path uint true "id"
// @Param params body request.UpdateRole true "params"
// @Router /role/update/{id} [PATCH]
func UpdateRoleById(c *gin.Context) {
ctx := tracing.RealCtx(c)
_, span := tracer.Start(ctx, tracing.Name(tracing.Rest, "UpdateRoleById"))
defer span.End()
var r request.UpdateRole
req.ShouldBind(c, &r)
id := req.UintId(c)
if r.Sort != nil {
user := GetCurrentUser(c)
if r.Sort != nil && *user.Role.Sort > uint(*r.Sort) {
resp.CheckErr("sort must >= %d", *user.Role.Sort)
}
}
user := GetCurrentUser(c)
if r.Status != nil && uint(*r.Status) == models.SysRoleStatusDisabled && id == user.RoleId {
resp.CheckErr("cannot disable your role")
}
my := service.New(c)
err := my.Q.UpdateById(id, r, new(models.SysRole))
resp.CheckErr(err)
resp.Success()
}
func RouterFindRoleKeywordByRoleIds(c *gin.Context, roleIds []uint) []string {
cs := cache_service.New(c)
roles := cs.FindRoleByIds(roleIds)
keywords := make([]string, 0)
for _, role := range roles {
keywords = append(keywords, role.Keyword)
}
return keywords
}
// BatchDeleteRoleByIds
// @Security Bearer
// @Accept json
// @Produce json
// @Success 201 {object} resp.Resp "success"
// @Tags Role
// @Description BatchDeleteRoleByIds
// @Param ids body req.Ids true "ids"
// @Router /role/delete/batch [DELETE]
func BatchDeleteRoleByIds(c *gin.Context) {
ctx := tracing.RealCtx(c)
_, span := tracer.Start(ctx, tracing.Name(tracing.Rest, "BatchDeleteRoleByIds"))
defer span.End()
var r req.Ids
req.ShouldBind(c, &r)
user := GetCurrentUser(c)
if utils.ContainsUint(r.Uints(), user.RoleId) {
resp.CheckErr("cannot delete your role")
}
my := service.New(c)
err := my.DeleteRoleByIds(r.Uints())
resp.CheckErr(err)
resp.Success()
}