/*
  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
 * 
 * A simple default implementation of the IJoUser interface.
 */
public class JoUser extends NSObject implements IJoUser {
  
  protected IJoAuthenticator authenticator;
  protected String   login;
  protected String[] roles;
  
  public JoUser(IJoAuthenticator _auth, String _login, String[] _roles) {
    this.authenticator = _auth;
    this.login = _login;
    this.roles = _roles;
  }
  
  /* accessors */
  
  public String login() {
    return this.login;
  }
  
  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);
  }
  
  /* description */
  
  @Override
  public void appendAttributesToDescription(StringBuilder _d) {
    super.appendAttributesToDescription(_d);
    
    _d.append(" login=" + this.login);
    if (this.roles != null)
      _d.append(" roles=" + UString.componentsJoinedByString(this.roles,","));

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