use of io.datarouter.web.user.session.service.Role in project datarouter by hotpads.
the class AdminEditUserHandler method getEditUserDetailsDto.
// TODO DATAROUTER-2788
private EditUserDetailsDto getEditUserDetailsDto(String username) {
SessionBasedUser user = userInfo.getUserByUsername(username, false).orElseThrow();
Set<Role> roles = userInfo.getRolesByUsername(username, false);
List<PermissionRequestDto> permissionRequests = datarouterPermissionRequestDao.scanPermissionRequestsForUser(user.getId()).listTo(requests -> Scanner.of(datarouterUserHistoryService.getResolvedRequestToHistoryChangesMap(requests).entrySet())).sort(Comparator.comparing(Entry::getKey, DatarouterPermissionRequest.REVERSE_CHRONOLOGICAL_COMPARATOR)).map(this::buildPermissionRequestDto).list();
return new EditUserDetailsDto(user.getUsername(), user.getId().toString(), user.getToken(), permissionRequests, deprovisionedUserDao.find(new DeprovisionedUserKey(username)).map(DeprovisionedUser::toDto).orElseGet(() -> buildDeprovisionedUserDto(user, roles)), roleManager.getConferrableRoles(getSessionInfo().getRoles()), roles, datarouterAccountUserService.getAllAccountNamesWithUserMappingsEnabled(), datarouterAccountUserService.findAccountNamesForUser(user), true, "", // zoneId can be configured through the UI, fallback to system default
user.getZoneId().map(ZoneId::getId).orElse(ZoneId.systemDefault().getId()));
}
use of io.datarouter.web.user.session.service.Role in project datarouter by hotpads.
the class AdminEditUserHandler method updateUserDetails.
// TODO DATAROUTER-2759 make this work without DatarouterUser
@Handler
private EditUserDetailsDto updateUserDetails(@RequestBody EditUserDetailsDto dto) {
if (dto == null || StringTool.isNullOrEmptyOrWhitespace(dto.username) || dto.currentAccounts == null || dto.currentRoles == null) {
return new EditUserDetailsDto("Invalid request.");
}
DatarouterUser currentUser = getCurrentUser();
DatarouterUser userToEdit = datarouterUserDao.getByUsername(new DatarouterUserByUsernameLookup(dto.username));
if (!userToEdit.isEnabled()) {
return new EditUserDetailsDto("This user is not editable.");
}
if (!checkEditPermission(currentUser, userToEdit, datarouterUserService::canEditUser)) {
return null;
}
Set<Role> requestedUserRoles = Scanner.of(dto.currentRoles.entrySet()).include(Entry::getValue).map(Entry::getKey).map(roleManager::getRoleFromPersistentString).collect(HashSet::new);
Set<DatarouterAccountKey> requestedAccounts = Scanner.of(dto.currentAccounts.entrySet()).include(Entry::getValue).map(Entry::getKey).map(DatarouterAccountKey::new).collect(HashSet::new);
datarouterUserEditService.editUser(userToEdit, currentUser, requestedUserRoles, null, getSigninUrl(), requestedAccounts, Optional.ofNullable(dto.currentZoneId).map(ZoneId::of), Optional.empty());
return getEditUserDetailsDto(dto.username);
}
use of io.datarouter.web.user.session.service.Role in project datarouter by hotpads.
the class SamlService method createAndSetSession.
private Session createAndSetSession(HttpServletRequest request, HttpServletResponse response, Assertion assertion) {
String username = assertion.getSubject().getNameID().getValue();
Set<Role> roles = determineRoles(assertion, username, samlSettings.getAttributeToRoleGroupIdMap());
Session session = userSessionService.signInUserWithCreateIfNecessary(request, username, roles, "SAML User");
userSessionService.setSessionCookies(response, session);
return session;
}
use of io.datarouter.web.user.session.service.Role in project datarouter by hotpads.
the class AdminEditUserHandler method createUserSubmit.
// TODO DATAROUTER-2786
@Handler
private Mav createUserSubmit() {
if (serverTypeDetector.mightBeProduction()) {
return pageFactory.message(request, "This is not supported on production");
}
DatarouterUser currentUser = getCurrentUser();
if (!roleManager.isAdmin(currentUser.getRoles())) {
handleInvalidRequest();
}
String username = params.required(authenticationConfig.getUsernameParam());
String password = params.required(authenticationConfig.getPasswordParam());
String[] roleStrings = params.optionalArray(authenticationConfig.getUserRolesParam()).orElse(EmptyArray.STRING);
Set<Role> requestedRoles = Arrays.stream(roleStrings).map(roleManager::getRoleFromPersistentString).collect(Collectors.toSet());
boolean enabled = params.optionalBoolean(authenticationConfig.getEnabledParam(), true);
datarouterUserCreationService.createManualUser(currentUser, username, password, requestedRoles, enabled, Optional.empty(), Optional.empty());
return new InContextRedirectMav(request, paths.admin.viewUsers);
}
use of io.datarouter.web.user.session.service.Role in project datarouter by hotpads.
the class AdminEditUserHandler method copyUser.
@Handler
private EditUserDetailsDto copyUser(String oldUsername, String newUsername) {
if (StringTool.isNullOrEmptyOrWhitespace(oldUsername) || StringTool.isNullOrEmptyOrWhitespace(newUsername)) {
return new EditUserDetailsDto("Invalid request.");
}
DatarouterUser editor = getCurrentUser();
DatarouterUser oldUser = datarouterUserDao.getByUsername(new DatarouterUserByUsernameLookup(oldUsername));
if (editor.getUsername().equals(oldUser.getUsername())) {
return new EditUserDetailsDto("Cannot copy yourself.");
}
if (!datarouterUserService.canEditUser(editor, oldUser)) {
return new EditUserDetailsDto("Cannot copy user.");
}
Set<Role> requestedRoles;
if (oldUser.isEnabled()) {
requestedRoles = new HashSet<>(oldUser.getRoles());
} else {
// copy roles from deprovisioned user info, if present
requestedRoles = deprovisionedUserDao.find(new DeprovisionedUserKey(oldUsername)).map(DeprovisionedUser::getRoles).orElseGet(HashSet::new);
}
Set<DatarouterAccountKey> requestedAccounts = Scanner.of(datarouterAccountUserService.findAccountNamesForUser(oldUser)).map(DatarouterAccountKey::new).collect(Collectors.toCollection(HashSet::new));
Optional<ZoneId> zoneId = oldUser.getZoneId();
// if newUser exists, do an "edit"; else do a "create" then "edit" (since accounts are not set in "create")
DatarouterUser newUser = datarouterUserDao.getByUsername(new DatarouterUserByUsernameLookup(newUsername));
var description = Optional.of("User copied from " + oldUsername + " by " + editor.getUsername());
if (newUser == null) {
newUser = datarouterUserCreationService.createManualUser(editor, newUsername, null, requestedRoles, true, zoneId, description);
} else {
// preserve existing roles and accounts that are not present on the source user of the copy
requestedRoles.addAll(newUser.getRoles());
Scanner.of(datarouterAccountUserService.findAccountNamesForUser(newUser)).map(DatarouterAccountKey::new).forEach(requestedAccounts::add);
}
var signinUrl = getSigninUrl();
datarouterUserEditService.editUser(newUser, editor, requestedRoles, true, signinUrl, requestedAccounts, zoneId, description);
// add history to user that was copied from
datarouterUserHistoryService.recordMessage(oldUser, editor, "User copied to " + newUsername + " by " + editor.getUsername());
copyUserListener.onCopiedUser(oldUsername, newUsername);
return getEditUserDetailsDto(oldUsername);
}
Aggregations