Search in sources :

Example 21 with JpaUser

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

the class JpaUserAndRoleProvider method updateUser.

/**
 * Updates a user to the persistence
 *
 * @param user
 *          the user to save
 * @throws NotFoundException
 * @throws org.opencastproject.security.api.UnauthorizedException
 *          if the current user is not allowed to update user with the given roles
 */
public User updateUser(JpaUser user) throws NotFoundException, UnauthorizedException {
    if (!UserDirectoryUtils.isCurrentUserAuthorizedHandleRoles(securityService, user.getRoles()))
        throw new UnauthorizedException("The user is not allowed to set the admin role on other users");
    JpaUser updateUser = UserDirectoryPersistenceUtil.findUser(user.getUsername(), user.getOrganization().getId(), emf);
    if (updateUser == null)
        throw new NotFoundException("User " + user.getUsername() + " not found.");
    logger.debug("updateUser({})", user.getUsername());
    if (!UserDirectoryUtils.isCurrentUserAuthorizedHandleRoles(securityService, updateUser.getRoles()))
        throw new UnauthorizedException("The user is not allowed to update an admin user");
    String encodedPassword = null;
    // only update Password if a value is set
    if (user.getPassword().isEmpty()) {
        JpaUser old = UserDirectoryPersistenceUtil.findUser(user.getUsername(), user.getOrganization().getId(), emf);
        encodedPassword = old.getPassword();
    } else {
        // Update an JPA user with an encoded password.
        encodedPassword = PasswordEncoder.encode(user.getPassword(), user.getUsername());
    }
    // Only save internal roles
    Set<JpaRole> roles = UserDirectoryPersistenceUtil.saveRoles(filterRoles(user.getRoles()), emf);
    JpaOrganization organization = UserDirectoryPersistenceUtil.saveOrganization((JpaOrganization) user.getOrganization(), emf);
    JpaUser updatedUser = UserDirectoryPersistenceUtil.saveUser(new JpaUser(user.getUsername(), encodedPassword, organization, user.getName(), user.getEmail(), user.getProvider(), true, roles), emf);
    cache.put(user.getUsername() + DELIMITER + organization.getId(), updatedUser);
    updateGroupMembership(user);
    return updatedUser;
}
Also used : JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) NotFoundException(org.opencastproject.util.NotFoundException) JpaUser(org.opencastproject.security.impl.jpa.JpaUser)

Example 22 with JpaUser

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

the class UserDirectoryPersistenceUtil method saveUser.

/**
 * Persist an user
 *
 * @param user
 *          the user to persist
 * @param emf
 *          the entity manager factory
 * @return the persisted organization
 */
public static JpaUser saveUser(JpaUser user, EntityManagerFactory emf) {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        JpaUser u = findUser(user.getUsername(), user.getOrganization().getId(), emf);
        if (u == null) {
            em.persist(user);
        } else {
            user.setId(u.getId());
            user = em.merge(user);
        }
        tx.commit();
        return user;
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) JpaUser(org.opencastproject.security.impl.jpa.JpaUser)

Example 23 with JpaUser

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

the class UserDirectoryPersistenceUtil method deleteUser.

/**
 * Delete the user with given name in the given organization
 *
 * @param username
 *          the name of the user to delete
 * @param orgId
 *          the organization id
 * @param emf
 *          the entity manager factory
 * @throws NotFoundException
 * @throws Exception
 */
public static void deleteUser(String username, String orgId, EntityManagerFactory emf) throws NotFoundException, Exception {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        JpaUser user = findUser(username, orgId, emf);
        if (user == null) {
            throw new NotFoundException("User with name " + username + " does not exist");
        }
        em.remove(em.merge(user));
        tx.commit();
    } catch (NotFoundException e) {
        throw e;
    } catch (Exception e) {
        if (tx.isActive()) {
            tx.rollback();
        }
        throw e;
    } finally {
        em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) NotFoundException(org.opencastproject.util.NotFoundException) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) NotFoundException(org.opencastproject.util.NotFoundException) NoResultException(javax.persistence.NoResultException)

Example 24 with JpaUser

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

the class JpaGroupRoleProviderTest method testRemoveGroupNotAllowedAsNonAdminUser.

@Test
public void testRemoveGroupNotAllowedAsNonAdminUser() throws UnauthorizedException {
    JpaGroup group = new JpaGroup("test", org1, "Test", "Test group", Collections.set(new JpaRole(SecurityConstants.GLOBAL_ADMIN_ROLE, org1)));
    try {
        provider.addGroup(group);
        Group loadGroup = provider.loadGroup(group.getGroupId(), group.getOrganization().getId());
        assertNotNull(loadGroup);
        assertEquals(group.getGroupId(), loadGroup.getGroupId());
    } catch (Exception e) {
        fail("The group should be added");
    }
    JpaUser user = new JpaUser("user", "pass1", org1, "User", "user@localhost", "opencast", true, Collections.set(new JpaRole("ROLE_USER", org1)));
    // Set the security sevice
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getUser()).andReturn(user).anyTimes();
    EasyMock.expect(securityService.getOrganization()).andReturn(org1).anyTimes();
    EasyMock.replay(securityService);
    provider.setSecurityService(securityService);
    Response removeGroupResponse = provider.removeGroup(group.getGroupId());
    assertNotNull(removeGroupResponse);
    assertEquals(HttpStatus.SC_FORBIDDEN, removeGroupResponse.getStatus());
}
Also used : JpaGroup(org.opencastproject.security.impl.jpa.JpaGroup) Response(javax.ws.rs.core.Response) JpaGroup(org.opencastproject.security.impl.jpa.JpaGroup) Group(org.opencastproject.security.api.Group) SecurityService(org.opencastproject.security.api.SecurityService) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) Test(org.junit.Test)

Example 25 with JpaUser

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

the class JpaGroupRoleProviderTest method testAddGroupNotAllowedAsNonAdminUser.

@Test(expected = UnauthorizedException.class)
public void testAddGroupNotAllowedAsNonAdminUser() throws UnauthorizedException {
    JpaUser user = new JpaUser("user", "pass1", org1, "User", "user@localhost", "opencast", true, Collections.set(new JpaRole("ROLE_USER", org1)));
    // Set the security sevice
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getUser()).andReturn(user).anyTimes();
    EasyMock.expect(securityService.getOrganization()).andReturn(org1).anyTimes();
    EasyMock.replay(securityService);
    provider.setSecurityService(securityService);
    JpaGroup group = new JpaGroup("test", org1, "Test", "Test group", Collections.set(new JpaRole(SecurityConstants.GLOBAL_ADMIN_ROLE, org1)));
    provider.addGroup(group);
    fail("The group with admin role should not be created by an non admin user");
}
Also used : JpaGroup(org.opencastproject.security.impl.jpa.JpaGroup) SecurityService(org.opencastproject.security.api.SecurityService) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) 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