forked from UtrechtUniversity/yoda-ruleset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiiFolderStatusTransitions.r
480 lines (443 loc) · 15.8 KB
/
iiFolderStatusTransitions.r
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# \file iiFolderStatusTransitions.r
# \brief Status transitions for folders in the research space.
# \author Paul Frederiks
# \author Lazlo Westerhof
# \copyright Copyright (c) 2015-2019 Utrecht University. All rights reserved.
# \license GPLv3, see LICENSE.
# \brief iiFolderStatus
#
# \param[in] folder Path of folder
# \param[out] folderStatus Current status of folder
#
iiFolderStatus(*folder, *folderStatus) {
*folderStatusKey = IISTATUSATTRNAME;
*folderStatus = FOLDER;
foreach(*row in SELECT META_COLL_ATTR_VALUE WHERE COLL_NAME = *folder AND META_COLL_ATTR_NAME = *folderStatusKey) {
*folderStatus = *row.META_COLL_ATTR_VALUE;
}
}
# \brief Schedule copy-to-vault (asynchronously).
#
iiScheduleCopyToVault() {
delay ("<INST_NAME>irods_rule_engine_plugin-irods_rule_language-instance</INST_NAME><PLUSET>1s</PLUSET>") {
msiExecCmd("scheduled-copytovault.sh", "", "", "", 0, *out);
}
}
# \brief iiFolderDatamanagerAction
#
# \param[in] folder
# \param[out] newFolderStatus Status to set as datamanager. Either ACCEPTED or REJECTED
# \param[out] status status of the action
# \param[out] statusInfo Informative message when action was not successful
#
iiFolderDatamanagerAction(*folder, *newFolderStatus, *status, *statusInfo) {
*status = "Unknown";
*statusInfo = "An internal error has occurred";
# Check if folder is a research group.
*groupName = "";
*err = errorcode(rule_collection_group_name(*folder, *groupName));
if (*err < 0) {
*status = "NoResearchGroup";
*statusInfo = "*folder is not accessible possibly due to insufficient rights or as it is not part of a research group. Therefore, the requested action can not be performed";
succeed;
} else {
# Research group, determine datamanager group.
uuGroupGetCategory(*groupName, *category, *subcategory);
*datamanagerGroup = "datamanager-*category";
}
*actor = uuClientFullName;
*aclKv.actor = *actor;
*err = errorcode(msiSudoObjAclSet("recursive", "write", *datamanagerGroup, *folder, *aclKv));
if (*err < 0) {
*status = "PermissionDenied";
iiCanDatamanagerAclSet(*folder, *actor, *datamanagerGroup, 0, "write", *allowed, *reason);
if (*allowed) {
*statusInfo = "Could not acquire datamanager access to *folder.";
} else {
*statusInfo = *reason;
}
succeed;
}
if (*newFolderStatus == REJECTED) {
# get permission to unlock ancestors, too
uuChopPath(*folder, *parent, *child);
while(*parent != "/$rodsZoneClient/home") {
msiSudoObjAclSet("", "write", *datamanagerGroup, *parent, *aclKv);
uuChopPath(*parent, *coll, *child);
*parent = *coll;
}
}
*folderStatusStr = IISTATUSATTRNAME ++ "=" ++ *newFolderStatus;
msiString2KeyValPair(*folderStatusStr, *folderStatusKvp);
*err = errormsg(msiSetKeyValuePairsToObj(*folderStatusKvp, *folder, "-C"), *msg);
if (*err < 0) {
iiFolderStatus(*folder, *currentFolderStatus);
iiCanTransitionFolderStatus(*folder, *currentFolderStatus, *newFolderStatus, *actor, *allowed, *reason);
if (!*allowed) {
*status = "PermissionDenied";
*statusInfo = *reason;
} else {
if (*err == -818000) {
*status = "PermissionDenied";
*statusInfo = "User is not permitted to modify folder status";
} else {
*status = "Unrecoverable";
*statusInfo = "*err - *msg";
}
}
}
*err = errormsg(msiSudoObjAclSet("recursive", "read", *datamanagerGroup, *folder, *aclKv), *msg);
if (*err < 0) {
*status = "FailedToRemoveTemporaryAccess";
iiCanDatamanagerAclSet(*folder, *actor, *datamanagerGroup, 0, "read", *allowed, *reason);
if (*allowed) {
*statusInfo = "*err - *msg";
} else {
*statusInfo = *reason;
}
succeed;
}
if (*newFolderStatus == REJECTED) {
# remove permission to modify ancestors
uuChopPath(*folder, *parent, *child);
while(*parent != "/$rodsZoneClient/home") {
msiSudoObjAclSet("", "read", *datamanagerGroup, *parent, *aclKv);
uuChopPath(*parent, *coll, *child);
*parent = *coll;
}
}
if (*status == "Unknown") {
*status = "Success";
*statusInfo = "";
}
}
# \brief iiFolderLockChange
#
# \param[in] rootCollection The COLL_NAME of the collection the dataset resides in
# \param[in] lockIt Boolean, true if the object should be locked.
# if false, the lock is removed (if allowed)
# \param[out] status Zero if no errors, non-zero otherwise
#
iiFolderLockChange(*rootCollection, *lockIt, *status){
msiString2KeyValPair("", *buffer);
msiAddKeyVal(*buffer, IILOCKATTRNAME, *rootCollection)
#DEBUG writeLine("ServerLog", "iiFolderLockChange: *buffer");
if (*lockIt == "lock") {
#DEBUG writeLine("serverLog", "iiFolderLockChange: recursive locking of *rootCollection");
*direction = "forward";
uuTreeWalk(*direction, *rootCollection, "iiAddMetadataToItem", *buffer, *error);
if (*error == 0) {
uuChopPath(*rootCollection, *parent, *child);
while(*parent != "/$rodsZoneClient/home") {
uuChopPath(*parent, *coll, *child);
iiAddMetadataToItem(*coll, *child, true, *buffer, *error);
*parent = *coll;
}
}
} else {
#DEBUG writeLine("serverLog", "iiFolderLockChange: recursive unlocking of *rootCollection");
*direction="reverse";
uuTreeWalk(*direction, *rootCollection, "iiRemoveMetadataFromItem", *buffer, *error);
if (*error == 0) {
uuChopPath(*rootCollection, *parent, *child);
while(*parent != "/$rodsZoneClient/home") {
uuChopPath(*parent, *coll, *child);
iiRemoveMetadataFromItem(*coll, *child, true, *buffer, *error);
*parent = *coll;
}
}
}
*status = "*error";
}
# \brief Return objectType string based on boolean itemIsCollection.
#
# \param[in] itemIsCollection boolean usually returned by treewalk when item is a Collection
# \returnvalue iRODS objectType string. "-C" for Collection, "-d" for DataObject
#
iitypeabbreviation(*itemIsCollection) = if *itemIsCollection then "-C" else "-d"
# \brief For use by uuTreewalk to add metadata.
#
# \param[in] itemParent full iRODS path to the parent of this object
# \param[in] itemName basename of collection or dataobject
# \param[in] itemIsCollection true if the item is a collection
# \param[in,out] buffer in/out Key-Value variable
# \param[out] error errorcode in case of failure
#
iiAddMetadataToItem(*itemParent, *itemName, *itemIsCollection, *buffer, *error) {
*objPath = "*itemParent/*itemName";
*objType = iitypeabbreviation(*itemIsCollection);
#DEBUG writeLine("serverLog", "iiAddMetadataToItem: Setting *buffer on *objPath");
*error = errorcode(msiAssociateKeyValuePairsToObj(*buffer, *objPath, *objType));
}
# \brief For use by uuTreeWalk to remove metadata.
#
# \param[in] itemParent full iRODS path to the parent of this object
# \param[in] itemName basename of collection or dataobject
# \param[in] itemIsCollection true if the item is a collection
# \param[in,out] buffer in/out Key-Value variable
# \param[out] error errorcode in case of failure
#
iiRemoveMetadataFromItem(*itemParent, *itemName, *itemIsCollection, *buffer, *error) {
*objPath = "*itemParent/*itemName";
*objType = iitypeabbreviation(*itemIsCollection);
#DEBUG writeLine("serverLog", "iiRemoveMetadataKeyFromItem: Removing *buffer on *objPath");
*error = errormsg(msiRemoveKeyValuePairsFromObj(*buffer, *objPath, *objType), *msg);
if (*error < 0) {
writeLine("serverLog", "iiRemoveMetadataFromItem: removing *buffer from *objPath failed with errorcode: *error");
writeLine("serverLog", *msg);
if (*error == -819000) {
# This happens when metadata was already removed or never there.
writeLine("serverLog", "iiRemoveMetadaFromItem: -819000 detected. Keep on trucking, this happens if metadata was already removed");
*error = 0;
}
}
}
# \brief iiFolderSecure Secure a folder to the vault. This function should only be called by a rodsadmin
# and should not be called from the portal. Thus no statusInfo is returned, but
# log messages are sent to stdout instead
#
# \param[in] folder
#
iiFolderSecure(*folder) {
uuGetUserType(uuClientFullName, *userType);
if (*userType != "rodsadmin") {
writeLine("stdout", "iiFolderSecure: Should only be called by a rodsadmin");
fail;
}
# Check modify access on research folder.
msiCheckAccess(*folder, "modify object", *modifyAccess);
# Set cronjob status.
msiString2KeyValPair(UUORGMETADATAPREFIX ++ "cronjob_copy_to_vault=" ++ CRONJOB_PROCESSING, *kvp);
if (*modifyAccess != 1) {
msiSetACL("default", "admin:write", uuClientFullName, *folder);
}
msiSetKeyValuePairsToObj(*kvp, *folder, "-C");
*found = false;
foreach (*row in SELECT META_COLL_ATTR_VALUE
WHERE COLL_NAME = '*folder'
AND META_COLL_ATTR_NAME = IICOPYPARAMSNAME) {
# retry with previous parameters
*target = *row.META_COLL_ATTR_VALUE;
*found = true;
}
if (*found) {
# Remove parameters from metadata
msiString2KeyValPair("", *kvp);
*key = IICOPYPARAMSNAME;
*kvp."*key" = *target;
msiRemoveKeyValuePairsFromObj(*kvp, *folder, "-C");
}
if (*modifyAccess != 1) {
msiSetACL("default", "admin:null", uuClientFullName, *folder);
}
if (!*found) {
# this file
*target = iiDetermineVaultTarget(*folder);
}
# Copy to vault.
iiCopyFolderToVault(*folder, *target);
# Continue securing process in PREP.
*return = "";
rule_folder_secure(*folder, *target, *return);
}
# \brief iiDetermineVaultTarget
#
# \param[in] folder
# \returnvalue target path
#
iiDetermineVaultTarget(*folder) {
*err = errorcode(iiCollectionGroupName(*folder, *groupName));
if (*err < 0) {
writeLine("stdout", "iiDetermineVaultTarget: Cannot determine which research group *folder belongs to");
fail;
} else {
writeLine("stdout", "iiDetermineVaultTarget: *folder belongs to *groupName");
}
uuChop(*groupName, *_, *baseName, "-", true);
uuChopPath(*folder, *parent, *datapackageName);
# Make room for the timestamp and sequence number
if (strlen(*datapackageName) > 235) {
*datapackageName = substr(*datapackageName, 0, 235);
}
msiGetIcatTime(*timestamp, "unix");
*timestamp = triml(*timestamp, "0");
*vaultGroupName = IIVAULTPREFIX ++ *baseName;
*target = "/$rodsZoneClient/home/*vaultGroupName/*datapackageName[*timestamp]";
*i = 0;
while (uuCollectionExists(*target)) {
writeLine("stdout", "iiDetermineVaultTarget: *target already exists");
*i = *i + 1;
*target = "/$rodsZoneClient/home/*vaultGroupName/*datapackageName[*timestamp][*i]";
}
writeLine("stdout", "iiDetermineVaultTarget: Target is *target");
*target;
}
# \brief Return the name of the group a collection belongs to.
#
# \param[in] folder
# \param[out] groupName
#
iiCollectionGroupName(*folder, *groupName) {
if (*folder like regex "/[^/]+/home/deposit-.[^/]*/.*") {
uuChopPath(*folder, *parent, *baseName);
*path = *parent;
} else {
*path = *folder;
}
*isfound = false;
*groupName = "";
foreach(*accessid in SELECT COLL_ACCESS_USER_ID WHERE COLL_NAME = *path) {
*id = *accessid.COLL_ACCESS_USER_ID;
foreach(*group in SELECT USER_GROUP_NAME WHERE USER_GROUP_ID = *id) {
*groupName = *group.USER_GROUP_NAME;
}
if (*groupName like regex "(deposit|research|intake)-.*") {
*isfound = true;
break;
}
}
if (!*isfound) {
foreach(*accessid in SELECT COLL_ACCESS_USER_ID WHERE COLL_NAME = *path) {
*id = *accessid.COLL_ACCESS_USER_ID;
foreach(*group in SELECT USER_GROUP_NAME WHERE USER_GROUP_ID = *id) {
*groupName = *group.USER_GROUP_NAME;
}
if (*groupName like regex "(datamanager|vault)-.*") {
*isfound = true;
break;
}
}
}
if (!*isfound){
# No results found. Not a group folder
writeLine("serverLog", "*path does not belong to a deposit, research or intake group or is not available to current user");
}
}
# \brief Check validity of requested folder status transition in a research area.
#
# \param[in] fromstatus folder status before requested transition
# \param[in] tostatus folder status after requested transition
#
iiIsStatusTransitionLegal(*fromstatus, *tostatus) {
*legal = false;
# IIFOLDERTRANSTIONS should be defined in iiConstants.r and lists all the legal status transitions
foreach(*legaltransition in IIFOLDERTRANSITIONS) {
(*legalfrom, *legalto) = *legaltransition;
if (*legalfrom == *fromstatus && *legalto == *tostatus) {
*legal = true;
break;
}
}
*legal;
}
# \brief Return a list of locks on an object.
#
# \param[in] objPath path of collection or data object
# \param[out] locks list of locks with the rootCollection of each lock as value
#
iiGetLocks(*objPath, *locks) {
*locks = list();
*lockattrname = IILOCKATTRNAME;
msiGetObjType(*objPath, *objType);
if (*objType == '-d') {
uuChopPath(*objPath, *collection, *dataName);
foreach (*row in SELECT META_DATA_ATTR_VALUE
WHERE COLL_NAME = *collection
AND DATA_NAME = *dataName
AND META_DATA_ATTR_NAME = *lockattrname
) {
*rootCollection= *row.META_DATA_ATTR_VALUE;
*locks = cons(*rootCollection, *locks);
}
} else {
foreach (*row in SELECT META_COLL_ATTR_VALUE
WHERE COLL_NAME = *objPath
AND META_COLL_ATTR_NAME = *lockattrname
) {
*rootCollection = *row.META_COLL_ATTR_VALUE;
*locks = cons(*rootCollection, *locks);
}
}
}
# \brief Check if a research folder status transition is legal.
#
# \param[in] folder
# \param[in] transitionFrom current status to transition from
# \param[in] transitionTo new status to transition to
# \param[out] allowed boolean to indicate if the action is allowed
# \param[out] reason reason the action is not allowed
#
iiCanTransitionFolderStatus(*folder, *transitionFrom, *transitionTo, *actor, *allowed, *reason) {
*allowed = false;
*reason = "Unknown error";
if (iiIsStatusTransitionLegal(*transitionFrom, *transitionTo)) {
*allowed = true;
*reason = "Legal status transition. *transitionFrom -> *transitionTo";
} else {
if (*transitionFrom == FOLDER) {
*reason = "Illegal status transition. Current folder has no status.";
} else {
*reason = "Illegal status transition. Current status is *transitionFrom.";
}
succeed;
}
if (*transitionTo == SUBMITTED) {
*metadataJsonPath = *folder ++ "/" ++ IIJSONMETADATA;
if (!uuFileExists(*metadataJsonPath)) {
*allowed = false;
*reason = "Metadata missing, unable to submit this folder.";
succeed;
} else {
*status = "";
*statusInfo = "";
rule_meta_validate(*metadataJsonPath, *status, *statusInfo);
if (*status != "0") {
*allowed = false;
*reason = "Metadata is invalid, please check metadata form.";
succeed;
}
}
}
if (*transitionTo == ACCEPTED || *transitionTo == REJECTED) {
*groupName = "";
*err1 = errorcode(rule_collection_group_name(*folder, *groupName));
*err2 = errorcode(uuGroupGetCategory(*groupName, *category, *subcategory));
*err3 = errorcode(uuGroupExists("datamanager-*category", *datamanagerExists));
if (*err1 < 0 || *err2 < 0 || *err3 < 0) {
*allowed = false;
*reason = "Could not determine if datamanager-*category exists";
succeed;
}
if (*datamanagerExists) {
uuGroupGetMemberType("datamanager-*category", *actor, *userTypeIfDatamanager);
if (*userTypeIfDatamanager == "normal" || *userTypeIfDatamanager == "manager") {
*allowed = true;
*reason = "Folder is *transitionTo by *actor from datamanager-*category";
} else {
*allowed = false;
*reason = "Only a member of datamanager-*category is allowed to accept or reject a submitted folder";
succeed;
}
} else {
*allowed = true;
*reason = "When no datamanager group exists, submitted folders are automatically accepted";
}
}
if (*transitionTo == SECURED) {
*allowed = false;
*reason = "Only a rodsadmin is allowed to secure a folder to the vault";
succeed;
}
if (*allowed) {
iiGetLocks(*folder, *locks);
if (size(*locks) > 0) {
foreach(*rootCollection in *locks) {
if (*rootCollection != *folder) {
*allowed = false;
*reason = "Found lock(s) starting from *rootCollection";
break;
}
}
}
}
}