forked from Meteor-Community-Packages/meteor-partitioner
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrouping_client.js
46 lines (38 loc) · 1.43 KB
/
grouping_client.js
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
function userFindHook(userId, selector /*, options */) {
// Do the usual find for no user or single selector
if (!userId || Helpers.isDirectUserSelector(selector)) return true;
// No hooking needed for regular users, taken care of on server
if (!Partitioner._isAdmin(Meteor.userId())) return true;
// Don't have admin see itself for global finds
if (selector == null) {
selector = {admin: {$exists: false}};
} else {
selector.admin = {$exists: false};
}
return true;
};
Meteor.users._partitionerBefore.find(userFindHook);
Meteor.users._partitionerBefore.findOne(userFindHook);
function insertHook(userId, doc) {
if (!userId) throw new Meteor.Error(403, ErrMsg.userIdErr);
const groupId = Partitioner.group();
if (!groupId) throw new Meteor.Error(403, ErrMsg.groupErr);
doc._groupId = groupId;
return true;
};
Partitioner = {
group() {
const userId = Meteor.userId();
return userId && (Meteor.users._partitionerDirect.findOne(userId, {fields: {group: 1}}) || {}).group;
},
// This can be replaced - currently not documented
// Don't retrieve full user object - fix bug #32
_isAdmin(_id) {
return Meteor.users._partitionerDirect.find({_id, admin: true}, {fields: {_id: 1}}).count() > 0;
},
// Add in groupId for client so as not to cause unexpected sync changes
partitionCollection(collection) {
// No find hooks needed if server side filtering works properly
return collection._partitionerBefore.insert(insertHook);
},
};