Package org.rhq.core.domain.authz

Examples of org.rhq.core.domain.authz.Role


        if (log.isTraceEnabled()) {
            log.trace("finding role [" + roleId + "]");
        }

        RoleManagerLocal roleManager = LookupUtil.getRoleManager();
        Role role = roleManager.getRoleById(roleId);

        request.setAttribute(Constants.ROLE_ATTR, role);
        request.setAttribute(Constants.TITLE_PARAM_ATTR, role.getName());
    }
View Full Code Here


        if (roleId == null) {
            roleId = RequestUtils.getRoleId(request);
        }

        Role role = (Role) request.getAttribute(Constants.ROLE_ATTR);
        if (role == null) {
            RequestUtils.setError(request, Constants.ERR_ROLE_NOT_FOUND);
            return null;
        }

        editForm.setR(role.getId());
        editForm.setName(role.getName());
        editForm.setDescription(role.getDescription());
        editForm.setPermissions(new ArrayList<Permission>(role.getPermissions()));

        return null;
    }
View Full Code Here

        HttpServletRequest request, HttpServletResponse response) throws Exception {
        Log log = LogFactory.getLog(ViewAction.class.getName());

        // make sure the request has selected a role
        Integer roleId = RequestUtils.getRoleId(request);
        Role role = (Role) request.getAttribute(Constants.ROLE_ATTR);
        if (role == null) {
            RequestUtils.setError(request, Constants.ERR_ROLE_NOT_FOUND);
            return null;
        }
View Full Code Here

        if (roleId == null) {
            roleId = RequestUtils.getRoleId(request);
        }

        Role role = (Role) request.getAttribute(Constants.ROLE_ATTR);
        if (role == null) {
            RequestUtils.setError(request, Constants.ERR_ROLE_NOT_FOUND);
            return null;
        }
        //use cached LDAP group list to avoid hitting ldap server each time ui pref changed.
        Set<Map<String, String>> cachedAvailableLdapGroups = null;
        cachedAvailableLdapGroups = (Set<Map<String, String>>) request.getSession().getAttribute(LDAP_GROUP_CACHE);

        addForm.setR(role.getId());

        PageControl pca = WebUtility.getPageControl(request, "a");
        PageControl pcp = WebUtility.getPageControl(request, "p");

        //BZ-580127 Refactor so that all lists are initialized regardless of ldap server
        // availability or state of filter params
        List<String> pendingGroupIds = new ArrayList<String>();
        Set<Map<String, String>> allGroups = new HashSet<Map<String, String>>();
        PageList<LdapGroup> assignedList = new PageList<LdapGroup>();
        Set<Map<String, String>> availableGroupsSet = new HashSet<Map<String, String>>();
        Set<Map<String, String>> pendingSet = new HashSet<Map<String, String>>();

        PageList<Map<String, String>> pendingGroups = new PageList<Map<String, String>>(pendingSet, 0, pcp);
        PageList<Map<String, String>> availableGroups = new PageList<Map<String, String>>(availableGroupsSet, 0, pca);
        /* pending groups are those on the right side of the "add
         * to list" widget- awaiting association with the role when the form's "ok" button is clicked. */
        pendingGroupIds = SessionUtils.getListAsListStr(request.getSession(), Constants.PENDING_RESGRPS_SES_ATTR);

        log.trace("getting pending groups for role [" + roleId + ")");
        String name = "foo";

        try { //defend against ldap communication runtime difficulties.

            if (cachedAvailableLdapGroups == null) {
                //                allGroups = LdapGroupManagerBean.getInstance().findAvailableGroups();
                allGroups = ldapManager.findAvailableGroups();
            } else {//reuse cached.
                allGroups = cachedAvailableLdapGroups;
            }
            //store unmodified list in session.
            cachedAvailableLdapGroups = allGroups;

            //retrieve currently assigned groups
            assignedList = ldapManager.findLdapGroupsByRole(role.getId(), PageControl.getUnlimitedInstance());

            //trim already defined from all groups returned.
            allGroups = filterExisting(assignedList, allGroups);
            Set<String> pendingIds = new HashSet<String>(pendingGroupIds);

View Full Code Here

        getTransactionManager().begin();
        EntityManager em = getEntityManager();
        try {
            /* bootstrap */
            Subject testSubject = SessionTestHelper.createNewSubject(em, "testSubject");
            Role testRole1 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole1");

            /* setup permissions */
            Set<Permission> globalPermissions = SessionTestHelper.getAllGlobalPerms();
            testRole1.getPermissions().addAll(globalPermissions);
            em.merge(testRole1);
            em.flush();

            /* verify getGlobalPermissions test */
            Set<Permission> globalResults = authorizationManager.getExplicitGlobalPermissions(testSubject);
            assert SessionTestHelper.samePermissions(globalPermissions, globalResults) : "Failed to get 1-subject, 1-role global permissions";

            /* verify hasGlobalPermission test */
            for (Permission permission : globalPermissions) {
                assert (authorizationManager.hasGlobalPermission(testSubject, permission)) : "Failed to get global permission "
                    + permission.toString();
            }
        } finally {
            getTransactionManager().rollback();
        }

        /*
         * accuracy test: redundant global permissions in 2 roles
         *
         *          p (all & only global perms)            | subject -- role       \        \- role            |
         *    p (all & only global perms)
         */
        getTransactionManager().begin();
        em = getEntityManager();
        try {
            /* bootstrap */
            Subject testSubject = SessionTestHelper.createNewSubject(em, "testSubject");
            Role testRole1 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole1");
            Role testRole2 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole2");

            /* setup permissions */
            Set<Permission> globalPermissions = SessionTestHelper.getAllGlobalPerms();
            testRole1.getPermissions().addAll(globalPermissions);
            testRole2.getPermissions().addAll(globalPermissions);
            em.merge(testRole1);
            em.merge(testRole2);
            em.flush();

            /* verify getGlobalPermissions test */
            Set<Permission> globalResults = authorizationManager.getExplicitGlobalPermissions(testSubject);
            assert SessionTestHelper.samePermissions(globalPermissions, globalResults) : "Failed to get 1-subject, 2-role distinct global permissions";

            /* verify hasGlobalPermission test */
            for (Permission permission : globalPermissions) {
                assert (authorizationManager.hasGlobalPermission(testSubject, permission)) : "Failed to get global permission "
                    + permission.toString();
            }
        } finally {
            getTransactionManager().rollback();
        }

        /*
         * negative test: no global perms on either role
         *
         *          p (no global perms)            | subject -- role       \        \- role            |            p
         * (no global perms)
         */
        getTransactionManager().begin();
        em = getEntityManager();
        try {
            /* bootstrap */
            Subject testSubject = SessionTestHelper.createNewSubject(em, "testSubject");
            Role testRole1 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole1");
            Role testRole2 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole2");

            /* setup permissions */
            testRole1.getPermissions().add(Permission.CONTROL);
            testRole2.getPermissions().add(Permission.CONFIGURE_WRITE);
            em.merge(testRole1);
            em.merge(testRole2);
            em.flush();

            /* verify getGlobalPermissions test */
 
View Full Code Here

         *          p (mixed permissions)            | subject -- role -- group
         */
        try {
            /* bootstrap */
            Subject testSubject = SessionTestHelper.createNewSubject(em, "testSubject");
            Role testRole1 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole1");
            ResourceGroup testGroup1 = SessionTestHelper.createNewMixedGroupForRole(em, testRole1, "testGroup1", false);

            /* setup permissions */
            Set<Permission> someMixedPermissions = EnumSet.of(Permission.CONFIGURE_WRITE, Permission.CONTROL,
                Permission.MANAGE_SECURITY);
            testRole1.getPermissions().addAll(someMixedPermissions);
            em.merge(testRole1);
            em.flush();

            /* verify getGroupPermissions test */
            Set<Permission> someMixedResults = authorizationManager.getExplicitGroupPermissions(testSubject, testGroup1
                .getId());
            assert SessionTestHelper.samePermissions(someMixedPermissions, someMixedResults) : "Failed to get all 1-role, 1-group permissions";

            /* verify hasGroupPermission test */
            for (Permission permission : someMixedPermissions) {
                assert (authorizationManager.hasGroupPermission(testSubject, permission, testGroup1.getId())) : "Failed to get single single 1-role, 1-group permission "
                    + permission.toString();
            }
        } finally {
            getTransactionManager().rollback();
        }

        /*
         * accuracy test: no overlap of permissions, groups should return different sets
         *
         *          p (some permissions)            | subject -- role -- group       \        \- role -- group
         *  |            p (other permissions)
         */
        getTransactionManager().begin();
        em = getEntityManager();
        try {
            /* bootstrap */
            Subject testSubject = SessionTestHelper.createNewSubject(em, "testSubject");
            Role testRole1 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole1");
            ResourceGroup testGroup1 = SessionTestHelper.createNewMixedGroupForRole(em, testRole1, "testGroup1", false);

            Role testRole2 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole2");
            ResourceGroup testGroup2 = SessionTestHelper.createNewMixedGroupForRole(em, testRole2, "testGroup2", false);

            /* setup permissions */
            Set<Permission> someMixedPermissions = EnumSet.of(Permission.MANAGE_INVENTORY, Permission.MANAGE_CONTENT,
                Permission.DELETE_RESOURCE);
            testRole1.getPermissions().addAll(someMixedPermissions);
            em.merge(testRole1);
            em.flush();

            Set<Permission> otherMixedPermissions = EnumSet.of(Permission.CONFIGURE_WRITE, Permission.MANAGE_INVENTORY,
                Permission.MANAGE_SECURITY);
            testRole2.getPermissions().addAll(otherMixedPermissions);
            em.merge(testRole2);
            em.flush();

            /* verify getGroupPermissions test */
            Set<Permission> someMixedResults = authorizationManager.getExplicitGroupPermissions(testSubject, testGroup1
                .getId());
            assert SessionTestHelper.samePermissions(someMixedPermissions, someMixedResults) : "Failed to get all 2-role, 2-group permissions";

            /* verify hasGroupPermission test */
            for (Permission permission : someMixedPermissions) {
                assert (authorizationManager.hasGroupPermission(testSubject, permission, testGroup1.getId())) : "Failed to get single single 1-role, 1-group permission "
                    + permission.toString();
            }

            /* verify getGroupPermissions test */
            Set<Permission> otherMixedResults = authorizationManager.getExplicitGroupPermissions(testSubject,
                testGroup2.getId());
            assert SessionTestHelper.samePermissions(otherMixedPermissions, otherMixedResults) : "Failed to get all 2-role, 2-group permissions";

            /* verify hasGroupPermission test */
            for (Permission permission : otherMixedPermissions) {
                assert (authorizationManager.hasGroupPermission(testSubject, permission, testGroup2.getId())) : "Failed to get single single 1-role, 1-group permission "
                    + permission.toString();
            }
        } finally {
            getTransactionManager().rollback();
        }

        /*
         * accuracy test: disjoint sets of permissions, results should be the simple union
         *
         *          p            | subject -- role -- group       \          /        \- role -/            |
         * p
         */
        getTransactionManager().begin();
        em = getEntityManager();
        try {
            /* bootstrap */
            Subject testSubject = SessionTestHelper.createNewSubject(em, "testSubject");
            Role testRole1 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole1");
            Role testRole2 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole2");
            ResourceGroup testGroup1 = SessionTestHelper.createNewMixedGroupForRole(em, testRole1, "testGroup1", false);

            testRole2.getResourceGroups().add(testGroup1);
            testGroup1.addRole(testRole2);
            em.merge(testRole2);
            em.merge(testGroup1);
            em.flush();

            /* setup permissions */
            Set<Permission> someMixedPermissions = EnumSet.of(Permission.CONFIGURE_WRITE, Permission.CONTROL,
                Permission.MANAGE_SECURITY);
            testRole1.getPermissions().addAll(someMixedPermissions);
            em.merge(testRole1);
            em.flush();

            Set<Permission> otherMixedPermissions = EnumSet.of(Permission.MANAGE_INVENTORY, Permission.MANAGE_CONTENT,
                Permission.DELETE_RESOURCE);
            testRole2.getPermissions().addAll(otherMixedPermissions);
            em.merge(testRole2);
            em.flush();

            /* setup shared object for result testing */
            Set<Permission> totalMixedPermissions = EnumSet.noneOf(Permission.class);
            totalMixedPermissions.addAll(someMixedPermissions);
            totalMixedPermissions.addAll(otherMixedPermissions);

            /* verify getGroupPermissions test */
            Set<Permission> totalMixedResults = authorizationManager.getExplicitGroupPermissions(testSubject,
                testGroup1.getId());
            assert SessionTestHelper.samePermissions(totalMixedPermissions, totalMixedResults) : "Failed to get all 1-role, 2-group permissions";

            /* verify hasGroupPermission test */
            for (Permission permission : totalMixedPermissions) {
                assert (authorizationManager.hasGroupPermission(testSubject, permission, testGroup1.getId())) : "Failed to get single single 1-role, 2-group permission "
                    + permission.toString();
            }
        } finally {
            getTransactionManager().rollback();
        }

        /*
         * accuracy test: intersecting sets of permissions, results should be the distinct union
         *
         *          p            | subject -- role -- group       \          /        \- role -/            |
         * p
         */
        getTransactionManager().begin();
        em = getEntityManager();
        try {
            /* bootstrap */
            Subject testSubject = SessionTestHelper.createNewSubject(em, "testSubject");
            Role testRole1 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole1");
            Role testRole2 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole2");
            ResourceGroup testGroup1 = SessionTestHelper.createNewMixedGroupForRole(em, testRole1, "testGroup1", false);

            testRole2.getResourceGroups().add(testGroup1);
            testGroup1.addRole(testRole2);
            em.merge(testRole2);
            em.merge(testGroup1);
            em.flush();

            /* setup permissions */
            Set<Permission> someMixedPermissions = EnumSet.of(Permission.CONFIGURE_WRITE, Permission.CONTROL,
                Permission.MANAGE_SECURITY);
            testRole1.getPermissions().addAll(someMixedPermissions);
            em.merge(testRole1);
            em.flush();

            Set<Permission> otherMixedPermissions = EnumSet.of(Permission.MANAGE_INVENTORY, Permission.MANAGE_CONTENT,
                Permission.DELETE_RESOURCE);
            testRole2.getPermissions().addAll(otherMixedPermissions);
            em.merge(testRole2);
            em.flush();

            /* setup redundant permissions */
            testRole2.getPermissions().addAll(someMixedPermissions);
            em.merge(testRole2);
            em.flush();

            /* setup shared object for result testing */
            Set<Permission> totalMixedPermissions = EnumSet.noneOf(Permission.class);
View Full Code Here

        EntityManager entityMgr = getEntityManager();

        try {
            Subject subject = SessionTestHelper.createNewSubject(entityMgr, "testSubject");

            Role roleWithSubject = SessionTestHelper.createNewRoleForSubject(entityMgr, subject, "role with subject");
            roleWithSubject.addPermission(Permission.VIEW_RESOURCE);

            ResourceGroup group = SessionTestHelper.createNewCompatibleGroupForRole(entityMgr, roleWithSubject,
                "accessible group");

            Resource r1 = SessionTestHelper.createNewResourceForGroup(entityMgr, group, "r1");
View Full Code Here

        EntityManager entityMgr = getEntityManager();

        try {
            Subject subject = SessionTestHelper.createNewSubject(entityMgr, "testSubject");

            Role role1 = SessionTestHelper.createNewRoleForSubject(entityMgr, subject, "role1");
            role1.addPermission(Permission.VIEW_RESOURCE);

            Role role2 = SessionTestHelper.createNewRoleForSubject(entityMgr, subject, "role2");
            role2.addPermission(Permission.VIEW_RESOURCE);

            ResourceGroup group1 = SessionTestHelper.createNewCompatibleGroupForRole(entityMgr, role1, "group 1");
            ResourceGroup group2 = SessionTestHelper.createNewCompatibleGroupForRole(entityMgr, role2, "group 2");

            Resource r1 = SessionTestHelper.createNewResourceForGroup(entityMgr, group1, "r1");
View Full Code Here

        try {
            Subject subject = SessionTestHelper.createNewSubject(entityMgr, "testSubject");
            Subject subjectNotInRole = SessionTestHelper.createNewSubject(entityMgr, "subjectNotInRole");

            Role roleWithSubject = SessionTestHelper.createNewRoleForSubject(entityMgr, subject, "role with subject");
            roleWithSubject.addPermission(Permission.VIEW_RESOURCE);

            ResourceGroup group = SessionTestHelper.createNewCompatibleGroupForRole(entityMgr, roleWithSubject,
                "accessible group");

            Resource r1 = SessionTestHelper.createNewResourceForGroup(entityMgr, group, "r1");
View Full Code Here

         *             p            | subject -- role -- group -- resource
         */
        try {
            /* bootstrap */
            Subject testSubject = SessionTestHelper.createNewSubject(em, "testSubject");
            Role testRole1 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole1");
            ResourceGroup testGroup1 = SessionTestHelper.createNewMixedGroupForRole(em, testRole1, "testGroup1", false);
            Resource resource1 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource1");
            Resource resource2 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource2");
            Resource resource3 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource3");

            /* setup permissions */
            Set<Permission> allPermissions = EnumSet.allOf(Permission.class);
            testRole1.getPermissions().addAll(allPermissions);
            em.merge(testRole1);
            em.flush();

            /* verify getResourcePermissions test */
            Set<Permission> allPermissionsResults = authorizationManager.getExplicitResourcePermissions(testSubject,
                resource1.getId());
            assert SessionTestHelper.samePermissions(allPermissions, allPermissionsResults) : "Failed to get all 1-role, 1-group, 1-resource permissions";

            /* verify hasResourcePermission test */
            for (Permission permission : allPermissions) {
                assert (authorizationManager.hasResourcePermission(testSubject, permission, resource1.getId())) : "Failed to get single 1-role, 1-group, 1-resource permission";
            }

            /* verify hasResourcePermission(collection) test */
            Collection<Integer> allResources = SessionTestHelper.getResourceList(resource1, resource2, resource3);

            for (Permission permission : allPermissions) {
                assert (authorizationManager.hasResourcePermission(testSubject, permission, allResources)) : "Failed to get every positive 1-role, 1-group, resource-list permission";
            }
        } finally {
            getTransactionManager().rollback();
        }

        /*
         *             p            | subject -- role -- group -- resource       \          /        \- role -/
         *   |            p          without redundant permissions
         */
        getTransactionManager().begin();
        em = getEntityManager();
        try {
            /* bootstrap */
            Subject testSubject = SessionTestHelper.createNewSubject(em, "testSubject");
            Role testRole1 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole1");
            ResourceGroup testGroup1 = SessionTestHelper.createNewMixedGroupForRole(em, testRole1, "testGroup1", false);
            Resource resource1 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource1");
            Resource resource2 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource2");
            Resource resource3 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource3");

            Role testRole2 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole2");
            testRole2.getResourceGroups().add(testGroup1);
            testGroup1.addRole(testRole2);
            em.merge(testRole2);
            em.merge(testGroup1);
            em.flush();

            /* setup permissions */
            Set<Permission> somePermissions = EnumSet.of(Permission.CONFIGURE_WRITE);
            Set<Permission> otherPermissions = EnumSet.of(Permission.CONTROL);
            testRole1.getPermissions().addAll(somePermissions);
            testRole2.getPermissions().addAll(otherPermissions);
            em.merge(testRole1);
            em.merge(testRole2);
            em.flush();

            /* setup shared object for result testing */
            Set<Permission> allPermissions = EnumSet.noneOf(Permission.class);
            allPermissions.addAll(somePermissions);
            allPermissions.addAll(otherPermissions);

            /* verify getResourcePermissions test */
            Set<Permission> allPermissionsResults = authorizationManager.getExplicitResourcePermissions(testSubject,
                resource1.getId());
            assert SessionTestHelper.samePermissions(allPermissions, allPermissionsResults) : "Failed to get all 2-role, 1-group, 1-resource permissions";

            /* verify hasResourcePermission test */
            for (Permission permission : allPermissions) {
                assert (authorizationManager.hasResourcePermission(testSubject, permission, resource1.getId())) : "Failed to get single 2-role, 1-group, 1-resource permission";
            }

            /* verify hasResourcePermission(collection) test */
            Collection<Integer> allResources = SessionTestHelper.getResourceList(resource1, resource2, resource3);
            for (Permission permission : allPermissions) {
                assert (authorizationManager.hasResourcePermission(testSubject, permission, allResources)) : "Failed to get every positive 2-role, 1-group, resource-list permission";
            }
        } finally {
            getTransactionManager().rollback();
        }

        /*
         *             p            | subject -- role -- group -- resource       \                   /        \- role --
         * group -/            |            p          without redundant permissions
         */
        getTransactionManager().begin();
        em = getEntityManager();
        try {
            /* bootstrap */
            Subject testSubject = SessionTestHelper.createNewSubject(em, "testSubject");
            Role testRole1 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole1");
            ResourceGroup testGroup1 = SessionTestHelper.createNewMixedGroupForRole(em, testRole1, "testGroup1", false);
            Resource resource1 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource1");
            Resource resource2 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource2");
            Resource resource3 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource3");

            Role testRole2 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole2");
            ResourceGroup testGroup2 = SessionTestHelper.createNewMixedGroupForRole(em, testRole2, "testGroup2", false);

            testGroup2.addExplicitResource(resource1);
            resource1.getExplicitGroups().add(testGroup2);

            /*
             * Single resource implies the implicit resource list should mirror the explicit one
             */
            testGroup2.addImplicitResource(resource1);
            resource1.getImplicitGroups().add(testGroup2);

            em.merge(testGroup2);
            em.merge(resource1);
            em.flush();

            /* setup permissions */
            Set<Permission> sharedPermissions = EnumSet.of(Permission.MANAGE_CONTENT);
            Set<Permission> otherPermissions = EnumSet.of(Permission.MANAGE_SECURITY);
            testRole1.getPermissions().addAll(sharedPermissions);
            testRole2.getPermissions().addAll(otherPermissions);
            em.merge(testRole1);
            em.merge(testRole2);
            em.flush();

            /* setup shared object for result testing */
            Set<Permission> allPermissions = EnumSet.noneOf(Permission.class);
            allPermissions.addAll(sharedPermissions);
            allPermissions.addAll(otherPermissions);

            /* verify getResourcePermissions test */
            Set<Permission> allPermissionsResults = authorizationManager.getExplicitResourcePermissions(testSubject,
                resource1.getId());
            assert SessionTestHelper.samePermissions(allPermissions, allPermissionsResults) : "Failed to get all 2-role, 2-group, 1-resource permissions";

            /* verify hasResourcePermission test */
            for (Permission permission : allPermissions) {
                assert (authorizationManager.hasResourcePermission(testSubject, permission, resource1.getId())) : "Failed to get single 2-role, 2-group, 1-resource permission";
            }

            /* verify hasResourcePermission(collection) test */
            Collection<Integer> allResources = SessionTestHelper.getResourceList(resource1, resource2, resource3);

            for (Permission permission : sharedPermissions) {
                assert (authorizationManager.hasResourcePermission(testSubject, permission, allResources)) : "Failed to get every positive 2-role, 2-group, resource-list permission";
            }

            for (Permission permission : otherPermissions) {
                assert (!authorizationManager.hasResourcePermission(testSubject, permission, allResources)) : "Failed to get every negative 2-role, 2-group, resource-list permission";
            }
        } finally {
            getTransactionManager().rollback();
        }

        /*
         *             p            | subject -- role -- group -- resource               \           /                \-
         * group -/
         */
        getTransactionManager().begin();
        em = getEntityManager();
        try {
            /* bootstrap */
            Subject testSubject = SessionTestHelper.createNewSubject(em, "testSubject");
            Role testRole1 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole1");
            ResourceGroup testGroup1 = SessionTestHelper.createNewMixedGroupForRole(em, testRole1, "testGroup1", false);
            Resource resource1 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource1");
            Resource resource2 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource2");
            Resource resource3 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource3");

            ResourceGroup testGroup2 = SessionTestHelper.createNewMixedGroupForRole(em, testRole1, "testGroup2", false);

            testGroup2.addExplicitResource(resource1);
            resource1.getExplicitGroups().add(testGroup2);

            /*
             * Single resource implies the implicit resource list should mirror the explicit one
             */
            testGroup2.addImplicitResource(resource1);
            resource1.getImplicitGroups().add(testGroup2);

            em.merge(testGroup2);
            em.merge(resource1);
            em.flush();

            /* setup permissions */
            Set<Permission> sharedPermissions = EnumSet.of(Permission.MANAGE_SETTINGS, Permission.MANAGE_ALERTS);
            testRole1.getPermissions().addAll(sharedPermissions);
            em.merge(testRole1);
            em.flush();

            /* verify getResourcePermissions test */
            Set<Permission> somePermissionsResults = authorizationManager.getExplicitResourcePermissions(testSubject,
                resource1.getId());
            assert SessionTestHelper.samePermissions(sharedPermissions, somePermissionsResults) : "Failed to get all 1-role, 2-group, 1-resource permissions";

            /* verify hasResourcePermission test */
            for (Permission permission : sharedPermissions) {
                assert (authorizationManager.hasResourcePermission(testSubject, permission, resource1.getId())) : "Failed to get single 1-role, 2-group, 1-resource permission";
            }

            /* verify hasResourcePermission(collection) test */
            Collection<Integer> allResources = SessionTestHelper.getResourceList(resource1, resource2, resource3);
            for (Permission permission : sharedPermissions) {
                assert (authorizationManager.hasResourcePermission(testSubject, permission, allResources)) : "Failed to get every positive 1-role, 2-group, resource-list permission";
            }
        } finally {
            getTransactionManager().rollback();
        }

        /*
         *             p            | subject -- role -- group -- resource       \        \/         /        \       /\
         *        /         \ role -- group /            |            p          with highly redundant permissions
         */
        getTransactionManager().begin();
        em = getEntityManager();
        try {
            /* bootstrap */
            Subject testSubject = SessionTestHelper.createNewSubject(em, "testSubject");
            Role testRole1 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole1");
            ResourceGroup testGroup1 = SessionTestHelper.createNewMixedGroupForRole(em, testRole1, "testGroup1", false);
            Resource resource1 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource1");
            Resource resource2 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource2");
            Resource resource3 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource3");

            Role testRole2 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole2");
            ResourceGroup testGroup2 = SessionTestHelper.createNewMixedGroupForRole(em, testRole2, "testGroup2", false);

            testGroup2.addRole(testRole1);
            testRole1.getResourceGroups().add(testGroup2);

            testGroup1.addRole(testRole2);
            testRole2.getResourceGroups().add(testGroup1);

            testGroup2.addExplicitResource(resource1);
            resource1.getExplicitGroups().add(testGroup2);

            /*
             * Single resource implies the implicit resource list should mirror the explicit one
             */
            testGroup2.addImplicitResource(resource1);
            resource1.getImplicitGroups().add(testGroup2);

            em.merge(testGroup1);
            em.merge(testGroup2);
            em.merge(testRole1);
            em.merge(testRole2);
            em.merge(resource1);
            em.flush();

            /* setup permissions */
            Set<Permission> somePermissions = EnumSet.of(Permission.CONFIGURE_WRITE, Permission.CONTROL,
                Permission.MANAGE_INVENTORY);
            Set<Permission> otherPermissions = EnumSet.of(Permission.CONTROL, Permission.MANAGE_SECURITY,
                Permission.MANAGE_INVENTORY);
            testRole1.getPermissions().addAll(somePermissions);
            testRole2.getPermissions().addAll(otherPermissions);
            em.merge(testRole1);
            em.merge(testRole2);
            em.flush();

            /* setup shared object for result testing */
            Set<Permission> allPermissions = EnumSet.noneOf(Permission.class);
            allPermissions.addAll(somePermissions);
            allPermissions.addAll(otherPermissions);

            /* verify getResourcePermissions test */
            Set<Permission> allPermissionsResults = authorizationManager.getExplicitResourcePermissions(testSubject,
                resource1.getId());
            assert SessionTestHelper.samePermissions(allPermissions, allPermissionsResults) : "Failed to get all 2-role crossed 2-group, 1-resource distinct permissions";

            /* verify hasResourcePermission test */
            for (Permission permission : allPermissions) {
                assert (authorizationManager.hasResourcePermission(testSubject, permission, resource1.getId())) : "Failed to get single 2-role crossed 2-group, 1-resource distinct permission";
            }

            /* verify hasResourcePermission(collection) test */
            Collection<Integer> allResources = SessionTestHelper.getResourceList(resource1, resource2, resource3);
            for (Permission permission : allPermissions) {
                assert (authorizationManager.hasResourcePermission(testSubject, permission, allResources)) : "Failed to get every positive 2-role crossed 2-group, resource-list permission";
            }
        } finally {
            getTransactionManager().rollback();
        }

        /*
         *             p            | subject -- role -- group -- resource       \        \- role -- group -- resource
         *          |            p
         */
        getTransactionManager().begin();
        em = getEntityManager();
        try {
            /* bootstrap */
            Subject testSubject = SessionTestHelper.createNewSubject(em, "testSubject");
            Role testRole1 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole1");
            Role testRole2 = SessionTestHelper.createNewRoleForSubject(em, testSubject, "testRole2");
            ResourceGroup testGroup1 = SessionTestHelper.createNewMixedGroupForRole(em, testRole1, "testGroup1", false);
            ResourceGroup testGroup2 = SessionTestHelper.createNewMixedGroupForRole(em, testRole2, "testGroup2", false);
            Resource resource1 = SessionTestHelper.createNewResourceForGroup(em, testGroup1, "testResource1");
            Resource resource2 = SessionTestHelper.createNewResourceForGroup(em, testGroup2, "testResource2");

            /* setup permissions */
            Set<Permission> somePermissions = EnumSet.of(Permission.MANAGE_ALERTS, Permission.MANAGE_SETTINGS);
            Set<Permission> otherPermissions = EnumSet.of(Permission.MANAGE_ALERTS, Permission.MANAGE_CONTENT);
            testRole1.getPermissions().addAll(somePermissions);
            testRole2.getPermissions().addAll(otherPermissions);
            em.merge(testRole1);
            em.merge(testRole2);
            em.flush();

            /* setup shared object for result testing */
 
View Full Code Here

TOP

Related Classes of org.rhq.core.domain.authz.Role

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.