PermissionUtil { public static PermissionSet computePermissions(PermissionSet base, @Nullable PermissionOverwrite everyoneOverwrite, List<PermissionOverwrite> roleOverwrites, @Nullable PermissionOverwrite memberOverwrite) { if (base.contains(Permission.ADMINISTRATOR)) { return PermissionSet.all(); } List<PermissionOverwrite> allOverwrites = new ArrayList<>(); if (everyoneOverwrite != null) allOverwrites.add(everyoneOverwrite); allOverwrites.addAll(roleOverwrites); if (memberOverwrite != null) allOverwrites.add(memberOverwrite); return allOverwrites.stream().reduce(base, PermissionUtil::applyOverwrite, PermissionSet::or); }  static PermissionSet computeBasePermissions(PermissionSet everyonePerms, List<PermissionSet> rolePerms); static PermissionSet computePermissions(PermissionSet base, @Nullable PermissionOverwrite everyoneOverwrite,
                                                   List<PermissionOverwrite> roleOverwrites,
                                                   @Nullable PermissionOverwrite memberOverwrite);  }
@Test public void testAdminBypassesOverwrites() { PermissionSet base = PermissionSet.of(SEND_MESSAGES, ADMINISTRATOR); List<PermissionOverwrite> roleOverwrites = Collections.emptyList(); PermissionOverwrite memberOverwrite = overwrite(PermissionSet.none(), PermissionSet.of(SEND_MESSAGES)); PermissionSet actual = PermissionUtil.computePermissions(base, null, roleOverwrites, memberOverwrite); PermissionSet expected = PermissionSet.all(); assertEquals(expected, actual); }
@Test public void testNoOverwritesYieldsOriginal() { PermissionSet base = PermissionSet.of(SEND_MESSAGES); List<PermissionOverwrite> roleOverwrites = Collections.emptyList(); PermissionSet actual = PermissionUtil.computePermissions(base, null, roleOverwrites, null); PermissionSet expected = PermissionSet.of(SEND_MESSAGES); assertEquals(expected, actual); }
@Test public void testRoleOverwriteAllows() { PermissionSet base = PermissionSet.of(SEND_MESSAGES); List<PermissionOverwrite> roleOverwrites = Collections.singletonList(overwrite(PermissionSet.of(PRIORITY_SPEAKER), PermissionSet.none())); PermissionSet actual = PermissionUtil.computePermissions(base, null, roleOverwrites, null); PermissionSet expected = PermissionSet.of(SEND_MESSAGES, PRIORITY_SPEAKER); assertEquals(expected, actual); }
@Test public void testMemberOverwriteAllows() { PermissionSet base = PermissionSet.of(SEND_MESSAGES); List<PermissionOverwrite> roleOverwrites = Collections.emptyList(); PermissionOverwrite memberOverwrite = overwrite(PermissionSet.of(PRIORITY_SPEAKER), PermissionSet.none()); PermissionSet actual = PermissionUtil.computePermissions(base, null, roleOverwrites, memberOverwrite); PermissionSet expected = PermissionSet.of(SEND_MESSAGES, PRIORITY_SPEAKER); assertEquals(expected, actual); }
@Test public void testRoleOverwriteDenies() { PermissionSet base = PermissionSet.of(SEND_MESSAGES); List<PermissionOverwrite> roleOverwrites = Collections.singletonList(overwrite(PermissionSet.none(), PermissionSet.of(SEND_MESSAGES))); PermissionSet actual = PermissionUtil.computePermissions(base, null, roleOverwrites, null); PermissionSet expected = PermissionSet.none(); assertEquals(expected, actual); }
@Test public void testOverwriteAllowsAndDenies() { PermissionSet base = PermissionSet.of(SEND_MESSAGES); List<PermissionOverwrite> roleOverwrites = Collections.emptyList(); PermissionOverwrite memberOverwrite = overwrite(PermissionSet.of(PRIORITY_SPEAKER), PermissionSet.of(SEND_MESSAGES)); PermissionSet actual = PermissionUtil.computePermissions(base, null, roleOverwrites, memberOverwrite); PermissionSet expected = PermissionSet.of(PRIORITY_SPEAKER); assertEquals(expected, actual); }
@Test public void testMemberOverwriteDenies() { PermissionSet base = PermissionSet.of(SEND_MESSAGES); List<PermissionOverwrite> roleOverwrites = Collections.emptyList(); PermissionOverwrite memberOverwrite = overwrite(PermissionSet.none(), PermissionSet.of(SEND_MESSAGES)); PermissionSet actual = PermissionUtil.computePermissions(base, null, roleOverwrites, memberOverwrite); PermissionSet expected = PermissionSet.none(); assertEquals(expected, actual); }
@Test public void testAdminGrantsAll() { PermissionSet base = PermissionSet.of(ADMINISTRATOR); List<PermissionOverwrite> roleOverwrites = Collections.emptyList(); PermissionSet actual = PermissionUtil.computePermissions(base, null, roleOverwrites, null); PermissionSet expected = PermissionSet.all(); assertEquals(expected, actual); }