Search in sources :

Example 6 with User

use of org.eclipse.kapua.service.user.User in project kapua by eclipse.

the class UserServiceImpl method update.

@Override
public User update(User user) throws KapuaException {
    // Validation of the fields
    ArgumentValidator.notNull(user.getId().getId(), "id");
    ArgumentValidator.notNull(user.getScopeId().getId(), "accountId");
    ArgumentValidator.notEmptyOrNull(user.getName(), "name");
    ArgumentValidator.match(user.getName(), ArgumentValidator.NAME_REGEXP, "name");
    // ArgumentValidator.match(user.getRawPassword(), ArgumentValidator.PASSWORD_REGEXP, "rawPassword");
    ArgumentValidator.match(user.getEmail(), ArgumentValidator.EMAIL_REGEXP, "email");
    validateSystemUser(user.getName());
    // 
    // Check Access
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(UserDomain.USER, Actions.write, user.getScopeId()));
    // 
    // Do update
    User userUpdated = null;
    EntityManager em = UserEntityManagerFactory.getInstance().createEntityManager();
    try {
        User currentUser = UserDAO.find(em, user.getId());
        if (currentUser == null) {
            throw new KapuaEntityNotFoundException(User.TYPE, user.getId());
        }
        em.beginTransaction();
        UserDAO.update(em, user);
        em.commit();
        userUpdated = UserDAO.find(em, user.getId());
    } catch (Exception pe) {
        em.rollback();
        throw KapuaExceptionUtils.convertPersistenceException(pe);
    } finally {
        em.close();
    }
    return userUpdated;
}
Also used : EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) User(org.eclipse.kapua.service.user.User) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) KapuaException(org.eclipse.kapua.KapuaException)

Example 7 with User

use of org.eclipse.kapua.service.user.User in project kapua by eclipse.

the class UserServiceImpl method findByName.

@Override
public User findByName(String name) throws KapuaException {
    // Validation of the fields
    ArgumentValidator.notEmptyOrNull(name, "name");
    // Do the find
    User user = null;
    EntityManager em = UserEntityManagerFactory.getInstance().createEntityManager();
    ;
    try {
        user = UserDAO.findByName(em, name);
    } catch (Exception pe) {
        throw KapuaExceptionUtils.convertPersistenceException(pe);
    } finally {
        em.close();
    }
    // Check Access
    if (user != null) {
        AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
        PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
        authorizationService.checkPermission(permissionFactory.newPermission(UserDomain.USER, Actions.read, user.getScopeId()));
    }
    return user;
}
Also used : EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) User(org.eclipse.kapua.service.user.User) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) KapuaException(org.eclipse.kapua.KapuaException)

Example 8 with User

use of org.eclipse.kapua.service.user.User in project kapua by eclipse.

the class UserServiceImpl method create.

@Override
public User create(UserCreator userCreator) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(userCreator.getScopeId().getId(), "scopeId");
    ArgumentValidator.notEmptyOrNull(userCreator.getName(), "name");
    // ArgumentValidator.notEmptyOrNull(userCreator.getRawPassword(), "rawPassword");
    ArgumentValidator.match(userCreator.getName(), ArgumentValidator.NAME_REGEXP, "name");
    // ArgumentValidator.match(userCreator.getRawPassword(), ArgumentValidator.PASSWORD_REGEXP, "rawPassword");
    ArgumentValidator.match(userCreator.getEmail(), ArgumentValidator.EMAIL_REGEXP, "email");
    // 
    // Check Access
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(UserDomain.USER, Actions.write, userCreator.getScopeId()));
    // 
    // Do create
    User user = null;
    EntityManager em = UserEntityManagerFactory.getInstance().createEntityManager();
    ;
    try {
        em.beginTransaction();
        user = UserDAO.create(em, userCreator);
        user = UserDAO.find(em, user.getId());
        em.commit();
    } catch (Exception pe) {
        em.rollback();
        throw KapuaExceptionUtils.convertPersistenceException(pe);
    } finally {
        em.close();
    }
    return user;
}
Also used : EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) User(org.eclipse.kapua.service.user.User) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) KapuaException(org.eclipse.kapua.KapuaException)

Example 9 with User

use of org.eclipse.kapua.service.user.User in project kapua by eclipse.

the class Users method postUser.

/**
 * Unlock a User based on the userId provided in the path request.
 *
 * @param userId
 *            The userId that refer to the user to unlock.
 *
 * @return The user unlocked.
 */
// FIXME see https://github.com/eurotech/kapua/issues/193
// @POST
// @Path("{userId}/unlock")
// @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
// public User unlockUser(@PathParam("userId") long userId) {
// User user = null;
// 
// try {
// long targetAccountId = getTargetAccountId();
// 
// ServiceLocator sl = ServiceLocator.getInstance();
// UserService us = sl.getUserService();
// user = us.unlockUser(targetAccountId, userId);
// user = us.findWithAccessInfo(targetAccountId, userId);
// } catch (Throwable t) {
// handleException(t);
// }
// return returnNotNullEntity(user);
// }
/**
 * Creates a new User based on the information provided in UserCreator
 * parameter.
 *
 * @param userCreator
 *            Provides the information for the new User to be created.
 * @return The newly created User object.
 */
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public User postUser(UserCreator userCreator) {
    User user = null;
    try {
        userCreator.setScopeId(KapuaSecurityUtils.getSession().getScopeId());
        user = userService.create(userCreator);
    } catch (Throwable t) {
        handleException(t);
    }
    return returnNotNullEntity(user);
}
Also used : User(org.eclipse.kapua.service.user.User) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 10 with User

use of org.eclipse.kapua.service.user.User in project kapua by eclipse.

the class Users method putUser.

/**
 * Updates the User based on the information provided in the User parameter.
 *
 * @param user
 *            The modified User whose attributed need to be updated.
 * @return The updated user.
 */
@PUT
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public User putUser(User user) {
    User userUpdated = null;
    try {
        ((UserImpl) user).setScopeId(KapuaSecurityUtils.getSession().getScopeId());
        userUpdated = userService.update(user);
    } catch (Throwable t) {
        handleException(t);
    }
    return returnNotNullEntity(userUpdated);
}
Also used : User(org.eclipse.kapua.service.user.User) UserImpl(org.eclipse.kapua.service.user.internal.UserImpl) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Aggregations

User (org.eclipse.kapua.service.user.User)19 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)11 UserService (org.eclipse.kapua.service.user.UserService)11 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)9 KapuaException (org.eclipse.kapua.KapuaException)7 GwtUser (org.eclipse.kapua.app.console.shared.model.GwtUser)7 KapuaId (org.eclipse.kapua.model.id.KapuaId)7 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)6 KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)5 KapuaIllegalArgumentException (org.eclipse.kapua.KapuaIllegalArgumentException)5 GwtUserService (org.eclipse.kapua.app.console.shared.service.GwtUserService)5 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)5 Produces (javax.ws.rs.Produces)3 KapuaSession (org.eclipse.kapua.commons.security.KapuaSession)3 CredentialService (org.eclipse.kapua.service.authentication.credential.CredentialService)3 Permission (org.eclipse.kapua.service.authorization.permission.Permission)3 UserPermissionFactory (org.eclipse.kapua.service.authorization.user.permission.UserPermissionFactory)3 UserPermissionService (org.eclipse.kapua.service.authorization.user.permission.UserPermissionService)3 UserFactory (org.eclipse.kapua.service.user.UserFactory)3 HashSet (java.util.HashSet)2