2017-06-01 01:54:43 +08:00
|
|
|
import { check } from 'meteor/check';
|
2017-06-06 00:40:36 +08:00
|
|
|
import deepMerge from '/imports/utils/deepMerge';
|
2017-06-01 01:54:43 +08:00
|
|
|
|
|
|
|
export class Acl {
|
|
|
|
|
2017-06-02 04:40:55 +08:00
|
|
|
constructor(config, Users) {
|
2017-06-01 01:54:43 +08:00
|
|
|
this.Users = Users;
|
2017-06-02 04:40:55 +08:00
|
|
|
this.config = config;
|
2017-06-01 01:54:43 +08:00
|
|
|
}
|
|
|
|
|
2017-06-03 04:37:22 +08:00
|
|
|
can(permission, credentials) {
|
|
|
|
check(permission, String);
|
2017-06-05 22:16:19 +08:00
|
|
|
const permissions = this.getPermissions(credentials);
|
2017-06-01 01:54:43 +08:00
|
|
|
|
2017-06-05 22:16:19 +08:00
|
|
|
if (permissions) {
|
|
|
|
return this.fetchPermission(permission, permissions);
|
2017-06-01 01:54:43 +08:00
|
|
|
}
|
2017-06-06 00:40:36 +08:00
|
|
|
|
2017-06-01 01:54:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-05 22:16:19 +08:00
|
|
|
fetchPermission(permission, permissions) {
|
2017-06-03 04:37:22 +08:00
|
|
|
if (!permission) return false;
|
2017-06-01 01:54:43 +08:00
|
|
|
|
2017-06-05 22:16:19 +08:00
|
|
|
if (Match.test(permissions, String)) {
|
|
|
|
return permissions.indexOf(permission) > -1;
|
|
|
|
} else if (Match.test(permissions, Array)) {
|
2017-06-06 03:12:06 +08:00
|
|
|
return permissions.some(internalAcl => (this.fetchPermission(permission, internalAcl)));
|
2017-06-05 22:16:19 +08:00
|
|
|
} else if (Match.test(permissions, Object)) {
|
2017-06-06 00:40:36 +08:00
|
|
|
if (permission.indexOf('.') > -1) {
|
|
|
|
return this.fetchPermission(permission.substring(permission.indexOf('.') + 1),
|
|
|
|
permissions[permission.substring(0, permission.indexOf('.'))]);
|
2017-06-03 04:37:22 +08:00
|
|
|
}
|
2017-06-05 22:16:19 +08:00
|
|
|
return permissions[permission];
|
2017-06-01 01:54:43 +08:00
|
|
|
}
|
2017-06-06 03:12:06 +08:00
|
|
|
return false;
|
2017-06-01 01:54:43 +08:00
|
|
|
}
|
|
|
|
|
2017-06-05 22:16:19 +08:00
|
|
|
getPermissions(credentials) {
|
2017-06-03 04:37:22 +08:00
|
|
|
if (!credentials) {
|
2017-06-01 01:54:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
2017-06-06 00:40:36 +08:00
|
|
|
|
2017-06-06 03:12:06 +08:00
|
|
|
const meetingId = credentials.meetingId;
|
|
|
|
const userId = credentials.requesterUserId;
|
2017-06-01 01:54:43 +08:00
|
|
|
|
|
|
|
const user = this.Users.findOne({
|
|
|
|
meetingId,
|
|
|
|
userId,
|
|
|
|
});
|
|
|
|
|
2017-06-03 04:37:22 +08:00
|
|
|
if (!user) {
|
2017-06-01 01:54:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
2017-06-06 00:40:36 +08:00
|
|
|
|
2017-06-05 22:16:19 +08:00
|
|
|
const roles = user.user.roles;
|
|
|
|
let permissions = {};
|
|
|
|
|
2017-06-06 00:40:36 +08:00
|
|
|
roles.forEach((role) => {
|
|
|
|
permissions = deepMerge(permissions, this.config[role]);
|
2017-06-05 22:16:19 +08:00
|
|
|
});
|
2017-06-06 00:40:36 +08:00
|
|
|
|
2017-06-05 22:16:19 +08:00
|
|
|
return permissions;
|
2017-06-01 01:54:43 +08:00
|
|
|
}
|
|
|
|
}
|