001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.modules.user;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Component;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ItemEvent;
024: import java.awt.event.ItemListener;
025: import java.util.List;
026: import java.util.Vector;
027: import java.util.logging.Level;
028: import java.util.logging.Logger;
029:
030: import javax.swing.Action;
031: import javax.swing.JComboBox;
032: import javax.swing.JLabel;
033: import javax.swing.JList;
034: import javax.swing.JPanel;
035: import javax.swing.JScrollPane;
036: import javax.swing.ListCellRenderer;
037: import javax.swing.ListSelectionModel;
038: import javax.swing.border.TitledBorder;
039:
040: import org.w3c.dom.Element;
041: import org.w3c.dom.Node;
042: import org.w3c.dom.NodeList;
043:
044: import de.finix.contelligent.client.base.UserManager;
045: import de.finix.contelligent.client.gui.ContelligentAction;
046: import de.finix.contelligent.client.i18n.Resources;
047: import de.finix.contelligent.client.modules.AbstractModule;
048: import de.finix.contelligent.client.modules.ModuleInitException;
049: import de.finix.contelligent.client.remote.ActionResult;
050: import de.finix.contelligent.client.remote.Actions;
051: import de.finix.contelligent.client.remote.RemoteActionException;
052: import de.finix.contelligent.client.util.ActionBundle;
053: import de.finix.contelligent.client.util.ExceptionDialog;
054: import de.finix.contelligent.client.util.MnemonicAbstractAction;
055: import de.finix.contelligent.client.util.TableLayout;
056: import de.finix.contelligent.client.util.list.NameValueComboBox;
057: import de.finix.contelligent.client.util.list.NameValueListModel;
058: import de.finix.contelligent.client.util.xml.XMLUtil;
059:
060: public class PrivilegeModule extends AbstractModule {
061:
062: private static Logger logger = Logger
063: .getLogger(PrivilegeModule.class.getName());
064:
065: private PrivilegeTable privilegeTable;
066:
067: private PrivilegeTableModel privilegeTableModel;
068:
069: private JComboBox privCombo;
070:
071: private NameValueComboBox groupCombo;
072:
073: private NameValueComboBox nameCombo;
074:
075: private boolean isEditMode = false;
076:
077: private ReloadAction reloadAction;
078:
079: private BeginAction beginAction;
080:
081: private CommitAction commitAction;
082:
083: private RollbackAction rollbackAction;
084:
085: private AddPrivilegeAction addPrivilegeAction;
086:
087: private RemovePrivilegeAction removePrivilegeAction;
088:
089: private static final String[] privileges = { "ignore_acls",
090: "ignore_ownership", "force_unlock", "bypass_locks",
091: "security_critical", "edit_users", "ignore_finals",
092: "configure_privileges", "access_jmx",
093: "preview_as_any_role", "component_transfer" };
094:
095: public Action[] getActions() {
096: if (isEditMode) {
097: return new Action[] { commitAction, rollbackAction,
098: addPrivilegeAction, removePrivilegeAction };
099: } else {
100: return new Action[] { reloadAction, beginAction };
101: }
102: }
103:
104: public void init() throws ModuleInitException {
105: reloadAction = new ReloadAction();
106: beginAction = new BeginAction();
107: commitAction = new CommitAction();
108: rollbackAction = new RollbackAction();
109: addPrivilegeAction = new AddPrivilegeAction();
110: removePrivilegeAction = new RemovePrivilegeAction();
111: removeAll();
112: setLayout(new TableLayout(
113: new double[][] {
114: { TableLayout.FILL, TableLayout.FILL,
115: TableLayout.FILL },
116: { TableLayout.PREFERRED, TableLayout.FILL } }));
117: JPanel usersPanel = new JPanel(new BorderLayout());
118: usersPanel.setOpaque(false);
119: usersPanel.setBorder(new TitledBorder("privileges"));
120: JPanel roleinfo = new JPanel(new BorderLayout());
121: roleinfo.setOpaque(false);
122: privilegeTableModel = new PrivilegeTableModel(new Vector(),
123: new Vector());
124: privilegeTable = new PrivilegeTable(privilegeTableModel);
125: privilegeTable
126: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
127: privilegeTable.setColumnSelectionAllowed(false);
128: privilegeTable.setRowSelectionAllowed(true);
129:
130: JScrollPane roleScroller = new JScrollPane(privilegeTable);
131: roleScroller.getViewport().setOpaque(false);
132: roleScroller.setOpaque(false);
133: roleinfo.add(roleScroller, BorderLayout.CENTER);
134:
135: privCombo = new JComboBox(privileges);
136: privCombo.setRenderer(new PrivilegeListCellRenderer());
137:
138: nameCombo = new NameValueComboBox();
139:
140: NameValueListModel groups = UserManager.getInstance()
141: .getGroupsDescriptions();
142: groupCombo = new NameValueComboBox(groups);
143: groupCombo.addItemListener(new ItemListener() {
144: public void itemStateChanged(ItemEvent e) {
145: String selection = (String) groupCombo
146: .getSelectedItemValue();
147: if (selection != null) {
148: nameCombo.setModel(UserManager.getInstance()
149: .getPrincipalsDescriptions(selection,
150: false, true));
151: if (nameCombo.getItemCount() > 0) {
152: nameCombo.setSelectedIndex(0);
153: }
154: }
155: }
156: });
157:
158: add(privCombo, "0,0");
159: add(groupCombo, "1,0");
160: add(nameCombo, "2,0");
161: add(roleinfo, "0,1,2,0");
162:
163: reload();
164: }
165:
166: private void reload() {
167: try {
168: ActionResult res = Actions.getPrivileges();
169: if (res.hasError()) {
170: privilegeTableModel = new PrivilegeTableModel(
171: new Vector(), new Vector());
172: privilegeTable.setModel(privilegeTableModel);
173: res.showErrors();
174: } else {
175: Element content = res.getContent();
176: NodeList childNodes = content.getChildNodes();
177: List<String> names = new Vector<String>();
178: List<String> principals = new Vector<String>();
179: for (int i = 0; i < childNodes.getLength(); i++) {
180: Node childElement = (Node) childNodes.item(i);
181: if (childElement.getNodeName().equals("privileges")) {
182: NodeList subNodes = childElement
183: .getChildNodes();
184: for (int j = 0; j < subNodes.getLength(); j++) {
185: Node subElement = (Node) subNodes.item(j);
186: if (subElement.getNodeName().equals(
187: "privilege")) {
188: String privName = XMLUtil
189: .getAttributeValue(subElement,
190: "name");
191: String principal = XMLUtil
192: .getAttributeValue(subElement,
193: "principal");
194: names.add(privName);
195: principals.add(principal);
196: }
197: }
198: }
199: }
200: privilegeTableModel = new PrivilegeTableModel(names,
201: principals);
202: privilegeTable.setModel(privilegeTableModel);
203: }
204: } catch (RemoteActionException rae) {
205: privilegeTableModel = new PrivilegeTableModel(new Vector(),
206: new Vector());
207: privilegeTable.setModel(privilegeTableModel);
208: logger.log(Level.SEVERE, "Could not load privileges.", rae);
209: ExceptionDialog.show(rae);
210: }
211: update();
212: }
213:
214: private void setEditable(boolean editable) {
215: this .isEditMode = editable;
216: update();
217: }
218:
219: private void update() {
220: privCombo.setEnabled(isEditMode);
221: groupCombo.setEnabled(isEditMode);
222: nameCombo.setEnabled(isEditMode);
223: privilegeTableModel.setEditable(isEditMode);
224: privilegeTable.clearSelection();
225: privilegeTable.setEnabled(isEditMode);
226: setEnabled(isEditMode);
227: beginAction.update();
228: reloadAction.update();
229: commitAction.update();
230: rollbackAction.update();
231: addPrivilegeAction.update();
232: removePrivilegeAction.update();
233: if (isActive()) {
234: getMenuAndToolbarComposer().removeActionBundle(
235: ActionBundle.MODULE_ACTIONS);
236: getMenuAndToolbarComposer().addTemporaryActionBundle(
237: ActionBundle.MODULE_ACTIONS, getActions());
238: }
239: }
240:
241: private class ReloadAction extends MnemonicAbstractAction implements
242: ContelligentAction {
243:
244: public ReloadAction() {
245: super ("reload", Resources.reloadIcon);
246: putValue(ROLLOVER_ICON, Resources.reloadIconRollOver);
247: // putValue(SHORT_DESCRIPTION, "clear_log_action_description");
248: putValue(TYPE, PUSH_ACTION);
249: putValue(ACTION_TYPE, ADMINISTRATION_ACTION);
250: putValue(ACTION_GROUP, ADMINISTRATION_USERMGMT_GROUP);
251: putValue(ACTION_POS, ADMINISTRATION_USERMGMT_RELOAD);
252: putValue(MENU_TARGET, MENU);
253: putValue(BUTTON_TARGET, TOOLBAR);
254: }
255:
256: public void actionPerformed(ActionEvent e) {
257: reload();
258: }
259:
260: public void update() {
261: setEnabled(!isEditMode);
262: }
263: }
264:
265: private class BeginAction extends MnemonicAbstractAction implements
266: ContelligentAction {
267:
268: public BeginAction() {
269: super ("begin_edit_action", Resources.beginEditIcon);
270: putValue(ROLLOVER_ICON, Resources.beginEditIconRollOver);
271: // putValue(SHORT_DESCRIPTION, "clear_log_action_description");
272: putValue(TYPE, PUSH_ACTION);
273: putValue(ACTION_TYPE, ADMINISTRATION_ACTION);
274: putValue(ACTION_GROUP, ADMINISTRATION_USERMGMT_GROUP);
275: putValue(ACTION_POS, ADMINISTRATION_USERMGMT_BEGIN);
276: putValue(MENU_TARGET, MENU);
277: putValue(BUTTON_TARGET, TOOLBAR);
278: }
279:
280: public void actionPerformed(ActionEvent e) {
281: setEditable(true);
282: }
283:
284: public void update() {
285: setEnabled(!isEditMode);
286: }
287: }
288:
289: private class CommitAction extends MnemonicAbstractAction implements
290: ContelligentAction {
291:
292: public CommitAction() {
293: super ("save_edit_action", Resources.commitEditIcon);
294: putValue(ROLLOVER_ICON, Resources.commitEditIconRollOver);
295: // putValue(SHORT_DESCRIPTION, "clear_log_action_description");
296: putValue(TYPE, PUSH_ACTION);
297: putValue(ACTION_TYPE, ADMINISTRATION_ACTION);
298: putValue(ACTION_GROUP, ADMINISTRATION_USERMGMT_GROUP);
299: putValue(ACTION_POS, ADMINISTRATION_USERMGMT_COMMIT);
300: putValue(MENU_TARGET, MENU);
301: putValue(BUTTON_TARGET, TOOLBAR);
302: }
303:
304: public void actionPerformed(ActionEvent e) {
305: setEditable(false);
306: StringBuffer sb = new StringBuffer();
307: sb.append("<privileges>");
308: int rows = privilegeTableModel.getRowCount();
309: for (int i = 0; i < rows; i++) {
310: String priv = (String) privilegeTableModel.getValueAt(
311: i, 0);
312: String principal = (String) privilegeTableModel
313: .getValueAt(i, 1);
314: sb.append("<privilege name=\"");
315: sb.append(priv);
316: sb.append("\" principal=\"");
317: sb.append(principal);
318: sb.append("\"/>\n");
319: }
320: sb.append("</privileges>");
321: try {
322: ActionResult res = Actions.setPrivileges(sb.toString());
323: if (res.hasError()) {
324: res.showErrors();
325: }
326: } catch (RemoteActionException rae) {
327: logger.log(Level.SEVERE, "Could not store privileges.",
328: rae);
329: ExceptionDialog.show(rae);
330: }
331: reload();
332: }
333:
334: public void update() {
335: setEnabled(isEditMode);
336: }
337: }
338:
339: private class RollbackAction extends MnemonicAbstractAction
340: implements ContelligentAction {
341:
342: public RollbackAction() {
343: super ("discard_edit_action", Resources.rollbackEditIcon);
344: putValue(ROLLOVER_ICON, Resources.rollbackEditIconRollOver);
345: // putValue(SHORT_DESCRIPTION, "clear_log_action_description");
346: putValue(TYPE, PUSH_ACTION);
347: putValue(ACTION_TYPE, ADMINISTRATION_ACTION);
348: putValue(ACTION_GROUP, ADMINISTRATION_USERMGMT_GROUP);
349: putValue(ACTION_POS, ADMINISTRATION_USERMGMT_ROLLBACK);
350: putValue(MENU_TARGET, MENU);
351: putValue(BUTTON_TARGET, TOOLBAR);
352: }
353:
354: public void actionPerformed(ActionEvent e) {
355: setEditable(false);
356: reload();
357: }
358:
359: public void update() {
360: setEnabled(isEditMode);
361: }
362: }
363:
364: private class AddPrivilegeAction extends MnemonicAbstractAction
365: implements ContelligentAction {
366:
367: public AddPrivilegeAction() {
368: super ("add_component_action", Resources.genericAddIcon);
369: putValue(ROLLOVER_ICON, Resources.genericAddIconRollOver);
370: // putValue(SHORT_DESCRIPTION, "clear_log_action_description");
371: putValue(TYPE, PUSH_ACTION);
372: putValue(ACTION_TYPE, ADMINISTRATION_ACTION);
373: putValue(ACTION_GROUP, ADMINISTRATION_USERMGMT_GROUP);
374: putValue(ACTION_POS, ADMINISTRATION_USERMGMT_NEWUSER);
375: putValue(MENU_TARGET, MENU);
376: putValue(BUTTON_TARGET, TOOLBAR);
377: }
378:
379: public void actionPerformed(ActionEvent e) {
380: String priv = (String) privCombo.getSelectedItem();
381: String group = (String) groupCombo.getSelectedItemValue();
382: String name = (String) nameCombo.getSelectedItemName();
383: if ((priv == null) || (group == null) || (name == null)) {
384: return;
385: }
386: if ((priv.equals("")) || (group.equals(""))
387: || (name.equals(""))) {
388: return;
389: }
390: String principal = group + ":" + name;
391: int rows = privilegeTableModel.getRowCount();
392: for (int i = 0; i < rows; i++) {
393: if ((priv.equals(privilegeTableModel.getValueAt(i, 0)))
394: && (principal.equals(privilegeTableModel
395: .getValueAt(i, 1)))) {
396: return;
397: }
398: }
399: privilegeTableModel
400: .addRow(new String[] { priv, principal });
401: }
402:
403: public void update() {
404: setEnabled(isEditMode);
405: }
406: }
407:
408: private class RemovePrivilegeAction extends MnemonicAbstractAction
409: implements ContelligentAction {
410:
411: public RemovePrivilegeAction() {
412: super ("delete_component_action",
413: Resources.genericDeleteIcon);
414: putValue(ROLLOVER_ICON, Resources.genericAddIconRollOver);
415: // putValue(SHORT_DESCRIPTION, "clear_log_action_description");
416: putValue(TYPE, PUSH_ACTION);
417: putValue(ACTION_TYPE, ADMINISTRATION_ACTION);
418: putValue(ACTION_GROUP, ADMINISTRATION_USERMGMT_GROUP);
419: putValue(ACTION_POS, ADMINISTRATION_USERMGMT_DELUSER);
420: putValue(MENU_TARGET, MENU);
421: putValue(BUTTON_TARGET, TOOLBAR);
422: }
423:
424: public void actionPerformed(ActionEvent e) {
425: int row = privilegeTable.getSelectedRow();
426: if (row != -1) {
427: privilegeTableModel.removeRow(row);
428: }
429: }
430:
431: public void update() {
432: setEnabled(isEditMode);
433: }
434: }
435:
436: class PrivilegeListCellRenderer extends JLabel implements
437: ListCellRenderer {
438: public PrivilegeListCellRenderer() {
439: setOpaque(true);
440: setVerticalAlignment(CENTER);
441: }
442:
443: public Component getListCellRendererComponent(JList list,
444: Object value, int index, boolean isSelected,
445: boolean cellHasFocus) {
446: if (isSelected) {
447: setBackground(list.getSelectionBackground());
448: setForeground(list.getSelectionForeground());
449: } else {
450: setBackground(list.getBackground());
451: setForeground(list.getForeground());
452: }
453: setText(Resources.getLocalString("privilege_"
454: + (String) value));
455: setFont(list.getFont());
456: return this;
457: }
458: }
459: }
|