Search in sources :

Example 16 with Transactional

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

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

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

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

the class ServiceUIServiceImpl method disable.

@Override
@Transactional
public Response disable(final String id, final String nonce) {
    nonceStore.validate(NONCE_USE, nonce);
    final OAuthServiceEntity entity = dao.getById(id);
    if (entity == null)
        throw new IllegalArgumentException("No such service with client_id: " + id);
    else if (!entity.isEnabled())
        throw new IllegalArgumentException("Cannot disable an already-disabled service: " + id);
    else if (entity.getOwner().getId() != userProvider.get().getId() && !userProvider.get().isAdmin())
        throw new IllegalArgumentException("Only the owner or an admin can change a service!");
    entity.setEnabled(false);
    dao.update(entity);
    return Response.seeOther(URI.create("/service/" + id)).build();
}
Also used : OAuthServiceEntity(com.peterphi.usermanager.db.entity.OAuthServiceEntity) Transactional(com.peterphi.std.guice.database.annotation.Transactional)

Example 20 with Transactional

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

the class ServiceUIServiceImpl method create.

@Override
@Transactional
public Response create(final String nonce, final String name, final String endpoints) {
    nonceStore.validate(NONCE_USE, nonce);
    final int userId = userProvider.get().getId();
    final UserEntity user = userDao.getById(userId);
    OAuthServiceEntity entity = new OAuthServiceEntity();
    entity.setOwner(user);
    entity.setName(name);
    entity.setEndpoints(StringUtils.trimToNull(endpoints));
    entity.setEnabled(true);
    dao.save(entity);
    return Response.seeOther(URI.create("/service/" + entity.getId())).build();
}
Also used : OAuthServiceEntity(com.peterphi.usermanager.db.entity.OAuthServiceEntity) AuthConstraint(com.peterphi.std.guice.common.auth.annotations.AuthConstraint) UserEntity(com.peterphi.usermanager.db.entity.UserEntity) 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