Skip to content

Commit

Permalink
move permission stuff to the resolver (#1185)
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek authored and amishshah committed Feb 22, 2017
1 parent f01b3f9 commit 566135d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
14 changes: 14 additions & 0 deletions src/client/ClientDataResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,20 @@ class ClientDataResolver {
return bitfield;
}

hasPermission(bitfield, name, explicit = false) {
const permission = this.resolvePermission(name);
if (!explicit && (bitfield & Constants.PermissionFlags.ADMINISTRATOR) > 0) return true;
return (bitfield & permission) > 0;
}

serializePermissions(bitfield) {
const serializedPermissions = {};
for (const name in Constants.PermissionFlags) {
serializedPermissions[name] = this.hasPermission(bitfield, name);
}
return serializedPermissions;
}

/**
* Data that can be resolved to give a string. This can be:
* * A string
Expand Down
12 changes: 3 additions & 9 deletions src/structures/Role.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,7 @@ class Role {
* console.log(role.serialize());
*/
serialize() {
const serializedPermissions = {};
for (const permissionName in Constants.PermissionFlags) {
serializedPermissions[permissionName] = this.hasPermission(permissionName);
}
return serializedPermissions;
return this.client.resolver.serializePermissions(this.permissions);
}

/**
Expand All @@ -160,10 +156,8 @@ class Role {
* console.log('This role can\'t ban members');
* }
*/
hasPermission(permission, explicit = false) {
permission = this.client.resolver.resolvePermission(permission);
if (!explicit && (this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) return true;
return (this.permissions & permission) > 0;
hasPermission(permission, explicit) {
return this.client.resolver.hasPermission(this.permissions, permission, explicit);
}

/**
Expand Down

0 comments on commit 566135d

Please sign in to comment.