-
Notifications
You must be signed in to change notification settings - Fork 0
/
role_ctrl.go
84 lines (75 loc) · 1.98 KB
/
role_ctrl.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
package admin
import (
"github.com/GoodHot/TinyCMS/common/ctrl"
"github.com/GoodHot/TinyCMS/model"
"github.com/GoodHot/TinyCMS/orm"
"github.com/GoodHot/TinyCMS/service"
)
type RoleCtrl struct {
RoleService *service.RoleService `ioc:"auto"`
}
func (s *RoleCtrl) AllPermission(ctx *ctrl.HTTPContext) error {
ctx.Put("permission", s.RoleService.AllPermission())
return ctx.ResultOK()
}
func (s *RoleCtrl) InitRole(ctx *ctrl.HTTPContext) error {
s.RoleService.Init()
return ctx.ResultOK()
}
type RoleForm struct {
ID uint `json:"id"`
Code string `json:"code"`
Name string `json:"name"`
Permission []uint `json:"permission"`
}
func (s *RoleCtrl) Save(ctx *ctrl.HTTPContext) error {
form := new(RoleForm)
if err := ctx.Bind(form); err != nil {
return ctx.ResultErr(err.Error())
}
var dbRole *model.Role
if form.ID != 0 {
dbRole, _ = s.RoleService.Get(int(form.ID))
}
if (dbRole != nil && !dbRole.IsSuper) && (form.Permission == nil || len(form.Permission) == 0) {
return ctx.ResultErr("请选择角色拥有权限")
}
isSuper := dbRole != nil && dbRole.IsSuper
role := model.Role{
Model: orm.Model{
ID: form.ID,
},
Code: form.Code,
RoleName: form.Name,
IsSuper: isSuper,
}
var permission []*model.Permission
for _, p := range form.Permission {
permission = append(permission, &model.Permission{
Model: orm.Model{ID: p},
})
}
err := s.RoleService.Save(role, permission)
if err != nil {
return ctx.ResultErr(err.Error())
}
return ctx.ResultOK()
}
func (s *RoleCtrl) Page(ctx *ctrl.HTTPContext) error {
page := ctx.ParamInt("page")
ctx.Put("page", s.RoleService.Page(page))
return ctx.ResultOK()
}
func (s *RoleCtrl) All(ctx *ctrl.HTTPContext) error {
ctx.Put("roles", s.RoleService.All())
return ctx.ResultOK()
}
func (s *RoleCtrl) Get(ctx *ctrl.HTTPContext) error {
id := ctx.ParamInt("id")
role, err := s.RoleService.Get(id)
if err != nil {
return ctx.ResultErr(err.Error())
}
ctx.Put("role", role)
return ctx.ResultOK()
}