-
Notifications
You must be signed in to change notification settings - Fork 0
/
roles_common.js
141 lines (109 loc) · 3.8 KB
/
roles_common.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
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
if (!Meteor.roles) Meteor.roles = new Meteor.Collection("roles");
if ('undefined' === typeof Roles) Roles = {};
if ('undefined' === typeof Roles._permission_checks) Roles._permission_checks = {};
_.extend(Roles, {
_flatten: function (ary) {
return [].concat.apply([], [ ary ]);
},
createRoles: function (roles) {
if (!roles) return;
roles = Roles._flatten(roles);
return _.map( roles, function(role) {
role = role.trim();
return Meteor.roles.upsert({ name: role }, { $set: { name: role } });
});
},
flushRoles: function () {
Meteor.roles.remove({});
},
deleteRoles: function (roles) {
if (!roles) return;
roles = Roles._flatten(roles);
Meteor.roles.remove({ name: { $in: roles } });
Meteor.users.update({ roles: { $in: roles } }, { $pullAll: { roles: roles } }, { multi: true } );
},
addPermissionsToRoles: function (permissions, roles) {
permissions = Roles._flatten(permissions);
roles = Roles._flatten(roles);
Meteor.roles.update({ name: { $in: roles } }, { $addToSet: { permissions: { $each: permissions } } }, { multi: true } );
},
addPermissionCheck: function (permission, check) {
Roles._permission_checks[permission] = check;
},
removePermissionsFromRoles: function (permissions, roles) {
permissions = Roles._flatten(permissions);
roles = Roles._flatten(roles);
Meteor.roles.update({ name: { $in: roles } }, { $pullAll: { permissions: permissions } }, { multi: true } );
},
addUsersToRoles: function (userIds, roles) {
userIds = Roles._flatten(userIds);
roles = Roles._flatten(roles);
Meteor.users.update({ _id: { $in: userIds } }, { $addToSet: { roles: { $each: roles } } }, { multi: true } );
},
removeUsersFromRoles: function (userIds, roles) {
userIds = Roles._flatten(userIds);
roles = Roles._flatten(roles);
Meteor.users.update({ _id: { $in: userIds } }, { $pullAll: { roles: roles } }, { multi: true } );
},
usersExistInRoles: function (roles) {
roles = Roles._flatten(roles);
var user = Meteor.users.findOne({ roles: { $in: roles } });
return user ? true : false;
},
userIs: function (roles, userId) {
if (!roles) return true;
roles = Roles._flatten(roles);
var user = Meteor.users.findOne( {
_id: userId,
roles: {
$in: roles
}
}, {
fields: { roles: 1 }
});
return user ? true : false;
},
userCan: function () {
var args = Array.prototype.slice.call(arguments);
var permissions = args.shift();
var userId = args[0];
if (!permissions) return true;
permissions = Roles._flatten(permissions);
var user = Meteor.users.findOne( {
_id: userId
}, {
fields: { roles: 1 }
});
if (user) {
var roles = Meteor.roles.find({ name: { $in: user.roles }, permissions: { $in: permissions } }).fetch();
var myPermissions = _.intersection(_.uniq(_.flatten(_.pluck(roles, 'permissions'))), permissions);
return _.any(myPermissions, function(permission){ return Roles._permission_checks[permission] ? Roles._permission_checks[permission](args) : true });
}
return false;
}
});
permissionDenied = function (router) {
var permissions = router.route.options.permissions || Router.options.permissions;
var hasPermission = Roles.userCan(permissions, Meteor.userId());
return !hasPermission;
}
roleDenied = function (router) {
var roles = router.route.options.roles || Router.options.roles;
var hasRole = Roles.userIs(roles, Meteor.userId());
return !hasRole;
}
isAuthorized = function () {
var router = this;
if (!Meteor.loggingIn()) {
if ( permissionDenied(router) || roleDenied(router)) {
router.render('errors.accessDenied');
}
else {
router.next();
}
}
else {
router.next();
}
}
Router.onBeforeAction(isAuthorized)