use of com.peterphi.usermanager.db.entity.RoleEntity in project stdlib by petergeneric.
the class RoleDaoImpl method getOrCreate.
@Transactional
public RoleEntity getOrCreate(final String id, final String caption) {
RoleEntity existing = getById(id);
if (existing == null) {
existing = new RoleEntity();
existing.setId(id);
existing.setCaption(caption);
save(existing);
}
return existing;
}
use of com.peterphi.usermanager.db.entity.RoleEntity in project stdlib by petergeneric.
the class RoleUIServiceImpl method delete.
@Override
@Transactional
public Response delete(final String id, final String nonce) {
nonceStore.validate(NONCE_USE, nonce);
if (StringUtils.equalsIgnoreCase(id, UserLogin.ROLE_ADMIN))
throw new IllegalArgumentException("Cannot delete the user manager admin role!");
final RoleEntity entity = dao.getById(id);
if (entity == null)
throw new IllegalArgumentException("No such Role: " + id);
dao.delete(entity);
return Response.seeOther(URI.create("/roles")).build();
}
use of com.peterphi.usermanager.db.entity.RoleEntity in project stdlib by petergeneric.
the class RoleUIServiceImpl method changeMembers.
@Override
@Transactional
public Response changeMembers(final String id, final String nonce, final List<Integer> members) {
nonceStore.validate(NONCE_USE, nonce);
final RoleEntity entity = dao.getById(id);
if (entity == null)
throw new IllegalArgumentException("No such Role: " + id);
final List<UserEntity> users = userDao.getListById(members);
if (users.size() != members.size())
throw new IllegalArgumentException("One or more members provided did not exist! " + members);
{
final List<Integer> existing = entity.getMembers().stream().map(UserEntity::getId).collect(Collectors.toList());
final List<Integer> added = members.stream().filter(i -> !existing.contains(i)).collect(Collectors.toList());
final List<Integer> removed = members.stream().filter(i -> existing.contains(i)).collect(Collectors.toList());
}
entity.getMembers().clear();
;
entity.getMembers().addAll(users);
dao.update(entity);
return Response.seeOther(URI.create("/role/" + id)).build();
}
use of com.peterphi.usermanager.db.entity.RoleEntity in project stdlib by petergeneric.
the class RoleUIServiceImpl method getRoles.
@Override
@Transactional(readOnly = true)
public String getRoles(UriInfo query) {
ConstrainedResultSet<RoleEntity> resultset = dao.findByUriQuery(new WebQuery().orderAsc("id").decode(query));
TemplateCall call = templater.template("roles");
call.set("resultset", resultset);
call.set("roles", resultset.getList());
call.set("nonce", nonceStore.getValue(NONCE_USE));
return call.process();
}
use of com.peterphi.usermanager.db.entity.RoleEntity 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();
}
Aggregations