Search in sources :

Example 1 with MailPackage

use of org.olat.core.util.mail.MailPackage in project OpenOLAT by OpenOLAT.

the class UserBulkChangeManager method changeSelectedIdentities.

public void changeSelectedIdentities(List<Identity> selIdentities, Map<String, String> attributeChangeMap, Map<String, String> roleChangeMap, List<String> notUpdatedIdentities, boolean isAdministrativeUser, List<Long> ownGroups, List<Long> partGroups, Translator trans, Identity addingIdentity) {
    Translator transWithFallback = userManager.getPropertyHandlerTranslator(trans);
    String usageIdentifyer = UserBulkChangeStep00.class.getCanonicalName();
    notUpdatedIdentities.clear();
    List<Identity> changedIdentities = new ArrayList<>();
    List<UserPropertyHandler> userPropertyHandlers = userManager.getUserPropertyHandlersFor(usageIdentifyer, isAdministrativeUser);
    String[] securityGroups = { Constants.GROUP_USERMANAGERS, Constants.GROUP_GROUPMANAGERS, Constants.GROUP_AUTHORS, Constants.GROUP_ADMIN, Constants.GROUP_POOL_MANAGER, Constants.GROUP_INST_ORES_MANAGER };
    // loop over users to be edited:
    for (Identity identity : selIdentities) {
        // reload identity from cache, to prevent stale object
        identity = securityManager.loadIdentityByKey(identity.getKey());
        User user = identity.getUser();
        String oldEmail = user.getEmail();
        String errorDesc = "";
        boolean updateError = false;
        // change pwd
        if (attributeChangeMap.containsKey(PWD_IDENTIFYER)) {
            String newPwd = attributeChangeMap.get(PWD_IDENTIFYER);
            if (StringHelper.containsNonWhitespace(newPwd)) {
                if (!userManager.syntaxCheckOlatPassword(newPwd)) {
                    errorDesc = transWithFallback.translate("error.password");
                    updateError = true;
                }
            } else {
                newPwd = null;
            }
            olatAuthManager.changePasswordAsAdmin(identity, newPwd);
        }
        // set language
        String userLanguage = user.getPreferences().getLanguage();
        if (attributeChangeMap.containsKey(LANG_IDENTIFYER)) {
            String inputLanguage = attributeChangeMap.get(LANG_IDENTIFYER);
            if (!userLanguage.equals(inputLanguage)) {
                Preferences preferences = user.getPreferences();
                preferences.setLanguage(inputLanguage);
                user.setPreferences(preferences);
            }
        }
        Context vcContext = new VelocityContext();
        // set all properties as context
        setUserContext(identity, vcContext);
        // org.olat.admin.user.bulkChange.UserBulkChangeStep00
        for (int k = 0; k < userPropertyHandlers.size(); k++) {
            UserPropertyHandler propHandler = userPropertyHandlers.get(k);
            String propertyName = propHandler.getName();
            String userValue = identity.getUser().getProperty(propertyName, null);
            String inputFieldValue = "";
            if (attributeChangeMap.containsKey(propertyName)) {
                inputFieldValue = attributeChangeMap.get(propertyName);
                inputFieldValue = inputFieldValue.replace("$", "$!");
                String evaluatedInputFieldValue = evaluateValueWithUserContext(inputFieldValue, vcContext);
                // validate evaluated property-value
                ValidationError validationError = new ValidationError();
                // do validation checks with users current locale!
                Locale locale = transWithFallback.getLocale();
                if (!propHandler.isValidValue(identity.getUser(), evaluatedInputFieldValue, validationError, locale)) {
                    errorDesc = transWithFallback.translate(validationError.getErrorKey(), validationError.getArgs()) + " (" + evaluatedInputFieldValue + ")";
                    updateError = true;
                    break;
                }
                if (!evaluatedInputFieldValue.equals(userValue)) {
                    String stringValue = propHandler.getStringValue(evaluatedInputFieldValue, locale);
                    propHandler.setUserProperty(user, stringValue);
                }
            }
        }
        // loop over securityGroups defined above
        for (String securityGroup : securityGroups) {
            SecurityGroup secGroup = securityManager.findSecurityGroupByName(securityGroup);
            Boolean isInGroup = securityManager.isIdentityInSecurityGroup(identity, secGroup);
            String thisRoleAction = "";
            if (roleChangeMap.containsKey(securityGroup)) {
                thisRoleAction = roleChangeMap.get(securityGroup);
                // user not anymore in security group, remove him
                if (isInGroup && thisRoleAction.equals("remove")) {
                    securityManager.removeIdentityFromSecurityGroup(identity, secGroup);
                    log.audit("User::" + addingIdentity.getName() + " removed system role::" + securityGroup + " from user::" + identity.getName(), null);
                }
                // user not yet in security group, add him
                if (!isInGroup && thisRoleAction.equals("add")) {
                    securityManager.addIdentityToSecurityGroup(identity, secGroup);
                    log.audit("User::" + addingIdentity.getName() + " added system role::" + securityGroup + " to user::" + identity.getName(), null);
                }
            }
        }
        // set status
        if (roleChangeMap.containsKey("Status")) {
            Integer status = Integer.parseInt(roleChangeMap.get("Status"));
            int oldStatus = identity.getStatus();
            String oldStatusText = (oldStatus == Identity.STATUS_PERMANENT ? "permanent" : (oldStatus == Identity.STATUS_ACTIV ? "active" : (oldStatus == Identity.STATUS_LOGIN_DENIED ? "login_denied" : (oldStatus == Identity.STATUS_DELETED ? "deleted" : "unknown"))));
            String newStatusText = (status == Identity.STATUS_PERMANENT ? "permanent" : (status == Identity.STATUS_ACTIV ? "active" : (status == Identity.STATUS_LOGIN_DENIED ? "login_denied" : (status == Identity.STATUS_DELETED ? "deleted" : "unknown"))));
            if (oldStatus != status && status == Identity.STATUS_LOGIN_DENIED && Boolean.parseBoolean(roleChangeMap.get("sendLoginDeniedEmail"))) {
                sendLoginDeniedEmail(identity);
            }
            identity = securityManager.saveIdentityStatus(identity, status);
            log.audit("User::" + addingIdentity.getName() + " changed accout status for user::" + identity.getName() + " from::" + oldStatusText + " to::" + newStatusText, null);
        }
        // persist changes:
        if (updateError) {
            String errorOutput = identity.getName() + ": " + errorDesc;
            log.debug("error during bulkChange of users, following user could not be updated: " + errorOutput);
            notUpdatedIdentities.add(errorOutput);
        } else {
            userManager.updateUserFromIdentity(identity);
            securityManager.deleteInvalidAuthenticationsByEmail(oldEmail);
            changedIdentities.add(identity);
            log.audit("User::" + addingIdentity.getName() + " successfully changed account data for user::" + identity.getName() + " in bulk change", null);
        }
        // commit changes for this user
        dbInstance.commit();
    }
    // FXOLAT-101: add identity to new groups:
    if (ownGroups.size() != 0 || partGroups.size() != 0) {
        List<BusinessGroupMembershipChange> changes = new ArrayList<BusinessGroupMembershipChange>();
        for (Identity selIdentity : selIdentities) {
            if (ownGroups != null && !ownGroups.isEmpty()) {
                for (Long tutorGroupKey : ownGroups) {
                    BusinessGroupMembershipChange change = new BusinessGroupMembershipChange(selIdentity, tutorGroupKey);
                    change.setTutor(Boolean.TRUE);
                    changes.add(change);
                }
            }
            if (partGroups != null && !partGroups.isEmpty()) {
                for (Long partGroupKey : partGroups) {
                    BusinessGroupMembershipChange change = new BusinessGroupMembershipChange(selIdentity, partGroupKey);
                    change.setParticipant(Boolean.TRUE);
                    changes.add(change);
                }
            }
        }
        MailPackage mailing = new MailPackage();
        businessGroupService.updateMemberships(addingIdentity, changes, mailing);
        dbInstance.commit();
    }
}
Also used : Context(org.apache.velocity.context.Context) VelocityContext(org.apache.velocity.VelocityContext) Locale(java.util.Locale) User(org.olat.core.id.User) MailPackage(org.olat.core.util.mail.MailPackage) VelocityContext(org.apache.velocity.VelocityContext) ArrayList(java.util.ArrayList) SecurityGroup(org.olat.basesecurity.SecurityGroup) BusinessGroupMembershipChange(org.olat.group.model.BusinessGroupMembershipChange) Translator(org.olat.core.gui.translator.Translator) ValidationError(org.olat.core.gui.components.form.ValidationError) Identity(org.olat.core.id.Identity) Preferences(org.olat.core.id.Preferences) UserPropertyHandler(org.olat.user.propertyhandlers.UserPropertyHandler)

Example 2 with MailPackage

use of org.olat.core.util.mail.MailPackage in project OpenOLAT by OpenOLAT.

the class UserImportController method processGroupAdditionForAllIdents.

private void processGroupAdditionForAllIdents(List<Identity> allIdents, List<Long> tutorGroups, List<Long> partGroups, boolean sendmail) {
    Collection<Identity> identities = getIdentities(allIdents);
    List<BusinessGroupMembershipChange> changes = new ArrayList<BusinessGroupMembershipChange>();
    for (Identity identity : identities) {
        if (tutorGroups != null && !tutorGroups.isEmpty()) {
            for (Long tutorGroupKey : tutorGroups) {
                BusinessGroupMembershipChange change = new BusinessGroupMembershipChange(identity, tutorGroupKey);
                change.setTutor(Boolean.TRUE);
                changes.add(change);
            }
        }
        if (partGroups != null && !partGroups.isEmpty()) {
            for (Long partGroupKey : partGroups) {
                BusinessGroupMembershipChange change = new BusinessGroupMembershipChange(identity, partGroupKey);
                change.setParticipant(Boolean.TRUE);
                changes.add(change);
            }
        }
    }
    MailPackage mailing = new MailPackage(sendmail);
    businessGroupService.updateMemberships(getIdentity(), changes, mailing);
    DBFactory.getInstance().commit();
}
Also used : BusinessGroupMembershipChange(org.olat.group.model.BusinessGroupMembershipChange) MailPackage(org.olat.core.util.mail.MailPackage) ArrayList(java.util.ArrayList) Identity(org.olat.core.id.Identity)

Example 3 with MailPackage

use of org.olat.core.util.mail.MailPackage in project OpenOLAT by OpenOLAT.

the class CourseOverviewController method doChangePermission.

private void doChangePermission(UserRequest ureq, MemberPermissionChangeEvent e, RepositoryEntry re, boolean sendMail) {
    MailPackage mailing = new MailPackage(sendMail);
    if (re != null) {
        List<RepositoryEntryPermissionChangeEvent> changes = Collections.singletonList((RepositoryEntryPermissionChangeEvent) e);
        repositoryManager.updateRepositoryEntryMemberships(getIdentity(), ureq.getUserSession().getRoles(), re, changes, mailing);
    }
    businessGroupService.updateMemberships(getIdentity(), e.getGroupChanges(), mailing);
    // make sure all is committed before loading the model again (I see issues without)
    DBFactory.getInstance().commitAndCloseSession();
    updateModel();
}
Also used : MailPackage(org.olat.core.util.mail.MailPackage) RepositoryEntryPermissionChangeEvent(org.olat.repository.model.RepositoryEntryPermissionChangeEvent)

Example 4 with MailPackage

use of org.olat.core.util.mail.MailPackage in project OpenOLAT by OpenOLAT.

the class CourseOverviewController method removeUserFromCourse.

private void removeUserFromCourse(UserRequest ureq, List<RepositoryEntry> repoEntriesToLeave, List<BusinessGroup> groupsToLeave, List<BusinessGroup> groupsToDelete, boolean doSendMail) {
    List<Identity> membersToRemove = Collections.singletonList(editedIdentity);
    for (BusinessGroup group : groupsToLeave) {
        if (groupsToDelete.contains(group)) {
            // really delete the group as it has no more owners/participants
            if (doSendMail) {
                String businessPath = getWindowControl().getBusinessControl().getAsString();
                businessGroupService.deleteBusinessGroupWithMail(group, businessPath, getIdentity(), getLocale());
            } else {
                businessGroupService.deleteBusinessGroup(group);
            }
        } else {
            // 1) remove as owner
            if (businessGroupService.hasRoles(editedIdentity, group, GroupRoles.coach.name())) {
                businessGroupService.removeOwners(ureq.getIdentity(), membersToRemove, group);
            }
            MailPackage mailing = new MailPackage(doSendMail);
            // 2) remove as participant
            businessGroupService.removeParticipants(getIdentity(), membersToRemove, group, mailing);
            MailHelper.printErrorsAndWarnings(mailing.getResult(), getWindowControl(), ureq.getUserSession().getRoles().isOLATAdmin(), getLocale());
        }
    }
    for (RepositoryEntry repoEntry : repoEntriesToLeave) {
        if (isRepoMember(repoEntry)) {
            MailPackage mailing = new MailPackage(doSendMail);
            repositoryManager.removeMembers(getIdentity(), membersToRemove, repoEntry, mailing);
        }
    }
    updateModel();
    StringBuilder groupNames = new StringBuilder();
    for (BusinessGroup group : groupsToLeave) {
        if (groupNames.length() > 0)
            groupNames.append(", ");
        groupNames.append(group.getName());
    }
    showInfo("unsubscribe.successful");
}
Also used : MailPackage(org.olat.core.util.mail.MailPackage) BusinessGroup(org.olat.group.BusinessGroup) RepositoryEntry(org.olat.repository.RepositoryEntry) Identity(org.olat.core.id.Identity)

Example 5 with MailPackage

use of org.olat.core.util.mail.MailPackage in project OpenOLAT by OpenOLAT.

the class GroupOverviewController method doAddToGroups.

private void doAddToGroups(AddToGroupsEvent e, boolean sendMail) {
    List<BusinessGroupMembershipChange> changes = new ArrayList<BusinessGroupMembershipChange>();
    if (e.getOwnerGroupKeys() != null && !e.getOwnerGroupKeys().isEmpty()) {
        for (Long tutorGroupKey : e.getOwnerGroupKeys()) {
            BusinessGroupMembershipChange change = new BusinessGroupMembershipChange(identity, tutorGroupKey);
            change.setTutor(Boolean.TRUE);
            changes.add(change);
        }
    }
    if (e.getParticipantGroupKeys() != null && !e.getParticipantGroupKeys().isEmpty()) {
        for (Long partGroupKey : e.getParticipantGroupKeys()) {
            BusinessGroupMembershipChange change = new BusinessGroupMembershipChange(identity, partGroupKey);
            change.setParticipant(Boolean.TRUE);
            changes.add(change);
        }
    }
    MailPackage mailing = new MailPackage(sendMail);
    businessGroupService.updateMemberships(getIdentity(), changes, mailing);
}
Also used : BusinessGroupMembershipChange(org.olat.group.model.BusinessGroupMembershipChange) MailPackage(org.olat.core.util.mail.MailPackage) ArrayList(java.util.ArrayList)

Aggregations

MailPackage (org.olat.core.util.mail.MailPackage)84 Identity (org.olat.core.id.Identity)58 RepositoryEntry (org.olat.repository.RepositoryEntry)42 Path (javax.ws.rs.Path)26 RestSecurityHelper.getIdentity (org.olat.restapi.security.RestSecurityHelper.getIdentity)26 IdentitiesAddEvent (org.olat.admin.securitygroup.gui.IdentitiesAddEvent)22 PUT (javax.ws.rs.PUT)16 Test (org.junit.Test)16 UserRequest (org.olat.core.gui.UserRequest)16 BusinessGroup (org.olat.group.BusinessGroup)16 BusinessGroupMembershipChange (org.olat.group.model.BusinessGroupMembershipChange)16 RepositoryManager (org.olat.repository.RepositoryManager)16 RestSecurityHelper.getUserRequest (org.olat.restapi.security.RestSecurityHelper.getUserRequest)16 BaseSecurity (org.olat.basesecurity.BaseSecurity)14 ArrayList (java.util.ArrayList)12 MailerResult (org.olat.core.util.mail.MailerResult)12 RepositoryEntryPermissionChangeEvent (org.olat.repository.model.RepositoryEntryPermissionChangeEvent)12 DELETE (javax.ws.rs.DELETE)10 Consumes (javax.ws.rs.Consumes)8 MailTemplate (org.olat.core.util.mail.MailTemplate)8