use of com.peterphi.usermanager.db.entity.UserEntity in project stdlib by petergeneric.
the class ImpersonationService method impersonate.
@AuthConstraint(id = "impersonation", role = UserLogin.ROLE_ADMIN, comment = "only admins can impersonate other users")
public String impersonate(final int userId) {
final UserLogin currentUser = userProvider.get();
final UserEntity newUser = authenticationService.getById(userId);
log.info("Admin user " + currentUser.getId() + " (" + currentUser.getEmail() + ") is changing their session to impersonate user " + newUser.getId() + " (" + newUser.getEmail() + ")");
currentUser.reload(newUser);
return newUser.getSessionReconnectKey();
}
use of com.peterphi.usermanager.db.entity.UserEntity in project stdlib by petergeneric.
the class UserLoginProvider method tryBasicAuthLogin.
/**
* Support proactive HTTP BASIC authentication
*
* @param authService
* the user authentication service
* @param request
* the HTTP request
*
* @return a UserLogin for the appropriate user if valid credentials were presented, otherwise null
*/
private UserLogin tryBasicAuthLogin(UserLogin login, UserAuthenticationService authService, HttpServletRequest request) {
final String header = request.getHeader(HttpHeaderNames.AUTHORIZATION);
if (header != null) {
final String[] credentials = BasicAuthHelper.parseHeader(header);
if (credentials != null) {
final String username = credentials[0];
final String password = credentials[1];
final Future<UserEntity> future = asynchService.get().submit(() -> tryLogin(authService, username, password, true));
try {
UserEntity user = LOGIN_TIMEOUT.start().resolveFuture(future, true);
login.reload(user);
} catch (Exception e) {
throw new RuntimeException("Error attempting asynchronous BASIC auth login: " + e.getMessage(), e);
}
}
}
// No authorisation (or unsupported authorisation type)
return null;
}
use of com.peterphi.usermanager.db.entity.UserEntity in project stdlib by petergeneric.
the class UserLoginProvider method tryRelogin.
private UserLogin tryRelogin(final UserLogin login, UserAuthenticationService auth, Cookie[] cookies) {
for (Cookie cookie : cookies) {
if (UserLogin.SESSION_RECONNECT_COOKIE.equals(cookie.getName())) {
final String key = cookie.getValue();
final Future<UserEntity> future = asynchService.get().submit(() -> trySessionReconnectLogin(auth, key));
try {
UserEntity user = LOGIN_TIMEOUT.start().resolveFuture(future, true);
login.reload(user);
} catch (Exception e) {
throw new RuntimeException("Error attempting asynchronous session reconnect auth login: " + e.getMessage(), e);
}
}
}
return null;
}
use of com.peterphi.usermanager.db.entity.UserEntity 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();
}
use of com.peterphi.usermanager.db.entity.UserEntity 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();
}
Aggregations