use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.
the class RegisterUIServiceImpl method getRegister.
@AuthConstraint(id = "register_service", skip = true, comment = "register page handles own constraints")
@Transactional(readOnly = true)
@Override
public String getRegister() {
if (!allowAnonymousRegistration && !login.isAdmin())
throw new AuthenticationFailureException("Anonymous registration is not enabled. Please log in to create other users");
TemplateCall call = templater.template("register");
call.set("nonce", nonceStore.allocate());
if (login.isAdmin())
// Admin user, role picker will be available
call.set("roles", roleDao.getAll());
else
// Anonymous registration, no role select
call.set("roles", Collections.emptyList());
return call.process();
}
use of com.peterphi.std.guice.database.annotation.Transactional 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();
}
use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.
the class ServiceUIServiceImpl method getList.
@Override
@Transactional(readOnly = true)
public String getList(final UriInfo query) {
final ConstrainedResultSet<OAuthServiceEntity> resultset = dao.findByUriQuery(new WebQuery().decode(query.getQueryParameters()));
final TemplateCall call = templater.template("services");
call.set("nonce", nonceStore.getValue(NONCE_USE));
call.set("resultset", resultset);
call.set("entities", resultset.getList());
return call.process();
}
use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.
the class ServiceUIServiceImpl method get.
@Override
@Transactional(readOnly = true)
public String get(final String id) {
final OAuthServiceEntity entity = dao.getById(id);
if (entity == null)
throw new IllegalArgumentException("No such service with client_id: " + id);
final TemplateCall call = templater.template("service");
call.set("nonce", nonceStore.getValue(NONCE_USE));
call.set("entity", entity);
call.set("localEndpoint", localEndpoint);
return call.process();
}
use of com.peterphi.std.guice.database.annotation.Transactional in project stdlib by petergeneric.
the class ServiceUIServiceImpl method setEndpoints.
@Override
@Transactional
public Response setEndpoints(final String nonce, final String id, final String endpoints) {
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 set endpoints on 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 endpoints of a service!");
entity.setEndpoints(endpoints);
dao.update(entity);
return Response.seeOther(URI.create("/service/" + id)).build();
}
Aggregations