-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmethods.js
More file actions
353 lines (326 loc) · 12.7 KB
/
methods.js
File metadata and controls
353 lines (326 loc) · 12.7 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
344
345
346
347
348
349
350
351
352
353
if (Meteor.isServer) {
checkRightsGroup = {
'isLeader': function(email, groupId){
var group = Groups.findOne({_id: groupId});
if(group.leader === email)
return true;
return Roles.userIsInRole(Meteor.userId(), ['admin','superAdmin']);
}
};
if(typeof checkRights !== 'undefined')
_.merge(checkRights, checkRightsGroup);
Meteor.methods({
/**
* Add a user to a group
* @param String userId
* @param String groupId
* @param Array roles
* @return Boolean
* true = user was added
*/
'addUserToGroup': function (userId, groupId, roles) {
var roles = roles || [];
if(checkRights.isLeader(Meteor.user().emails[0].address, groupId)) {
if (!groupId)
throw new Meteor.Error("groups", "You did not specify a groupId");
var identifier;
if (userId)
identifier = {_id: userId};
var user = Meteor.users.update(identifier,
{
$addToSet: {
'profile.groups': {
id: groupId
}
}
});
Groups.update({
_id: groupId
}, {
$addToSet: {
users: {
id: userId,
roles:roles
}
}
}
);
if (user == 1)
return true;
}
},
/**
* removes a user from a group
* @param String userId
* @param String email
* @param String groupId
* @return Boolean
* true = removed user from group successfull
*/
'removeUserFromGroup': function (userId, email, groupId) {
if(checkRights.isLeader(Meteor.user().emails[0].address, groupId)) {
if (groupId) {
var update = {
$pull: {
'profile.groups': {
id: groupId
}
}
};
var user;
if (userId != "" && userId != undefined) {
user = Meteor.users.update({_id: userId}, update);
} else if (email != "" && userId != undefined) {
user = Meteor.users.update({'emails.0.address': email}, update);
}
Groups.update({
_id: groupId
}, {
$pull: {
users: {
id: userId
}
}
}
);
if (!user)
throw new Meteor.Error("user", "Found no user!");
} else {
throw new Meteor.Error("group", "Found no group with specified groupid!");
}
return true;
}
},
/**
* checks if a given groupname already exists
* @param String groupname
* @return Boolean
* true = groupname is existing
* false = groupname not existing
*/
checkGroupnameExisting: function (groupname) {
var existingGroupname = Groups.find({groupname: groupname}).fetch();
if (existingGroupname.length > 0)
return true;
return false;
},
/**
* creates a group (autoform method call)
* @param Object doc
* @param Object mod
* @param String documentId
* @return Boolean
* true = created group successfull
*/
createGroup: function (doc, mod, documentId) {
//TODO check if project exists
//if(checkRights.checkUserRight("",Meteor.userId())) {
var parentId = "";
var parentGroup = Groups.findOne({
groupname: doc.parentGroup
});
if (typeof parentGroup !== "undefined")
parentId = parentGroup._id;
var leaderId = null;
leader = Meteor.users.findOne({'emails.0.address': doc.leader});
if(leader){
leaderId = leader._id;
}
if (doc.groupname) {
var group = Groups.insert({
groupname: doc.groupname,
parentGroup: parentId,
leader: leaderId,
projects: doc.projects,
agency: doc.agency,
users: doc.users
});
if (!group)
throw new Meteor.Error("group", "Create group failed!");
if(leaderId)
Meteor.call('addUserToGroup', leaderId, group);
} else {
if (name === "")
throw new Meteor.Error("group", "Name was not specified!");
}
return true;
//}
},
/**
* updates a group (autoform method call)
* @param Object doc
* @param Object mod
* @param String documentId
* @return Boolean
* true = edit group successfull
*/
updateGroup: function (doc, mod, documentId) {
if(!Meteor.user()){
throw new Meteor.Error("group", "you need to be logged in to do this");
}
if(checkRights.isLeader(Meteor.user().emails[0].address, documentId)) {
var parentId = "";
var parentGroup = Groups.findOne({
groupname: doc.parentGroup
});
if(typeof parentGroup !== "undefined")
parentId = parentGroup._id;
var leaderId = null;
leader = Meteor.users.findOne({'emails.0.address': doc.leader});
if(leader){
leaderId = leader._id;
}
var group = Groups.update({
_id: documentId
}, {
$set: {
groupname: doc.groupname,
parentGroup: parentId,
leader: leaderId,
projects: doc.projects,
agency: doc.agency,
users: doc.users
}
}
);
if (group != 1)
throw new Meteor.Error("group", "updating the group failed");
if(leaderId)
Meteor.call('addUserToGroup', leaderId, documentId);
return true;
}
},
/**
* assigns a sub group to a group
* @param String groupId
* @param String parentGroupId
* @return Boolean
* true = assign subgroup successfull
*/
assignSubGroup: function (groupId, parentGroupId) {
if(checkRights.isLeader(Meteor.user().emails[0].address, groupId)) {
if (groupId && parentGroupId) {
Groups.update({_id: groupId}, {
$set: {
parentGroup: parentGroupId
}
});
} else {
if (parentGroupId === "")
throw new Meteor.Error("group", "Parent group id was not specified!");
if (groupId === "")
throw new Meteor.Error("group", "Group id was not specified");
}
}
},
/**
* remove a sub group from a group
* @param String groupId
* @param String parentGroupId
* @return Boolean
* true = removed subgroup successfull
*/
removeSubGroup: function (groupId, parentGroupId) {
if(checkRights.isLeader(Meteor.user().emails[0].address, groupId)) {
if (groupId && parentGroupId) {
Groups.update({_id: grouId}, {
$set: {
parentGroup: null
}
});
} else {
if (parentGroupId === "")
throw new Meteor.Error("group", "Parent group id was not specified!");
if (groupId === "")
throw new Meteor.Error("group", "Group id was not specified");
}
}
},
/**
* removes a group
* @param String groupId
* @return Boolean
* true = removed group successfull
*/
removeGroup: function (groupId) {
if (Roles.userIsInRole(Meteor.userId(),['admin','superAdmin'])) {
if (groupId) {
var users = Groups.findOne({_id: groupId}).users;
var group = Groups.remove({_id: groupId});
//Find children and remove parentId
var children = Groups.update({parentGroup: groupId}, {
$set: {parentGroup: null}
});
_.each(users, function (user) {
Meteor.users.update(
{
_id: user.id
}, {
$pull: {
'profile.groups': {
id: groupId
}
}
});
});
Meteor.roles.remove({groupId:groupId});
return true;
} else {
throw new Meteor.Error("groups", "No group id was specified while removing group");
}
}
},
/**
* add user role
* @param String groupId
* @param String userId
* @param String role
* @return Boolean
* true = added role
*/
addUserRoleInGroup: function(groupId, userId, role){
if(Roles.userIsInGroupRole(Meteor.userId(), groupId, 'admin') || Roles.userIsInRole(Meteor.userId(),['superAdmin','admin'])) {
if (typeof groupId !== 'undefined'
&& typeof userId !== 'undefined'
&& typeof role !== 'undefined') {
var roleExists = Meteor.roles.findOne({name: role, groupId: groupId});
if (!roleExists)
throw new Meteor.Error("GroupUserRoles", "The role does not exist");
Groups.update({_id: groupId, 'users.id': userId}, {
$addToSet: {
'users.$.roles': role
}
});
}
}else{
throw new Meteor.error("addUserRoleInGroup", "You are not allowed to add User Roles in this Group");
}
},
/**
* add user role
* @param String groupId
* @param String userId
* @param String role
* @return Boolean
* true = added role
*/
deleteUserRoleInGroup: function(groupId, userId, role){
if(Roles.userIsInGroupRole(Meteor.userId(), groupId, 'admin') || Roles.userIsInRole(Meteor.userId(),['superAdmin','admin'])) {
if (typeof groupId !== 'undefined'
&& typeof userId !== 'undefined'
&& typeof role !== 'undefined') {
var roleExists = Meteor.roles.findOne({name: role, groupId: groupId});
if (!roleExists)
throw new Meteor.Error("GroupUserRoles", "The role does not exist");
Groups.update({_id: groupId, 'users.id': userId}, {
$pull: {
'users.$.roles': role
}
});
}
}else{
throw new Meteor.error("deleteUserRoleInGroup", "You are not allowed to delete User Roles in this Group");
}
}
});
}