package com.dbxml.db.admin.nodes;
/*
* 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: RoleNode.java,v 1.5 2006/02/02 18:53:46 bradford Exp $
*/
import com.dbxml.db.admin.Admin;
import com.dbxml.db.admin.dialogs.PermissionsDialog;
import com.dbxml.db.client.CollectionClient;
import com.dbxml.db.common.security.AccessManagerClient;
import com.dbxml.db.core.security.Access;
import com.dbxml.util.dbXMLException;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
/**
* RoleNode
*/
public final class RoleNode extends AdminNode implements HasMenu, HasCollection {
private static final int[] INTS = {Access.NONE, Access.READ, Access.WRITE, Access.EXECUTE, Access.CREATE};
private static final String[] STRS = {"None", "Read", "Write", "Execute", "Create"};
private static final Icon RoleIcon = new ImageIcon(RoleNode.class.getResource("role_mini.gif"));
public static final int DATABASE = 0;
public static final int COLLECTION = 1;
public static final int USER = 2;
public static final int ACTION_REMOVE_DATABASE = 0;
public static final int ACTION_PERMISSIONS = 1;
public static final int ACTION_REMOVE_USER = 2;
private CollectionClient col;
private String id;
private String userID;
private int type;
private int permissions;
public RoleNode(AdminNode parentNode, CollectionClient col, String id, int type, int permissions) {
this(parentNode, col, id, type);
this.permissions = permissions;
}
public RoleNode(AdminNode parentNode, CollectionClient col, String id, int type, String userID) {
this(parentNode, col, id, type);
this.userID = userID;
}
public RoleNode(AdminNode parentNode, CollectionClient col, String id, int type) {
super(parentNode);
this.col = col;
this.id = id;
this.type = type;
}
public String getLabel() {
return id;
}
public String getTooltip() {
if ( type == COLLECTION ) {
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]);
}
}
return sb.toString();
}
else
return super.getTooltip();
}
public Icon getIcon() {
return RoleIcon;
}
public CollectionClient getCollection() {
return col;
}
public MenuItem[] getMenu() {
switch ( type ) {
case RoleNode.DATABASE:
return new MenuItem[] {new MenuItem(ACTION_REMOVE_DATABASE, "Remove Role") };
case RoleNode.COLLECTION:
return new MenuItem[] {new MenuItem(ACTION_PERMISSIONS, "Modify Permissions") };
case RoleNode.USER:
return new MenuItem[] {new MenuItem(ACTION_REMOVE_USER, "Remove Role") };
}
return null;
}
public String getMenuTitle() {
return "Role";
}
public int menuAction(int action) {
Admin admin = Admin.getInstance();
AccessManagerClient manager = new AccessManagerClient(col.getClient());
switch ( action ) {
case ACTION_REMOVE_DATABASE:
case ACTION_REMOVE_USER:
try {
String title;
String message;
if ( type == USER ) {
title = "Remove Role from User";
message = "Are you sure you want to remove the\n"
+ "Role '"+id+"' from User '"+userID+"'?";
}
else {
title = "Remove Role from Database";
message = "Are you sure you want to remove the\n"
+ "Role '"+id+"' from the Database?";
}
int res = JOptionPane.showConfirmDialog(Admin.getInstance(), message, title, JOptionPane.YES_NO_OPTION);
if ( res == JOptionPane.YES_OPTION ) {
switch ( type ) {
case DATABASE:
manager.removeRole(id);
break;
case USER:
manager.removeRoleFromUser(userID, id);
break;
}
return REMOVE_SELF;
}
}
catch ( dbXMLException e ) {
admin.addMessage(e.getMessage());
}
break;
case ACTION_PERMISSIONS:
PermissionsDialog perms = new PermissionsDialog(admin, col, id);
perms.show();
return REFRESH_PARENT;
}
return REFRESH_NONE;
}
}