001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: Identified.java 3721 2007-04-28 19:35:35Z gbevin $
007: */
008: package com.uwyn.rife.authentication.elements;
009:
010: import com.uwyn.rife.authentication.SessionManager;
011: import com.uwyn.rife.authentication.SessionValidator;
012: import com.uwyn.rife.authentication.credentialsmanagers.IdentifiableUsersManager;
013: import com.uwyn.rife.authentication.credentialsmanagers.RoleUserAttributes;
014: import com.uwyn.rife.authentication.credentialsmanagers.RoleUserIdentity;
015: import com.uwyn.rife.authentication.exceptions.CredentialsManagerException;
016: import com.uwyn.rife.authentication.exceptions.SessionManagerException;
017: import com.uwyn.rife.engine.Element;
018: import com.uwyn.rife.engine.ElementInfo;
019: import com.uwyn.rife.engine.annotations.Elem;
020: import com.uwyn.rife.engine.exceptions.EngineException;
021:
022: @Elem
023: public class Identified extends Element {
024: public final static String IDENTITY_ATTRIBUTE_NAME = RoleUserIdentity.class
025: .getName();
026:
027: public String getAuthElementId() {
028: return getPropertyString("authElementId");
029: }
030:
031: public ElementInfo getAuthElement() {
032: return getSite()
033: .resolveId(getAuthElementId(), getElementInfo());
034: }
035:
036: public void processElement() {
037: child();
038: }
039:
040: public void setIdentityAttribute(String name, String[] values) {
041: RoleUserIdentity identity = getIdentity(name, values);
042: if (identity != null) {
043: setRequestAttribute(IDENTITY_ATTRIBUTE_NAME, identity);
044: }
045: }
046:
047: public RoleUserIdentity getIdentity(String name, String[] values) {
048: if (null == values || 0 == values.length) {
049: return null;
050: }
051:
052: String value = values[0];
053:
054: String login = null;
055: RoleUserAttributes attributes = null;
056: try {
057: ElementInfo authenticated = getAuthElement();
058: if (authenticated != null
059: && authenticated.getDeployer() != null
060: && authenticated.containsProperty("authvar_name")
061: && authenticated.containsProperty("authvar_type")
062: && authenticated.getDeployer() instanceof AuthenticatedDeployer) {
063: AuthenticatedDeployer deployer = (AuthenticatedDeployer) authenticated
064: .getDeployer();
065: SessionValidator validator = deployer
066: .getSessionValidator();
067: if (validator.getCredentialsManager() instanceof IdentifiableUsersManager) {
068: String authentication_request_attribute = Authenticated
069: .createAuthenticationRequestAttributeName(
070: getElementInfo(), name, values[0]);
071:
072: if (hasRequestAttribute(authentication_request_attribute)
073: || validator.getSessionManager()
074: .isSessionValid(value,
075: getRemoteAddr())) {
076: IdentifiableUsersManager credentials = ((IdentifiableUsersManager) validator
077: .getCredentialsManager());
078: SessionManager sessions = validator
079: .getSessionManager();
080: long userid = -1;
081: String authvar_name = authenticated
082: .getPropertyString("authvar_name");
083: if (authvar_name.equals(name)) {
084: userid = sessions.getSessionUserId(value);
085:
086: if (userid > -1) {
087: login = credentials.getLogin(userid);
088: if (!hasRequestAttribute(authentication_request_attribute)) {
089: sessions.continueSession(value);
090: }
091: }
092:
093: if (login != null) {
094: attributes = credentials
095: .getAttributes(login);
096: }
097: }
098: }
099: }
100: }
101: } catch (SessionManagerException e) {
102: throw new EngineException(e);
103: } catch (CredentialsManagerException e) {
104: throw new EngineException(e);
105: }
106:
107: if (login != null && attributes != null) {
108: return new RoleUserIdentity(login, attributes);
109: }
110:
111: return null;
112: }
113:
114: public boolean childTriggered(String name, String[] values) {
115: if (!hasRequestAttribute(IDENTITY_ATTRIBUTE_NAME)) {
116: setIdentityAttribute(name, values);
117: }
118:
119: return true;
120: }
121: }
|