From 64e29b952bba5231d43c73b51dc5bef0377188e9 Mon Sep 17 00:00:00 2001 From: Jared Frees Date: Mon, 11 Jun 2018 15:05:20 -0400 Subject: [PATCH] GUACAMOLE-524: Add arbitrary attributes on AuthenticatedUser Add attribute map to AuthenticatedUser along with methods for retrieving and setting map. Also, make AuthenticatedUser implement Attributes. --- .../auth/ldap/user/AuthenticatedUser.java | 75 ++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/user/AuthenticatedUser.java b/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/user/AuthenticatedUser.java index 669efcd54..86c4de1dc 100644 --- a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/user/AuthenticatedUser.java +++ b/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/user/AuthenticatedUser.java @@ -20,7 +20,10 @@ package org.apache.guacamole.auth.ldap.user; import com.google.inject.Inject; +import java.util.Map; +import java.util.HashMap; import org.apache.guacamole.net.auth.AbstractAuthenticatedUser; +import org.apache.guacamole.net.auth.Attributes; import org.apache.guacamole.net.auth.AuthenticationProvider; import org.apache.guacamole.net.auth.Credentials; @@ -28,7 +31,8 @@ import org.apache.guacamole.net.auth.Credentials; * An LDAP-specific implementation of AuthenticatedUser, associating a * particular set of credentials with the LDAP authentication provider. */ -public class AuthenticatedUser extends AbstractAuthenticatedUser { +public class AuthenticatedUser extends AbstractAuthenticatedUser + implements Attributes { /** * Reference to the authentication provider associated with this @@ -42,6 +46,11 @@ public class AuthenticatedUser extends AbstractAuthenticatedUser { */ private Credentials credentials; + /** + * Arbitrary attributes associated with this AuthenticatedUser object. + */ + private Map attributes = new HashMap(); + /** * Initializes this AuthenticatedUser using the given credentials. * @@ -53,6 +62,70 @@ public class AuthenticatedUser extends AbstractAuthenticatedUser { setIdentifier(credentials.getUsername()); } + /** + * Get a map of attributes associated with this AuthenticatedUser. + * + * @return + * The Map of arbitrary attributes associated with this + * AuthenticatedUser object. + */ + public Map getAttributes() { + return attributes; + } + + /** + * Sets a map of attributes associated with this AuthenticatedUser. + * + * @param attributes + * A map of attribute key/value pairs to add to this AuthenticatedUser. + */ + public void setAttributes(Map attributes) { + this.attributes = attributes; + } + + /** + * Add the Map of attributes to the current set, without completely + * replacing the existing set. However, if duplicate keys exist the new + * values will replace any existing ones. + * + * @param attributes + * A Map of attributes to add to the existing attributes, without + * completely overwriting them. + */ + public void addAttributes(Map attributes) { + this.attributes.putAll(attributes); + } + + /** + * Retrieve a single attribute value from the map of arbitrary attributes + * stored in this AuthenticatedUser object. + * + * @param key + * The key of the attribute to retrieve. + * + * @return + * The value of the attribute with the specified key. + */ + public String getAttribute(String key) { + return attributes.get(key); + } + + /** + * Set the attribute of the given key to the given value, either adding + * a new value if the specified key does not exist, or replacing an existing + * value. + * + * @param key + * The key name of the attribute to set (or overwrite, if it + * already exists). + * + * @param value + * The value of the attribute to set or overwrite. + */ + public void setAttribute(String key, String value) { + attributes.put(key, value); + } + @Override public AuthenticationProvider getAuthenticationProvider() { return authProvider;