package com.dbxml.db.client.tools.command;
/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: Role.java,v 1.7 2006/02/02 18:53:46 bradford Exp $
*/
import com.dbxml.db.client.CollectionClient;
import com.dbxml.db.client.tools.CommandLine;
import com.dbxml.db.common.security.AccessManagerClient;
import com.dbxml.util.dbXMLException;
import java.io.PrintWriter;
/**
* Role (ROLE) provides several Role management functions.
*/
public final class Role extends CommandBase {
private static final String ACTION_ADDUSER = "ADDUSER";
private static final String ACTION_RMUSER = "RMUSER";
private static final String ACTION_LSUSER = "LSUSER";
public void process() throws dbXMLException {
CollectionClient col = (CollectionClient)cl.getProperty(CommandLine.COLLECTION);
if ( col == null )
throw new dbXMLException("Collection context required");
AccessManagerClient manager = new AccessManagerClient(cl.getClient());
PrintWriter pw = cl.getWriter();
String roleID = cl.getNextToken("Role ID");
String action = cl.getNextToken("Role Action").toUpperCase();
if ( action.equals(ACTION_ADDUSER) ) {
String userID = cl.getNextToken("User ID");
manager.addRoleToUser(userID, roleID);
pw.println("User '"+userID+"' added to Role '"+roleID+"'");
}
else if ( action.equals(ACTION_RMUSER) ) {
String userID = cl.getNextToken("User ID");
manager.removeRoleFromUser(userID, roleID);
pw.println("User '"+userID+"' removed from Role '"+roleID+"'");
}
else if ( action.equals(ACTION_LSUSER) ) {
String[] list = manager.listUsersForRole(roleID);
int count = 0;
for ( int i = 0; i < list.length; i++ ) {
pw.println(list[i]);
count++;
}
if ( cl.isPropertyTrue(CommandLine.TOTALS) ) {
if ( count > 0 )
pw.println("-----------------------");
if ( count != 1 )
pw.println(count + " Users");
else
pw.println(count + " User");
}
}
else
throw new dbXMLException("Unknown action '"+action+"'");
}
}