use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.
the class UserUIServiceImpl method changePassword.
@Override
@Transactional
public Response changePassword(final int userId, final String nonce, final String newPassword, final String newPasswordConfirm) {
nonceStore.validate(NONCE_USE, nonce);
final int localUser = login.getId();
if (localUser != userId && !login.isAdmin())
throw new AuthenticationFailureException("Only a User Admin can change the password of another user!");
if (newPassword == null || newPasswordConfirm == null)
throw new IllegalArgumentException("Passwords do not match (or no password supplied)");
if (!newPassword.equals(newPasswordConfirm))
throw new IllegalArgumentException("Passwords do not match!");
if (newPassword.length() == 0)
throw new IllegalArgumentException("No password supplied!");
accountDao.changePassword(userId, newPassword);
// Redirect back to the user page
return Response.seeOther(URI.create("/user/" + userId)).build();
}
use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.
the class UserUIServiceImpl method getUserEdit.
@Override
@Transactional(readOnly = true)
public String getUserEdit(final int userId) {
final int localUser = login.getId();
if (localUser != userId && !login.isAdmin())
throw new AuthenticationFailureException("Only a User Admin can edit the profile of another user!");
TemplateCall call = templater.template("user_edit");
final UserEntity user = accountDao.getById(userId);
call.set("entity", user);
call.set("user", user);
call.set("timezones", Arrays.asList(TimeZone.getAvailableIDs()));
call.set("dateformats", Arrays.asList("YYYY-MM-dd HH:mm:ss zzz", "YYYY-MM-dd HH:mm:ss", "YYYY-MM-dd HH:mm"));
call.set("entityRoleIds", getRoles(user));
call.set("roles", roleDao.getAll());
call.set("nonce", nonceStore.getValue(NONCE_USE));
return call.process();
}
use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.
the class UserUIServiceImpl method editUserProfile.
@Override
@Transactional
public Response editUserProfile(final int userId, final String nonce, final String dateFormat, final String timeZone, final String name, final String email, final List<String> roles) {
nonceStore.validate(NONCE_USE, nonce);
final int localUser = login.getId();
if (localUser != userId && !login.isAdmin())
throw new AuthenticationFailureException("Only a User Admin can edit the profile of another user!");
// Change regular account settings
final UserEntity user = accountDao.changeProfile(userId, name, email, dateFormat, timeZone);
// Change roles (if we're admin and the user is local)
if (login.isAdmin() && user.isLocal()) {
final Set<String> currentRoles = getRoles(user);
// Roles to add to user
final Set<String> addRoles = new HashSet<>(roles);
addRoles.removeAll(currentRoles);
// Roles to remove from user
final Set<String> delRoles = new HashSet<>(currentRoles);
delRoles.removeAll(roles);
// Add roles as necessary
if (addRoles.size() > 0) {
for (String role : addRoles) {
RoleEntity entity = roleDao.getById(role);
entity.getMembers().add(user);
roleDao.update(entity);
}
}
// Remove roles as necessary
if (delRoles.size() > 0) {
for (String role : delRoles) {
RoleEntity entity = roleDao.getById(role);
entity.getMembers().removeIf(u -> u.getId() == user.getId());
roleDao.update(entity);
}
}
}
// Redirect back to the user page
return Response.seeOther(URI.create("/user/" + userId)).build();
}
use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.
the class UserUIServiceImpl method deleteUser.
@Override
@Transactional
@AuthConstraint(role = UserLogin.ROLE_ADMIN)
public Response deleteUser(final int userId, final String nonce) {
nonceStore.validate(NONCE_USE, nonce);
final int localUser = login.getId();
accountDao.deleteById(userId);
if (localUser == userId) {
// Invalidate the current session
login.clear();
return Response.seeOther(URI.create("/logout")).build();
} else {
// Redirect back to the user list page
return Response.seeOther(URI.create("/users")).build();
}
}
use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.
the class ResourceProvisionService method getOrCreateTemplate.
@Transactional
public ResourceTemplateEntity getOrCreateTemplate(final String name) {
final String revision = templateConfig.config.getLastRevision();
ResourceTemplateEntity entity = templateDao.getById(name);
if (entity != null) {
// Check if the template has been updated
if (!StringUtils.equals(entity.getLatestRevision(), revision)) {
entity.setLatestRevision(revision);
entity.setRevisions(entity.getRevisions() + 1);
templateDao.update(entity);
}
return entity;
} else {
entity = new ResourceTemplateEntity();
entity.setId(name);
entity.setLatestRevision(revision);
entity.setRevisions(1);
templateDao.save(entity);
return templateDao.getById(name);
}
}
Aggregations