From 5f56ac2eac7f5d136dbeb595f6ef3826a37b66fa Mon Sep 17 00:00:00 2001 From: Afra Hussaindeen Date: Sun, 19 Jan 2025 18:05:13 +0530 Subject: [PATCH] Add ResolvedUser class. --- .../common/model/ResolvedUser.java | 97 +++++++++++++++++++ .../application/common/model/User.java | 42 +------- .../framework/model/AuthenticatedUser.java | 7 +- 3 files changed, 104 insertions(+), 42 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/model/ResolvedUser.java diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/model/ResolvedUser.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/model/ResolvedUser.java new file mode 100644 index 000000000000..44eef918ca8c --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/model/ResolvedUser.java @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.common.model; + +import java.util.Objects; + +/** + * ResolvedUser is the class that represents a user whose unique user ID has been resolved. + * This extends the basic User class by adding the user ID information. + */ +public class ResolvedUser extends User { + + private static final long serialVersionUID = -4835843966924064267L; + + private String userId = null; + + /** + * Instantiates a ResolvedUser. + */ + public ResolvedUser() { + + super(); + } + + /** + * Creates a ResolvedUser instance from a User object. + * + * @param user The User object to create the ResolvedUser from. + */ + public ResolvedUser(User user) { + + this.setUserName(user.getUserName()); + this.setUserStoreDomain(user.getUserStoreDomain()); + this.setTenantDomain(user.getTenantDomain()); + } + + /** + * Returns the user ID. + * + * @return The user ID. + */ + public String getUserId() { + + return userId; + } + + /** + * Sets the user ID. + * + * @param userId The user ID to set. + */ + public void setUserId(String userId) { + + this.userId = userId; + } + + @Override + public boolean equals(Object o) { + + if (this == o) { + return true; + } + if (!(o instanceof ResolvedUser)) { + return false; + } + if (!super.equals(o)) { + return false; + } + + ResolvedUser resolvedUser = (ResolvedUser) o; + return Objects.equals(this.userId, resolvedUser.userId); + } + + @Override + public int hashCode() { + + int result = super.hashCode(); + result = 31 * result + (userId != null ? userId.hashCode() : 0); + return result; + } +} diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/model/User.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/model/User.java index 830b68823d37..08c28fc9836c 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/model/User.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/model/User.java @@ -31,7 +31,6 @@ import java.io.Serializable; import java.util.Iterator; import java.util.Locale; -import java.util.Objects; /** * Representation of a user. @@ -43,7 +42,6 @@ public class User implements Serializable { protected String tenantDomain; protected String userStoreDomain; protected String userName; - protected String userId = null; protected boolean isUsernameCaseSensitive = true; public User() { @@ -64,7 +62,6 @@ public User(org.wso2.carbon.user.core.common.User commonUser) { * * * - * * * * @param userOM OMElement to populate user @@ -88,8 +85,6 @@ public static User build(OMElement userOM) { user.setUserStoreDomain(member.getText()); } else if ("UserName".equalsIgnoreCase(member.getLocalName())) { user.setUserName(member.getText()); - } else if ("UserId".equalsIgnoreCase(member.getLocalName()) && StringUtils.isNotBlank(member.getText())) { - user.setUserId(member.getText()); } } return user; @@ -155,41 +150,6 @@ public void setUserName(String userName) { this.userName = userName; } - /** - * Returns the userId of the user. This method may return null if the userId is not set. - * - * @return The userId of the user, or null if not set - */ - public String getNullableUserId() { - - return userId; - } - - /** - * Retrieves the userId of the user. - * - * This method must be implemented by subclasses to ensure that the user ID is properly resolved. - * If not implemented, it throws an {@link UnsupportedOperationException}, indicating the method - * is not supported in the base {@link User} class. - * - * @return The userId of the user - * @throws Exception If there is an error retrieving the userId - */ - public String getUserId() throws Exception { - - throw new UnsupportedOperationException("getUserId is not implemented in base User class."); - } - - /** - * Sets the userId of the user - * - * @param userId - */ - public void setUserId(String userId) { - - this.userId = userId; - } - public boolean equals(Object o) { if (this == o) { return true; @@ -221,7 +181,7 @@ public boolean equals(Object o) { return false; } - return Objects.equals(userId, user.userId); + return true; } /** diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/model/AuthenticatedUser.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/model/AuthenticatedUser.java index 4cffcd05acba..5b186765b5c3 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/model/AuthenticatedUser.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/model/AuthenticatedUser.java @@ -57,6 +57,7 @@ public class AuthenticatedUser extends User { private static final Log log = LogFactory.getLog(AuthenticatedUser.class); + protected String userId; private String authenticatedSubjectIdentifier; private String federatedIdPName; private boolean isFederatedUser; @@ -303,7 +304,6 @@ public void setAuthenticatedSubjectIdentifier(String authenticatedSubjectIdentif this.authenticatedSubjectIdentifier = authenticatedSubjectIdentifier; } - @Override public String getUserId() throws UserIdNotFoundException { if (this.userId != null) { @@ -322,6 +322,11 @@ public boolean isUserIdExists() { return this.userId != null; } + public void setUserId(String userId) { + + this.userId = userId; + } + private String resolveUserIdInternal() { String userId;