Search in sources :

Example 21 with Transactional

use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.

the class UserUIServiceImpl method changePassword.

@Override
@Transactional
public Response changePassword(final int userId, final String nonce, final String newPassword, final String newPasswordConfirm) {
    nonceStore.validate(NONCE_USE, nonce);
    final int localUser = login.getId();
    if (localUser != userId && !login.isAdmin())
        throw new AuthenticationFailureException("Only a User Admin can change the password of another user!");
    if (newPassword == null || newPasswordConfirm == null)
        throw new IllegalArgumentException("Passwords do not match (or no password supplied)");
    if (!newPassword.equals(newPasswordConfirm))
        throw new IllegalArgumentException("Passwords do not match!");
    if (newPassword.length() == 0)
        throw new IllegalArgumentException("No password supplied!");
    accountDao.changePassword(userId, newPassword);
    // Redirect back to the user page
    return Response.seeOther(URI.create("/user/" + userId)).build();
}
Also used : AuthenticationFailureException(com.peterphi.usermanager.guice.authentication.AuthenticationFailureException) AuthConstraint(com.peterphi.std.guice.common.auth.annotations.AuthConstraint) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 22 with Transactional

use of com.peterphi.std.guice.database.annotation.Transactional 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 23 with Transactional

use of com.peterphi.std.guice.database.annotation.Transactional 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 24 with Transactional

use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.

the class UserUIServiceImpl method deleteUser.

@Override
@Transactional
@AuthConstraint(role = UserLogin.ROLE_ADMIN)
public Response deleteUser(final int userId, final String nonce) {
    nonceStore.validate(NONCE_USE, nonce);
    final int localUser = login.getId();
    accountDao.deleteById(userId);
    if (localUser == userId) {
        // Invalidate the current session
        login.clear();
        return Response.seeOther(URI.create("/logout")).build();
    } else {
        // Redirect back to the user list page
        return Response.seeOther(URI.create("/users")).build();
    }
}
Also used : AuthConstraint(com.peterphi.std.guice.common.auth.annotations.AuthConstraint) AuthConstraint(com.peterphi.std.guice.common.auth.annotations.AuthConstraint) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 25 with Transactional

use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.

the class ResourceProvisionService method getOrCreateTemplate.

@Transactional
public ResourceTemplateEntity getOrCreateTemplate(final String name) {
    final String revision = templateConfig.config.getLastRevision();
    ResourceTemplateEntity entity = templateDao.getById(name);
    if (entity != null) {
        // Check if the template has been updated
        if (!StringUtils.equals(entity.getLatestRevision(), revision)) {
            entity.setLatestRevision(revision);
            entity.setRevisions(entity.getRevisions() + 1);
            templateDao.update(entity);
        }
        return entity;
    } else {
        entity = new ResourceTemplateEntity();
        entity.setId(name);
        entity.setLatestRevision(revision);
        entity.setRevisions(1);
        templateDao.save(entity);
        return templateDao.getById(name);
    }
}
Also used : ResourceTemplateEntity(com.peterphi.servicemanager.service.db.entity.ResourceTemplateEntity) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Aggregations

Transactional (com.peterphi.std.guice.database.annotation.Transactional)46 UserEntity (com.peterphi.usermanager.db.entity.UserEntity)13 WebQuery (com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery)11 TemplateCall (com.peterphi.std.guice.web.rest.templating.TemplateCall)9 RoleEntity (com.peterphi.usermanager.db.entity.RoleEntity)9 AuthConstraint (com.peterphi.std.guice.common.auth.annotations.AuthConstraint)8 OAuthServiceEntity (com.peterphi.usermanager.db.entity.OAuthServiceEntity)5 AuthenticationFailureException (com.peterphi.usermanager.guice.authentication.AuthenticationFailureException)5 OAuthSessionEntity (com.peterphi.usermanager.db.entity.OAuthSessionEntity)4 Test (org.junit.Test)4 ResourceInstanceEntity (com.peterphi.servicemanager.service.db.entity.ResourceInstanceEntity)3 ResourceTemplateEntity (com.peterphi.servicemanager.service.db.entity.ResourceTemplateEntity)3 Criteria (org.hibernate.Criteria)3 List (java.util.List)2 DateTime (org.joda.time.DateTime)2 Timer (com.codahale.metrics.Timer)1 Inject (com.google.inject.Inject)1 Singleton (com.google.inject.Singleton)1 ServiceInstanceEntity (com.peterphi.servicemanager.service.db.entity.ServiceInstanceEntity)1 ResourceNetworkConfig (com.peterphi.servicemanager.service.guice.ResourceNetworkConfig)1