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;
}
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;
}
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;
}
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);
}
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);
}
Aggregations