Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issues with permission management in RoleManager #664

Merged
merged 1 commit into from
Apr 21, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/main/java/net/dv8tion/jda/core/managers/RoleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ public RoleManager setName(String name)
public RoleManager setPermissions(long perms)
{
long selfPermissions = PermissionUtil.getEffectivePermission(getGuild().getSelfMember());
long missingPerms = ~selfPermissions & perms;
setupPermissions();
long missingPerms = perms; // include permissions we want to set to
missingPerms &= ~selfPermissions; // exclude permissions we have
missingPerms &= ~this.permissions; // exclude permissions the role has
// if any permissions remain, we have an issue
if (missingPerms != 0 && isPermissionChecksEnabled())
{
List<Permission> permissionList = Permission.getPermissions(missingPerms);
Expand Down Expand Up @@ -407,6 +411,7 @@ public RoleManager givePermissions(Permission... perms)
public RoleManager givePermissions(Collection<Permission> perms)
{
Checks.noneNull(perms, "Permissions");
setupPermissions();
return setPermissions(this.permissions | Permission.getRaw(perms));
}

Expand Down Expand Up @@ -458,6 +463,7 @@ public RoleManager revokePermissions(Permission... perms)
public RoleManager revokePermissions(Collection<Permission> perms)
{
Checks.noneNull(perms, "Permissions");
setupPermissions();
return setPermissions(this.permissions & ~Permission.getRaw(perms));
}

Expand All @@ -483,18 +489,28 @@ protected RequestBody finalizeData()
protected boolean checkPermissions()
{
Member selfMember = getGuild().getSelfMember();
long selfPermissions = PermissionUtil.getEffectivePermission(selfMember);
if ((selfPermissions & Permission.MANAGE_ROLES.getRawValue()) == 0)
if (!selfMember.hasPermission(Permission.MANAGE_ROLES))
throw new InsufficientPermissionException(Permission.MANAGE_ROLES);
if (!selfMember.canInteract(role))
throw new HierarchyException("Cannot modify a role that is higher or equal in hierarchy");
long missingRaw = ~selfPermissions & permissions;
return super.checkPermissions();
/*
//we can't reliably check the permissions of the role here
long missingRaw = permissions;
missingRaw &= ~selfPermissions; // exclude own perms
missingRaw &= ~role.getPermissionsRaw(); // exclude role perms
if (missingRaw != 0)
{
List<Permission> missingPermissions = Permission.getPermissions(missingRaw);
if (!missingPermissions.isEmpty())
throw new InsufficientPermissionException(missingPermissions.get(0));
}
return super.checkPermissions();
*/
}

private void setupPermissions()
{
if (!shouldUpdate(PERMISSION))
this.permissions = role.getPermissionsRaw();
}
}