package com.dbxml.db.common.security;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: AccessManager.java,v 1.3 2004/02/12 00:17:56 bradford Exp $
 */

import com.dbxml.db.core.security.*;

import com.dbxml.db.core.Collection;
import com.dbxml.db.core.DBException;
import com.dbxml.db.core.Database;
import com.dbxml.db.core.extension.SimpleExtension;
import java.util.HashMap;
import java.util.Map;

/**
 * AccessManager is a proxy facade that is used to expose AccessManager
 * functionality via Labrador.
 */

public final class AccessManager extends SimpleExtension {
   private AccessUtils utils;

   public AccessManager() {
   }

   public void setCollection(Collection collection) {
      super.setCollection(collection);
      Database database = collection.getDatabase();
      utils = new AccessUtils(database);
   }

   AccessUtils getAccessUtils() {
      return utils;
   }

   // User methods

   public String[] listUsers() throws DBException {
      return utils.listUsers();
   }

   public static final String[] PARAMS_addUser = {"userID"};

   public void addUser(String userID) throws DBException {
      User user = null;
      try {
         user = utils.readUser(userID);
         throw new InvalidUserException("User '"+userID+"' already exists");
      }
      catch ( DBException e ) {
         user = new User();
         user.setId(userID);
         utils.storeUser(user);
      }
   }

   public static final String[] PARAMS_setUserPassword = {"userID", "password"};

   public void setUserPassword(String userID, String password) throws DBException {
      User user = utils.readUser(userID);
      user.setPassword(password);
      utils.storeUser(user);
   }

   public static final String[] PARAMS_listRolesForUser = {"userID"};

   public String[] listRolesForUser(String userID) throws DBException {
      User user = utils.readUser(userID);
      return user.listRoles();
   }

   public static final String[] PARAMS_addRoleToUser = {"userID", "roleID"};

   public void addRoleToUser(String userID, String roleID) throws DBException {
      User user = utils.readUser(userID);
      Role role = utils.readRole(roleID);
      user.addRole(role);
      utils.storeUser(user);
   }

   public static final String[] PARAMS_removeRoleFromUser = {"userID", "roleID"};

   public void removeRoleFromUser(String userID, String roleID) throws DBException {
      User user = utils.readUser(userID);
      Role role = utils.readRole(roleID);
      user.removeRole(role);
      utils.storeUser(user);
   }

   public static final String[] PARAMS_removeUser = {"userID"};

   public void removeUser(String userID) throws DBException {
		User user = utils.readUser(userID);
      utils.removeUser(user);
   }

   // Role methods

   public String[] listRoles() throws DBException {
      return utils.listRoles();
   }

   public static final String[] PARAMS_listUsersForRole = {"roleID"};

   public String[] listUsersForRole(String roleID) throws DBException {
      return utils.listUsersForRole(roleID);
   }

   public static final String[] PARAMS_addRole = {"roleID"};

   public void addRole(String roleID) throws DBException {
      Role role = null;
      try {
         role = utils.readRole(roleID);
         throw new InvalidRoleException("Role '"+roleID+"' already exists");
      }
      catch ( DBException e ) {
         role = new Role();
         role.setId(roleID);
         utils.storeRole(role);
      }
   }

   public static final String[] PARAMS_removeRole = {"roleID"};

   public void removeRole(String roleID) throws DBException {
		Role role = utils.readRole(roleID);
      utils.removeRole(role);
   }

   // Access methods

   public static final String[] PARAMS_listAccessControl = {"path"};

   public Map listAccessControl(String path) throws DBException {
		Access access = utils.readAccess(path);

      Map data = new HashMap();
      String[] roles = access.listRoles();
      for ( int i = 0; i < roles.length; i++ ) {
         String roleID = roles[i];
			Role role = utils.readRole(roleID);
         int permissions = access.getPermissions(role);
         data.put(roleID, new Integer(permissions));
      }
      return data;
   }

   public static final String[] PARAMS_grant = {"path", "roleID", "permissions"};

   public void grant(String path, String roleID, int permissions) throws DBException {
      Access access = utils.readAccess(path);

		Role role = utils.readRole(roleID);
      int oldPermissions = access.getPermissions(role);
      access.setPermissions(role, oldPermissions | permissions);
      utils.storeAccess(access);
   }

   public static final String[] PARAMS_revoke = {"path", "roleID", "permissions"};

   public void revoke(String path, String roleID, int permissions) throws DBException {
      Access access = utils.readAccess(path);

		Role role = utils.readRole(roleID);
      int oldPermissions = access.getPermissions(role);
      access.setPermissions(role, oldPermissions & ~permissions);
      utils.storeAccess(access);
   }
}

