@Test public void testFilePermissionManager() throws Exception { String groupName = "Test", userName = "Tester", permission = "test.permission", groupPermission = "role.permission"; new File("build/h2database.mv.db").delete(); new File("build/group_permissions.json").delete(); AbstractDatabaseProvider databaseProvider = new H2DatabaseProvider("build/h2database", false); Assert.assertTrue(databaseProvider.init()); System.setProperty("cloudnet.permissions.json.path", "build/group_permissions.json"); IPermissionManagement permissionManagement = new DefaultDatabasePermissionManagement(() -> databaseProvider); IPermissionUser permissionUser = permissionManagement.addUser(userName, "1234", (byte) 5); Assert.assertNotNull(permissionUser); Assert.assertEquals(1, permissionManagement.getUsers().size()); permissionUser.addGroup(groupName); permissionManagement.updateUser(permissionUser); Assert.assertNotNull(permissionManagement.getUser(permissionUser.getUniqueId())); Assert.assertNotNull(permissionManagement.getUsers(permissionUser.getName())); Assert.assertFalse(permissionManagement.getUsers(permissionUser.getName()).isEmpty()); permissionUser.addPermission(new Permission(permission, 10)); permissionManagement.updateUser(permissionUser); Assert.assertNotNull(permissionManagement.getUser(permissionUser.getUniqueId())); Assert.assertTrue(permissionManagement.hasPermission(permissionUser, permission)); Assert.assertFalse(permissionManagement.hasPermission(permissionUser, new Permission(permission, 11))); permissionUser.addPermission("Test_Group", new Permission(permission, 10)); permissionManagement.updateUser(permissionUser); Assert.assertNotNull(permissionManagement.getUser(permissionUser.getUniqueId())); Assert.assertTrue(permissionManagement.hasPermission(permissionUser, "Test_Group", new Permission(permission))); Assert.assertFalse(permissionManagement.hasPermission(permissionUser, "Test_Group", new Permission(permission, 11))); permissionUser.addPermission(new Permission("test.permission.1234", 10, 5, TimeUnit.MILLISECONDS)); Thread.sleep(10); Assert.assertTrue(permissionManagement.testPermissionUser(permissionUser)); permissionManagement.updateUser(permissionUser); Assert.assertFalse(permissionManagement.hasPermission(permissionUser, "test.permission.1234")); permissionManagement.addGroup(groupName, (byte) 64); Assert.assertNotNull(permissionManagement.getGroup(groupName)); Assert.assertEquals(1, permissionManagement.getGroups().size()); IPermissionGroup permissionGroup = permissionManagement.getGroup(groupName); Assert.assertNotNull(permissionGroup); permissionGroup.addPermission(new Permission(groupPermission, 4)); permissionManagement.updateGroup(permissionGroup); Assert.assertTrue(permissionManagement.hasPermission(permissionUser, groupPermission)); Assert.assertFalse(permissionManagement.hasPermission(permissionUser, new Permission(groupPermission, 600))); permissionGroup.addPermission(new Permission("test.test.5678", 10, 1)); permissionManagement.updateGroup(permissionGroup); Assert.assertFalse(permissionManagement.getGroup(groupName).hasPermission("test.test.5678").asBoolean()); permissionGroup.addPermission("City", new Permission("test.test.5678", 10, 1)); permissionManagement.updateGroup(permissionGroup); Assert.assertFalse(permissionManagement.getGroup(groupName).hasPermission("City", new Permission("test.test.5678")).asBoolean()); permissionGroup.addPermission("City", new Permission("test.test.91011", -1)); permissionManagement.updateGroup(permissionGroup); Assert.assertFalse(permissionManagement.getGroup(groupName).hasPermission("City", new Permission("test.test.91011")).asBoolean()); Assert.assertEquals(1, permissionManagement.getUsersByGroup(groupName).size()); permissionUser.removeGroup(groupName); permissionManagement.updateUser(permissionUser); Assert.assertFalse(permissionManagement.getUsers(permissionUser.getName()).get(0).inGroup(groupName)); permissionManagement.deleteUser(userName); Assert.assertEquals(0, permissionManagement.getUsersByGroup(groupName).size()); Assert.assertEquals(0, permissionManagement.getUsers().size()); permissionManagement.deleteGroup(groupName); Assert.assertNull(permissionManagement.getGroup(groupName)); Assert.assertTrue(permissionUser.checkPassword("1234")); this.testRecursiveImplementations(permissionManagement, groupName, userName, permission); }
@Override public Collection<IPermissionGroup> getGroups() { for (IPermissionGroup permissionGroup : this.permissionGroupsMap.values()) { if (this.testPermissionGroup(permissionGroup)) { this.updateGroup(permissionGroup); } } return this.permissionGroupsMap.values(); }
DefaultDatabasePermissionManagement extends ClusterSynchronizedPermissionManagement implements DefaultSynchronizedPermissionManagement, DefaultPermissionManagement { @Override public Collection<IPermissionGroup> getGroups() { for (IPermissionGroup permissionGroup : this.permissionGroupsMap.values()) { if (this.testPermissionGroup(permissionGroup)) { this.updateGroup(permissionGroup); } } return this.permissionGroupsMap.values(); } }
DefaultDatabasePermissionManagement extends ClusterSynchronizedPermissionManagement implements DefaultSynchronizedPermissionManagement, DefaultPermissionManagement { @Override public Collection<IPermissionGroup> getGroups() { for (IPermissionGroup permissionGroup : this.permissionGroupsMap.values()) { if (this.testPermissionGroup(permissionGroup)) { this.updateGroup(permissionGroup); } } return this.permissionGroupsMap.values(); } DefaultDatabasePermissionManagement(Callable<AbstractDatabaseProvider> databaseProviderCallable); }
DefaultDatabasePermissionManagement extends ClusterSynchronizedPermissionManagement implements DefaultSynchronizedPermissionManagement, DefaultPermissionManagement { @Override public Collection<IPermissionGroup> getGroups() { for (IPermissionGroup permissionGroup : this.permissionGroupsMap.values()) { if (this.testPermissionGroup(permissionGroup)) { this.updateGroup(permissionGroup); } } return this.permissionGroupsMap.values(); } DefaultDatabasePermissionManagement(Callable<AbstractDatabaseProvider> databaseProviderCallable); @Override ITask<IPermissionUser> addUserWithoutClusterSyncAsync(IPermissionUser permissionUser); @Override ITask<Void> updateUserWithoutClusterSyncAsync(IPermissionUser permissionUser); @Override ITask<Boolean> deleteUserWithoutClusterSyncAsync(IPermissionUser permissionUser); @Override boolean containsUser(@NotNull UUID uniqueId); @Override @NotNull ITask<Boolean> containsUserAsync(@NotNull UUID uniqueId); @Override @NotNull ITask<Boolean> containsUserAsync(@NotNull String name); @Override @NotNull ITask<IPermissionUser> getUserAsync(@NotNull UUID uniqueId); @Override @NotNull ITask<List<IPermissionUser>> getUsersAsync(@NotNull String name); @Override @NotNull ITask<IPermissionUser> getFirstUserAsync(String name); @Override @NotNull ITask<Collection<IPermissionUser>> getUsersAsync(); @Override ITask<Void> setUsersWithoutClusterSyncAsync(Collection<? extends IPermissionUser> users); @Override @NotNull ITask<Collection<IPermissionUser>> getUsersByGroupAsync(@NotNull String group); @Override @NotNull ITask<IPermissionGroup> addGroupAsync(@NotNull String role, int potency); @Override @NotNull ITask<Boolean> containsGroupAsync(@NotNull String group); @Override ITask<IPermissionGroup> addGroupWithoutClusterSyncAsync(IPermissionGroup permissionGroup); @Override ITask<Void> updateGroupWithoutClusterSyncAsync(IPermissionGroup permissionGroup); @Override ITask<Void> deleteGroupWithoutClusterSyncAsync(String group); @Override ITask<Void> deleteGroupWithoutClusterSyncAsync(IPermissionGroup group); @Override boolean containsGroup(@NotNull String name); @Override @NotNull ITask<IPermissionGroup> getGroupAsync(@NotNull String name); @Override ITask<IPermissionGroup> getDefaultPermissionGroupAsync(); @Override @NotNull ITask<Collection<IPermissionGroup>> getGroupsAsync(); @Override Collection<IPermissionGroup> getGroups(); @Override @NotNull ITask<Collection<IPermissionGroup>> getGroupsAsync(@Nullable IPermissionUser permissionUser); @Override @NotNull ITask<IPermissionUser> addUserAsync(@NotNull String name, @NotNull String password, int potency); @Override ITask<Void> setGroupsWithoutClusterSyncAsync(Collection<? extends IPermissionGroup> groups); @Override boolean reload(); IDatabase getDatabase(); Map<String, IPermissionGroup> getPermissionGroupsMap(); Callable<AbstractDatabaseProvider> getDatabaseProviderCallable(); IPermissionManagementHandler getPermissionManagementHandler(); void setPermissionManagementHandler(IPermissionManagementHandler permissionManagementHandler); }
DefaultDatabasePermissionManagement extends ClusterSynchronizedPermissionManagement implements DefaultSynchronizedPermissionManagement, DefaultPermissionManagement { @Override public Collection<IPermissionGroup> getGroups() { for (IPermissionGroup permissionGroup : this.permissionGroupsMap.values()) { if (this.testPermissionGroup(permissionGroup)) { this.updateGroup(permissionGroup); } } return this.permissionGroupsMap.values(); } DefaultDatabasePermissionManagement(Callable<AbstractDatabaseProvider> databaseProviderCallable); @Override ITask<IPermissionUser> addUserWithoutClusterSyncAsync(IPermissionUser permissionUser); @Override ITask<Void> updateUserWithoutClusterSyncAsync(IPermissionUser permissionUser); @Override ITask<Boolean> deleteUserWithoutClusterSyncAsync(IPermissionUser permissionUser); @Override boolean containsUser(@NotNull UUID uniqueId); @Override @NotNull ITask<Boolean> containsUserAsync(@NotNull UUID uniqueId); @Override @NotNull ITask<Boolean> containsUserAsync(@NotNull String name); @Override @NotNull ITask<IPermissionUser> getUserAsync(@NotNull UUID uniqueId); @Override @NotNull ITask<List<IPermissionUser>> getUsersAsync(@NotNull String name); @Override @NotNull ITask<IPermissionUser> getFirstUserAsync(String name); @Override @NotNull ITask<Collection<IPermissionUser>> getUsersAsync(); @Override ITask<Void> setUsersWithoutClusterSyncAsync(Collection<? extends IPermissionUser> users); @Override @NotNull ITask<Collection<IPermissionUser>> getUsersByGroupAsync(@NotNull String group); @Override @NotNull ITask<IPermissionGroup> addGroupAsync(@NotNull String role, int potency); @Override @NotNull ITask<Boolean> containsGroupAsync(@NotNull String group); @Override ITask<IPermissionGroup> addGroupWithoutClusterSyncAsync(IPermissionGroup permissionGroup); @Override ITask<Void> updateGroupWithoutClusterSyncAsync(IPermissionGroup permissionGroup); @Override ITask<Void> deleteGroupWithoutClusterSyncAsync(String group); @Override ITask<Void> deleteGroupWithoutClusterSyncAsync(IPermissionGroup group); @Override boolean containsGroup(@NotNull String name); @Override @NotNull ITask<IPermissionGroup> getGroupAsync(@NotNull String name); @Override ITask<IPermissionGroup> getDefaultPermissionGroupAsync(); @Override @NotNull ITask<Collection<IPermissionGroup>> getGroupsAsync(); @Override Collection<IPermissionGroup> getGroups(); @Override @NotNull ITask<Collection<IPermissionGroup>> getGroupsAsync(@Nullable IPermissionUser permissionUser); @Override @NotNull ITask<IPermissionUser> addUserAsync(@NotNull String name, @NotNull String password, int potency); @Override ITask<Void> setGroupsWithoutClusterSyncAsync(Collection<? extends IPermissionGroup> groups); @Override boolean reload(); IDatabase getDatabase(); Map<String, IPermissionGroup> getPermissionGroupsMap(); Callable<AbstractDatabaseProvider> getDatabaseProviderCallable(); IPermissionManagementHandler getPermissionManagementHandler(); void setPermissionManagementHandler(IPermissionManagementHandler permissionManagementHandler);  }