Skip to content

Commit

Permalink
feat: remove deprecated UserCredentialsDto usage (#17751)
Browse files Browse the repository at this point in the history
  • Loading branch information
netroms authored Jun 22, 2024
1 parent 8d4698d commit 035abb8
Show file tree
Hide file tree
Showing 21 changed files with 11 additions and 1,095 deletions.
4 changes: 0 additions & 4 deletions dhis-2/dhis-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
216 changes: 0 additions & 216 deletions dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,14 @@
package org.hisp.dhis.user;

import static org.apache.commons.collections4.CollectionUtils.emptyIfNull;
import static org.springframework.beans.BeanUtils.copyProperties;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
Expand All @@ -66,11 +60,8 @@
import org.hisp.dhis.schema.annotation.Property;
import org.hisp.dhis.schema.annotation.PropertyRange;
import org.hisp.dhis.security.Authorities;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.util.ClassUtils;

/**
* @author Nguyen Hong Duc
Expand Down Expand Up @@ -193,10 +184,6 @@ public class User extends BaseIdentifiableObject implements MetadataObject {

private FileResource avatar;

// Backward comp. field, will be removed when front-end has converted to new
// User model
private transient UserCredentialsDto userCredentialsRaw;

/** Organisation units for data input and data capture operations. */
private Set<OrganisationUnit> organisationUnits = new HashSet<>();

Expand Down Expand Up @@ -1212,207 +1199,4 @@ public static String username(User user, String defaultValue) {
public static String username(UserDetails user, String defaultValue) {
return user != null ? user.getUsername() : defaultValue;
}

// TODO: MAS To remove when we remove old UserCredentials compatibility layer
// This is a temporary fix to maintain backwards compatibility with the old
// UserCredentials class. This method should not be used in new code!
@JsonProperty
public UserCredentialsDto getUserCredentials() {
UserCredentialsDto userCredentialsDto = new UserCredentialsDto();
copyProperties(
this,
userCredentialsDto,
"uid",
"userCredentials",
"password",
"userRoles",
"secret",
"previousPasswords");

userCredentialsDto.setId(this.getUid());

Set<UserRole> roles = this.getUserRoles();
if (roles != null && !roles.isEmpty()) {
userCredentialsDto.setUserRoles(roles);
}

userCredentialsDto.setTwoFA(this.isTwoFactorEnabled());

return userCredentialsDto;
}

// TODO: To remove when we remove old UserCredentials compatibility layer
public UserCredentialsDto getUserCredentialsRaw() {
return this.userCredentialsRaw;
}

// TODO: To remove when we remove old UserCredentials compatibility layer
// This is a temporary fix to maintain backwards compatibility with the old
// UserCredentials class. This method should not be used in new code!
protected void setUserCredentials(UserCredentialsDto user) {
this.userCredentialsRaw = user;
}

// TODO: To remove when we remove old UserCredentials compatibility layer
/** This should be called when legacy UserCredentials model is merged into the new model */
public void removeLegacyUserCredentials() {
this.setUserCredentials(null);
}

// TODO: To remove when we remove old UserCredentials compatibility layer
/**
* Copies the "transient" properties from the old UserCredentials model (temp. saved in the
* userCredentialsRaw property). The userCredentialsRaw only exists if the input JSON contains the
* old UserCredentials model.
*
* <p>The userCredentialsRaw field represent the input JSON version of the object, not the real
* data model. This is to make the input format backward compatible with the old UserCredentials
* model.
*
* @param user The user object that is being populated.
*/
public static void populateUserCredentialsDtoFields(User user) {
UserCredentialsDto userCredentialsRaw = user.getUserCredentialsRaw();
if (userCredentialsRaw != null) {
copyProperties(
userCredentialsRaw, user, "uid", "password", "userRoles", "secret", "previousPasswords");
if (userCredentialsRaw.getPassword() != null) {
user.setPassword(userCredentialsRaw.getPassword());
}

Set<UserRole> userRoles = userCredentialsRaw.getUserRoles();
if (userRoles != null) {
user.setUserRoles(userRoles);
}

user.removeLegacyUserCredentials();
}
}

// TODO: To remove when we remove old UserCredentials compatibility layer
/**
* Copy only changed properties from the old user to the new user, and then set the new user's
* userCredentials to null.
*
* @param oldUser The user object that is currently in the database
* @param newUser The new user object that is being created.
*/
public static void populateUserCredentialsDtoCopyOnlyChanges(User oldUser, User newUser) {
UserCredentialsDto oldCredentialsRaw = oldUser.getUserCredentials();
UserCredentialsDto newUserCredentialsRaw = newUser.getUserCredentialsRaw();

if (newUserCredentialsRaw != null) {
UserCredentialsDto newUserCredentialsCopiedFromBase = newUser.getUserCredentials();

copyOnlyChangedProperties(
oldCredentialsRaw,
newUserCredentialsRaw,
newUser,
"uid",
"password",
"userRoles",
"secret",
"previousPasswords");
copyOnlyChangedProperties(
oldCredentialsRaw,
newUserCredentialsCopiedFromBase,
newUser,
"uid",
"password",
"userRoles",
"secret",
"previousPasswords");

if (newUserCredentialsRaw.getPassword() != null) {
newUser.setPassword(newUserCredentialsRaw.getPassword());
}

Set<UserRole> oldRoles = oldUser.getUserRoles();
Set<UserRole> userRoles = newUserCredentialsRaw.getUserRoles();
if (userRoles != null && !userRoles.equals(oldRoles)) {
newUser.setUserRoles(userRoles);
}

Set<Category> oldCatDimCon = oldUser.getCatDimensionConstraints();
Set<CategoryOptionGroupSet> oldCogDimCon = oldUser.getCogsDimensionConstraints();

Set<Category> newCatDimCon = newUserCredentialsRaw.getCatDimensionConstraints();
Set<CategoryOptionGroupSet> newCogDimCon =
newUserCredentialsRaw.getCogsDimensionConstraints();

if (oldCatDimCon != null && newCatDimCon != null && !oldCatDimCon.equals(newCatDimCon)) {
newUser.setCatDimensionConstraints(newCatDimCon);
}

if (oldCogDimCon != null && newCogDimCon != null && !oldCogDimCon.equals(newCogDimCon)) {
newUser.setCogsDimensionConstraints(newCogDimCon);
}

newUser.removeLegacyUserCredentials();
}
}

// TODO: To remove when we remove old UserCredentials compatibility layer
private static void copyOnlyChangedProperties(
UserCredentialsDto oldObject,
UserCredentialsDto source,
User target,
@Nullable String... ignoreProperties) {
List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);

PropertyDescriptor[] targetPds;
try {
targetPds = Introspector.getBeanInfo(target.getClass()).getPropertyDescriptors();
} catch (IntrospectionException e) {
return;
}

for (PropertyDescriptor targetPd : targetPds) {
Method writeMethod = targetPd.getWriteMethod();
if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
PropertyDescriptor sourcePd;
try {
sourcePd = new PropertyDescriptor(targetPd.getName(), source.getClass());
} catch (IntrospectionException e) {
continue;
}

Method readMethod = sourcePd.getReadMethod();
if (readMethod != null) {
compareAndWriteProperty(oldObject, source, target, writeMethod, readMethod);
}
}
}
}

// TODO: To remove when we remove old UserCredentials compatibility layer
private static void compareAndWriteProperty(
UserCredentialsDto oldObject,
UserCredentialsDto source,
User target,
Method writeMethod,
Method readMethod) {
ResolvableType sourceResolvableType = ResolvableType.forMethodReturnType(readMethod);
ResolvableType targetResolvableType = ResolvableType.forMethodParameter(writeMethod, 0);

boolean isAssignable =
(sourceResolvableType.hasUnresolvableGenerics()
|| targetResolvableType.hasUnresolvableGenerics()
? ClassUtils.isAssignable(
writeMethod.getParameterTypes()[0], readMethod.getReturnType())
: targetResolvableType.isAssignableFrom(sourceResolvableType));

if (isAssignable) {
try {
Object oldValue = readMethod.invoke(oldObject);
Object value = readMethod.invoke(source);

if (value != null && !value.equals(oldValue)) {
writeMethod.invoke(target, value);
}
} catch (Exception ex) {
// do nothing
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,6 @@ public void validate(PreheatParams params) throws PreheatException {
.collect(toList());

for (Object object : targets.get(klass)) {
handleLegacyUserCredentials(klass, object);

if (schema.isIdentifiableObject()) {
IdentifiableObject identifiableObject = (IdentifiableObject) object;
identifiableObject
Expand Down Expand Up @@ -978,16 +976,4 @@ private boolean skipConnect(Class<?> klass) {
private boolean isOnlyUID(Class<?> klass) {
return UserGroup.class.isAssignableFrom(klass) || User.class.isAssignableFrom(klass);
}

// TODO: To remove when we remove old UserCredentials compatibility layer
/**
* This is a temporary fix to maintain backwards compatibility with the old UserCredentials class
*/
private void handleLegacyUserCredentials(Class<?> klass, Object object) {
if (!User.class.isAssignableFrom(klass) || object == null) {
return;
}

User.populateUserCredentialsDtoFields((User) object);
}
}
Loading

0 comments on commit 035abb8

Please sign in to comment.