2017-06-01 01:54:43 +08:00
|
|
|
import { check } from 'meteor/check';
|
2017-06-03 04:37:22 +08:00
|
|
|
import { Match } from 'meteor/check';
|
2017-06-05 22:16:19 +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
|
|
|
}
|
|
|
|
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)) {
|
|
|
|
return permissions.some((internalAcl)=>(this.fetchPermission(permission, internalAcl)));
|
|
|
|
} else if (Match.test(permissions, Object)) {
|
2017-06-03 04:37:22 +08:00
|
|
|
if (permission.indexOf(".") > -1) {
|
2017-06-05 22:16:19 +08:00
|
|
|
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-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-02 01:32:04 +08:00
|
|
|
let meetingId = credentials.meetingId;
|
|
|
|
let userId = credentials.requesterUserId;
|
|
|
|
let authToken = credentials.requesterToken;
|
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-05 22:16:19 +08:00
|
|
|
const roles = user.user.roles;
|
|
|
|
let permissions = {};
|
|
|
|
|
|
|
|
roles.forEach((role)=>{
|
|
|
|
permissions = deepMerge(permissions,this.config[role]);
|
|
|
|
});
|
|
|
|
|
|
|
|
return permissions;
|
2017-06-01 01:54:43 +08:00
|
|
|
}
|
|
|
|
}
|