Search in sources :

Example 1 with DatarouterUser

use of io.datarouter.web.user.databean.DatarouterUser in project datarouter by hotpads.

the class DatarouterAuthConfigScanner method checkForDefaultUserId.

public ConfigScanDto checkForDefaultUserId() {
    Long defaultAdminId = DatarouterUserCreationService.ADMIN_ID;
    Optional<DatarouterUser> defaultUser = datarouterUserDao.find(new DatarouterUserKey(defaultAdminId));
    if (defaultUser.isEmpty()) {
        return ConfigScanResponseTool.buildEmptyResponse();
    }
    String userName = defaultUser.get().getUsername();
    return ConfigScanResponseTool.buildResponse("Found a user with the default admin id=" + defaultAdminId + " and username=" + userName);
}
Also used : DatarouterUser(io.datarouter.web.user.databean.DatarouterUser) DatarouterUserKey(io.datarouter.web.user.databean.DatarouterUserKey)

Example 2 with DatarouterUser

use of io.datarouter.web.user.databean.DatarouterUser in project datarouter by hotpads.

the class DatarouterUserEditService method handleAccountChanges.

private Optional<String> handleAccountChanges(DatarouterUser user, Set<DatarouterAccountKey> requestedAccounts) {
    Set<DatarouterUserAccountMapKey> currentAccounts = datarouterUserAccountMapDao.scanKeysWithPrefix(new DatarouterUserAccountMapKey(user.getId(), null)).collect(HashSet::new);
    Set<DatarouterUserAccountMapKey> accountsToDelete = currentAccounts.stream().filter(currentAccountKey -> !requestedAccounts.contains(currentAccountKey.getDatarouterAccountKey())).collect(Collectors.toSet());
    Set<DatarouterUserAccountMap> accountsToAdd = requestedAccounts.stream().map(accountKey -> new DatarouterUserAccountMap(user.getId(), accountKey.getAccountName())).filter(requestedAccount -> !currentAccounts.contains(requestedAccount.getKey())).collect(Collectors.toSet());
    if (!accountsToDelete.isEmpty() || !accountsToAdd.isEmpty()) {
        if (!accountsToDelete.isEmpty()) {
            datarouterUserAccountMapDao.deleteMulti(accountsToDelete);
        }
        if (!accountsToAdd.isEmpty()) {
            datarouterUserAccountMapDao.putMulti(accountsToAdd);
        }
        String original = currentAccounts.stream().map(DatarouterUserAccountMapKey::getDatarouterAccountKey).map(DatarouterAccountKey::getAccountName).sorted(String.CASE_INSENSITIVE_ORDER).collect(Collectors.joining(","));
        String current = requestedAccounts.stream().map(DatarouterAccountKey::getAccountName).sorted(String.CASE_INSENSITIVE_ORDER).collect(Collectors.joining(","));
        return Optional.of(change("accounts", original, current));
    }
    return Optional.empty();
}
Also used : DatarouterAccountKey(io.datarouter.auth.storage.account.DatarouterAccountKey) Scanner(io.datarouter.scanner.Scanner) LoggerFactory(org.slf4j.LoggerFactory) Singleton(javax.inject.Singleton) ServiceName(io.datarouter.web.config.service.ServiceName) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) DatarouterUser(io.datarouter.web.user.databean.DatarouterUser) DatarouterUserRole(io.datarouter.web.user.role.DatarouterUserRole) BaseDatarouterUserAccountMapDao(io.datarouter.auth.storage.useraccountmap.BaseDatarouterUserAccountMapDao) DatarouterUserAccountMap(io.datarouter.auth.storage.useraccountmap.DatarouterUserAccountMap) Logger(org.slf4j.Logger) DatarouterSessionDao(io.datarouter.web.user.DatarouterSessionDao) Set(java.util.Set) DatarouterUserAccountMapKey(io.datarouter.auth.storage.useraccountmap.DatarouterUserAccountMapKey) PasswordTool(io.datarouter.web.util.PasswordTool) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) DatarouterSession(io.datarouter.web.user.session.DatarouterSession) List(java.util.List) EnvironmentName(io.datarouter.storage.config.properties.EnvironmentName) Role(io.datarouter.web.user.session.service.Role) BooleanTool(io.datarouter.util.BooleanTool) Optional(java.util.Optional) DatarouterAccountKey(io.datarouter.auth.storage.account.DatarouterAccountKey) DatarouterUserAccountMapKey(io.datarouter.auth.storage.useraccountmap.DatarouterUserAccountMapKey) DatarouterUserAccountMap(io.datarouter.auth.storage.useraccountmap.DatarouterUserAccountMap) HashSet(java.util.HashSet)

Example 3 with DatarouterUser

use of io.datarouter.web.user.databean.DatarouterUser in project datarouter by hotpads.

the class DatarouterUserHistoryService method sendRoleEditEmail.

private void sendRoleEditEmail(DatarouterUser user, DatarouterUserHistory history, String signInUrl) {
    DatarouterUser editor = datarouterUserService.getUserById(history.getEditor());
    var p1 = p(String.format("%s permissions have been edited by %s", user.getUsername(), editor.getUsername()));
    var p2 = p("Changes: " + history.getChanges());
    var content = div(p1, p2, makeSignInParagraph(signInUrl));
    var emailBuilder = htmlEmailService.startEmailBuilder().withSubject(userEditService.getPermissionRequestEmailSubject(user)).withTitle("Permissions Changed").withTitleHref(signInUrl).withContent(content).from(user.getUsername()).to(user.getUsername()).to(editor.getUsername()).to(permissionRequestEmailType, serverTypeDetector.mightBeProduction()).toSubscribers(serverTypeDetector.mightBeProduction()).toAdmin(serverTypeDetector.mightBeDevelopment());
    htmlEmailService.trySendJ2Html(emailBuilder);
}
Also used : DatarouterUser(io.datarouter.web.user.databean.DatarouterUser)

Example 4 with DatarouterUser

use of io.datarouter.web.user.databean.DatarouterUser in project datarouter by hotpads.

the class DatarouterUserService method assertUserDoesNotExist.

public void assertUserDoesNotExist(Long id, String userToken, String username) {
    DatarouterUser userWithId = getUserById(id);
    if (userWithId != null) {
        throw new IllegalArgumentException("DatarouterUser already exists with id=" + id);
    }
    DatarouterUser userWithUserToken = nodes.getByUserToken(new DatarouterUserByUserTokenLookup(userToken));
    if (userWithUserToken != null) {
        throw new IllegalArgumentException("DatarouterUser already exists with userToken=" + userToken);
    }
    DatarouterUser userWithEmail = nodes.getByUsername(new DatarouterUserByUsernameLookup(username));
    if (userWithEmail != null) {
        throw new IllegalArgumentException("DatarouterUser already exists with username=" + username);
    }
}
Also used : DatarouterUser(io.datarouter.web.user.databean.DatarouterUser) DatarouterUserByUsernameLookup(io.datarouter.web.user.databean.DatarouterUser.DatarouterUserByUsernameLookup) DatarouterUserByUserTokenLookup(io.datarouter.web.user.databean.DatarouterUser.DatarouterUserByUserTokenLookup)

Example 5 with DatarouterUser

use of io.datarouter.web.user.databean.DatarouterUser 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);
}
Also used : Role(io.datarouter.web.user.session.service.Role) DatarouterAccountKey(io.datarouter.auth.storage.account.DatarouterAccountKey) Entry(java.util.Map.Entry) DatarouterUser(io.datarouter.web.user.databean.DatarouterUser) DatarouterUserByUsernameLookup(io.datarouter.web.user.databean.DatarouterUser.DatarouterUserByUsernameLookup) HashSet(java.util.HashSet) BaseHandler(io.datarouter.web.handler.BaseHandler)

Aggregations

DatarouterUser (io.datarouter.web.user.databean.DatarouterUser)19 BaseHandler (io.datarouter.web.handler.BaseHandler)9 DatarouterUserByUsernameLookup (io.datarouter.web.user.databean.DatarouterUser.DatarouterUserByUsernameLookup)5 DatarouterPermissionRequest (io.datarouter.auth.storage.permissionrequest.DatarouterPermissionRequest)4 InContextRedirectMav (io.datarouter.web.handler.mav.imp.InContextRedirectMav)4 Role (io.datarouter.web.user.session.service.Role)4 Optional (java.util.Optional)4 Inject (javax.inject.Inject)4 DatarouterAccountKey (io.datarouter.auth.storage.account.DatarouterAccountKey)3 DatarouterPermissionRequestDao (io.datarouter.auth.storage.permissionrequest.DatarouterPermissionRequestDao)3 PermissionRequestEmailType (io.datarouter.email.type.DatarouterEmailTypes.PermissionRequestEmailType)3 Scanner (io.datarouter.scanner.Scanner)3 AdminEmail (io.datarouter.storage.config.properties.AdminEmail)3 ServerTypeDetector (io.datarouter.storage.servertype.ServerTypeDetector)3 DatarouterHtmlEmailService (io.datarouter.web.email.DatarouterHtmlEmailService)3 MessageMav (io.datarouter.web.handler.mav.imp.MessageMav)3 HashSet (java.util.HashSet)3 DeprovisionedUser (io.datarouter.auth.storage.deprovisioneduser.DeprovisionedUser)2 DatarouterUserDao (io.datarouter.auth.storage.user.DatarouterUserDao)2 DatarouterUserHistory (io.datarouter.auth.storage.userhistory.DatarouterUserHistory)2