-
Notifications
You must be signed in to change notification settings - Fork 59
/
role.go
384 lines (340 loc) · 8.08 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// (c) Copyright 2015-2017 JONNALAGADDA Srinivas
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package flow
import (
"database/sql"
"errors"
"fmt"
"math"
"strings"
)
// RoleID is the type of unique role identifiers.
type RoleID int64
// Role represents a collection of privileges.
//
// Each group in the system can have one or more roles assigned.
type Role struct {
ID RoleID `json:"ID"` // globally-unique ID of this role
Name string `json:"Name"` // name of this role
}
// Unexported type, only for convenience methods.
type _Roles struct{}
// Roles provides a resource-like interface to roles in the system.
var Roles _Roles
// New creates a role with the given name.
func (_Roles) New(otx *sql.Tx, name string) (RoleID, error) {
name = strings.TrimSpace(name)
if name == "" {
return 0, errors.New("name cannot not be empty")
}
var tx *sql.Tx
var err error
if otx == nil {
tx, err = db.Begin()
if err != nil {
return 0, err
}
defer tx.Rollback()
} else {
tx = otx
}
res, err := tx.Exec("INSERT INTO wf_roles_master(name) VALUES(?)", name)
if err != nil {
return 0, err
}
id, err := res.LastInsertId()
if err != nil {
return 0, err
}
if otx == nil {
err = tx.Commit()
if err != nil {
return 0, err
}
}
return RoleID(id), nil
}
// List answers a subset of the roles, based on the input
// specification.
//
// Result set begins with ID >= `offset`, and has not more than
// `limit` elements. A value of `0` for `offset` fetches from the
// beginning, while a value of `0` for `limit` fetches until the end.
func (_Roles) List(offset, limit int64) ([]*Role, error) {
if offset < 0 || limit < 0 {
return nil, errors.New("offset and limit must be non-negative integers")
}
if limit == 0 {
limit = math.MaxInt64
}
q := `
SELECT id, name
FROM wf_roles_master
ORDER BY id
LIMIT ? OFFSET ?
`
rows, err := db.Query(q, limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
ary := make([]*Role, 0, 10)
for rows.Next() {
var elem Role
err = rows.Scan(&elem.ID, &elem.Name)
if err != nil {
return nil, err
}
ary = append(ary, &elem)
}
if err = rows.Err(); err != nil {
return nil, err
}
return ary, nil
}
// Get loads the role object corresponding to the given role ID from
// the database, and answers that.
func (_Roles) Get(id RoleID) (*Role, error) {
if id <= 0 {
return nil, errors.New("ID must be a positive integer")
}
var elem Role
row := db.QueryRow("SELECT id, name FROM wf_roles_master WHERE id = ?", id)
err := row.Scan(&elem.ID, &elem.Name)
if err != nil {
return nil, err
}
return &elem, nil
}
// GetByName answers the role, if one with the given name is
// registered; `nil` and the error, otherwise.
func (_Roles) GetByName(name string) (*Role, error) {
name = strings.TrimSpace(name)
if name == "" {
return nil, errors.New("role cannot be empty")
}
var elem Role
row := db.QueryRow("SELECT id, name FROM wf_roles_master WHERE name = ?", name)
err := row.Scan(&elem.ID, &elem.Name)
if err != nil {
return nil, err
}
return &elem, nil
}
// Rename renames the given role.
func (_Roles) Rename(otx *sql.Tx, id RoleID, name string) error {
name = strings.TrimSpace(name)
if name == "" {
return errors.New("name cannot be empty")
}
var tx *sql.Tx
var err error
if otx == nil {
tx, err = db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
} else {
tx = otx
}
_, err = tx.Exec("UPDATE wf_roles_master SET name = ? WHERE id = ?", name, id)
if err != nil {
return err
}
if otx == nil {
err = tx.Commit()
if err != nil {
return err
}
}
return nil
}
// Delete deletes the given role from the system, if no access context
// is actively using it.
func (_Roles) Delete(otx *sql.Tx, id RoleID) error {
if id <= 0 {
return errors.New("role ID must be a positive integer")
}
row := db.QueryRow("SELECT COUNT(*) FROM wf_ac_group_roles WHERE role_id = ?", id)
var n int64
err := row.Scan(&n)
if n > 0 {
return errors.New("role is being used in at least one access context; cannot delete")
}
var tx *sql.Tx
if otx == nil {
tx, err = db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
} else {
tx = otx
}
_, err = tx.Exec("DELETE FROM wf_role_docactions WHERE role_id = ?", id)
if err != nil {
return err
}
res, err := tx.Exec("DELETE FROM wf_roles_master WHERE id = ?", id)
if err != nil {
return err
}
n, err = res.RowsAffected()
if n != 1 {
return fmt.Errorf("expected number of affected rows : 1; actual affected : %d", n)
}
if otx == nil {
err = tx.Commit()
if err != nil {
return err
}
}
return nil
}
// AddPermissions adds the given actions to this role, for the given
// document type.
func (_Roles) AddPermissions(otx *sql.Tx, rid RoleID, dtype DocTypeID, actions []DocActionID) error {
var tx *sql.Tx
var err error
if otx == nil {
tx, err = db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
} else {
tx = otx
}
q := `
INSERT INTO wf_role_docactions(role_id, doctype_id, docaction_id)
VALUES(?, ?, ?)
`
for _, action := range actions {
_, err = tx.Exec(q, rid, dtype, action)
if err != nil {
return err
}
}
if otx == nil {
err = tx.Commit()
if err != nil {
return err
}
}
return nil
}
// RemovePermissions removes the given actions from this role, for the
// given document type.
func (_Roles) RemovePermissions(otx *sql.Tx, rid RoleID, dtype DocTypeID, actions []DocActionID) error {
var tx *sql.Tx
var err error
if otx == nil {
tx, err = db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
} else {
tx = otx
}
q := `
DELETE FROM wf_role_docactions
WHERE role_id = ?
AND doctype_id = ?
AND docaction_id = ?
`
for _, action := range actions {
_, err = tx.Exec(q, rid, dtype, action)
if err != nil {
return err
}
}
if otx == nil {
err = tx.Commit()
if err != nil {
return err
}
}
return nil
}
// Permissions answers the current set of permissions this role has.
// It answers `nil` in case the given document type does not have any
// permissions set in this role.
func (_Roles) Permissions(rid RoleID) (map[string]struct {
DocTypeID DocTypeID
Actions []*DocAction
}, error) {
q := `
SELECT dtm.id, dtm.name, dam.id, dam.name, dam.reconfirm
FROM wf_doctypes_master dtm
JOIN wf_role_docactions rdas ON dtm.id = rdas.doctype_id
JOIN wf_docactions_master dam ON dam.id = rdas.docaction_id
WHERE rdas.role_id = ?
`
rows, err := db.Query(q, rid)
if err != nil {
return nil, err
}
defer rows.Close()
das := make(map[string]struct {
DocTypeID DocTypeID
Actions []*DocAction
})
for rows.Next() {
var dt DocType
var da DocAction
err = rows.Scan(&dt.ID, &dt.Name, &da.ID, &da.Name, &da.Reconfirm)
if err != nil {
return nil, err
}
st, ok := das[dt.Name]
if !ok {
st.DocTypeID = dt.ID
st.Actions = make([]*DocAction, 0, 1)
}
st.Actions = append(st.Actions, &da)
das[dt.Name] = st
}
if err = rows.Err(); err != nil {
return nil, err
}
return das, nil
}
// HasPermission answers `true` if this role has the queried
// permission for the given document type.
func (_Roles) HasPermission(rid RoleID, dtype DocTypeID, action DocActionID) (bool, error) {
q := `
SELECT rdas.id FROM wf_role_docactions rdas
JOIN wf_doctypes_master dtm ON rdas.doctype_id = dtm.id
JOIN wf_docactions_master dam ON rdas.docaction_id = dam.id
WHERE rdas.role_id = ?
AND dtm.id = ?
AND dam.id = ?
ORDER BY rdas.id
LIMIT 1
`
row := db.QueryRow(q, rid, dtype, action)
var n int64
err := row.Scan(&n)
if err != nil {
switch err {
case sql.ErrNoRows:
return false, nil
default:
return false, err
}
}
return true, nil
}