-
Notifications
You must be signed in to change notification settings - Fork 1
/
permissions.go
380 lines (368 loc) · 13.3 KB
/
permissions.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
package constants
// Permission is a string that keys into permission maps for
// different roles. We use string instead of bitmask or array index
// for a few reasons:
//
// 1. We may wind up with more than 64 of these, which would be too
// many for a bitmask.
// 2. Our models need to construct permission names from strings made
// up of model names and actions. E.g. "User" + "Create" or
// "Object" + "Read".
// 3. We will likely insert permissions as the application grows, and
// adding bitmasks and array indices is order-dependent, while
// adding string keys is value-dependent, which means we can insert
// them anywhere in the list.
type Permission string
const (
AlertCreate = "AlertCreate"
AlertDelete = "AlertDelete"
AlertRead = "AlertRead"
AlertUpdate = "AlertUpdate"
BillingReportShow = "BillingReportShow"
ChecksumCreate = "ChecksumCreate"
ChecksumDelete = "ChecksumDelete"
ChecksumRead = "ChecksumRead"
ChecksumUpdate = "ChecksumUpdate"
DashboardShow = "DashboardShow"
DeletionRequestApprove = "DeletionRequestApprove"
DeletionRequestList = "DeletionRequestList"
DeletionRequestShow = "DeletionRequestShow"
DepositReportShow = "DepositReportShow"
EventCreate = "EventCreate"
EventDelete = "EventDelete"
EventRead = "EventRead"
EventUpdate = "EventUpdate"
FileCreate = "FileCreate"
FileDelete = "FileDelete"
FileFinishBulkDelete = "FileFinishBulkDelete"
FileRead = "FileRead"
FileRequestDelete = "FileRequestDelete"
FileRestore = "FileRestore"
FileUpdate = "FileUpdate"
InstitutionCreate = "InstitutionCreate"
InstitutionDelete = "InstitutionDelete"
InstitutionList = "InstitutionList"
InstitutionRead = "InstitutionRead"
InstitutionUpdate = "InstitutionUpdate"
InstitutionUpdatePrefs = "InstitutionUpdatePrefs"
IntellectualObjectBatchDelete = "IntellectualObjectBatchDelete"
IntellectualObjectCreate = "IntellectualObjectCreate"
IntellectualObjectDelete = "IntellectualObjectDelete"
IntellectualObjectFinishBulkDelete = "IntellectualObjectFinishBulkDelete"
IntellectualObjectRead = "IntellectualObjectRead"
IntellectualObjectRequestDelete = "IntellectualObjectRequestDelete"
IntellectualObjectRestore = "IntellectualObjectRestore"
IntellectualObjectUpdate = "IntellectualObjectUpdate"
InternalMetadataRead = "InternalMetadataRead"
NsqAdmin = "NsqAdmin"
PrepareFileDelete = "PrepareFileDelete"
PrepareObjectDelete = "PrepareObjectDelete"
ReportRead = "ReportRead"
RedisList = "RedisList"
RedisRead = "RedisRead"
StorageRecordCreate = "StorageRecordCreate"
StorageRecordDelete = "StorageRecordDelete"
StorageRecordRead = "StorageRecordRead"
StorageRecordUpdate = "StorageRecordUpdate"
UserComplete2FASetup = "UserComplete2FASetup"
UserConfirmPhone = "UserConfirmPhone"
UserCreate = "UserCreate"
UserDelete = "UserDelete"
UserDeleteSelf = "UserDeleteSelf"
UserGenerateBackupCodes = "UserGenerateBackupCodes"
UserInit2FASetup = "UserInit2FASetup"
UserRead = "UserRead"
UserReadSelf = "UserReadSelf"
UserSignIn = "UserSignIn"
UserSignOut = "UserSignOut"
UserTwoFactorBackup = "UserTwoFactorBackup"
UserTwoFactorChoose = "UserTwoFactorChoose"
UserTwoFactorGenerateSMS = "UserTwoFactorGenerateSMS"
UserTwoFactorPush = "UserTwoFactorPush"
UserTwoFactorResend = "UserTwoFactorResend"
UserTwoFactorVerify = "UserTwoFactorVerify"
UserUpdate = "UserUpdate"
UserUpdateSelf = "UserUpdateSelf"
WorkItemCreate = "WorkItemCreate"
WorkItemDelete = "WorkItemDelete"
WorkItemRead = "WorkItemRead"
WorkItemRedisDelete = "WorkItemRedisDelete"
WorkItemRequeue = "WorkItemRequeue"
WorkItemUpdate = "WorkItemUpdate"
)
var Permissions = []Permission{
AlertCreate,
AlertDelete,
AlertRead,
AlertUpdate,
BillingReportShow,
ChecksumCreate,
ChecksumDelete,
ChecksumRead,
ChecksumUpdate,
DashboardShow,
DeletionRequestApprove,
DeletionRequestList,
DeletionRequestShow,
DepositReportShow,
EventCreate,
EventDelete,
EventRead,
EventUpdate,
FileCreate,
FileDelete,
FileFinishBulkDelete,
FileRead,
FileRequestDelete,
FileRestore,
FileUpdate,
InstitutionCreate,
InstitutionDelete,
InstitutionList,
InstitutionRead,
InstitutionUpdate,
InstitutionUpdatePrefs,
IntellectualObjectBatchDelete,
IntellectualObjectCreate,
IntellectualObjectDelete,
IntellectualObjectFinishBulkDelete,
IntellectualObjectRead,
IntellectualObjectRequestDelete,
IntellectualObjectRestore,
IntellectualObjectUpdate,
InternalMetadataRead,
NsqAdmin,
PrepareFileDelete,
PrepareObjectDelete,
ReportRead,
RedisList,
RedisRead,
StorageRecordCreate,
StorageRecordDelete,
StorageRecordRead,
StorageRecordUpdate,
UserComplete2FASetup,
UserConfirmPhone,
UserCreate,
UserDelete,
UserDeleteSelf,
UserGenerateBackupCodes,
UserInit2FASetup,
UserRead,
UserReadSelf,
UserSignIn,
UserSignOut,
UserTwoFactorBackup,
UserTwoFactorChoose,
UserTwoFactorGenerateSMS,
UserTwoFactorPush,
UserTwoFactorResend,
UserTwoFactorVerify,
UserUpdate,
UserUpdateSelf,
WorkItemCreate,
WorkItemDelete,
WorkItemRead,
WorkItemRedisDelete,
WorkItemRequeue,
WorkItemUpdate,
}
var ForbiddenToAll = []Permission{
ChecksumUpdate,
ChecksumDelete,
EventUpdate,
EventDelete,
}
// SelfAccountPermissions are those which permit users to modify their
// own account information (password, email, name, API key, etc.).
// These are treated specially in the auth middleware because unlike
// other permissions, which are based on the subject resource's insitution id,
// these are based on the subject resource's user id. See
// ResourceAuthorization.checkPermission to understand how this specific
// set of permissions is used.
var SelfAccountPermissions = []Permission{
UserComplete2FASetup,
UserConfirmPhone,
UserDeleteSelf,
UserGenerateBackupCodes,
UserInit2FASetup,
UserReadSelf,
UserUpdateSelf,
}
var permissionsInitialized = false
// Permission lists for different roles. Bools default to false in Go,
// so roles will have only the permissions we explicitly grant below.
// Note that emptyList gets zero permissions. This is the list we'll check
// if we can't figure out a user's role, or if a user has been deactivated.
var instUser = make(map[Permission]bool)
var instAdmin = make(map[Permission]bool)
var sysAdmin = make(map[Permission]bool)
var emptyList = make(map[Permission]bool)
func initPermissions() {
// Institutional User Role
instUser[AlertRead] = true
instUser[AlertUpdate] = true
instUser[ChecksumRead] = true
instUser[DashboardShow] = true
instUser[DeletionRequestList] = true
instUser[DeletionRequestShow] = true
instUser[DepositReportShow] = true
instUser[EventRead] = true
instUser[FileRead] = true
instUser[FileRestore] = true
instUser[InstitutionRead] = true
instUser[IntellectualObjectRead] = true
instUser[IntellectualObjectRestore] = true
instUser[ReportRead] = true
instUser[StorageRecordRead] = true
instUser[UserComplete2FASetup] = true
instUser[UserConfirmPhone] = true
instUser[UserGenerateBackupCodes] = true
instUser[UserInit2FASetup] = true
instUser[UserReadSelf] = true
instUser[UserSignIn] = true
instUser[UserSignOut] = true
instUser[UserTwoFactorBackup] = true
instUser[UserTwoFactorChoose] = true
instUser[UserTwoFactorGenerateSMS] = true
instUser[UserTwoFactorPush] = true
instUser[UserTwoFactorResend] = true
instUser[UserTwoFactorVerify] = true
instUser[UserUpdateSelf] = true
instUser[WorkItemRead] = true
// Institutional Admin Role
instAdmin[AlertRead] = true
instAdmin[AlertUpdate] = true
instAdmin[ChecksumRead] = true
instAdmin[DashboardShow] = true
instAdmin[DeletionRequestApprove] = true
instAdmin[DeletionRequestList] = true
instAdmin[DeletionRequestShow] = true
instAdmin[DepositReportShow] = true
instAdmin[EventRead] = true
instAdmin[FileDelete] = true
instAdmin[FileRead] = true
instAdmin[FileRequestDelete] = true
instAdmin[FileRestore] = true
instAdmin[InstitutionRead] = true
instAdmin[InstitutionUpdatePrefs] = true
instAdmin[IntellectualObjectDelete] = true
instAdmin[IntellectualObjectRead] = true
instAdmin[IntellectualObjectRequestDelete] = true
instAdmin[IntellectualObjectRestore] = true
instAdmin[ReportRead] = true
instAdmin[StorageRecordRead] = true
instAdmin[UserComplete2FASetup] = true
instAdmin[UserConfirmPhone] = true
instAdmin[UserCreate] = true
instAdmin[UserDelete] = true
instAdmin[UserGenerateBackupCodes] = true
instAdmin[UserInit2FASetup] = true
instAdmin[UserReadSelf] = true
instAdmin[UserRead] = true
instAdmin[UserSignIn] = true
instAdmin[UserSignOut] = true
instAdmin[UserTwoFactorBackup] = true
instAdmin[UserTwoFactorChoose] = true
instAdmin[UserTwoFactorGenerateSMS] = true
instAdmin[UserTwoFactorPush] = true
instAdmin[UserTwoFactorResend] = true
instAdmin[UserTwoFactorVerify] = true
instAdmin[UserUpdateSelf] = true
instAdmin[UserUpdate] = true
instAdmin[WorkItemRead] = true
// Sys Admin Role
sysAdmin[AlertCreate] = true
sysAdmin[AlertDelete] = true
sysAdmin[AlertRead] = true
sysAdmin[AlertUpdate] = true
sysAdmin[BillingReportShow] = true
sysAdmin[ChecksumCreate] = true
sysAdmin[ChecksumDelete] = false // no one can do this
sysAdmin[ChecksumRead] = true
sysAdmin[ChecksumUpdate] = false // no one can do this
sysAdmin[DashboardShow] = true
sysAdmin[DeletionRequestApprove] = false // only inst admin can do this
sysAdmin[DeletionRequestList] = true
sysAdmin[DeletionRequestShow] = true
sysAdmin[DepositReportShow] = true
sysAdmin[EventCreate] = true
sysAdmin[EventDelete] = false // no one can do this
sysAdmin[EventRead] = true
sysAdmin[EventUpdate] = false // no one can do this
sysAdmin[FileCreate] = true
sysAdmin[FileDelete] = true // // preserv workers do this with sys admin account
sysAdmin[FileFinishBulkDelete] = true // not implemented yet
sysAdmin[FileRead] = true
sysAdmin[FileRequestDelete] = false
sysAdmin[FileRestore] = true
sysAdmin[FileUpdate] = true
sysAdmin[InstitutionCreate] = true
sysAdmin[InstitutionDelete] = true
sysAdmin[InstitutionList] = true
sysAdmin[InstitutionRead] = true
sysAdmin[InstitutionUpdate] = true
sysAdmin[InstitutionUpdatePrefs] = true
sysAdmin[IntellectualObjectBatchDelete] = true
sysAdmin[IntellectualObjectCreate] = true
sysAdmin[IntellectualObjectDelete] = true // preserv workers do this with sys admin account
sysAdmin[IntellectualObjectFinishBulkDelete] = true // not implemented yet
sysAdmin[IntellectualObjectRead] = true
sysAdmin[IntellectualObjectRequestDelete] = false // inst admin only
sysAdmin[IntellectualObjectRestore] = true
sysAdmin[IntellectualObjectUpdate] = true
sysAdmin[InternalMetadataRead] = true
sysAdmin[NsqAdmin] = true
sysAdmin[PrepareFileDelete] = true
sysAdmin[PrepareObjectDelete] = true
sysAdmin[ReportRead] = true
sysAdmin[RedisList] = true
sysAdmin[RedisRead] = true
sysAdmin[StorageRecordCreate] = true
sysAdmin[StorageRecordDelete] = true
sysAdmin[StorageRecordRead] = true
sysAdmin[StorageRecordUpdate] = true
sysAdmin[UserComplete2FASetup] = true
sysAdmin[UserConfirmPhone] = true
sysAdmin[UserCreate] = true
sysAdmin[UserDeleteSelf] = true
sysAdmin[UserDelete] = true
sysAdmin[UserGenerateBackupCodes] = true
sysAdmin[UserInit2FASetup] = true
sysAdmin[UserReadSelf] = true
sysAdmin[UserRead] = true
sysAdmin[UserSignIn] = true
sysAdmin[UserSignOut] = true
sysAdmin[UserTwoFactorBackup] = true
sysAdmin[UserTwoFactorChoose] = true
sysAdmin[UserTwoFactorGenerateSMS] = true
sysAdmin[UserTwoFactorPush] = true
sysAdmin[UserTwoFactorResend] = true
sysAdmin[UserTwoFactorVerify] = true
sysAdmin[UserUpdateSelf] = true
sysAdmin[UserUpdate] = true
sysAdmin[WorkItemCreate] = true
sysAdmin[WorkItemDelete] = true
sysAdmin[WorkItemRead] = true
sysAdmin[WorkItemRedisDelete] = true
sysAdmin[WorkItemRequeue] = true
sysAdmin[WorkItemUpdate] = true
permissionsInitialized = true
}
func CheckPermission(role string, permission Permission) bool {
if !permissionsInitialized {
initPermissions()
}
var permissions map[Permission]bool
switch role {
case RoleSysAdmin:
permissions = sysAdmin
case RoleInstAdmin:
permissions = instAdmin
case RoleInstUser:
permissions = instUser
default:
permissions = emptyList
}
return permissions[permission]
}