Package org.apache.jetspeed.security.impl

Examples of org.apache.jetspeed.security.impl.RolePrincipalImpl


            combo.append(role.getName());
            count++;                       
        }
        Set principals = new HashSet();
        principals.add(SecurityHelper.getBestPrincipal(currentSubject, UserPrincipal.class));
        principals.add(new RolePrincipalImpl(combo.toString()));
        Subject subject =
            new Subject(true, principals, new HashSet(), new HashSet());
        return subject;
    }
View Full Code Here


        try
        {
            rms.addRoleToUser("anonuser1", "testusertorole1.role1");

            Collection principals = ums.getUser("anonuser1").getSubject().getPrincipals();
            assertTrue("anonuser1 should contain testusertorole1.role1", principals.contains(new RolePrincipalImpl(
                    "testusertorole1.role1")));
        }
        catch (SecurityException sex)
        {
            assertTrue("should add user to role. exception caught: " + sex, false);
        }
        // Add role with existing roles.
        try
        {
            rms.addRoleToUser("anonuser1", "testusertorole1.role2");
            Collection principals = ums.getUser("anonuser1").getSubject().getPrincipals();
            assertTrue("anonuser1 should contain testusertorole1.role2", principals.contains(new RolePrincipalImpl(
                    "testusertorole1.role2")));
        }
        catch (SecurityException sex)
        {
            assertTrue("should add user to role. exception caught: " + sex, false);
View Full Code Here

            rms.removeRole("testrole1.role1");
            Collection principals = ums.getUser("anonuser2").getSubject().getPrincipals();
            // because of hierarchical roles with generalization strategy.
            assertEquals("principal size should be == 5 after removing testrole1.role1, for principals: "
                    + principals.toString(), 5, principals.size());
            assertFalse("anonuser2 should not contain testrole1.role1", principals.contains(new RolePrincipalImpl(
                    "testrole1.role1")));
            // Make sure that the children are removed as well.
            rms.removeRole("testrole2");
            boolean roleExists = rms.roleExists("testrole2.role1");
            assertFalse(roleExists);
View Full Code Here

        {
            StringTokenizer toke = new StringTokenizer(roleNames, ",");
            while (toke.hasMoreTokens())
            {
                String roleName = toke.nextToken();
                Principal role = new RolePrincipalImpl(roleName);
                principals.add(role);
            }               
        }
        return pm.updatePermission(permission, principals);                   
    }
View Full Code Here

        ////////////
        UserPrincipal adminUser = new UserPrincipalImpl("adminTEST");
        UserPrincipal userUser = new UserPrincipalImpl("userTEST");
        PortletPermission adminPerm = new PortletPermission("adminTEST::*", "view, edit");
        PortletPermission userPerm = new PortletPermission("demoTEST::*", "view, edit");
        RolePrincipal adminRole = new RolePrincipalImpl("adminTEST");
        RolePrincipal userRole = new RolePrincipalImpl("userTEST");
       
        try
        {
            ums.addUser(adminUser.getName(), "password");
            ums.addUser(userUser.getName(), "password");           
            rms.addRole(adminRole.getName());
            rms.addRole(userRole.getName());           
            rms.addRoleToUser(adminUser.getName(), adminRole.getName());
            rms.addRoleToUser(userUser.getName(), userRole.getName());
            rms.addRoleToUser(adminUser.getName(), userRole.getName());           
            pms.addPermission(adminPerm);
            pms.addPermission(userPerm);
            pms.grantPermission(adminRole, adminPerm);
            pms.grantPermission(userRole, userPerm);                       
        }
        catch (SecurityException sex)
        {
            assertTrue("failed to init testRemovePrincipalPermissions(), " + sex, false);
        }
       
        //////////////////////////////////////////////////////////////////////////
        // Run Test
        ////////////       
        Set adminPrincipals = new PrincipalsSet();
        Set adminPublicCredentials = new HashSet();
        Set adminPrivateCredentials = new HashSet();
        Set userPrincipals = new PrincipalsSet();
        Set userPublicCredentials = new HashSet();
        Set userPrivateCredentials = new HashSet();
       
        adminPrincipals.add(adminUser);
        adminPrincipals.add(adminRole);
        adminPrincipals.add(userRole);

        userPrincipals.add(userUser);
        userPrincipals.add(userRole);
       
        try
        {
            Subject adminSubject = new Subject(true, adminPrincipals, adminPublicCredentials, adminPrivateCredentials);
            Subject userSubject = new Subject(true, userPrincipals, userPublicCredentials, userPrivateCredentials);                   
           
            boolean access = pms.checkPermission(adminSubject, adminPerm);
            assertTrue("access to admin Perm should be granted to Admin ", access);
           
            access = pms.checkPermission(adminSubject, userPerm);
            assertTrue("access to user should NOT be granted to Admin ", access);

            access = pms.checkPermission(userSubject, userPerm);
            assertTrue("access to User Perm should be granted to User ", access);
           
            access = pms.checkPermission(userSubject, adminPerm);
            assertFalse("access to Admin Perm should NOT be granted to User ", access);
           
        }
        catch (AccessControlException e)
        {
            fail("failed permission check");
        }
        finally
        {
            //////////////////////////////////////////////////////////////////////////
            // cleanup
            ////////////
            try
            {
                ums.removeUser(adminUser.getName());
                ums.removeUser(userUser.getName());
                rms.removeRole(adminRole.getName());
                rms.removeRole(userRole.getName());
               
                pms.removePermission(adminPerm);
                pms.removePermission(userPerm);
            }
            catch (SecurityException sex)
View Full Code Here

        UserPrincipal user = new UserPrincipalImpl("test");
        PortletPermission perm1 = new PortletPermission("PortletOne", "view, edit");
        PortletPermission perm2 = new PortletPermission("PortletTwo", "view");
        PortletPermission perm3 = new PortletPermission("PortletThree", "view");
        PortletPermission perm3a = new PortletPermission("PortletThreeA", "view, edit");
        RolePrincipal role1 = new RolePrincipalImpl("Role1");
        RolePrincipal role2 = new RolePrincipalImpl("Role2");
       
        try
        {
            ums.addUser(user.getName(), "password");
            rms.addRole(role1.getName());
            rms.addRole(role2.getName());           
            rms.addRoleToUser(user.getName(), role1.getName());
            rms.addRoleToUser(user.getName(), role2.getName());
            pms.addPermission(perm1);
            pms.addPermission(perm2);
            pms.addPermission(perm3);
            pms.addPermission(perm3a);
            pms.grantPermission(user, perm1);
            pms.grantPermission(role1, perm2);                       
            pms.grantPermission(role2, perm3);           
        }
        catch (SecurityException sex)
        {
            assertTrue("failed to init testRemovePrincipalPermissions(), " + sex, false);
        }
       
        //////////////////////////////////////////////////////////////////////////
        // Run Test
        ////////////       
        Set principals = new PrincipalsSet();
        Set publicCredentials = new HashSet();
        Set privateCredentials = new HashSet();
        principals.add(user);
        principals.add(role1);
        principals.add(role2);

        try
        {
            Subject subject = new Subject(true, principals, publicCredentials, privateCredentials);       
            boolean access = pms.checkPermission(subject, perm1);
            assertTrue("access to perm1 should be granted ", access);
            access = pms.checkPermission(subject, perm2);
            assertTrue("access to perm2 should be granted ", access);
            access = pms.checkPermission(subject, perm3);
            assertTrue("access to perm3 should be granted ", access);
            access = pms.checkPermission(subject, perm3a);
            assertFalse("access to perm3a should be denied ", access);
        }
        catch (AccessControlException e)
        {
            fail("failed permission check");
        }
        finally
        {
            //////////////////////////////////////////////////////////////////////////
            // cleanup
            ////////////
            try
            {
                ums.removeUser(user.getName());
                rms.removeRole(role1.getName());
                rms.removeRole(role2.getName());           
                pms.removePermission(perm1);
                pms.removePermission(perm2);
                pms.removePermission(perm3);
                pms.removePermission(perm3a);               
            }
View Full Code Here

     */
    public void testRemovePermission()
    {
        // Init test.
        UserPrincipal user = new UserPrincipalImpl("removepermission");
        RolePrincipal role = new RolePrincipalImpl("removepermissionrole");
        PortletPermission perm1 = new PortletPermission("removepermission1", "view, edit, secure, minimized, maximized");
        PortletPermission perm2 = new PortletPermission("removepermission2", "view, edit, minimized, maximized");
        try
        {
            ums.addUser(user.getName(), "password");
            rms.addRole(role.getName());
            pms.addPermission(perm1);
            pms.addPermission(perm2);
            pms.grantPermission(user, perm1);
            pms.grantPermission(user, perm2);
            pms.grantPermission(role, perm1);
            pms.grantPermission(role, perm2);
        }
        catch (SecurityException sex)
        {
            assertTrue("failed to init testRemovePermission(), " + sex, false);
        }
        try
        {
            pms.removePermission(perm1);
            Permissions permCol1 = pms.getPermissions(new UserPrincipalImpl("removepermission"));
            assertTrue(
                "should only contain permission == {name = "
                    + perm2.getName()
                    + "}, {action = "
                    + perm2.getActions()
                    + "}, in collection of size == 1, actual size: "
                    + (Collections.list(permCol1.elements())).size(),
                validatePermissions(permCol1, perm2, 1));
            Permissions permCol2 = pms.getPermissions(new RolePrincipalImpl("removepermissionrole"));
            assertTrue(
                "should only contain permission == {name = "
                    + perm2.getName()
                    + "}, {action = "
                    + perm2.getActions()
View Full Code Here

     */
    public void testGetPermissions()
    {
        // Init test.
        UserPrincipal user = new UserPrincipalImpl("anon");
        RolePrincipal role1 = new RolePrincipalImpl("anonrole1");
        RolePrincipal role2 = new RolePrincipalImpl("anonrole2");
        GroupPrincipal group1 = new GroupPrincipalImpl("anongroup1");
        GroupPrincipal group2 = new GroupPrincipalImpl("anongroup2");
        PortletPermission perm1 = new PortletPermission("anontestportlet", "view");
        PortletPermission perm2 = new PortletPermission("anontestportlet", "view, edit");
        PortletPermission perm3 = new PortletPermission("anontestportlet", "view, edit, secure");
        PortletPermission perm4 = new PortletPermission("anontestportlet", "view, edit, secure, minimized");
        try
        {
            ums.addUser(user.getName(), "password");
            rms.addRole(role1.getName());
            rms.addRole(role2.getName());
            gms.addGroup(group1.getName());
            gms.addGroup(group2.getName());
            pms.addPermission(perm1);
            pms.addPermission(perm2);
            pms.addPermission(perm3);
View Full Code Here

    }
   
    public void testUpdatePermission()
    {
        // Init test.
        RolePrincipal role1 = new RolePrincipalImpl("role1");
        RolePrincipal role2 = new RolePrincipalImpl("role2");
        RolePrincipal role3 = new RolePrincipalImpl("role3");
        RolePrincipal role4 = new RolePrincipalImpl("role4");
        PortletPermission perm1 = new PortletPermission("testportlet", "view");
        try
        {
            rms.addRole(role1.getName());
            rms.addRole(role2.getName());
            rms.addRole(role3.getName());
            rms.addRole(role4.getName());           
            pms.addPermission(perm1);
        }
        catch (SecurityException sex)
        {
            assertTrue("failed to init testUpdatePermission(), " + sex, false);
        }

        // Grant 1 and 2     
        try
        {
            pms.grantPermission(role1, perm1);
            pms.grantPermission(role2, perm1);
        }
        catch (SecurityException sex)
        {
            assertTrue("failed to grant on testUpdatePermission. caught exception, " + sex, false);
        }

        Collection principals = pms.getPrincipals(perm1);       
        assertTrue("principal count should be 2 ", principals.size() == 2);       
        Object [] array = (Object[])principals.toArray();
        Arrays.sort(array, principalComparator);
        assertTrue("element is Principal ", array[0] instanceof Principal);
        assertTrue("first element not found ", ((Principal)array[0]).getName().equals("role1"));
        assertTrue("second element not found ", ((Principal)array[1]).getName().equals("role2"));
       
       
        // Try to update collection
        try
        {
            Collection roles = new Vector();
            roles.add(role1);
            roles.add(role3);
            roles.add(role4);
            pms.updatePermission(perm1, roles);
        }
        catch (SecurityException sex)
        {
            assertTrue("principal does not exist. caught exception, " + sex, false);
        }
        principals = pms.getPrincipals(perm1);
        assertTrue("principal count should be 3 ", principals.size() == 3);
        array = (Object[])principals.toArray();
        Arrays.sort(array, principalComparator);
        assertTrue("first element should be [role1] but found ["+((Principal)array[0]).getName()+"]", ((Principal)array[0]).getName().equals("role1"));
        assertTrue("second element not found ", ((Principal)array[1]).getName().equals("role3"));
        assertTrue("third element not found ", ((Principal)array[2]).getName().equals("role4"));
       
        // Cleanup test.
        try
        {
            rms.removeRole(role1.getName());
            rms.removeRole(role2.getName());
            rms.removeRole(role3.getName());
            rms.removeRole(role4.getName());
            pms.removePermission(perm1);
        }
        catch (SecurityException sex)
        {
            assertTrue("could not remove user and permission. exception caught: " + sex, false);
View Full Code Here

     * </p>
     */
    public void testRemoveRolePrincipal() throws Exception
    {
        initMappedRole();
        rsh.removeRolePrincipal(new RolePrincipalImpl("mappedrole"));
        // The user should still exist.
        assertTrue(ums.userExists("mappedroleuser"));
        // The group should still exist.
        assertTrue(gms.groupExists("mappedgroup"));
        // The permission should still exist.
        assertTrue(pms.permissionExists(new PortletPermission("myportlet", "view")));
        // The user-role mapping should be gone.
        assertFalse(rms.isUserInRole("mappedroleuser", "mappedrole"));
        // The group-role mapping should be gone.
        assertFalse(rms.isGroupInRole("mappedgroup", "mappedroleuser"));
        // The permission-role mapping should be gone.
        Permissions perms = pms.getPermissions(new RolePrincipalImpl("mappedrole"));
        assertFalse(perms.implies(new PortletPermission("myportlet", "view")));
       
        destroyMappedRole();
    }
View Full Code Here

TOP

Related Classes of org.apache.jetspeed.security.impl.RolePrincipalImpl

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.