Skip to content

Commit

Permalink
Add ResolvedUser class.
Browse files Browse the repository at this point in the history
  • Loading branch information
AfraHussaindeen committed Jan 20, 2025
1 parent b1d3858 commit 5f56ac2
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.io.Serializable;
import java.util.Iterator;
import java.util.Locale;
import java.util.Objects;

/**
* Representation of a user.
Expand All @@ -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() {
Expand All @@ -64,7 +62,6 @@ public User(org.wso2.carbon.user.core.common.User commonUser) {
* <TenantDomain></TenantDomain>
* <UserStoreDomain></UserStoreDomain>
* <UserName></UserName>
* <UserId></UserId>
* </User>
*
* @param userOM OMElement to populate user
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -221,7 +181,7 @@ public boolean equals(Object o) {
return false;
}

return Objects.equals(userId, user.userId);
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -303,7 +304,6 @@ public void setAuthenticatedSubjectIdentifier(String authenticatedSubjectIdentif
this.authenticatedSubjectIdentifier = authenticatedSubjectIdentifier;
}

@Override
public String getUserId() throws UserIdNotFoundException {

if (this.userId != null) {
Expand All @@ -322,6 +322,11 @@ public boolean isUserIdExists() {
return this.userId != null;
}

public void setUserId(String userId) {

this.userId = userId;
}

private String resolveUserIdInternal() {

String userId;
Expand Down

0 comments on commit 5f56ac2

Please sign in to comment.