Search in sources :

Example 11 with UserEntity

use of com.peterphi.usermanager.db.entity.UserEntity 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();
}
Also used : AuthenticationFailureException(com.peterphi.usermanager.guice.authentication.AuthenticationFailureException) AuthConstraint(com.peterphi.std.guice.common.auth.annotations.AuthConstraint) UserEntity(com.peterphi.usermanager.db.entity.UserEntity) TemplateCall(com.peterphi.std.guice.web.rest.templating.TemplateCall) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 12 with UserEntity

use of com.peterphi.usermanager.db.entity.UserEntity 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)

Example 13 with UserEntity

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

the class LDAPUserAuthenticationService method registerOrUpdateUser.

private UserEntity registerOrUpdateUser(final LDAPUserRecord ldap) {
    UserEntity existing = dao.getUserByEmail(ldap.username);
    if (existing == null) {
        dao.registerRemote(ldap.username, ldap.fullName);
        existing = dao.getUserByEmail(ldap.username);
        assert (existing != null);
    }
    // Now sync the role information
    setRoles(existing, ldap);
    return existing;
}
Also used : UserEntity(com.peterphi.usermanager.db.entity.UserEntity)

Example 14 with UserEntity

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

the class LDAPUserAuthenticationService method authenticate.

@Override
@Transactional
public UserEntity authenticate(final String username, final String password, final boolean basicAuth) {
    // TODO Authenticate with LDAP and get user record
    LDAPUserRecord record = ldapAuthenticate(username, password);
    // Sync LDAP record into our database
    UserEntity entity = ensureRolesFetched(registerOrUpdateUser(record));
    // Update the last login timestamp
    entity.setLastLogin(DateTime.now());
    dao.update(entity);
    return entity;
}
Also used : UserEntity(com.peterphi.usermanager.db.entity.UserEntity) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 15 with UserEntity

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

the class UserDaoImpl method register.

@Transactional
public int register(String name, String email, String password, final String dateFormat, final String timeZone) {
    if (userExists(email))
        throw new IllegalArgumentException("User '" + email + "' already exists!");
    if (password.isEmpty())
        throw new IllegalArgumentException("Must supply a password!");
    final UserEntity account = new UserEntity();
    account.setLocal(true);
    account.setName(name);
    account.setEmail(email);
    account.setPassword(hashPassword(password));
    account.setDateFormat(dateFormat);
    account.setTimeZone(timeZone);
    account.setSessionReconnectKey(UUID.randomUUID().toString());
    return save(account);
}
Also used : UserEntity(com.peterphi.usermanager.db.entity.UserEntity) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Aggregations

UserEntity (com.peterphi.usermanager.db.entity.UserEntity)19 Transactional (com.peterphi.std.guice.database.annotation.Transactional)13 AuthConstraint (com.peterphi.std.guice.common.auth.annotations.AuthConstraint)7 RoleEntity (com.peterphi.usermanager.db.entity.RoleEntity)3 AuthenticationFailureException (com.peterphi.usermanager.guice.authentication.AuthenticationFailureException)3 WebQuery (com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery)2 TemplateCall (com.peterphi.std.guice.web.rest.templating.TemplateCall)2 DateTime (org.joda.time.DateTime)2 OAuthServiceEntity (com.peterphi.usermanager.db.entity.OAuthServiceEntity)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Cookie (javax.servlet.http.Cookie)1 NewCookie (javax.ws.rs.core.NewCookie)1 Response (javax.ws.rs.core.Response)1