-
Notifications
You must be signed in to change notification settings - Fork 0
/
permission.go
316 lines (294 loc) · 7.97 KB
/
permission.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package models
import (
"database/sql"
"encoding/json"
"io"
"code.google.com/p/go-uuid/uuid"
"github.com/hashicorp/go-multierror"
"github.com/mistifyio/mistify-operator-admin/db"
)
type (
// Permission represents an action that can be taken on an entity by a user
Permission struct {
ID string `json:"id"`
Name string `json:"name"`
Service string `json:"service"`
Action string `json:"action"`
EntityType string `json:"entityType"`
Owner bool `json:"owner"`
Description string `json:"description"`
Metadata map[string]string `json:"metadata"`
Projects []*Project `json:"-"`
}
)
// id returns the ID, required by the relatable interface
func (permission *Permission) id() string {
return permission.ID
}
// pkeyName returns the database primary key name, required by the relatable
// interface
func (permission *Permission) pkeyName() string {
return "permission_id"
}
// Validate ensures the permission properties are set correctly
func (permission *Permission) Validate() error {
var results *multierror.Error
if permission.ID == "" {
results = multierror.Append(results, ErrNoID)
}
if uuid.Parse(permission.ID) == nil {
results = multierror.Append(results, ErrBadID)
}
if permission.Service == "" {
results = multierror.Append(results, ErrNoService)
}
if permission.Action == "" {
results = multierror.Append(results, ErrNoAction)
}
if permission.Metadata == nil {
results = multierror.Append(results, ErrNilMetadata)
}
return results.ErrorOrNil()
}
// Save persists the permission to the database
func (permission *Permission) Save() error {
if err := permission.Validate(); err != nil {
return err
}
d, err := db.Connect(nil)
if err != nil {
return err
}
// Writable CTE for an Upsert
// See: http://stackoverflow.com/a/8702291
// And: http://dba.stackexchange.com/a/78535
sql := `
WITH new_values (permission_id, name, service, action, entitytype, owner,
description, metadata) AS (
VALUES ($1::uuid, $2, $3, $4, $5, $6::boolean, $7, $8::json)
),
upsert as (
UPDATE permissions p SET
permission_id = nv.permission_id,
name = nv.name,
service = nv.service,
action = nv.action,
entitytype = nv.entitytype,
owner = nv.owner,
metadata = nv.metadata
FROM new_values nv
WHERE p.permission_id = nv.permission_id
RETURNING nv.permission_id
)
INSERT INTO permissions
(permission_id, name, service, action, entitytype, owner, description,
metadata)
SELECT permission_id, name, service, action, entitytype, owner, description,
metadata
FROM new_values nv
WHERE NOT EXISTS
(SELECT 1 FROM upsert u WHERE nv.permission_id = u.permission_id)
`
metadata, err := json.Marshal(permission.Metadata)
if err != nil {
return err
}
_, err = d.Exec(sql,
permission.ID,
permission.Name,
permission.Service,
permission.Action,
permission.EntityType,
permission.Owner,
permission.Description,
string(metadata),
)
return err
}
// Delete deletes the permission from the database
func (permission *Permission) Delete() error {
d, err := db.Connect(nil)
if err != nil {
return err
}
sql := "DELETE FROM permissions WHERE permission_id = $1"
_, err = d.Exec(sql, permission.ID)
return err
}
// Load retrieves the permission from the database
func (permission *Permission) Load() error {
d, err := db.Connect(nil)
if err != nil {
return err
}
sql := `
SELECT permission_id, name, service, action, entitytype, owner, description,
metadata
FROM permissions
WHERE permission_id = $1
`
rows, err := d.Query(sql, permission.ID)
if err != nil {
return err
}
defer rows.Close()
rows.Next()
if err := permission.fromRows(rows); err != nil {
return err
}
return rows.Err()
}
// fromRows unmarshals a database query result row into the permission object
func (permission *Permission) fromRows(rows *sql.Rows) error {
var metadata string
err := rows.Scan(
&permission.ID,
&permission.Name,
&permission.Service,
&permission.Action,
&permission.EntityType,
&permission.Owner,
&permission.Description,
&metadata,
)
if err != nil {
return err
}
return json.Unmarshal([]byte(metadata), &permission.Metadata)
}
// Decode unmarshals JSON into the permission object
func (permission *Permission) Decode(data io.Reader) error {
if err := json.NewDecoder(data).Decode(permission); err != nil {
return err
}
if permission.Metadata == nil {
permission.Metadata = make(map[string]string)
} else {
for key, value := range permission.Metadata {
if value == "" {
delete(permission.Metadata, key)
}
}
}
return nil
}
// LoadProjects retrieves the projects associated with the permission
func (permission *Permission) LoadProjects() error {
projects, err := ProjectsByPermission(permission)
if err != nil {
return err
}
permission.Projects = projects
return nil
}
// SetProjects creates and ensures the only relations the permission has with
// projects
func (permission *Permission) SetProjects(projects []*Project) error {
if len(projects) == 0 {
return ClearRelations("projects_permissions", permission)
}
relatables := make([]relatable, len(projects))
for i, project := range projects {
relatables[i] = relatable(project)
}
if err := SetRelations("projects_permissions", permission, relatables); err != nil {
return err
}
return permission.LoadProjects()
}
// AddProject adds a relation to a project
func (permission *Permission) AddProject(project *Project) error {
return AddRelation("projects_permissions", permission, project)
}
// RemoveProject removes a relation with a project
func (permission *Permission) RemoveProject(project *Project) error {
return RemoveRelation("projects_permissions", permission, project)
}
// NewID generates a new uuid ID
func (permission *Permission) NewID() string {
permission.ID = uuid.New()
return permission.ID
}
// NewPermission creates and initializes a new permission object
func NewPermission() *Permission {
permission := &Permission{
ID: uuid.New(),
Metadata: make(map[string]string),
}
return permission
}
// FetchPermission retrieves a permission object from the database by ID
func FetchPermission(id string) (*Permission, error) {
permission := &Permission{
ID: id,
}
err := permission.Load()
if err != nil {
return nil, err
}
return permission, nil
}
// ListPermissions retrieve an array of all permission objects from the database
func ListPermissions() ([]*Permission, error) {
d, err := db.Connect(nil)
if err != nil {
return nil, err
}
sql := `
SELECT permission_id, name, service, action, entitytype, owner, description,
metadata
FROM permissions
ORDER BY permission_id asc
`
rows, err := d.Query(sql)
if err != nil {
return nil, err
}
permissions, err := permissionsFromRows(rows)
if err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return permissions, nil
}
// PermissionsByProject retrieves an array of permission related to a project
func PermissionsByProject(project *Project) ([]*Permission, error) {
d, err := db.Connect(nil)
if err != nil {
return nil, err
}
sql := `
SELECT p.permission_id, p.name, p.service, p.action, p.entitytype, p.owner,
p.description, p.metadata
FROM permissions p
JOIN projects_permissions pp ON p.permission_id = pp.permission_id
WHERE pp.project_id = $1
ORDER BY permission_id asc
`
rows, err := d.Query(sql, project.ID)
if err != nil {
return nil, err
}
permissions, err := permissionsFromRows(rows)
if err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return permissions, nil
}
// permissionsFromRows unmarshals multiple query rows into an array of permissions
func permissionsFromRows(rows *sql.Rows) ([]*Permission, error) {
permissions := make([]*Permission, 0, 1)
for rows.Next() {
permission := &Permission{}
if err := permission.fromRows(rows); err != nil {
return nil, err
}
permissions = append(permissions, permission)
}
return permissions, nil
}