Skip to content
Snippets Groups Projects
Commit 37d5910e authored by srosse's avatar srosse
Browse files

OO-2241: remove optimized getter access to user properties, remove stupid unit...

OO-2241: remove optimized getter access to user properties, remove stupid unit test, change unit tests with the !keePEmail option
parent 3e38a07f
No related branches found
No related tags found
No related merge requests found
...@@ -329,26 +329,29 @@ public class UserDeletionManager extends BasicManager { ...@@ -329,26 +329,29 @@ public class UserDeletionManager extends BasicManager {
identity = securityManager.loadIdentityByKey(identity.getKey()); identity = securityManager.loadIdentityByKey(identity.getKey());
//keep login-name only -> change email //keep login-name only -> change email
if (!keepUserEmailAfterDeletion){
List<UserPropertyHandler> userPropertyHandlers = UserManager.getInstance().getUserPropertyHandlersFor("org.olat.admin.user.UsermanagerUserSearchForm", true); User persistedUser = identity.getUser();
User persistedUser = identity.getUser(); List<UserPropertyHandler> userPropertyHandlers = UserManager.getInstance().getAllUserPropertyHandlers();
String actualProperty; for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) { String actualProperty = userPropertyHandler.getName();
actualProperty = userPropertyHandler.getName(); if (userPropertyHandler.isDeletable()
if (actualProperty.equals(UserConstants.EMAIL)){ && !(keepUserEmailAfterDeletion && UserConstants.EMAIL.equals(actualProperty))) {
String oldEmail = userPropertyHandler.getUserProperty(persistedUser, null); persistedUser.setProperty(actualProperty, null);
String newEmail = ""; }
if (StringHelper.containsNonWhitespace(oldEmail)){
newEmail = getBackupStringWithDate(oldEmail); if((!keepUserEmailAfterDeletion && UserConstants.EMAIL.equals(actualProperty))) {
} String oldEmail = userPropertyHandler.getUserProperty(persistedUser, null);
logInfo("Update user-property user=" + persistedUser); String newEmail = "";
userPropertyHandler.setUserProperty(persistedUser, 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=" + identity.getUser()); logInfo("deleteUserProperties user=" + persistedUser);
UserManager.getInstance().deleteUserProperties(identity.getUser(), keepUserEmailAfterDeletion);
DBFactory.getInstance().commit(); DBFactory.getInstance().commit();
identity = securityManager.loadIdentityByKey(identity.getKey()); identity = securityManager.loadIdentityByKey(identity.getKey());
//keep email only -> change login-name //keep email only -> change login-name
......
...@@ -52,7 +52,6 @@ public abstract class AbstractUserPropertyHandler implements UserPropertyHandler ...@@ -52,7 +52,6 @@ public abstract class AbstractUserPropertyHandler implements UserPropertyHandler
private String name; private String name;
private String group; private String group;
private boolean deletable = true; // default private boolean deletable = true; // default
private Field getter;
private String databaseColumnName; private String databaseColumnName;
/** /**
...@@ -144,15 +143,9 @@ public abstract class AbstractUserPropertyHandler implements UserPropertyHandler ...@@ -144,15 +143,9 @@ public abstract class AbstractUserPropertyHandler implements UserPropertyHandler
*/ */
protected String getInternalValue(User user) { protected String getInternalValue(User user) {
if (user instanceof UserImpl) { if (user instanceof UserImpl) {
//String value = ((UserImpl)user).getUserProperty(name); String value = ((UserImpl)user).getUserProperty(name);
String value = null; if("_".equals(value) && "oracle".equals(DBFactory.getInstance().getDbVendor())) {
try { value = null;
value = (String)getter.get(user);
if("_".equals(value) && "oracle".equals(DBFactory.getInstance().getDbVendor())) {
value = null;
}
} catch (IllegalArgumentException | IllegalAccessException e) {
log.error("", e);
} }
return value; return value;
} else if (user != null) { } else if (user != null) {
...@@ -165,17 +158,13 @@ public abstract class AbstractUserPropertyHandler implements UserPropertyHandler ...@@ -165,17 +158,13 @@ public abstract class AbstractUserPropertyHandler implements UserPropertyHandler
* @param value The raw value in a 18n independent form * @param value The raw value in a 18n independent form
*/ */
protected void setInternalValue(User user, String value) { protected void setInternalValue(User user, String value) {
if (user instanceof UserImpl && getter != null) { if (user instanceof UserImpl) {
try { // remove fields with null or empty value from o_userfield table (hibernate)
// remove fields with null or empty value from o_userfield table (hibernate) // sparse data storage
// sparse data storage if (value == null || value.length() == 0) {
if (value == null || value.length() == 0) { ((UserImpl)user).setUserProperty(name, null);
getter.set(user, null); } else {
} else { ((UserImpl)user).setUserProperty(name, value);
getter.set(user, value);
}
} catch (IllegalArgumentException | IllegalAccessException e) {
log.error("", e);
} }
} else { } else {
log.warn("Set read-only value: " + name, null); log.warn("Set read-only value: " + name, null);
...@@ -188,6 +177,7 @@ public abstract class AbstractUserPropertyHandler implements UserPropertyHandler ...@@ -188,6 +177,7 @@ public abstract class AbstractUserPropertyHandler implements UserPropertyHandler
* @return String internal user field info * @return String internal user field info
* @see java.lang.Object#toString() * @see java.lang.Object#toString()
*/ */
@Override
public String toString() { public String toString() {
String quickinfo = "AbstractUserPropertyHandler("+this.getClass().getName()+")["+getName()+"]" ; String quickinfo = "AbstractUserPropertyHandler("+this.getClass().getName()+")["+getName()+"]" ;
return quickinfo + "," + super.toString(); return quickinfo + "," + super.toString();
...@@ -197,6 +187,7 @@ public abstract class AbstractUserPropertyHandler implements UserPropertyHandler ...@@ -197,6 +187,7 @@ public abstract class AbstractUserPropertyHandler implements UserPropertyHandler
* Spring setter * Spring setter
* @param group * @param group
*/ */
@Override
public void setGroup(String group) { public void setGroup(String group) {
this.group = group; this.group = group;
} }
...@@ -220,7 +211,7 @@ public abstract class AbstractUserPropertyHandler implements UserPropertyHandler ...@@ -220,7 +211,7 @@ public abstract class AbstractUserPropertyHandler implements UserPropertyHandler
protected void setInternalGetterSetter(String name) { protected void setInternalGetterSetter(String name) {
try { try {
getter = UserImpl.class.getDeclaredField(name); Field getter = UserImpl.class.getDeclaredField(name);
getter.setAccessible(true); getter.setAccessible(true);
if(getter.isAnnotationPresent(Column.class)) { if(getter.isAnnotationPresent(Column.class)) {
Column col = getter.getAnnotation(Column.class); Column col = getter.getAnnotation(Column.class);
......
...@@ -104,7 +104,7 @@ public class UserImpl implements Persistable, User { ...@@ -104,7 +104,7 @@ public class UserImpl implements Persistable, User {
@Transient @Transient
private boolean webdav; private boolean webdav;
@Column(name="u_firstName", nullable=true, insertable=true, updatable=true) @Column(name="u_firstname", nullable=true, insertable=true, updatable=true)
private String firstName; private String firstName;
@Column(name="u_lastname", nullable=true, insertable=true, updatable=true) @Column(name="u_lastname", nullable=true, insertable=true, updatable=true)
private String lastName; private String lastName;
......
...@@ -195,12 +195,6 @@ public abstract class UserManager extends BasicManager { ...@@ -195,12 +195,6 @@ public abstract class UserManager extends BasicManager {
return userPropertiesConfig; return userPropertiesConfig;
} }
/**
* Delete all user-properties which are deletable.
* @param user
*/
public abstract User deleteUserProperties(User user, boolean keepUserEmail);
public List<UserPropertyHandler> getUserPropertyHandlersFor(String usageIdentifyer, boolean isAdministrativeUser) { public List<UserPropertyHandler> getUserPropertyHandlersFor(String usageIdentifyer, boolean isAdministrativeUser) {
return userPropertiesConfig.getUserPropertyHandlersFor(usageIdentifyer, isAdministrativeUser); return userPropertiesConfig.getUserPropertyHandlersFor(usageIdentifyer, isAdministrativeUser);
} }
......
...@@ -55,7 +55,6 @@ import org.olat.core.util.i18n.I18nModule; ...@@ -55,7 +55,6 @@ import org.olat.core.util.i18n.I18nModule;
import org.olat.core.util.mail.MailHelper; import org.olat.core.util.mail.MailHelper;
import org.olat.properties.Property; import org.olat.properties.Property;
import org.olat.properties.PropertyManager; import org.olat.properties.PropertyManager;
import org.olat.user.propertyhandlers.UserPropertyHandler;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
/** /**
...@@ -426,30 +425,6 @@ public class UserManagerImpl extends UserManager { ...@@ -426,30 +425,6 @@ public class UserManagerImpl extends UserManager {
return charset; return charset;
} }
/**
* Delete all user-properties which are deletable.
* @param user
*/
@Override
public User deleteUserProperties(User user, boolean keepUserEmail) {
// prevent stale objects, reload first
user = loadUserByKey(user.getKey());
// loop over user fields and remove them form the database if they are
// deletable
List<UserPropertyHandler> propertyHandlers = userPropertiesConfig.getAllUserPropertyHandlers();
for (UserPropertyHandler propertyHandler : propertyHandlers) {
String fieldName = propertyHandler.getName();
if (propertyHandler.isDeletable()
&& !(keepUserEmail && UserConstants.EMAIL.equals(propertyHandler.getName()))) {
user.setProperty(fieldName, null);
}
}
// persist changes
User updatedUser = updateUser(user);
if(isLogDebugEnabled()) logDebug("Delete all user-attributtes for user=" + user);
return updatedUser;
}
@Override @Override
public int warmUp() { public int warmUp() {
EntityManager em = dbInstance.getCurrentEntityManager(); EntityManager em = dbInstance.getCurrentEntityManager();
......
...@@ -83,7 +83,8 @@ public class UserDeletionManagerTest extends OlatTestCase { ...@@ -83,7 +83,8 @@ public class UserDeletionManagerTest extends OlatTestCase {
@Test @Test
public void testDeleteIdentity() { public void testDeleteIdentity() {
String username = "id-to-del-" + UUID.randomUUID(); String username = "id-to-del-" + UUID.randomUUID();
User user = userManager.createUser("first" + username, "last" + username, username + "@frentix.com"); String email = username + "@frentix.com";
User user = userManager.createUser("first" + username, "last" + username, email);
user.setProperty(UserConstants.COUNTRY, ""); user.setProperty(UserConstants.COUNTRY, "");
user.setProperty(UserConstants.CITY, "Basel"); user.setProperty(UserConstants.CITY, "Basel");
user.setProperty(UserConstants.INSTITUTIONALNAME, "Del-23"); user.setProperty(UserConstants.INSTITUTIONALNAME, "Del-23");
...@@ -136,6 +137,9 @@ public class UserDeletionManagerTest extends OlatTestCase { ...@@ -136,6 +137,9 @@ public class UserDeletionManagerTest extends OlatTestCase {
Assert.assertFalse(StringHelper.containsNonWhitespace(institutionalName)); Assert.assertFalse(StringHelper.containsNonWhitespace(institutionalName));
String institutionalId = deletedUser.getProperty(UserConstants.INSTITUTIONALUSERIDENTIFIER, null); String institutionalId = deletedUser.getProperty(UserConstants.INSTITUTIONALUSERIDENTIFIER, null);
Assert.assertFalse(StringHelper.containsNonWhitespace(institutionalId)); Assert.assertFalse(StringHelper.containsNonWhitespace(institutionalId));
String deletedEmail = deletedUser.getProperty(UserConstants.EMAIL, null);
Assert.assertNotNull(deletedEmail);
Assert.assertFalse(email.equals(deletedEmail));
} }
@Test @Test
......
This diff is collapsed.
...@@ -24,7 +24,7 @@ is.translation.server=disabled ...@@ -24,7 +24,7 @@ is.translation.server=disabled
deploy.course.exports=false deploy.course.exports=false
# for UserTest # for UserTest
keepUserEmailAfterDeletion=true keepUserEmailAfterDeletion=false
keepUserLoginAfterDeletion=true keepUserLoginAfterDeletion=true
# do not run upgrades and scheduled jobs and such # do not run upgrades and scheduled jobs and such
......
...@@ -19,7 +19,7 @@ instance.id=${test.env.instance.id:2} ...@@ -19,7 +19,7 @@ instance.id=${test.env.instance.id:2}
generate.index.at.startup=false generate.index.at.startup=false
# for UserTest # for UserTest
keepUserEmailAfterDeletion=true keepUserEmailAfterDeletion=false
keepUserLoginAfterDeletion=true keepUserLoginAfterDeletion=true
# do not run upgrades and scheduled jobs and such # do not run upgrades and scheduled jobs and such
......
...@@ -24,7 +24,7 @@ is.translation.server=disabled ...@@ -24,7 +24,7 @@ is.translation.server=disabled
deploy.course.exports=false deploy.course.exports=false
# for UserTest # for UserTest
keepUserEmailAfterDeletion=true keepUserEmailAfterDeletion=false
keepUserLoginAfterDeletion=true keepUserLoginAfterDeletion=true
# do not run upgrades and scheduled jobs and such # do not run upgrades and scheduled jobs and such
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment