Skip to content

Commit

Permalink
Implement and add getAttribute(String key) method to the User class…
Browse files Browse the repository at this point in the history
… to easily retrieve the value of a specific attribute
  • Loading branch information
besidev committed Mar 15, 2024
1 parent 3ffd326 commit 6b14956
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package one.jpro.platform.auth.core.authentication;

import one.jpro.platform.auth.core.utils.AuthUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
Expand Down Expand Up @@ -122,10 +123,22 @@ public Map<String, Object> getAttributes() {
return Collections.unmodifiableMap(attributes);
}

/**
* Checks if the user has a specific role.
*
* @param role the role to check.
* @return true if the user has the specified role, false otherwise.
*/
public boolean hasRole(String role) {
return getRoles().contains(role);
}

/**
* Checks if the user has a specific attribute.
*
* @param key the key of the attribute to check
* @return true if the user has the specified attribute, false otherwise
*/
public boolean hasAttribute(String key) {
return hasKey(toJSON().getJSONObject(KEY_ATTRIBUTES), key);
}
Expand All @@ -144,6 +157,17 @@ private boolean hasKey(JSONObject json, String key) {
return exists;
}

/**
* Retrieves the value of a specific attribute.
*
* @param key the key of the attribute to retrieve
* @return the value of the attribute as a String, or null if the attribute does not exist
*/
public Optional<String> getAttribute(String key) {
final Object value = AuthUtils.findValueByKey(toJSON().getJSONObject(KEY_ATTRIBUTES), key);
return Optional.ofNullable(value).map(Object::toString);
}

/**
* Retrieve the user's email from the user's attributes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,48 @@ static JSONObject queryToJson(String query) {
return json;
}

/**
* Find the value of a key in a JSON object.
*
* @param json the JSON object
* @param key the key to search for
* @return the value of the key
*/
static Object findValueByKey(JSONObject json, String key) {
// Check if the current JSON object has the key.
if (json.has(key)) {
return json.get(key);
}

// If the key is not in the current object, search deeper.
for (String currentKey : json.keySet()) {
Object value = json.get(currentKey);

if (value instanceof JSONObject) {
// If it's a nested JSONObject, search recursively in this object.
final Object result = findValueByKey((JSONObject) value, key);
if (result != null) {
return result;
}
} else if (value instanceof JSONArray array) {
// If it's a JSONArray, iterate through its elements.
for (int i = 0; i < array.length(); i++) {
Object arrayElement = array.get(i);
if (arrayElement instanceof JSONObject) {
// If an element of the array is a JSONObject, search recursively in this object.
Object result = findValueByKey((JSONObject) arrayElement, key);
if (result != null) {
return result;
}
}
}
}
}

// If the key was not found in the current object or any of its sub objects, return null.
return null;
}

/**
* Checks if the specified headers contain the specified value.
*
Expand Down

0 comments on commit 6b14956

Please sign in to comment.