use of io.datarouter.auth.storage.deprovisioneduser.DeprovisionedUserKey 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.auth.storage.deprovisioneduser.DeprovisionedUserKey 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