-
Notifications
You must be signed in to change notification settings - Fork 180
/
roleResourceController.go
190 lines (173 loc) · 4.85 KB
/
roleResourceController.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package controllers
import (
"html/template"
"github.com/astaxie/beego/context"
"github.com/hzwy23/hauth/core/hrpc"
"github.com/hzwy23/hauth/core/models"
"github.com/hzwy23/hauth/utils/hret"
"github.com/hzwy23/hauth/utils/i18n"
"github.com/hzwy23/hauth/utils/logs"
"github.com/hzwy23/hauth/utils/validator"
)
type roleAndResourceController struct {
model *models.RoleModel
resRoleModel *models.RoleAndResourceModel
resModel *models.ResourceModel
}
var RoleAndResourceCtl = &roleAndResourceController{
new(models.RoleModel),
new(models.RoleAndResourceModel),
new(models.ResourceModel),
}
// swagger:operation GET /v1/auth/role/resource/details StaticFiles domainShareControll
//
// 角色菜单资源配置管理页面
//
// 如果用户被授权,将会返回指定角色资源管理页面.
// ---
// produces:
// - application/json
// - application/xml
// - text/xml
// - text/html
// parameters:
// - name: domain_id
// in: query
// description: domain code number
// required: true
// type: string
// format:
// responses:
// '200':
// description: success
func (this roleAndResourceController) ResourcePage(ctx *context.Context) {
ctx.Request.ParseForm()
if !hrpc.BasicAuth(ctx.Request) {
hret.Error(ctx.ResponseWriter, 403, i18n.NoAuth(ctx.Request))
return
}
var role_id = ctx.Request.FormValue("role_id")
rst, err := this.model.GetRow(role_id)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 419, i18n.Get(ctx.Request, "error_role_resource_query"))
return
}
file, _ := template.ParseFiles("./views/hauth/res_role_rel_page.tpl")
file.Execute(ctx.ResponseWriter, rst)
}
// swagger:operation GET /v1/auth/role/resource/get roleAndResourceController roleAndResourceController
//
// 查询角色指定的拥有的菜单资源和没有拥有的菜单资源
//
// type_id = 0 表示查询角色拥有的菜单资源, type_id = 1 表示查询角色没有获取的菜单资源
//
// ---
// produces:
// - application/json
// - application/xml
// - text/xml
// - text/html
// parameters:
// - name: domain_id
// in: query
// description: domain code number
// required: true
// type: string
// format:
// responses:
// '200':
// description: success
func (this roleAndResourceController) GetResource(ctx *context.Context) {
ctx.Request.ParseForm()
if !hrpc.BasicAuth(ctx.Request) {
hret.Error(ctx.ResponseWriter, 403, i18n.NoAuth(ctx.Request))
return
}
role_id := ctx.Request.FormValue("role_id")
type_id := ctx.Request.FormValue("type_id")
if type_id == "0" {
// 查询角色已经获取到的资源信息
rst, err := this.resRoleModel.Get(role_id)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 419, i18n.Get(ctx.Request, "error_role_get_resource"))
return
}
hret.Json(ctx.ResponseWriter, rst)
} else if type_id == "1" {
// 查询角色没有获取到的资源信息
rst, err := this.resRoleModel.UnGetted(role_id)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 419, i18n.Get(ctx.Request, "error_role_unget_resource"))
return
}
hret.Json(ctx.ResponseWriter, rst)
}
}
// swagger:operation POST /v1/auth/role/resource/rights roleAndResourceController roleAndResourceController
//
// 授予角色菜单资源或删除角色菜单资源
//
// type_id = 0 表示移除某个指定角色的菜单资源.
//
// type_id = 1 表示给某个指定角色增加菜单资源.
//
// ---
// produces:
// - application/json
// - application/xml
// - text/xml
// - text/html
// parameters:
// - name: domain_id
// in: query
// description: domain code number
// required: true
// type: string
// format:
// responses:
// '200':
// description: success
func (this roleAndResourceController) HandleResource(ctx *context.Context) {
ctx.Request.ParseForm()
if !hrpc.BasicAuth(ctx.Request) {
hret.Error(ctx.ResponseWriter, 403, i18n.NoAuth(ctx.Request))
return
}
res_id := ctx.Request.FormValue("res_id")
role_id := ctx.Request.FormValue("role_id")
type_id := ctx.Request.FormValue("type_id")
if !validator.IsWord(res_id) {
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "error_resource_res_id"))
return
}
if !validator.IsWord(role_id) {
hret.Error(ctx.ResponseWriter, 421, i18n.Get(ctx.Request, "error_role_id_format"))
return
}
// 撤销权限操作
if type_id == "0" {
err := this.resRoleModel.Delete(role_id, res_id)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 419, i18n.Get(ctx.Request, "error_role_delete_failed"))
return
} else {
hret.Success(ctx.ResponseWriter, i18n.Success(ctx.Request))
return
}
} else {
//授权操作
err := this.resRoleModel.Post(role_id, res_id)
if err != nil {
logs.Error(err)
hret.Error(ctx.ResponseWriter, 419, i18n.Get(ctx.Request, "error_role_delete_failed"))
return
} else {
hret.Success(ctx.ResponseWriter, i18n.Success(ctx.Request))
return
}
}
}