forked from UtrechtUniversity/yoda-ruleset
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiiFolderStatusTransitions.r
More file actions
343 lines (315 loc) · 11.5 KB
/
Copy pathiiFolderStatusTransitions.r
File metadata and controls
343 lines (315 loc) · 11.5 KB
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
# \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 ("<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);
# From HERE relay to python
*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");
}
}