Search in sources :

Example 51 with UserImpl

use of org.eclipse.che.api.user.server.model.impl.UserImpl in project che by eclipse.

the class JpaUserDao method doUpdate.

@Transactional
protected void doUpdate(UserImpl update) throws NotFoundException {
    final EntityManager manager = managerProvider.get();
    final UserImpl user = manager.find(UserImpl.class, update.getId());
    if (user == null) {
        throw new NotFoundException(format("Couldn't update user with id '%s' because it doesn't exist", update.getId()));
    }
    final String password = update.getPassword();
    if (password != null) {
        update.setPassword(encryptor.encrypt(password));
    } else {
        update.setPassword(user.getPassword());
    }
    manager.merge(update);
    manager.flush();
}
Also used : EntityManager(javax.persistence.EntityManager) UserImpl(org.eclipse.che.api.user.server.model.impl.UserImpl) NotFoundException(org.eclipse.che.api.core.NotFoundException) Transactional(com.google.inject.persist.Transactional)

Example 52 with UserImpl

use of org.eclipse.che.api.user.server.model.impl.UserImpl in project che by eclipse.

the class JpaUserDao method getByAliasAndPassword.

@Override
@Transactional
public UserImpl getByAliasAndPassword(String emailOrName, String password) throws NotFoundException, ServerException {
    requireNonNull(emailOrName, "Required non-null email or name");
    requireNonNull(password, "Required non-null password");
    try {
        final UserImpl user = managerProvider.get().createNamedQuery("User.getByAliasAndPassword", UserImpl.class).setParameter("alias", emailOrName).getSingleResult();
        if (!encryptor.test(password, user.getPassword())) {
            throw new NotFoundException(format("User with email or name '%s' and given password doesn't exist", emailOrName));
        }
        return erasePassword(user);
    } catch (NoResultException x) {
        throw new NotFoundException(format("User with email or name '%s' and given password doesn't exist", emailOrName));
    } catch (RuntimeException x) {
        throw new ServerException(x.getLocalizedMessage(), x);
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) UserImpl(org.eclipse.che.api.user.server.model.impl.UserImpl) NotFoundException(org.eclipse.che.api.core.NotFoundException) NoResultException(javax.persistence.NoResultException) Transactional(com.google.inject.persist.Transactional)

Example 53 with UserImpl

use of org.eclipse.che.api.user.server.model.impl.UserImpl in project che by eclipse.

the class JpaUserDao method getAll.

@Override
@Transactional
public Page<UserImpl> getAll(int maxItems, long skipCount) throws ServerException {
    // TODO need to ensure that 'getAll' query works with same data as 'getTotalCount'
    checkArgument(maxItems >= 0, "The number of items to return can't be negative.");
    checkArgument(skipCount >= 0 && skipCount <= Integer.MAX_VALUE, "The number of items to skip can't be negative or greater than " + Integer.MAX_VALUE);
    try {
        final List<UserImpl> list = managerProvider.get().createNamedQuery("User.getAll", UserImpl.class).setMaxResults(maxItems).setFirstResult((int) skipCount).getResultList().stream().map(JpaUserDao::erasePassword).collect(toList());
        return new Page<>(list, skipCount, maxItems, getTotalCount());
    } catch (RuntimeException x) {
        throw new ServerException(x.getLocalizedMessage(), x);
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) UserImpl(org.eclipse.che.api.user.server.model.impl.UserImpl) Page(org.eclipse.che.api.core.Page) Transactional(com.google.inject.persist.Transactional)

Example 54 with UserImpl

use of org.eclipse.che.api.user.server.model.impl.UserImpl in project che by eclipse.

the class UserManagerTest method shouldGetUserById.

@Test
public void shouldGetUserById() throws Exception {
    final User user = new UserImpl("identifier", "test@email.com", "testName", "password", Collections.singletonList("alias"));
    when(manager.getById(user.getId())).thenReturn(user);
    assertEquals(manager.getById(user.getId()), user);
}
Also used : User(org.eclipse.che.api.core.model.user.User) UserImpl(org.eclipse.che.api.user.server.model.impl.UserImpl) Test(org.testng.annotations.Test)

Example 55 with UserImpl

use of org.eclipse.che.api.user.server.model.impl.UserImpl in project che by eclipse.

the class UserManagerTest method shouldThrowConflictExceptionOnCreationIfUserNameIsReserved.

@Test(expectedExceptions = ConflictException.class)
public void shouldThrowConflictExceptionOnCreationIfUserNameIsReserved() throws Exception {
    final User user = new UserImpl("id", "test@email.com", "reserved");
    manager.create(user, false);
}
Also used : User(org.eclipse.che.api.core.model.user.User) UserImpl(org.eclipse.che.api.user.server.model.impl.UserImpl) Test(org.testng.annotations.Test)

Aggregations

UserImpl (org.eclipse.che.api.user.server.model.impl.UserImpl)65 Test (org.testng.annotations.Test)45 User (org.eclipse.che.api.core.model.user.User)13 Response (com.jayway.restassured.response.Response)10 BeforeMethod (org.testng.annotations.BeforeMethod)9 ServerException (org.eclipse.che.api.core.ServerException)8 Transactional (com.google.inject.persist.Transactional)6 ConflictException (org.eclipse.che.api.core.ConflictException)5 BeforeUserRemovedEvent (org.eclipse.che.api.user.server.event.BeforeUserRemovedEvent)5 EntityManager (javax.persistence.EntityManager)4 NotFoundException (org.eclipse.che.api.core.NotFoundException)4 Page (org.eclipse.che.api.core.Page)4 PostUserPersistedEvent (org.eclipse.che.api.user.server.event.PostUserPersistedEvent)4 ProfileImpl (org.eclipse.che.api.user.server.model.impl.ProfileImpl)4 UserDao (org.eclipse.che.api.user.server.spi.UserDao)4 ArrayList (java.util.ArrayList)3 Inject (javax.inject.Inject)3 UserRemovedEvent (org.eclipse.che.api.user.server.event.UserRemovedEvent)3 UserDto (org.eclipse.che.api.user.shared.dto.UserDto)3 TckRepository (org.eclipse.che.commons.test.tck.repository.TckRepository)3