/
role.go
154 lines (124 loc) · 3.47 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
151
152
153
154
// This file is part of the Smart Home
// Program complex distribution https://github.com/e154/smart-home
// Copyright (C) 2016-2020, Filippov Alex
//
// This library is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library. If not, see
// <https://www.gnu.org/licenses/>.
package adaptors
import (
"github.com/jinzhu/gorm"
"github.com/e154/smart-home/db"
m "github.com/e154/smart-home/models"
)
type Role struct {
table *db.Roles
db *gorm.DB
}
func GetRoleAdaptor(d *gorm.DB) *Role {
return &Role{
table: &db.Roles{Db: d},
db: d,
}
}
func (n *Role) Add(role *m.Role) (err error) {
dbRole := n.toDb(role)
err = n.table.Add(dbRole)
return
}
func (n *Role) GetByName(name string) (role *m.Role, err error) {
var dbRole *db.Role
if dbRole, err = n.table.GetByName(name); err != nil {
return
}
role = n.fromDb(dbRole)
err = n.GetAccessList(role)
return
}
func (n *Role) Update(role *m.Role) (err error) {
dbRole := n.toDb(role)
err = n.table.Update(dbRole)
return
}
func (n *Role) Delete(name string) (err error) {
err = n.table.Delete(name)
return
}
func (n *Role) List(limit, offset int64, orderBy, sort string) (list []*m.Role, total int64, err error) {
var dbList []*db.Role
if dbList, total, err = n.table.List(limit, offset, orderBy, sort); err != nil {
return
}
list = make([]*m.Role, 0)
for _, dbRole := range dbList {
role := n.fromDb(dbRole)
list = append(list, role)
}
return
}
func (n *Role) Search(query string, limit, offset int) (list []*m.Role, total int64, err error) {
var dbList []*db.Role
if dbList, total, err = n.table.Search(query, limit, offset); err != nil {
return
}
list = make([]*m.Role, 0)
for _, dbRole := range dbList {
role := n.fromDb(dbRole)
list = append(list, role)
}
return
}
func (n *Role) GetAccessList(role *m.Role) (err error) {
role.AccessList = make(map[string][]string)
permissionAdaptor := GetPermissionAdaptor(n.db)
var permissions []*m.Permission
if permissions, err = permissionAdaptor.GetAllPermissions(role.Name); err != nil {
return
}
for _, perm := range permissions {
if _, ok := role.AccessList[perm.PackageName]; !ok {
role.AccessList[perm.PackageName] = []string{}
}
role.AccessList[perm.PackageName] = append(role.AccessList[perm.PackageName], perm.LevelName)
}
return
}
func (n *Role) fromDb(dbRole *db.Role) (role *m.Role) {
role = &m.Role{
Name: dbRole.Name,
Description: dbRole.Description,
CreatedAt: dbRole.CreatedAt,
UpdatedAt: dbRole.UpdatedAt,
Children: []*m.Role{},
}
if dbRole.Role != nil {
role.Parent = n.fromDb(dbRole.Role)
}
if len(dbRole.Children) > 0 {
for _, dbCh := range dbRole.Children {
ch := n.fromDb(dbCh)
role.Children = append(role.Children, ch)
}
}
return
}
func (n *Role) toDb(role *m.Role) (dbRole *db.Role) {
dbRole = &db.Role{
Name: role.Name,
Description: role.Description,
}
if role.Parent !=nil {
dbRole.RoleName.Scan(role.Parent.Name)
}
return
}