Search in sources :

Example 11 with JpaRole

use of org.opencastproject.security.impl.jpa.JpaRole in project opencast by opencast.

the class JpaGroupRoleProviderTest method testDuplicateGroup.

@Test
public void testDuplicateGroup() throws UnauthorizedException {
    Set<JpaRole> roles1 = set(new JpaRole("ROLE_ASTRO_101_SPRING_2011_STUDENT", org1));
    Set<JpaRole> roles2 = set(new JpaRole("ROLE_ASTRO_101_SPRING_2011_STUDENT", org2));
    Set<String> members = set("admin");
    provider.addGroup(new JpaGroup("test1", org1, "Test", "Test group", roles1, members));
    provider.addGroup(new JpaGroup("test1", org2, "Test 2", "Test group 2", roles2, members));
    assertEquals("Test", provider.loadGroup("test1", org1.getId()).getName());
    // duplicate group, but add group does an update so it will pass
    provider.addGroup(new JpaGroup("test1", org1, "Test 1", "Test group 1", roles1, members));
    assertEquals("Test 1", provider.loadGroup("test1", org1.getId()).getName());
}
Also used : JpaGroup(org.opencastproject.security.impl.jpa.JpaGroup) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) Test(org.junit.Test)

Example 12 with JpaRole

use of org.opencastproject.security.impl.jpa.JpaRole in project opencast by opencast.

the class JpaUserProviderTest method testRoles.

@Test
public void testRoles() throws Exception {
    JpaUser userOne = createUserWithRoles(org1, "user1", "ROLE_ONE");
    provider.addUser(userOne);
    Set<JpaRole> authoritiesTwo = new HashSet<JpaRole>();
    authoritiesTwo.add(new JpaRole("ROLE_ONE", org1));
    authoritiesTwo.add(new JpaRole("ROLE_TWO", org1));
    JpaUser userTwo = createUserWithRoles(org1, "user2", "ROLE_ONE", "ROLE_TWO");
    provider.addUser(userTwo);
    assertEquals("There should be two roles", 2, IteratorUtils.toList(provider.getRoles()).size());
}
Also used : JpaRole(org.opencastproject.security.impl.jpa.JpaRole) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with JpaRole

use of org.opencastproject.security.impl.jpa.JpaRole 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 14 with JpaRole

use of org.opencastproject.security.impl.jpa.JpaRole 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)

Example 15 with JpaRole

use of org.opencastproject.security.impl.jpa.JpaRole in project opencast by opencast.

the class JpaUserProviderTest method testUpdateUserForbiddenForNonAdminUsers.

@Test
public void testUpdateUserForbiddenForNonAdminUsers() throws Exception {
    JpaUser adminUser = createUserWithRoles(org1, "admin", SecurityConstants.GLOBAL_ADMIN_ROLE);
    JpaUser user = createUserWithRoles(org1, "user", "ROLE_USER");
    provider.addUser(adminUser);
    provider.addUser(user);
    provider.setSecurityService(mockSecurityServiceWithUser(user));
    // try to add ROLE_USER
    Set<JpaRole> updatedRoles = Collections.set(new JpaRole("ROLE_USER", org1), new JpaRole(SecurityConstants.GLOBAL_ADMIN_ROLE, org1));
    try {
        provider.updateUser(new JpaUser(adminUser.getUsername(), adminUser.getPassword(), org1, adminUser.getName(), true, updatedRoles));
        fail("The current user may not edit an admin user");
    } catch (UnauthorizedException e) {
    // pass
    }
    // try to remove ROLE_ADMIN
    updatedRoles = Collections.set(new JpaRole("ROLE_USER", org1));
    try {
        provider.updateUser(new JpaUser(adminUser.getUsername(), adminUser.getPassword(), org1, adminUser.getName(), true, updatedRoles));
        fail("The current user may not remove the admin role on other user");
    } catch (UnauthorizedException e) {
    // pass
    }
}
Also used : JpaRole(org.opencastproject.security.impl.jpa.JpaRole) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) Test(org.junit.Test)

Aggregations

JpaRole (org.opencastproject.security.impl.jpa.JpaRole)37 HashSet (java.util.HashSet)18 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)18 JpaUser (org.opencastproject.security.impl.jpa.JpaUser)18 Test (org.junit.Test)16 JpaOrganization (org.opencastproject.security.impl.jpa.JpaOrganization)14 JpaGroup (org.opencastproject.security.impl.jpa.JpaGroup)12 NotFoundException (org.opencastproject.util.NotFoundException)11 Role (org.opencastproject.security.api.Role)9 Path (javax.ws.rs.Path)6 RestQuery (org.opencastproject.util.doc.rest.RestQuery)6 EntityManager (javax.persistence.EntityManager)5 EntityTransaction (javax.persistence.EntityTransaction)4 Group (org.opencastproject.security.api.Group)4 SecurityService (org.opencastproject.security.api.SecurityService)4 User (org.opencastproject.security.api.User)4 Date (java.util.Date)3 POST (javax.ws.rs.POST)3 PUT (javax.ws.rs.PUT)3 JSONArray (org.json.simple.JSONArray)3