Search in sources :

Example 16 with TemporaryKey

use of org.olat.registration.TemporaryKey in project openolat by klemens.

the class UserDeletionManager method deleteIdentity.

/**
 * Delete all user-data in registered deleteable resources.
 * @param identity
 * @return true
 */
public void deleteIdentity(Identity identity) {
    logInfo("Start deleteIdentity for identity=" + identity);
    String newName = getBackupStringWithDate(identity.getName());
    logInfo("Start Deleting user=" + identity);
    File archiveFilePath = getArchivFilePath(identity);
    Map<String, UserDataDeletable> userDataDeletableResourcesMap = CoreSpringFactory.getBeansOfType(UserDataDeletable.class);
    List<UserDataDeletable> userDataDeletableResources = new ArrayList<>(userDataDeletableResourcesMap.values());
    Collections.sort(userDataDeletableResources, new UserDataDeletableComparator());
    for (UserDataDeletable element : userDataDeletableResources) {
        logInfo("UserDataDeletable-Loop element=" + element);
        element.deleteUserData(identity, newName, archiveFilePath);
    }
    // Delete all authentications for certain identity
    List<Authentication> authentications = securityManager.getAuthentications(identity);
    for (Authentication auth : authentications) {
        logInfo("deleteAuthentication auth=" + auth);
        securityManager.deleteAuthentication(auth);
        logDebug("Delete auth=" + auth + "  of identity=" + identity);
    }
    // remove identity from its security groups
    List<SecurityGroup> securityGroups = securityManager.getSecurityGroupsForIdentity(identity);
    for (SecurityGroup secGroup : securityGroups) {
        securityManager.removeIdentityFromSecurityGroup(identity, secGroup);
        logInfo("Removing user=" + identity + " from security group=" + secGroup.toString());
    }
    // remove identity from groups
    groupDao.removeMemberships(identity);
    String key = identity.getUser().getProperty("emchangeKey", null);
    TemporaryKey tempKey = registrationManager.loadTemporaryKeyByRegistrationKey(key);
    if (tempKey != null) {
        registrationManager.deleteTemporaryKey(tempKey);
    }
    identity = securityManager.loadIdentityByKey(identity.getKey());
    // keep login-name only -> change email
    User persistedUser = identity.getUser();
    List<UserPropertyHandler> userPropertyHandlers = UserManager.getInstance().getAllUserPropertyHandlers();
    for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
        String actualProperty = userPropertyHandler.getName();
        if (userPropertyHandler.isDeletable() && !(keepUserEmailAfterDeletion && UserConstants.EMAIL.equals(actualProperty))) {
            persistedUser.setProperty(actualProperty, null);
        }
        if ((!keepUserEmailAfterDeletion && UserConstants.EMAIL.equals(actualProperty))) {
            String oldEmail = userPropertyHandler.getUserProperty(persistedUser, null);
            String newEmail = "";
            if (StringHelper.containsNonWhitespace(oldEmail)) {
                newEmail = getBackupStringWithDate(oldEmail);
            }
            logInfo("Update user-property user=" + persistedUser);
            userPropertyHandler.setUserProperty(persistedUser, newEmail);
        }
    }
    UserManager.getInstance().updateUserFromIdentity(identity);
    logInfo("deleteUserProperties user=" + persistedUser);
    dbInstance.commit();
    identity = securityManager.loadIdentityByKey(identity.getKey());
    // keep email only -> change login-name
    if (!keepUserEmailAfterDeletion) {
        identity = securityManager.saveIdentityName(identity, newName, null);
    }
    // keep everything, change identity.status to deleted
    logInfo("Change stater identity=" + identity);
    identity = securityManager.saveIdentityStatus(identity, Identity.STATUS_DELETED);
    LifeCycleManager.createInstanceFor(identity).deleteTimestampFor(SEND_DELETE_EMAIL_ACTION);
    LifeCycleManager.createInstanceFor(identity).markTimestampFor(USER_DELETED_ACTION, createLifeCycleLogDataFor(identity));
    logAudit("User-Deletion: Delete all userdata for identity=" + identity);
}
Also used : User(org.olat.core.id.User) TemporaryKey(org.olat.registration.TemporaryKey) ArrayList(java.util.ArrayList) SecurityGroup(org.olat.basesecurity.SecurityGroup) Authentication(org.olat.basesecurity.Authentication) UserDataDeletable(org.olat.user.UserDataDeletable) File(java.io.File) UserPropertyHandler(org.olat.user.propertyhandlers.UserPropertyHandler)

Aggregations

TemporaryKey (org.olat.registration.TemporaryKey)16 User (org.olat.core.id.User)8 File (java.io.File)6 MailBundle (org.olat.core.util.mail.MailBundle)6 MailerResult (org.olat.core.util.mail.MailerResult)6 UserPropertyHandler (org.olat.user.propertyhandlers.UserPropertyHandler)6 XStream (com.thoughtworks.xstream.XStream)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 Locale (java.util.Locale)4 PUT (javax.ws.rs.PUT)4 Authentication (org.olat.basesecurity.Authentication)4 FormItem (org.olat.core.gui.components.form.flexible.FormItem)4 Translator (org.olat.core.gui.translator.Translator)4 Identity (org.olat.core.id.Identity)4 RegistrationManager (org.olat.registration.RegistrationManager)4 UserModule (org.olat.user.UserModule)4 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2