-
Notifications
You must be signed in to change notification settings - Fork 2
/
role.go
150 lines (143 loc) · 4.34 KB
/
role.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
package handler
import (
"gitee.com/cruvie/kk_go_kit/kk_http"
"gitee.com/cruvie/kk_go_kit/kk_models/kk_pb_type"
"github.com/cruvie/kk_etcd_go/internal/handler/service"
"github.com/cruvie/kk_etcd_go/internal/utils/check_user"
"github.com/cruvie/kk_etcd_go/internal/utils/global_model"
"github.com/cruvie/kk_etcd_go/kk_etcd_const"
"github.com/cruvie/kk_etcd_go/kk_etcd_models"
"github.com/gin-gonic/gin"
"log/slog"
)
// RoleAdd
//
// @Description Add role
// @Accept octet-stream
// @Produce octet-stream
// @Param pbRole body kk_etcd_models.PBRole true "Add role info"
// @Router /RoleAdd [post]
func RoleAdd(c *gin.Context) {
stage := global_model.GetRequestStage(c)
if !check_user.CheckRootRole(stage) {
kk_http.ResponseProtoBuf(c, kk_http.Fail(stage, &kk_pb_type.PBResponse{
Msg: "you don't have root role!"}, nil))
return
}
var pbRole kk_etcd_models.PBRole
if err := kk_http.ReadProtoBuf(stage, &pbRole); err != nil {
kk_http.ResponseProtoBuf(c, kk_http.Fail(stage, nil, nil))
return
}
res := service.RoleAdd(stage, &pbRole)
switch res {
case 1:
kk_http.ResponseProtoBuf(c, kk_http.Success(stage, nil, nil))
return
}
kk_http.ResponseProtoBuf(c, kk_http.Fail(stage, nil, nil))
}
// RoleDelete
//
// @Description Delete role
// @Accept octet-stream
// @Produce octet-stream
// @Param pbRole body kk_etcd_models.PBRole true "Delete role info"
// @Router /RoleDelete [post]
func RoleDelete(c *gin.Context) {
stage := global_model.GetRequestStage(c)
if !check_user.CheckRootRole(stage) {
kk_http.ResponseProtoBuf(c, kk_http.Fail(stage, &kk_pb_type.PBResponse{
Msg: "you don't have root role!"}, nil))
return
}
var pbRole kk_etcd_models.PBRole
if err := kk_http.ReadProtoBuf(stage, &pbRole); err != nil {
slog.Info("failed to read protobuf", "err", err)
kk_http.ResponseProtoBuf(c, kk_http.Fail(stage, nil, nil))
return
}
res := service.RoleDelete(stage, pbRole.Name)
switch res {
case 1:
kk_http.ResponseProtoBuf(c, kk_http.Success(stage, nil, nil))
return
case -1:
kk_http.ResponseProtoBuf(c, kk_http.Fail(stage, &kk_pb_type.PBResponse{
Msg: "illegal delete root role!"}, nil))
return
}
kk_http.ResponseProtoBuf(c, kk_http.Fail(stage, nil, nil))
}
// RoleList
//
// @Description Get role list
// @Accept octet-stream
// @Produce octet-stream
// @Router /RoleList [post]
func RoleList(c *gin.Context) {
stage := global_model.GetRequestStage(c)
res, roles := service.RoleList(stage)
switch res {
case 1:
kk_http.ResponseProtoBuf(c, kk_http.Success(stage, nil, roles))
return
}
kk_http.ResponseProtoBuf(c, kk_http.Fail(stage, nil, nil))
}
// RoleGet
//
// @Description Get role
// @Accept octet-stream
// @Produce octet-stream
// @Param pbRole body kk_etcd_models.PBRole true "Get role info"
// @Router /RoleGet [post]
func RoleGet(c *gin.Context) {
stage := global_model.GetRequestStage(c)
var pbRole kk_etcd_models.PBRole
if err := kk_http.ReadProtoBuf(stage, &pbRole); err != nil {
slog.Info("failed to read protobuf", "err", err)
kk_http.ResponseProtoBuf(c, kk_http.Fail(stage, nil, nil))
return
}
role, res := service.RoleGet(stage, pbRole.Name)
switch res {
case 1:
kk_http.ResponseProtoBuf(c, kk_http.Success(stage, nil, role))
return
}
kk_http.ResponseProtoBuf(c, kk_http.Fail(stage, nil, nil))
}
// RoleGrantPermission
//
// @Description Grant permission to role
// @Accept octet-stream
// @Produce octet-stream
// @Param pbRole body kk_etcd_models.PBRole true "Grant permission to role info"
// @Router /RoleGrantPermission [post]
func RoleGrantPermission(c *gin.Context) {
stage := global_model.GetRequestStage(c)
if !check_user.CheckRootRole(stage) {
kk_http.ResponseProtoBuf(c, kk_http.Fail(stage, &kk_pb_type.PBResponse{
Msg: "you don't have root role!"}, nil))
return
}
var pbRole kk_etcd_models.PBRole
if err := kk_http.ReadProtoBuf(stage, &pbRole); err != nil {
slog.Info("failed to read protobuf", "err", err)
kk_http.ResponseProtoBuf(c, kk_http.Fail(stage, nil, nil))
return
}
if pbRole.Name == kk_etcd_const.RoleRoot {
kk_http.ResponseProtoBuf(c, kk_http.Fail(stage, &kk_pb_type.PBResponse{
Msg: "illegal change root role permission!"}, nil))
return
}
res := service.RoleGrantPermission(stage, &pbRole)
switch res {
case 1:
kk_http.ResponseProtoBuf(c, kk_http.Success(stage, nil, nil))
return
}
kk_http.ResponseProtoBuf(c, kk_http.Fail(stage, nil, nil))
}