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: PermissionsBase.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.core.security.Access;
import com.dbxml.util.dbXMLException;
import java.io.PrintWriter;
/**
* PermissionsBase serves as a base class for permission related Commands.
*/
public abstract class PermissionsBase extends CommandBase {
private static final String ALL = "ALL";
private static final String RECURSIVE = "RECURSIVE";
private static final String[] STRS = {"NONE", "READ", "WRITE", "EXECUTE", "CREATE"};
private static final int[] INTS = {Access.NONE, Access.READ, Access.WRITE, Access.EXECUTE, Access.CREATE};
public void process() throws dbXMLException {
CollectionClient col = (CollectionClient)cl.getProperty(CommandLine.COLLECTION);
if ( col == null )
throw new dbXMLException("Collection context required");
String roleID = cl.getNextToken("Role ID");
int permissions = 0;
boolean recursive = false;
if ( !cl.hasMoreTokens() )
throw new dbXMLException("At least one permission must be specified");
while ( cl.hasMoreTokens() ) {
String mode = cl.getNextToken("Mode").toUpperCase();
boolean found = false;
for ( int i = 0; i < STRS.length; i++ ) {
if ( mode.equals(STRS[i]) ) {
permissions |= INTS[i];
found = true;
}
}
if ( mode.equals(ALL) ) {
for ( int i = 0; i < INTS.length; i++ )
permissions |= INTS[i];
found = true;
}
if ( mode.equals(RECURSIVE) ) {
recursive = true;
found = true;
}
if ( !found )
throw new dbXMLException("Unknown mode '"+mode+"'");
}
process(col, roleID, permissions, recursive);
StringBuffer sb = new StringBuffer();
for ( int i = 0; i < STRS.length; i++ ) {
if ( (permissions & INTS[i]) != 0 ) {
if ( sb.length() != 0 )
sb.append(", ");
sb.append(STRS[i]);
}
}
String perms = sb.toString();
sb = new StringBuffer();
if ( recursive )
sb.append("Recursive ");
sb.append(getAction());
sb.append(" Successful (");
sb.append(perms);
sb.append(")");
PrintWriter pw = cl.getWriter();
pw.println(sb.toString());
}
protected void process(CollectionClient col, String roleID, int permissions, boolean recursive) throws dbXMLException {
process(col.getCanonicalName(), roleID, permissions);
if ( recursive ) {
String[] children = col.listCollections();
for ( int i = 0; i < children.length; i++ ) {
CollectionClient child = col.getCollection(children[i]);
process(child, roleID, permissions, recursive);
}
}
}
protected abstract String getAction();
protected abstract void process(String path, String roleID, int permissions) throws dbXMLException;
}