001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.security;
011:
012: import org.mmbase.util.logging.Logger;
013: import org.mmbase.util.logging.Logging;
014:
015: /**
016: * A UserContext object is the result of an authentication, on which authorization can be
017: * based. Normally your authorization/authentication implementation will also provide an extension
018: * to this class.
019: *
020: * This default implementation is the most simple one, actually implementing 'no authorization'
021: * (because the rank is fixed to 'administrator').
022: *
023: * This class is <em>not</em> necessarily also the container class for the client's credentials,
024: * although this is possible.
025: *
026: * @author Eduard Witteveen
027: * @version $Id: BasicUser.java,v 1.6 2008/03/17 15:37:59 michiel Exp $
028: */
029: public class BasicUser implements UserContext {
030: private static final Logger log = Logging
031: .getLoggerInstance(BasicUser.class);
032:
033: protected final String authenticationType;
034: private final String identifier;
035:
036: public BasicUser(String authenticationType) {
037: this (authenticationType, "anonymous");
038: }
039:
040: public BasicUser(String authenticationType, String identifier) {
041: this .authenticationType = authenticationType;
042: this .identifier = identifier;
043: }
044:
045: /**
046: * Gets the unique identifier for this user. This should be unique
047: * for every different user on the system.
048: *
049: * @return A unique identifier for this user.
050: */
051: public String getIdentifier() {
052: return identifier;
053: }
054:
055: /**
056: * Gets the owner field value of new objects created by this user. The default implementation
057: * returns the user's identifier. This can be changed if the authorization implementation does
058: * not attribute rights to users directly ('context' implementations).
059: * @return A possible value for the owner field
060: */
061: public String getOwnerField() {
062: return getIdentifier();
063: }
064:
065: /**
066: * Gets the rank of this user.
067: * @return the user rank
068: */
069: public Rank getRank() throws org.mmbase.security.SecurityException {
070: // we need the highest rank.. to fool the security checks that we are allowed...
071: return Rank.ADMIN;
072: }
073:
074: /**
075: * Gets a string representation of this user context (for debugging)
076: * @return a string describing the usercontext
077: */
078: public String toString() {
079: return getIdentifier() + " (" + getRank() + ")";
080: }
081:
082: public boolean isValid() {
083: try {
084: return org.mmbase.module.core.MMBase.getMMBase()
085: .getMMBaseCop().getAuthentication().isValid(this );
086: } catch (Exception e) {
087: log.warn(e);
088: return false;
089: }
090: }
091:
092: public String getAuthenticationType() {
093: return authenticationType;
094: }
095:
096: public boolean equals(Object o) {
097: if (o instanceof BasicUser) {
098: BasicUser ou = (BasicUser) o;
099: return (authenticationType == null ? ou.authenticationType == null
100: : authenticationType.equals(ou.authenticationType))
101: && getIdentifier() == null ? ou.getIdentifier() == null
102: : getIdentifier().equals(ou.getIdentifier())
103: && getRank() == null ? ou.getRank() == null
104: : getRank().equals(ou.getRank());
105: } else {
106: return false;
107: }
108: }
109:
110: public int hashCode() {
111: int result = 0;
112: result = org.mmbase.util.HashCodeUtil.hashCode(result,
113: authenticationType);
114: return result;
115: }
116:
117: }
|