diff --git a/jpro-auth/core/src/main/java/one/jpro/platform/auth/core/authentication/User.java b/jpro-auth/core/src/main/java/one/jpro/platform/auth/core/authentication/User.java index e129934e..5e9c5fd4 100755 --- a/jpro-auth/core/src/main/java/one/jpro/platform/auth/core/authentication/User.java +++ b/jpro-auth/core/src/main/java/one/jpro/platform/auth/core/authentication/User.java @@ -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; @@ -122,10 +123,22 @@ public Map 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); } @@ -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 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. * diff --git a/jpro-auth/core/src/main/java/one/jpro/platform/auth/core/utils/AuthUtils.java b/jpro-auth/core/src/main/java/one/jpro/platform/auth/core/utils/AuthUtils.java index 1a87b2da..16e6735c 100644 --- a/jpro-auth/core/src/main/java/one/jpro/platform/auth/core/utils/AuthUtils.java +++ b/jpro-auth/core/src/main/java/one/jpro/platform/auth/core/utils/AuthUtils.java @@ -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. *