/
user_roles.go
164 lines (157 loc) · 4.05 KB
/
user_roles.go
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
// 用户与角色管理
// 1. 删除用户与角色关系
// 2. 新增用户与角色关系
// 3. 用户与角色关系列表和搜索
package controllers
import (
"context"
"fmt"
"strings"
"time"
"github.com/1046102779/grbac/common/consts"
"github.com/1046102779/grbac/conf"
pb "github.com/1046102779/grbac/igrpc"
. "github.com/1046102779/grbac/logger"
"github.com/1046102779/grbac/models"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)
// UserRolesController operations for UserRoles
type UserRolesController struct {
beego.Controller
}
// 1. 删除用户与角色关系
// @router /:id/invalid [PUT]
func (t *UserRolesController) DeleteUserRoles() {
id, _ := t.GetInt(":id")
if id <= 0 {
err := errors.New("param `:id` empty")
Logger.Error(err.Error())
t.Data["json"] = map[string]interface{}{
"err_code": consts.ERROR_CODE__SOURCE_DATA__ILLEGAL,
"err_msg": errors.Cause(err).Error(),
}
t.ServeJSON()
return
}
o := orm.NewOrm()
now := time.Now()
userRole := &models.UserRole{
UserRoleId: id,
}
if retcode, err := userRole.ReadUserRoleNoLock(&o); err != nil {
Logger.Error(err.Error())
t.Data["json"] = map[string]interface{}{
"err_code": retcode,
"err_msg": errors.Cause(err).Error(),
}
t.ServeJSON()
return
}
userRole.Status = consts.STATUS_DELETED
userRole.UpdatedAt = now
if retcode, err := userRole.UpdateUserRoleNoLock(&o); err != nil {
Logger.Error(err.Error())
t.Data["json"] = map[string]interface{}{
"err_code": retcode,
"err_msg": errors.Cause(err).Error(),
}
t.ServeJSON()
return
}
t.Data["json"] = map[string]interface{}{
"err_code": 0,
"err_msg": "",
}
t.ServeJSON()
return
}
// 2. 新增用户与角色关系
// @router / [POST]
func (t *UserRolesController) InsertUserRole() {
type UserInfo struct {
Mobile string `json:"mobile"`
RoleId int `json:"role_id"`
}
var (
info *UserInfo = new(UserInfo)
)
if err := jsoniter.Unmarshal(t.Ctx.Input.RequestBody, info); err != nil {
Logger.Error(err.Error())
t.Data["json"] = map[string]interface{}{
"err_code": consts.ERROR_CODE__JSON__PARSE_FAILED,
"err_msg": errors.Cause(err).Error(),
}
t.ServeJSON()
return
}
if strings.TrimSpace(info.Mobile) == "" || info.RoleId <= 0 {
err := errors.New("param `mobile | role_id` empty")
t.Data["json"] = map[string]interface{}{
"err_code": consts.ERROR_CODE__SOURCE_DATA__ILLEGAL,
"err_msg": errors.Cause(err).Error(),
}
t.ServeJSON()
return
}
// rpc服务,通过手机号,获取用户ID
user := &pb.User{
Mobile: info.Mobile,
}
conf.AccountClient.Call(context.Background(), fmt.Sprintf("%s.%s", "accounts", "GetUserByMobile"), user, user)
// 通过角色名称, 获取角色ID
// 新增角色与用户的关系
o := orm.NewOrm()
now := time.Now()
userRole := &models.UserRole{
UserId: int(user.UserId),
RoleId: info.RoleId,
Status: consts.STATUS_VALID,
UpdatedAt: now,
CreatedAt: now,
}
if retcode, err := userRole.InsertUserRoleNoLock(&o); err != nil {
Logger.Error(err.Error())
t.Data["json"] = map[string]interface{}{
"err_code": retcode,
"err_msg": errors.Cause(err).Error(),
}
t.ServeJSON()
return
}
t.Data["json"] = map[string]interface{}{
"err_code": 0,
"err_msg": "",
}
t.ServeJSON()
return
}
// 3. 用户与角色关系列表和搜索
// @router / [GET]
func (t *UserRolesController) GetUserRoles() {
pageIndex, _ := t.GetInt64("page_index", 1)
pageSize, _ := t.GetInt64("page_size", 100)
roleId, _ := t.GetInt("role_id")
searchKey := t.GetString("search_key")
userRoles, count, realCount, retcode, err := models.GetUserRoles(pageIndex-1, pageSize, roleId, searchKey)
if err != nil {
Logger.Error(err.Error())
t.Data["json"] = map[string]interface{}{
"err_code": retcode,
"err_msg": errors.Cause(err).Error(),
}
t.ServeJSON()
return
}
t.Data["json"] = map[string]interface{}{
"err_code": 0,
"err_msg": "",
"count": count,
"real_count": realCount,
"user_roles": userRoles,
}
t.ServeJSON()
return
}