001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.security.provider;
007:
008: import java.util.Enumeration;
009: import java.util.Hashtable;
010: import java.util.Iterator;
011: import java.util.List;
012: import java.util.Map;
013:
014: import org.jasig.portal.EntityIdentifier;
015: import org.jasig.portal.security.IPerson;
016: import org.jasig.portal.security.ISecurityContext;
017: import org.jasig.portal.security.PersonFactory;
018:
019: /**
020: * This is a reference IPerson implementation.
021: * @author Adam Rybicki, arybicki@unicon.net
022: * @version $Revision: 36677 $
023: */
024: public class PersonImpl implements IPerson {
025: protected Hashtable m_Attributes = null;
026: protected String m_FullName = null;
027: protected int m_ID = -1;
028: protected ISecurityContext m_securityContext = null;
029: protected EntityIdentifier m_eid = new EntityIdentifier(null,
030: IPerson.class);
031: protected boolean entityIdentifierSet = false;
032:
033: public ISecurityContext getSecurityContext() {
034: return m_securityContext;
035: }
036:
037: public void setSecurityContext(ISecurityContext securityContext) {
038: m_securityContext = securityContext;
039: }
040:
041: /**
042: * Returns an attribute for a key. For objects represented as strings,
043: * a <code>java.lang.String</code> will be returned. Binary values will
044: * be represented as byte arrays.
045: * @param key the attribute name.
046: * @return value the attribute value identified by the key.
047: */
048: public Object getAttribute(String key) {
049: if (m_Attributes == null)
050: return null;
051: Object value = m_Attributes.get(key);
052: if (value instanceof List)
053: return ((List) value).get(0);
054: else
055: return value;
056: }
057:
058: /**
059: * Returns multiple attributes for a key. If only one
060: * value exists, it will be returned in an array of size one.
061: * @param key the attribute name
062: * @return the array of attribute values identified by the key
063: */
064: public Object[] getAttributeValues(String key) {
065: if (m_Attributes == null)
066: return null;
067: Object value = m_Attributes.get(key);
068: if (value == null) {
069: return null;
070: } else if (value instanceof List) {
071: return ((List) value).toArray();
072: } else {
073: return new Object[] { value };
074: }
075: }
076:
077: /**
078: * Returns a <code>java.util.Enumeration</code> of all the attribute values.
079: * @return <code>java.util.Enumeration</code> of the attributes.
080: */
081: public Enumeration getAttributes() {
082: if (m_Attributes == null)
083: return null;
084: return m_Attributes.elements();
085: }
086:
087: /**
088: * Returns an enumeration of all of the attribute names associated with the user
089: * @return enumeration of all of the attribute names associated with the user
090: */
091: public Enumeration getAttributeNames() {
092: if (m_Attributes == null) {
093: return (null);
094: } else {
095: return (m_Attributes.keys());
096: }
097: }
098:
099: /**
100: * Sets the specified attribute to a value.
101: *
102: * Reference impementation checks for the setting of the username attribute
103: * and updates the EntityIdentifier accordingly
104: *
105: * @param key Attribute's name
106: * @param value Attribute's value
107: */
108: public void setAttribute(String key, Object value) {
109: if (m_Attributes == null)
110: m_Attributes = new Hashtable();
111: if (value != null)
112: m_Attributes.put(key, value);
113: else
114: m_Attributes.remove(key);
115: if (!entityIdentifierSet && key.equals(IPerson.USERNAME)) {
116: m_eid = new EntityIdentifier(String.valueOf(value),
117: IPerson.class);
118: }
119: }
120:
121: /**
122: * Sets the specified attributes. Uses {@link #setAttribute(String, Object)}
123: * to set each.
124: *
125: * @see org.jasig.portal.security.IPerson#setAttributes(java.util.Map)
126: */
127: public void setAttributes(final Map attrs) {
128: for (final Iterator attrItr = attrs.keySet().iterator(); attrItr
129: .hasNext();) {
130: final String key = (String) attrItr.next();
131: final Object val = attrs.get(key);
132:
133: this .setAttribute(key, val);
134: }
135: }
136:
137: /**
138: * Returns the user's ID that was used for authentication.
139: * Does not correlate to any eduPerson attribute but is the key
140: * for user data in uPortal reference rdbms.
141: * @return User's ID.
142: */
143: public int getID() {
144: return m_ID;
145: }
146:
147: /**
148: * Sets the user's ID.
149: * @param sID User's ID as supplied for authentication
150: * Does not correlate to any eduPerson attribute but is the key
151: * for user data in uPortal reference rdbms.
152: */
153: public void setID(int sID) {
154: m_ID = sID;
155: }
156:
157: /**
158: * Returns the user's name that was established during authentication.
159: * Correlates to cn (common name) in the eduPerson 1.0 specification.
160: * @return User's name.
161: */
162: public String getFullName() {
163: return m_FullName;
164: }
165:
166: /**
167: * Sets the user's full name.
168: * @param sFullName User's name as established during authentication
169: * Correlates to cn (common name) in the eduPerson 1.0 specification.
170: */
171: public void setFullName(String sFullName) {
172: m_FullName = sFullName;
173: }
174:
175: /**
176: * Determines whether or not this person is a "guest" user.
177: * <p>
178: * This person is a "guest" if both of the following are true:
179: * <ol>
180: * <li>This person has not successfully authenticated with the portal.</li>
181: * <li>This person's user name matches the value of the property
182: * <code>org.jasig.portal.security.PersonImpl.guest_user_name</code>
183: * in <code>portal.properties</code>.</li>
184: * </ol>
185: * @return <code>true</code> if person is a guest, otherwise <code>false</code>
186: */
187: public boolean isGuest() {
188: boolean isGuest = false;
189: String userName = (String) getAttribute(IPerson.USERNAME);
190: if (userName.equals(PersonFactory.GUEST_USERNAME)
191: && !m_securityContext.isAuthenticated()) {
192: isGuest = true;
193: }
194: return isGuest;
195: }
196:
197: /**
198: * Returns an EntityIdentifier for this person. The key contains the value
199: * of the eudPerson username attribute, or null
200: *
201: * @return EntityIdentifier with attribute 'username' as key.
202: */
203: public EntityIdentifier getEntityIdentifier() {
204: return m_eid;
205: }
206:
207: /**
208: * One time set of the entity identifier
209: * @param ei
210: */
211: public void setEntityIdentifier(final EntityIdentifier ei) {
212: m_eid = ei;
213: entityIdentifierSet = true;
214: }
215:
216: public String toString() {
217: StringBuffer sb = new StringBuffer();
218: sb.append(PersonImpl.class.getName());
219: sb.append(" fullName=[");
220: sb.append(this .m_FullName);
221: sb.append("]");
222: sb.append(" id=[");
223: sb.append(this .m_ID);
224: sb.append("]");
225: sb.append(" securityContext=[");
226: sb.append(this .m_securityContext);
227: sb.append("]");
228: sb.append(" attributes=[");
229: sb.append(this .m_Attributes);
230: sb.append("]");
231: sb.append(" isGuest:");
232: sb.append(this.isGuest());
233: return sb.toString();
234: }
235: }
|