ForumServiceUtils { public static boolean hasPermission(String[] userGroupMembership, String userId) throws Exception { IdentityRegistry identityRegistry = (IdentityRegistry) ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(IdentityRegistry.class); Identity identity = identityRegistry.getIdentity(userId); if (identity == null) { OrganizationService oService = (OrganizationService) ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(OrganizationService.class); Collection<MembershipEntry> memberships = oService.getMembershipHandler().findMembershipsByUser(userId); identity = new Identity(userId, memberships); } if (userGroupMembership == null || userGroupMembership.length <= 0 || userGroupMembership[0].equals(" ")) return false; for (String item : userGroupMembership) { String expr = item.trim(); if (isMembershipExpression(expr)) { String[] array = expr.split(":"); String membershipType = array[0]; String group = array[1]; if (identity.isMemberOf(group, membershipType)) { return true; } } else if (isGroupExpression(expr)) { String group = expr; if (identity.isMemberOf(group)) { return true; } } else { String username = expr; if (username.equals(userId)) { return true; } } } return false; }  static boolean hasPermission(String[] userGroupMembership, String userId); @SuppressWarnings("unchecked") static List<String> getUserPermission(String[] userGroupMembership); static void reparePermissions(Node node, String owner); static SessionProvider getSessionProvider(); static SessionManager getSessionManager();  }
@Test public void testHasPermission() throws Exception { String user = "user1"; organizationService.addMemberships(user, "*:/platform/users"); simulateAuthenticate(user); assertFalse(ForumServiceUtils.hasPermission(null, user)); assertFalse(ForumServiceUtils.hasPermission(new String[] { " " }, user)); assertTrue(ForumServiceUtils.hasPermission(new String[] { user }, user)); assertFalse(ForumServiceUtils.hasPermission(new String[] { "foo" }, user)); assertTrue(ForumServiceUtils.hasPermission(new String[] { "/platform/users" }, user)); assertTrue(ForumServiceUtils.hasPermission(new String[] { "/platform/users", user }, user)); assertTrue(ForumServiceUtils.hasPermission(new String[] { "/platform/users", "user2" }, user)); assertFalse(ForumServiceUtils.hasPermission(new String[] { "/foo" }, user)); assertTrue(ForumServiceUtils.hasPermission(new String[] { "admin:/platform/users" }, user)); assertTrue(ForumServiceUtils.hasPermission(new String[] { "*:/platform/users" }, user)); }