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: AccessManagerClient.java,v 1.2 2004/02/12 00:17:56 bradford Exp $
 */

import java.util.Map;

import com.dbxml.db.client.dbXMLClient;
import com.dbxml.db.client.xmlrpc.ConnectionManager;
import com.dbxml.util.dbXMLException;

/**
 * AccessManagerClient
 */

public final class AccessManagerClient extends ConnectionManager {
   private static final String PATH = "/system/accessmanager";

   public AccessManagerClient(dbXMLClient dbxmlClient) {
      super(dbxmlClient);
      setClientPath(PATH);
   }

   /**
    * listUsers returns a list of the available Users
    *
    * @return The User list
    */
   public String[] listUsers() throws dbXMLException {
      return executeList("listUsers", null);
   }

   /**
    * addUser adds a new user to the Database
    *
    * @param userID The User's ID
    */
   public void addUser(String userID) throws dbXMLException {
      execute("addUser", new Object[]{userID});
   }

   /**
    * setUserPassword sets the User's password.  A User cannot be
    * authenticated against Database resources without having a
    * password set.
    *
    * @param userID The User's ID
    * @param password The User's new password
    */
   public void setUserPassword(String userID, String password) throws dbXMLException {
      execute("setUserPassword", new Object[]{userID, password});
   }

   /**
    * listRolesForUser lists the Roles that the specified userID is assigned
    *
    * @param userID The User's ID
    * @return The Role list
    */
   public String[] listRolesForUser(String userID) throws dbXMLException {
      return executeList("listRolesForUser", new Object[]{userID});
   }

   /**
    * addRoleToUser adds the specified roleID to the specified userID
    *
    * @param userID The User's ID
    * @param roleID the Role ID to assign
    */
   public void addRoleToUser(String userID, String roleID) throws dbXMLException {
      execute("addRoleToUser", new Object[]{userID, roleID});
   }

   /**
    * removeRoleFromUser removes the specified roleID from the specified userID
    *
    * @param userID The User's ID
    * @param roleID the Role ID to remove
    */
   public void removeRoleFromUser(String userID, String roleID) throws dbXMLException {
      execute("removeRoleFromUser", new Object[]{userID, roleID});
   }

   /**
    * removeUser removes the specified userID from the Database
    *
    * @param userID The User's ID
    */
   public void removeUser(String userID) throws dbXMLException {
      execute("removeUser", new Object[]{userID});
   }

   /**
    * listRoles returns a list of the available Roles
    *
    * @return The Role list
    */
   public String[] listRoles() throws dbXMLException {
      return executeList("listRoles", null);
   }

   /**
    * listUsersForRole lists the Users that are assigned to a specified Role ID
    *
    * @param roleID The Role ID
    * @return The User list
    */
   public String[] listUsersForRole(String roleID) throws dbXMLException {
      return executeList("listUsersForRole", new Object[]{roleID});
   }

   /**
    * addRole adds a Role to the Database
    *
    * @param roleID The Role ID to add
    */
   public void addRole(String roleID) throws dbXMLException {
      execute("addRole", new Object[]{roleID});
   }

   /**
    * removeRole removes a Role from the Database
    *
    * @param roleID The Role ID to remove
    */
   public void removeRole(String roleID) throws dbXMLException {
      execute("removeRole", new Object[]{roleID});
   }

   /**
    * listAccessControl returns an access control list for the specified
    * path.  The access control list is a Map that keys role IDs to Integer
    * permission requirements.
    *
    * @param path The Path to check
    * @return The access control list
    */
   public Map listAccessControl(String path) throws dbXMLException {
      return executeMap("listAccessControl", new Object[]{path});
   }

   /**
    * grant grants access rights to the specified Role ID for the specified
    * path.
    *
    * @param path The path to modify
    * @param roleID The Role to grant permissions to
    * @param permissions A permission set to grant
    */
   public void grant(String path, String roleID, int permissions) throws dbXMLException {
      execute("grant", new Object[]{path, roleID, new Integer(permissions)});
   }

   /**
    * revoke revokes access rights from the specified role ID for the specified
    * path.
    *
    * @param path The path to modify
    * @param roleID The Role to grant permissions to
    * @param permissions A permission set to grant
    */
   public void revoke(String path, String roleID, int permissions) throws dbXMLException {
      execute("revoke", new Object[]{path, roleID, new Integer(permissions)});
   }
}


