Search in sources :

Example 11 with JpaUser

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

Example 12 with JpaUser

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

the class JpaUserProviderTest method testAddUserWithGlobalAdminRoleNotAllowedAsNonAdminUser.

@Test(expected = UnauthorizedException.class)
public void testAddUserWithGlobalAdminRoleNotAllowedAsNonAdminUser() throws Exception {
    provider.setSecurityService(mockSecurityServiceWithUser(createUserWithRoles(org1, "user1", "ROLE_USER")));
    JpaUser newUser = createUserWithRoles(org1, "admin2", SecurityConstants.GLOBAL_ADMIN_ROLE);
    provider.addUser(newUser);
    fail("The current user shouldn't able to create an global admin user.");
}
Also used : JpaUser(org.opencastproject.security.impl.jpa.JpaUser) Test(org.junit.Test)

Example 13 with JpaUser

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

the class JpaUserProviderTest method testAddUserWithOrgAdminRoleNotAllowedAsNonAdminUser.

@Test(expected = UnauthorizedException.class)
public void testAddUserWithOrgAdminRoleNotAllowedAsNonAdminUser() throws Exception {
    provider.setSecurityService(mockSecurityServiceWithUser(createUserWithRoles(org1, "user1", "ROLE_USER")));
    JpaUser newUser = createUserWithRoles(org1, "org_admin2", org1.getAdminRole());
    provider.addUser(newUser);
    fail("The current user shouldn't able to create an global admin user.");
}
Also used : JpaUser(org.opencastproject.security.impl.jpa.JpaUser) Test(org.junit.Test)

Example 14 with JpaUser

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

the class JpaUserProviderTest method testUpdateUser.

@Test
public void testUpdateUser() throws Exception {
    Set<JpaRole> authorities = new HashSet<JpaRole>();
    authorities.add(new JpaRole("ROLE_ASTRO_101_SPRING_2011_STUDENT", org1));
    JpaUser user = new JpaUser("user1", "pass1", org1, provider.getName(), true, authorities);
    provider.addUser(user);
    User loadUser = provider.loadUser("user1");
    assertNotNull(loadUser);
    authorities.add(new JpaRole("ROLE_ASTRO_101_SPRING_2013_STUDENT", org1));
    String newPassword = "newPassword";
    JpaUser updateUser = new JpaUser(user.getUsername(), newPassword, org1, provider.getName(), true, authorities);
    User loadUpdatedUser = provider.updateUser(updateUser);
    // User loadUpdatedUser = provider.loadUser(user.getUsername());
    assertNotNull(loadUpdatedUser);
    assertEquals(user.getUsername(), loadUpdatedUser.getUsername());
    assertEquals(PasswordEncoder.encode(newPassword, user.getUsername()), loadUpdatedUser.getPassword());
    assertEquals(authorities.size(), loadUpdatedUser.getRoles().size());
    updateUser = new JpaUser("unknown", newPassword, org1, provider.getName(), true, authorities);
    try {
        provider.updateUser(updateUser);
        fail("Should throw a NotFoundException");
    } catch (NotFoundException e) {
        assertTrue("User not found.", true);
    }
}
Also used : User(org.opencastproject.security.api.User) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) NotFoundException(org.opencastproject.util.NotFoundException) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with JpaUser

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

the class JpaUserProviderTest method testAddUserWithOrgAdminRoleAsOrgAdmin.

@Test
public void testAddUserWithOrgAdminRoleAsOrgAdmin() throws Exception {
    provider.setSecurityService(mockSecurityServiceWithUser(createUserWithRoles(org1, "org_admin", org1.getAdminRole())));
    JpaUser newUser = createUserWithRoles(org1, "org_admin2", org1.getAdminRole());
    provider.addUser(newUser);
    User loadedUser = provider.loadUser(newUser.getUsername());
    assertNotNull("The currently added user isn't loaded as expected", loadedUser);
    assertEquals(newUser.getUsername(), loadedUser.getUsername());
    assertEquals(newUser.getRoles(), loadedUser.getRoles());
}
Also used : User(org.opencastproject.security.api.User) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) Test(org.junit.Test)

Aggregations

JpaUser (org.opencastproject.security.impl.jpa.JpaUser)35 Test (org.junit.Test)19 JpaRole (org.opencastproject.security.impl.jpa.JpaRole)18 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)13 User (org.opencastproject.security.api.User)12 NotFoundException (org.opencastproject.util.NotFoundException)9 HashSet (java.util.HashSet)8 JpaOrganization (org.opencastproject.security.impl.jpa.JpaOrganization)8 EntityManager (javax.persistence.EntityManager)5 Path (javax.ws.rs.Path)4 SecurityService (org.opencastproject.security.api.SecurityService)4 RestQuery (org.opencastproject.util.doc.rest.RestQuery)4 EntityTransaction (javax.persistence.EntityTransaction)3 NoResultException (javax.persistence.NoResultException)3 Before (org.junit.Before)3 Role (org.opencastproject.security.api.Role)3 JpaGroup (org.opencastproject.security.impl.jpa.JpaGroup)3 JObject (com.entwinemedia.fn.data.json.JObject)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2