2017-06-01 01:54:43 +08:00
|
|
|
import { check } from 'meteor/check';
|
|
|
|
|
|
|
|
export class Acl {
|
|
|
|
|
|
|
|
constructor(aclConfig, Users) {
|
|
|
|
this.Users = Users;
|
|
|
|
this.aclConfig = aclConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribe(channel,credentials){
|
|
|
|
check(channel, String);
|
2017-06-01 21:12:11 +08:00
|
|
|
console.log("Channell",channel);
|
|
|
|
console.log("credentials",credentials);
|
2017-06-01 01:54:43 +08:00
|
|
|
|
|
|
|
let subscriptions = this.getSubscriptions(credentials);
|
|
|
|
|
2017-06-01 21:12:11 +08:00
|
|
|
console.log("subscriptions",subscriptions);
|
|
|
|
|
2017-06-01 01:54:43 +08:00
|
|
|
if (subscriptions) {
|
|
|
|
return !!this.checkPermission(channel, subscriptions);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
getSubscriptions(credentials){
|
|
|
|
let role = this.getRole(credentials);
|
|
|
|
|
2017-06-01 21:12:11 +08:00
|
|
|
if(!role.subscribe){
|
|
|
|
return [];
|
|
|
|
}
|
2017-06-01 01:54:43 +08:00
|
|
|
return role.subscriptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkSubscription(channel, subscriptions) {
|
|
|
|
check(channel, String);
|
|
|
|
|
|
|
|
const isInList = subscriptions.some((perm)=> perm.indexOf(permission) > -1 );
|
|
|
|
|
|
|
|
return isInList;
|
|
|
|
}
|
|
|
|
|
|
|
|
getMethods(credentials){
|
|
|
|
let role = this.getRole(credentials);
|
|
|
|
|
2017-06-01 21:12:11 +08:00
|
|
|
if(!role.methods){
|
|
|
|
return [];
|
|
|
|
}
|
2017-06-01 01:54:43 +08:00
|
|
|
return role.methods;
|
|
|
|
}
|
|
|
|
|
|
|
|
can(permission, credentials) {
|
|
|
|
let methods = this.getMethods(credentials);
|
|
|
|
check(permission, String);
|
|
|
|
|
|
|
|
if (methods) {
|
|
|
|
return !!this.checkPermission(permission, methods);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRole(credentials){
|
|
|
|
if(!credentials){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const meetingId = credentials.meetingId;
|
|
|
|
const userId = credentials.requesterUserId;
|
|
|
|
const authToken = credentials.requesterToken;
|
|
|
|
|
|
|
|
const user = this.Users.findOne({
|
|
|
|
meetingId,
|
|
|
|
userId,
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!user){
|
2017-06-01 21:12:11 +08:00
|
|
|
console.log("Usuario vazio");
|
2017-06-01 01:54:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this.roleExist(this.aclConfig, user.user.role);
|
|
|
|
}
|
|
|
|
|
|
|
|
checkPermission(permission, permissions) {
|
|
|
|
check(permissions, Array);
|
|
|
|
check(permission, String);
|
|
|
|
|
|
|
|
const isInList = permissions.some((perm)=> perm.indexOf(permission) > -1 );
|
|
|
|
return isInList;
|
|
|
|
}
|
|
|
|
|
|
|
|
roleExist(acl, userRole) {
|
|
|
|
check(acl, Object);
|
|
|
|
check(userRole, String);
|
|
|
|
return acl[userRole.toLowerCase()];
|
|
|
|
}
|
|
|
|
}
|