Search in sources :

Example 16 with Role

use of org.opencastproject.security.api.Role in project opencast by opencast.

the class OpencastLdapAuthoritiesPopulatorTest method doTest.

/**
 * Perform the test of an instance of the class OpencastLdapAuthoritiesPopulator.
 *
 * @param populator
 *          an instance of {@link OpencastLdapAuthoritiesPopulator} to test
 * @param mappings
 *          a {@link Map} containing the LDAP attribute name - value pairs, where the name is a {@link String} and the
 *          value an array of {@code String}s, possibly {@code null} or empty.
 */
private void doTest(OpencastLdapAuthoritiesPopulator populator, Map<String, String[]> mappings, String rolePrefix, String[] excludePrefixes, boolean toUppercase, String[] additionalAuthorities, JpaGroupRoleProvider groupRoleProvider) {
    DirContextOperations dirContextMock = EasyMock.createNiceMock(DirContextOperations.class);
    // Populate the DirContextOperations class
    for (String attrName : mappings.keySet()) {
        EasyMock.expect(dirContextMock.getStringAttributes(attrName)).andReturn(mappings.get(attrName)).anyTimes();
    }
    EasyMock.replay(dirContextMock);
    // Prepare the expected result
    HashSet<GrantedAuthority> expectedResult = new HashSet<>();
    for (String attrName : mappings.keySet()) {
        if (mappings.get(attrName) != null) {
            for (String attrValues : mappings.get(attrName)) {
                addRoles(expectedResult, rolePrefix, excludePrefixes, toUppercase, groupRoleProvider, org, attrValues.split(","));
            }
        }
    }
    // Add the internal roles
    for (Role role : DEFAULT_INTERNAL_ROLES) {
        expectedResult.add(new SimpleGrantedAuthority(role.getName()));
    }
    // Add the additional authorities
    addRoles(expectedResult, rolePrefix, excludePrefixes, toUppercase, groupRoleProvider, org, additionalAuthorities);
    // Check the response is correct
    checkResponse(populator.getGrantedAuthorities(dirContextMock, USERNAME), expectedResult);
}
Also used : Role(org.opencastproject.security.api.Role) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) DirContextOperations(org.springframework.ldap.core.DirContextOperations) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) HashSet(java.util.HashSet)

Example 17 with Role

use of org.opencastproject.security.api.Role in project opencast by opencast.

the class JpaGroupRoleProviderTest method testRoles.

@Test
@SuppressWarnings("unchecked")
public void testRoles() throws Exception {
    Set<JpaRole> authorities = new HashSet<JpaRole>();
    authorities.add(new JpaRole("ROLE_ASTRO_101_SPRING_2011_STUDENT", org1));
    authorities.add(new JpaRole("ROLE_ASTRO_109_SPRING_2012_STUDENT", org1));
    Set<String> members = new HashSet<String>();
    members.add("admin");
    JpaGroup group = new JpaGroup("test", org1, "Test", "Test group", authorities, members);
    provider.addGroup(group);
    authorities.clear();
    authorities.add(new JpaRole("ROLE_ASTRO_122_SPRING_2011_STUDENT", org1));
    authorities.add(new JpaRole("ROLE_ASTRO_124_SPRING_2012_STUDENT", org1));
    JpaGroup group2 = new JpaGroup("test2", org1, "Test2", "Test 2 group", authorities, members);
    provider.addGroup(group2);
    authorities.clear();
    authorities.add(new JpaRole("ROLE_ASTRO_134_SPRING_2011_STUDENT", org2));
    authorities.add(new JpaRole("ROLE_ASTRO_144_SPRING_2012_STUDENT", org2));
    JpaGroup group3 = new JpaGroup("test2", org2, "Test2", "Test 2 group", authorities, members);
    provider.addGroup(group3);
    List<Role> roles = IteratorUtils.toList(provider.getRoles());
    Assert.assertEquals("There should be four role", 6, roles.size());
    roles.contains(new JpaRole(group.getRole(), org1));
    roles.contains(new JpaRole(group2.getRole(), org1));
}
Also used : JpaGroup(org.opencastproject.security.impl.jpa.JpaGroup) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) Role(org.opencastproject.security.api.Role) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with Role

use of org.opencastproject.security.api.Role in project opencast by opencast.

the class UserAndRoleDirectoryServiceImplTest method setUp.

@Before
public void setUp() throws Exception {
    org = new DefaultOrganization();
    userName = "sampleUser";
    role1 = new JaxbRole("role1", org);
    role2 = new JaxbRole("role2", org);
    role3 = new JaxbRole("role3", org);
    JaxbUser user1 = new JaxbUser(userName, "matterhorn", org, role1, role2);
    user1.setManageable(true);
    User user2 = new JaxbUser(userName, "secret", "test", true, org, role2, role3);
    User user3 = new JaxbUser("userSample", "test", org, role2, role3);
    List<User> users = new ArrayList<User>();
    users.add(user1);
    UserProvider provider1 = EasyMock.createNiceMock(UserProvider.class);
    EasyMock.expect(provider1.getOrganization()).andReturn(org.getId()).anyTimes();
    EasyMock.expect(provider1.loadUser((String) EasyMock.anyObject())).andReturn(user1).anyTimes();
    EasyMock.expect(provider1.findUsers("%mple%", 0, 0)).andReturn(users.iterator()).once();
    EasyMock.expect(provider1.findUsers("%mple%", 0, 0)).andReturn(users.iterator()).once();
    EasyMock.expect(provider1.getUsers()).andReturn(users.iterator()).once();
    EasyMock.expect(provider1.getName()).andReturn("test").once();
    List<User> users2 = new ArrayList<User>();
    users2.add(user3);
    UserProvider provider2 = EasyMock.createNiceMock(UserProvider.class);
    EasyMock.expect(provider2.getOrganization()).andReturn(org.getId()).anyTimes();
    EasyMock.expect(provider2.loadUser((String) EasyMock.anyObject())).andReturn(user2).anyTimes();
    EasyMock.expect(provider2.findUsers("%mple%", 0, 0)).andReturn(users2.iterator()).once();
    EasyMock.expect(provider2.findUsers("%mple%", 0, 0)).andReturn(users2.iterator()).once();
    EasyMock.expect(provider2.getUsers()).andReturn(users2.iterator()).once();
    EasyMock.expect(provider2.getName()).andReturn("matterhorn").once();
    List<Role> roles1 = new ArrayList<Role>();
    roles1.add(new JaxbRole("ROLE_ASTRO_2011", org));
    roles1.add(new JaxbRole("ROLE_ASTRO_2012", org));
    List<Role> rolesForUser1 = new ArrayList<Role>();
    rolesForUser1.add(new JaxbRole("ROLE_ASTRO_2012", org));
    List<Role> findRoles1 = new ArrayList<Role>();
    findRoles1.add(new JaxbRole("ROLE_ASTRO_2012", org));
    RoleProvider roleProvider1 = EasyMock.createNiceMock(RoleProvider.class);
    EasyMock.expect(roleProvider1.getOrganization()).andReturn(org.getId()).anyTimes();
    EasyMock.expect(roleProvider1.getRoles()).andReturn(roles1.iterator()).anyTimes();
    EasyMock.expect(roleProvider1.getRolesForUser((String) EasyMock.anyObject())).andReturn(rolesForUser1).anyTimes();
    EasyMock.expect(roleProvider1.findRoles("%2012%", Role.Target.ALL, 0, 0)).andReturn(findRoles1.iterator()).once();
    EasyMock.expect(roleProvider1.findRoles("%2012%", Role.Target.ALL, 0, 0)).andReturn(findRoles1.iterator()).once();
    List<Role> roles2 = new ArrayList<Role>();
    roles2.add(new JaxbRole("ROLE_MATH_2011", org));
    roles2.add(new JaxbRole("ROLE_MATH_2012", org));
    List<Role> rolesForUser2 = new ArrayList<Role>();
    rolesForUser2.add(new JaxbRole("ROLE_MATH_2012", org));
    List<Role> findRoles2 = new ArrayList<Role>();
    findRoles2.add(new JaxbRole("ROLE_MATH_2012", org));
    RoleProvider roleProvider2 = EasyMock.createNiceMock(RoleProvider.class);
    EasyMock.expect(roleProvider2.getOrganization()).andReturn(org.getId()).anyTimes();
    EasyMock.expect(roleProvider2.getRoles()).andReturn(roles2.iterator()).anyTimes();
    EasyMock.expect(roleProvider2.getRolesForUser((String) EasyMock.anyObject())).andReturn(rolesForUser2).anyTimes();
    EasyMock.expect(roleProvider2.findRoles("%2012%", Role.Target.ALL, 0, 0)).andReturn(findRoles2.iterator()).once();
    EasyMock.expect(roleProvider2.findRoles("%2012%", Role.Target.ALL, 0, 0)).andReturn(findRoles2.iterator()).once();
    RoleProvider otherOrgRoleProvider = EasyMock.createNiceMock(RoleProvider.class);
    EasyMock.expect(otherOrgRoleProvider.getOrganization()).andReturn("otherOrg").anyTimes();
    EasyMock.expect(otherOrgRoleProvider.getRoles()).andReturn(roles2.iterator()).anyTimes();
    EasyMock.expect(otherOrgRoleProvider.getRolesForUser((String) EasyMock.anyObject())).andReturn(rolesForUser2).anyTimes();
    EasyMock.expect(otherOrgRoleProvider.findRoles("%2012%", Role.Target.ALL, 0, 0)).andReturn(new ArrayList<Role>().iterator()).anyTimes();
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getOrganization()).andReturn(org).anyTimes();
    EasyMock.replay(provider1, provider2, roleProvider1, roleProvider2, otherOrgRoleProvider, securityService);
    directory = new UserAndRoleDirectoryServiceImpl();
    directory.activate(null);
    directory.setSecurityService(securityService);
    directory.addUserProvider(provider1);
    directory.addUserProvider(provider2);
    directory.addRoleProvider(roleProvider1);
    directory.addRoleProvider(roleProvider2);
    directory.addRoleProvider(otherOrgRoleProvider);
}
Also used : User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) ArrayList(java.util.ArrayList) JaxbUser(org.opencastproject.security.api.JaxbUser) RoleProvider(org.opencastproject.security.api.RoleProvider) Role(org.opencastproject.security.api.Role) JaxbRole(org.opencastproject.security.api.JaxbRole) JaxbRole(org.opencastproject.security.api.JaxbRole) UserProvider(org.opencastproject.security.api.UserProvider) SecurityService(org.opencastproject.security.api.SecurityService) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Before(org.junit.Before)

Example 19 with Role

use of org.opencastproject.security.api.Role in project opencast by opencast.

the class JpaUserProviderTest method testRolesForUser.

@Test
public void testRolesForUser() {
    JpaRole astroRole = new JpaRole("ROLE_ASTRO_105_SPRING_2013_STUDENT", org1, "Astro role");
    provider.addRole(astroRole);
    JpaUser userOne = createUserWithRoles(org1, "user1", "ROLE_ONE", "ROLE_TWO");
    try {
        provider.addUser(userOne);
    } catch (UnauthorizedException e) {
        fail("User should be created");
    }
    assertEquals("There should be three roles", 3, IteratorUtils.toList(provider.getRoles()).size());
    List<Role> rolesForUser = provider.getRolesForUser("user1");
    assertEquals("There should be two roles", 2, rolesForUser.size());
    assertEquals("ROLE_ONE", rolesForUser.get(0).getName());
    assertEquals("ROLE_TWO", rolesForUser.get(1).getName());
}
Also used : JpaRole(org.opencastproject.security.impl.jpa.JpaRole) Role(org.opencastproject.security.api.Role) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) Test(org.junit.Test)

Example 20 with Role

use of org.opencastproject.security.api.Role in project opencast by opencast.

the class JpaUserProviderTest method testAddAndGetRole.

@Test
public void testAddAndGetRole() throws Exception {
    JpaRole astroRole = new JpaRole("ROLE_ASTRO_105_SPRING_2013_STUDENT", org1, "Astro role");
    provider.addRole(astroRole);
    Iterator<Role> roles = provider.getRoles();
    assertTrue("There should be one role", roles.hasNext());
    Role role = roles.next();
    assertEquals(astroRole.getName(), role.getName());
    assertEquals(astroRole.getOrganization(), role.getOrganization());
    assertEquals(astroRole.getDescription(), role.getDescription());
    assertFalse("There should onyl be one role", roles.hasNext());
}
Also used : JpaRole(org.opencastproject.security.impl.jpa.JpaRole) Role(org.opencastproject.security.api.Role) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) Test(org.junit.Test)

Aggregations

Role (org.opencastproject.security.api.Role)48 JaxbRole (org.opencastproject.security.api.JaxbRole)21 User (org.opencastproject.security.api.User)21 HashSet (java.util.HashSet)17 JpaRole (org.opencastproject.security.impl.jpa.JpaRole)16 ArrayList (java.util.ArrayList)14 Organization (org.opencastproject.security.api.Organization)13 JaxbOrganization (org.opencastproject.security.api.JaxbOrganization)12 JaxbUser (org.opencastproject.security.api.JaxbUser)7 Test (org.junit.Test)6 JpaGroup (org.opencastproject.security.impl.jpa.JpaGroup)6 LinkedList (java.util.LinkedList)5 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)5 Path (javax.ws.rs.Path)4 RoleProvider (org.opencastproject.security.api.RoleProvider)4 JpaUser (org.opencastproject.security.impl.jpa.JpaUser)4 RestQuery (org.opencastproject.util.doc.rest.RestQuery)4 JSONArray (org.json.simple.JSONArray)3 JSONObject (org.json.simple.JSONObject)3 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)3