/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.core.internals;

import org.apache.log4j.*;
import com.silverwrist.venice.except.AccessError;
import com.silverwrist.venice.security.AuditRecord;
import com.silverwrist.venice.security.Role;
import com.silverwrist.venice.security.SecurityMonitor;

public class EnvUser extends EnvEngine
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Category logger = Category.getInstance(EnvUser.class);

  public static final String PERM_SHOWHIDDENCATS = "Global.ShowHiddenCategories";
  public static final String PERM_NOEMAILVERIFY = "Global.NoEmailVerify";
  public static final String PERM_SEEHIDDENCONTACTINFO = "Global.SeeHiddenContactInfo";
  public static final String PERM_SEARCHHIDDENCOMMUNITIES = "Global.SearchHiddenCommunities";
  public static final String PERM_SHOWHIDDENCOMMUNITIES = "Global.ShowHiddenCommunities";
  public static final String PERM_SEARCHHIDDENCATS = "Global.SearchHiddenCategories";
  public static final String PERM_SYSADMINACCESS = "Global.SysAdminAccess";
  public static final String PERM_PUBLISH_FP = "Global.PublishFP";
  public static final String PERM_DESIGNATEPFY = "Global.DesignatePFY";
  public static final String PERM_CREATECOMMUNITY = "Global.CreateCommunity";

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private UserBackend user;    // the user backend object
  private SecurityMonitor sm;  // security monitor at this level

  /*--------------------------------------------------------------------------------
   * Constructors
   *--------------------------------------------------------------------------------
   */

  public EnvUser(EnvEngine parent, UserBackend user)
  {
    super(parent);
    this.user = user;
    this.sm = getEngine().env_getSecurityMonitor(EngineBackend.SM_GLOBAL);

  } // end constructor

  protected EnvUser(EnvUser other)
  {
    super(other);
    this.user = other.user;
    this.sm = other.sm;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  protected boolean testPermissionDynamic(String symbol, String errormsg) throws AccessError
  {
    if (symbol.equals(PERM_CREATECOMMUNITY))
    { // the Create Community permission test
      Role r = getEngine().getParamRole(EngineBackend.ROLEP_CREATECOMMUNITY);
      if (r.isSatisfiedBy(user.realBaseLevel()))
	return true;
      logger.error("testPermission() fail for permission " + PERM_CREATECOMMUNITY);
      if (errormsg==null)
	errormsg = "You are not authorized to create new communities.";
      throw new AccessError(errormsg);

    } // end if

    return false;

  } // end testPermission

  protected boolean testPermissionDynamic(String symbol)
  {
    if (symbol.equals(PERM_CREATECOMMUNITY))
    { // do the "Create Community" test here
      Role r = getEngine().getParamRole(EngineBackend.ROLEP_CREATECOMMUNITY);
      return r.isSatisfiedBy(user.realBaseLevel());

    } // end if

    return false;

  } // end testPermission

  protected int levelSelf()
  {
    return user.realBaseLevel();
  }

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public AuditRecord newAudit(int type, String data1, String data2, String data3, String data4)
  {
    return new AuditRecord(type,getUserID(),user.userRemoteAddress(),data1,data2,data3,data4);

  } // end newAudit

  public AuditRecord newAudit(int type, String data1, String data2, String data3)
  {
    return new AuditRecord(type,getUserID(),user.userRemoteAddress(),data1,data2,data3);

  } // end newAudit

  public AuditRecord newAudit(int type, String data1, String data2)
  {
    return new AuditRecord(type,getUserID(),user.userRemoteAddress(),data1,data2);

  } // end newAudit

  public AuditRecord newAudit(int type, String data1)
  {
    return new AuditRecord(type,getUserID(),user.userRemoteAddress(),data1);

  } // end newAudit

  public AuditRecord newAudit(int type)
  {
    return new AuditRecord(type,getUserID(),user.userRemoteAddress());

  } // end newAudit

  public boolean testPermission(String symbol, String errormsg) throws AccessError
  {
    if (testPermissionDynamic(symbol,errormsg))
      return true;
    if (symbol.equals(PERM_CREATECOMMUNITY))
    { // the Create Community permission test
      Role r = getEngine().getParamRole(EngineBackend.ROLEP_CREATECOMMUNITY);
      if (r.isSatisfiedBy(user.realBaseLevel()))
	return true;
      logger.error("testPermission() fail for permission " + PERM_CREATECOMMUNITY);
      if (errormsg==null)
	errormsg = "You are not authorized to create new communities.";
      throw new AccessError(errormsg);

    } // end if

    return sm.testPermission(symbol,user.realBaseLevel(),errormsg);

  } // end testPermission

  public boolean testPermission(String symbol)
  {
    if (symbol.equals(PERM_CREATECOMMUNITY))
    { // do the "Create Community" test here
      Role r = getEngine().getParamRole(EngineBackend.ROLEP_CREATECOMMUNITY);
      return r.isSatisfiedBy(user.realBaseLevel());

    } // end if

    return sm.testPermission(symbol,user.realBaseLevel());

  } // end testPermission

  public boolean satisfy(Role role)
  {
    return role.isSatisfiedBy(user.realBaseLevel());

  } // end satisfy

  public final UserBackend getUser()
  {
    return user;

  } // end getUser

  public final int getUserID()
  {
    return user.realUID();

  } // end getUserID

  public final String getUserName()
  {
    return user.realUserName();

  } // end getUserName

  public final int getUserBaseLevel()
  {
    return user.realBaseLevel();

  } // end getUserBaseLevel

  public final Role getCommunityDefaultRole(String symbol)
  {
    return getEngine().env_getSecurityMonitor(EngineBackend.SM_COMMUNITY).getDefaultRole(symbol);

  } // end getCommunityDefaultRole

} // end class EnvUser
