-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_role_logic.go
58 lines (51 loc) · 1.57 KB
/
add_role_logic.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
package role
import (
"context"
"errors"
"fmt"
"github.com/haozheyu/oam_system/admin-api/model/user"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"time"
"github.com/haozheyu/oam_system/admin-api/internal/svc"
"github.com/haozheyu/oam_system/admin-api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type AddRoleLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewAddRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddRoleLogic {
return &AddRoleLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *AddRoleLogic) AddRole(req *types.AddRoleReq) (resp *types.AddRoleResp, err error) {
userName := fmt.Sprintf("%s", l.ctx.Value("name"))
srcUser, err := l.svcCtx.UserModel.FindOneByName(l.ctx, userName)
switch err {
case nil:
if srcUser.RoleId != 1 {
return nil, errors.New("非系统管理员,不能添加角色")
}
_, err := l.svcCtx.UserRoleModel.Insert(l.ctx, &user.OamUserRole{
Name: req.Name,
RoleType: 8, // 自定义角色类型
CreateBy: userName,
CreateTime: time.Now().Unix(),
LastUpdateBy: userName,
LastUpdateTime: time.Now().Unix(),
DelFlag: 0,
Status: 0,
})
return &types.AddRoleResp{Message: "ok"}, err
case sqlc.ErrNotFound:
logx.WithContext(l.ctx).Errorf("用户不存在,参数:%s,异常:%s", srcUser.Name, err.Error())
return nil, errors.New("用户不存在")
default:
logx.WithContext(l.ctx).Errorf("token 解析,参数:%s,异常:%s", srcUser.Name, err.Error())
return nil, err
}
}