/*
  Copyright (C) 2007 Helge Hess

  This file is part of JOPE.

  JOPE is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  JOPE is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with JOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/
package org.opengroupware.jope.appserver.publisher;

import org.opengroupware.jope.foundation.NSObject;
import org.opengroupware.jope.foundation.UString;

/**
 * JoUser
 * <p>
 * A simple default implementation of the IJoUser interface.
 */
public class JoUser extends NSObject implements IJoUser {
  
  protected IJoAuthenticator authenticator;
  protected String   name;
  protected String[] roles;
  
  public JoUser(IJoAuthenticator _auth, String _login, String[] _roles) {
    this.authenticator = _auth;
    this.name = _login;
    this.roles = _roles;
  }
  
  /* accessors */
  
  public String getName() {
    return this.name;
  }
  
  public IJoAuthenticator authenticator() {
    return this.authenticator;
  }
  
  /* roles */

  public String[] rolesInContext(IJoContext _ctx) {
    return this.roles;
  }
  
  public String[] rolesForObjectInContext(Object _object, IJoContext _ctx) {
    // TBD: object local roles
    /* 
     * TODO: collect all local roles (of the object and its parents, local
     * roles are stored in __ac_local_roles__ of the object in Zope. Note
     * that this attribute can be a callable returning the roles !
     */
    return this.rolesInContext(_ctx);
  }
  
  /* equality */
  
  @Override
  public int hashCode() {
    return this.name != null ? this.name.hashCode() : -1;
  }
  
  @Override
  public boolean equals(Object _other) {
    if (this == _other)
      return true;
    if (!(_other instanceof JoUser))
      return false;
    
    return ((JoUser)_other).isEqualToJoUser(this);
  }
  
  public boolean isEqualToJoUser(JoUser _user) {
    if (!(this.authenticator.equals(_user.authenticator)))
      return false; /* different scope */
    
    /* same scope, assume principal name is exact? */
    return this.getName().equals(_user.getName());
  }
  
  /* description */
  
  @Override
  public void appendAttributesToDescription(StringBuilder _d) {
    super.appendAttributesToDescription(_d);
    
    _d.append(" name=" + this.name);
    if (this.roles != null)
      _d.append(" roles=" + UString.componentsJoinedByString(this.roles,","));

    if (this.authenticator != null) {
      _d.append(" authenticator=");
      _d.append(this.authenticator);
    }
  }
}
