Search in sources :

Example 6 with RoleEntity

use of com.peterphi.usermanager.db.entity.RoleEntity in project stdlib by petergeneric.

the class LDAPUserAuthenticationService method setRoles.

private void setRoles(final UserEntity existing, final LDAPUserRecord ldap) {
    // First, figure out which roles are new by excluding those that have been removed + those that are unchanged
    // At the same time, remove the roles the user no longer has
    // PKs for roles with no change
    Set<String> noChange = new HashSet<>();
    {
        // role entities to remove
        Set<RoleEntity> toRemove = new HashSet<>();
        for (RoleEntity roleEntity : existing.getRoles()) {
            if (ldap.getRoleIds().contains(roleEntity.getId()))
                // User still has this role
                noChange.add(roleEntity.getId());
            else {
                // User no longer has this role
                toRemove.add(roleEntity);
                // remove us from the role's membership list
                roleEntity.getMembers().remove(existing);
            }
        }
        // Remove the roles the user no longer has
        existing.getRoles().removeAll(toRemove);
    }
    // Add the new roles (creating them if necessary)
    for (LDAPGroup group : ldap.roles) {
        if (!noChange.contains(group.id)) {
            RoleEntity role = roleDao.getOrCreate(group.id, "LDAP: " + group.dn);
            role.getMembers().add(existing);
            existing.getRoles().add(role);
        }
    }
    dao.update(existing);
}
Also used : RoleEntity(com.peterphi.usermanager.db.entity.RoleEntity) HashSet(java.util.HashSet) Set(java.util.Set) HashSet(java.util.HashSet)

Example 7 with RoleEntity

use of com.peterphi.usermanager.db.entity.RoleEntity in project stdlib by petergeneric.

the class RoleUIServiceImpl method get.

@Override
@Transactional(readOnly = true)
public String get(final String id) {
    TemplateCall call = templater.template("role");
    final RoleEntity entity = dao.getById(id);
    if (entity == null)
        throw new IllegalArgumentException("No such Role: " + id);
    call.set("entity", entity);
    call.set("allUsers", userDao.getAll());
    call.set("users", userDao.findByUriQuery(new WebQuery().eq("roles.id", id)).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 8 with RoleEntity

use of com.peterphi.usermanager.db.entity.RoleEntity in project stdlib by petergeneric.

the class RoleUIServiceImpl method changeCaption.

@Override
@Transactional
public Response changeCaption(final String id, final String nonce, final String caption) {
    nonceStore.validate(NONCE_USE, nonce);
    final RoleEntity entity = dao.getById(id);
    if (entity == null)
        throw new IllegalArgumentException("No such Role: " + id);
    entity.setCaption(caption);
    dao.update(entity);
    return Response.seeOther(URI.create("/role/" + id)).build();
}
Also used : RoleEntity(com.peterphi.usermanager.db.entity.RoleEntity) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 9 with RoleEntity

use of com.peterphi.usermanager.db.entity.RoleEntity in project stdlib by petergeneric.

the class RoleUIServiceImpl method create.

@Override
@Transactional
public Response create(final String id, final String nonce, final String caption) {
    nonceStore.validate(NONCE_USE, nonce);
    if (dao.getById(id) != null)
        throw new IllegalArgumentException("Role with name already exists: " + id);
    RoleEntity entity = new RoleEntity();
    entity.setId(id);
    entity.setCaption(caption);
    dao.save(entity);
    return Response.seeOther(URI.create("/role/" + id)).build();
}
Also used : RoleEntity(com.peterphi.usermanager.db.entity.RoleEntity) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 10 with RoleEntity

use of com.peterphi.usermanager.db.entity.RoleEntity in project stdlib by petergeneric.

the class RegisterUIServiceImpl method doRegister.

@AuthConstraint(id = "register_service", skip = true, comment = "register page handles own constraints")
@Override
@Transactional
public Response doRegister(String nonce, String email, String name, String dateFormat, String timeZone, String password, String passwordConfirm, List<String> roles) {
    nonceStore.validate(nonce, true);
    if (!allowAnonymousRegistration && !login.isAdmin())
        throw new AuthenticationFailureException("Anonymous registration is not enabled. Please log in as an admin to register users");
    if (!password.equals(passwordConfirm))
        throw new IllegalArgumentException("The passwords you supplied do not match");
    if ((roles != null && roles.size() > 0) && !login.isAdmin())
        throw new IllegalArgumentException("Cannot specify roles with user registration: you are not an admin!");
    if (accountDao.getAll().size() == 0) {
        log.warn("User with e-mail " + email + " will be the first user in the system and so will be granted the role " + UserLogin.ROLE_ADMIN);
        roles = Arrays.asList(UserLogin.ROLE_ADMIN);
    }
    log.info("Creating user " + name + " with e-mail " + email + ". Created by " + login.getName() + " (" + login.getId() + ") with roles " + roles);
    // Create a user
    final int newUser = accountDao.register(name, email, password, dateFormat, timeZone);
    final UserEntity entity = accountDao.getById(newUser);
    for (String role : roles) {
        final RoleEntity roleEntity = roleDao.getById(role);
        if (roleEntity == null)
            throw new IllegalArgumentException("Role does not exist: " + role);
        roleEntity.getMembers().add(entity);
        roleDao.update(roleEntity);
    }
    log.info("Created user " + newUser + " with e-mail " + email);
    if (login.isLoggedIn())
        return Response.seeOther(URI.create("/users")).build();
    else
        return Response.seeOther(URI.create("/login")).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) AuthConstraint(com.peterphi.std.guice.common.auth.annotations.AuthConstraint) 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