Search in sources :

Example 1 with RoleEntity

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;
}
Also used : RoleEntity(com.peterphi.usermanager.db.entity.RoleEntity) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 2 with RoleEntity

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();
}
Also used : RoleEntity(com.peterphi.usermanager.db.entity.RoleEntity) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 3 with RoleEntity

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();
}
Also used : RoleEntity(com.peterphi.usermanager.db.entity.RoleEntity) List(java.util.List) UserEntity(com.peterphi.usermanager.db.entity.UserEntity) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 4 with RoleEntity

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();
}
Also used : RoleEntity(com.peterphi.usermanager.db.entity.RoleEntity) WebQuery(com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery) TemplateCall(com.peterphi.std.guice.web.rest.templating.TemplateCall) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 5 with RoleEntity

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();
}
Also used : RoleEntity(com.peterphi.usermanager.db.entity.RoleEntity) AuthenticationFailureException(com.peterphi.usermanager.guice.authentication.AuthenticationFailureException) AuthConstraint(com.peterphi.std.guice.common.auth.annotations.AuthConstraint) UserEntity(com.peterphi.usermanager.db.entity.UserEntity) HashSet(java.util.HashSet) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Aggregations

RoleEntity (com.peterphi.usermanager.db.entity.RoleEntity)10 Transactional (com.peterphi.std.guice.database.annotation.Transactional)9 UserEntity (com.peterphi.usermanager.db.entity.UserEntity)3 AuthConstraint (com.peterphi.std.guice.common.auth.annotations.AuthConstraint)2 WebQuery (com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery)2 TemplateCall (com.peterphi.std.guice.web.rest.templating.TemplateCall)2 AuthenticationFailureException (com.peterphi.usermanager.guice.authentication.AuthenticationFailureException)2 HashSet (java.util.HashSet)2 List (java.util.List)1 Set (java.util.Set)1