Search in sources :

Example 1 with UserEntity

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

the class UserDaoImpl method changeProfile.

@Transactional
public UserEntity changeProfile(final int id, final String name, final String email, final String dateFormat, final String timeZone) {
    final UserEntity account = getById(id);
    if (account != null) {
        if (dateFormat == null || timeZone == null)
            throw new IllegalArgumentException("Must specify dateFormat and timeZone!");
        account.setName(name);
        account.setEmail(email);
        account.setDateFormat(dateFormat);
        account.setTimeZone(timeZone);
        update(account);
        return account;
    } else {
        throw new IllegalArgumentException("No such user: " + id);
    }
}
Also used : UserEntity(com.peterphi.usermanager.db.entity.UserEntity) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 2 with UserEntity

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

the class UserDaoImpl method login.

@Transactional
public UserEntity login(String email, String password) {
    final UserEntity account = getUserByEmail(email);
    if (account != null && account.isLocal()) {
        final boolean correct = BCrypt.verify(account.getPassword(), password.toCharArray());
        if (correct) {
            account.setLastLogin(new DateTime());
            update(account);
            return account;
        }
    }
    // User doesn't exist (or password is wrong)
    return null;
}
Also used : UserEntity(com.peterphi.usermanager.db.entity.UserEntity) DateTime(org.joda.time.DateTime) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 3 with UserEntity

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

the class UserDaoImpl method changePassword.

public void changePassword(final int id, final String newPassword) {
    final UserEntity account = getById(id);
    if (account != null) {
        if (!account.isLocal())
            throw new IllegalArgumentException("Cannot change password: user is authenticated by remote service!");
        account.setPassword(hashPassword(newPassword));
        update(account);
    } else {
        throw new IllegalArgumentException("No such user: " + id);
    }
}
Also used : UserEntity(com.peterphi.usermanager.db.entity.UserEntity)

Example 4 with UserEntity

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

the class UserDaoImpl method loginBySessionReconnectKey.

@Transactional
public UserEntity loginBySessionReconnectKey(String key) {
    final UserEntity account = uniqueResult(new WebQuery().eq("local", true).eq("sessionReconnectKey", key));
    if (account != null) {
        log.info("Allowed login by session reconnect key for user: " + account.getEmail());
        account.setLastLogin(new DateTime());
        update(account);
    }
    return account;
}
Also used : WebQuery(com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery) UserEntity(com.peterphi.usermanager.db.entity.UserEntity) DateTime(org.joda.time.DateTime) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 5 with UserEntity

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

the class UserDaoImpl method registerRemote.

@Transactional
public int registerRemote(final String username, final String fullName) {
    if (userExists(username))
        throw new IllegalArgumentException("User '" + username + "' already exists!");
    final UserEntity account = new UserEntity();
    account.setLocal(false);
    account.setEmail(username);
    account.setName(fullName);
    // Won't allow password logins anyway, but we also set a value that won't match any BCrypt hash
    account.setPassword("NONE");
    account.setSessionReconnectKey(null);
    account.setTimeZone(CurrentUser.DEFAULT_TIMEZONE);
    account.setDateFormat(CurrentUser.DEFAULT_DATE_FORMAT_STRING);
    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