forked from UtrechtUniversity/yoda-ruleset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiiPolicyChecks.r
236 lines (219 loc) · 7.4 KB
/
iiPolicyChecks.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
# \file iiPolicyChecks.r
# \brief Helper function to check for policy pre and post conditions
# used by the locking mechanism and the folder status transition mechanism.
# \author Paul Frederiks
# \author Lazlo Westerhof
# \copyright Copyright (c) 2016-2018, Utrecht University. All rights reserved.
# \license GPLv3, see LICENSE.
# \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 Check validity of requested status transition in the vault.
#
# \param[in] fromstatus folder status before requested transition
# \param[in] tostatus folder status after requested transition
#
iiIsVaultStatusTransitionLegal(*fromstatus, *tostatus) {
*legal = false;
foreach(*legaltransition in IIVAULTTRANSITIONS) {
(*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 user metadata can be modified.
#
# \param[in] option parameter of the action passed to the PEP. 'add', 'set' or 'rm'
# \param[in] itemType type of item (-C for collection, -d for data object)
# \param[in] itemName name of item (path in case of collection or data object)
# \param[in] attributeName attribute name of AVU
# \param[out] allowed boolean to indicate if the action is allowed
# \param[out] reason reason the action is not allowed\
#
iiCanModifyUserMetadata(*option, *itemType, *itemName, *attributeName, *allowed, *reason) {
*allowed = false;
*reason = "Unknown error";
iiGetLocks(*itemName, *locks);
if (size(*locks) > 0) {
if (*itemType == "-C") {
foreach(*rootCollection in *locks) {
if (strlen(*rootCollection) > strlen(*itemName)) {
*allowed = true;
*reason = "Lock found, but in subcollection *rootCollection";
} else {
*allowed = false;
*reason = "Lock found, starting from *rootCollection";
break;
}
}
} else {
*reason = "Locks found. *locks";
}
} else {
*allowed = true;
*reason = "No locks found";
}
#DEBUG writeLine("serverLog", "iiCanModifyUserMetadata: *itemName; allowed=*allowed; reason=*reason");
}
# \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;
}
}
}
}
}
# \brief Check if a vault folder status transition is legal.
#
# \param[in] folder
# \param[in] transitionFrom current status
# \param[in] transitionTo status to transition to
# \param[in] actor user name of actor requesting the transition
# \param[out] allowed boolean to indicate if the action is allowed
# \param[out] reason reason the action is not allowed
#
iiCanTransitionVaultStatus(*folder, *transitionFrom, *transitionTo, *actor, *allowed, *reason) {
*allowed = false;
*reason = "Unknown error";
if (iiIsVaultStatusTransitionLegal(*transitionFrom, *transitionTo)) {
*allowed = true;
*reason = "Legal status transition. *transitionFrom -> *transitionTo";
} else {
*reason = "Illegal status transition. Current status is *transitionFrom.";
succeed;
}
if (*transitionTo == PUBLISHED) {
iiGetDOIFromMetadata(*folder, *yodaDOI);
if (*yodaDOI == "") {
*allowed = false;
*reason = "*folder has no DOI"
succeed;
}
iiGetLandingPageFromMetadata(*folder, *landingPage);
if (*landingPage == "") {
*allowed = false;
*reason = "*folder has no landing page";
succeed;
}
}
}