Andrzej Szeszo
2013-06-04 453bf5a3004107204e1858532a38b3e703e32995
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
 
/*
 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
 */
 
package com.oracle.solaris.vp.panels.usermgr.client.swing;
 
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.swing.border.*;
 
import java.awt.*;
import java.awt.event.*;
import java.util.*;
 
import com.oracle.solaris.vp.util.misc.finder.Finder;
 
/**
 * SMC code adapted for Visual Panels
 *
 * Rights Profiles Panel for Rights Settings
 */
public class AuthRightsPanel extends JPanel {
 
 
    public static Arranger profPanel;
    private static DblTreeNode srcRoot;
    private static DblTreeNode dstRoot;
    private DefaultTreeModel srcModel, dstModel;
 
    private DblTreeNode [] profNodes;
    private ProfTreeNode [] profTreeNodes;
 
    private ProfTreeNode currProfTreeNode;
    private DblTreeNode currDblTreeNode;
 
    private String targetRightName;
    private String targetRight;
    private JPanel securityPanel;
 
    private UserManagedObject userObj = null;
    private UserMgrPanelDescriptor panelDesc = null;
 
    private Vector vAllRightObjs = null;
 
    private boolean isProfListOK = true;
 
    /**
     * Constructs a panel to contain supplementary rights properties for
     * the right object.
     */
    public AuthRightsPanel(UserMgrPanelDescriptor panelDesc,
        UserManagedObject userObj) {
 
    super();
    this.panelDesc = panelDesc;
    this.userObj = userObj;
 
    createGui();
    loadProfiles();
    } // constructor
 
 
    /**
     * Method for creating GUI
     */
    private void createGui() {
 
    // Set the panel layout
    GridBagConstraints gbc = new GridBagConstraints();
    this.setLayout(new GridBagLayout());
 
    // Excluded and Included rights panel
       securityPanel = null;
 
    profPanel = new Arranger("usermgr.advanced.auth_rights.available",
        "usermgr.advanced.auth_rights.granted");
 
    Dimension dimension = new Dimension(200, 300);
    profPanel.srcTree.setCellRenderer(new ProfileRenderer(this));
    profPanel.srcTree.setVisibleRowCount(10);
    profPanel.srcTree.setSize(dimension);
 
    profPanel.dstTree.setCellRenderer(new ProfileRenderer(this));
    profPanel.dstTree.setVisibleRowCount(10);
    profPanel.dstTree.setSize(dimension);
 
    Constraints.constrain(this, profPanel,
        0, 0, 1, 1,
        gbc.BOTH, gbc.CENTER,
        1.0, 1.0, 10, 10, 10, 10);
 
    } // end createGui
 
 
    /**
     * Load the Source and Destination trees for rights excluded and
     * included lists.
     */
    private void loadProfiles() {
 
    // Reset the data models. So old data is discarded
    profPanel.resetModels();
 
 
    // Initialize the excluded rights list with all profiles
    // except for the current one, since a profile can not be
    // assigned to itself. Note that all profiles are created
    // as top-level profiles.
 
    vAllRightObjs = getAvailableProfs();
 
    srcModel = (DefaultTreeModel) profPanel.srcTree.getModel();
    srcRoot = (DblTreeNode) srcModel.getRoot();
 
    dstModel = (DefaultTreeModel) profPanel.dstTree.getModel();
    dstRoot = (DblTreeNode) dstModel.getRoot();
 
    profNodes = new DblTreeNode[vAllRightObjs.size()];
    profTreeNodes = new ProfTreeNode[vAllRightObjs.size()];
 
    int n = 0;
    Enumeration e = vAllRightObjs.elements();
 
    // Build an array of tree nodes that can be used for sorting.
 
    while (e.hasMoreElements()) {
        String rightObj = (String)e.nextElement();
        String rightName = rightObj;
 
        profTreeNodes[n] = new ProfTreeNode(rightObj);
 
        n++;
    }
 
    // Sort the list of ProfTreeNode objects.
 
    if (profTreeNodes.length > 1) {
        NodeCompare comp = new NodeCompare();
        Sort.sort(profTreeNodes, comp);
    }
 
    // With the sorted tree nodes, now we build the src tree.
    for (int i = 0; i < profTreeNodes.length; i++) {
        profNodes[i] = new DblTreeNode((Object)profTreeNodes[i]);
 
        srcModel.insertNodeInto(profNodes[i], srcRoot, i);
        TreePath nodepath = new TreePath(profNodes[i].getPath());
        // Make initial display of excluded list
        profPanel.srcTree.scrollPathToVisible(nodepath);
    }
 
 
    // Set TreeModel listeners
    srcModel.addTreeModelListener(new TreeModelListener() {
 
        public void treeNodesInserted(TreeModelEvent e) {
        enableProfileChoices(e);
        }
 
        public void treeNodesRemoved(TreeModelEvent e) {
        disableProfileChoices(e);
        }
 
        public void treeNodesChanged(TreeModelEvent e) {}
 
        public void treeStructureChanged(TreeModelEvent e) {}
 
    });
 
    dstModel.addTreeModelListener(new TreeModelListener() {
 
        public void treeNodesInserted(TreeModelEvent e) {
        updateDstTree(e);
        }
 
        public void treeNodesRemoved(TreeModelEvent e) {
        updateDstTree(e);
        }
 
        public void treeNodesChanged(TreeModelEvent e) {}
 
        public void treeStructureChanged(TreeModelEvent e) {}
 
    });
 
    // Build subprofiles recursively
    for (int i = 0; i < profNodes.length; i++) {
        rebuildSubProfiles(profNodes[i], profNodes[i]);
    }
 
    String [] subProfList = null;
 
    // Get the list of profiles that are assigned to current user/role
    // (in user_attr entry).
    String rights = userObj.getAuthRights();
    if (rights != null) {
        subProfList = userObj.getAuthRights().split(",");
    }
 
    profPanel.setInitSelection();
    if (rights == null || subProfList == null) {
        return;
    }
 
    // Move subprofiles of current profile from excluded list
    // to included list.
    // Note that a dimmed profile will stay dimmed after the move.
    // Therefore, a user can not "unassign" a dimmed profile on the
    // included list.
 
 
    for (int j = 0; j < subProfList.length; j++) {
        boolean foundSubProf = false;
 
        int count = 0;
        for (int i = 0; i < profNodes.length; i++) {
        String profString = profNodes[i].toString();
 
        if (profString.equals(subProfList[j])) {
            foundSubProf = true;
            count = i;
            break;
        }
 
        }
 
        if (foundSubProf) {
        profPanel.initLeaf(profPanel.srcTree,
            profPanel.dstTree, profNodes[count]);
        }
    }
 
    profPanel.setInitSelection();
 
    } // end loadProfiles
 
 
    private void updateDstTree(TreeModelEvent e) {
 
    Vector<DblTreeNode> inclProfsVec = new Vector<DblTreeNode>();
    Enumeration inclProfsEnum = dstRoot.breadthFirstEnumeration();
 
    while (inclProfsEnum.hasMoreElements()) {
        DblTreeNode node = (DblTreeNode)inclProfsEnum.nextElement();
        inclProfsVec.addElement(node);
        node.setConflict(false);
    }
 
    // Maintain included names as array as optimization
 
    DblTreeNode [] inclProfs = new DblTreeNode[inclProfsVec.size()];
    inclProfsVec.copyInto(inclProfs);
 
    if (inclProfs.length < 2)
        return;
 
    for (int i = 0; i < inclProfs.length; i++) {
        String testString = inclProfs[i].toString();
 
        for (int j = i + 1; j < inclProfs.length; j++) {
        if (testString.equals(inclProfs[j].toString())) {
            inclProfs[j].setConflict(true);
            inclProfs[i].setConflict(true);
            TreeNode [] nodes = inclProfs[j].getPath();
            DblTreeNode parentProf = (DblTreeNode)nodes[1];
            parentProf.setConflict(true);
        }
        }
    }
 
    } // end updateDstTree
 
 
    private void enableProfileChoices(TreeModelEvent e) {
 
    // Get names of currently included profiles
    if (e.getPath().length > 1)
        return;
 
    Vector<String> inclProfsVec = new Vector<String>();
    Enumeration inclProfs = dstRoot.depthFirstEnumeration();
 
    while (inclProfs.hasMoreElements()) {
        inclProfsVec.addElement(inclProfs.nextElement().toString());
    }
 
    // Maintain included names as array as optimization
 
    String [] inclProfNames = new String[inclProfsVec.size()];
    inclProfsVec.copyInto(inclProfNames);
 
    // Look for excluded list items needing updating
 
    Enumeration topProfs = srcRoot.children();
    while (topProfs.hasMoreElements()) {
        DblTreeNode nextTop = (DblTreeNode)topProfs.nextElement();
        boolean matchFound = false;
        Enumeration exclProfs = nextTop.depthFirstEnumeration();
 
        while (exclProfs.hasMoreElements()) {
        String testString = exclProfs.nextElement().toString();
        for (int i = 0; i < inclProfNames.length; i++) {
            if (testString.equals(inclProfNames[i])) {
            matchFound = true;
            break;
            }
        }
        if (matchFound)
            break;
        }
 
        // A Profile hierarchy needs to be enabled
 
        if (!matchFound) {
        exclProfs = nextTop.depthFirstEnumeration();
        while (exclProfs.hasMoreElements()) {
            DblTreeNode onNode =
            (DblTreeNode) exclProfs.nextElement();
            onNode.setConflict(false);
        }
        nextTop.setConflict(false);
        }
    }
 
    } // end enableProfileChoices
 
 
    private void disableProfileChoices(TreeModelEvent e) {
 
    if (e.getPath().length > 1)
        return;
 
    Object[] children = e.getChildren();
    DblTreeNode node = (DblTreeNode)children[0];
    Enumeration kids = node.breadthFirstEnumeration();
    while (kids.hasMoreElements()) {
        disableRecursively((DblTreeNode)kids.nextElement());
    }
 
    } // end disableProfileChoices
 
 
    private void disableRecursively(DblTreeNode node) {
 
    String testString = node.toString();
    Enumeration exclProfs = srcRoot.breadthFirstEnumeration();
    while (exclProfs.hasMoreElements()) {
        DblTreeNode exclNode = (DblTreeNode)exclProfs.nextElement();
        if (testString.equals(exclNode.toString())) {
        TreeNode [] nodes = exclNode.getPath();
 
        DblTreeNode parentProf = (DblTreeNode)nodes[1];
        if (!parentProf.isConflict()) {
            parentProf.setConflict(true);
            exclNode.setConflict(true);
            }
        }
    }
 
    } // end disableRecursively
 
 
    private void rebuildSubProfiles(DblTreeNode profNode, DblTreeNode topNode) {
 
    // First unlink the old children
    int childCount = profNode.getChildCount();
    for (int i = 0; i < childCount; i++) {
        DblTreeNode child = (DblTreeNode) srcModel.getChild(profNode, 0);
        srcModel.removeNodeFromParent(child);
    }
 
    // Then recursively construct new nodes for all the children
    DblTreeNode childNode;
    int i = 0;
    Enumeration topList = topNode.breadthFirstEnumeration();
 
    // Find sub-profile list in RightObj of cached tree node
    ProfTreeNode pTreeNode = (ProfTreeNode)profNode.getUserObject();
    String rObj = pTreeNode.getRightObj();
    // ProfAttrObj targetProfAttr = rObj.getProfAttr();
 
    String [] profList = null; // targetProfAttr.getProfNames();
 
    if (profList == null)
        return;
 
    for (int k = 0; k < profList.length; k++) {
        String name = profList[k];
 
        // Prevent infinite recursive loops
        boolean cyclic = false;
        while (topList.hasMoreElements()) {
        if (name.equals(topList.nextElement().toString())) {
            cyclic = true;
            // Mark the parent as cyclic
            topNode.setCyclic(true);
            Enumeration dupNodes = topNode.breadthFirstEnumeration();
            while (dupNodes.hasMoreElements()) {
            childNode = (DblTreeNode)dupNodes.nextElement();
            if (name.equals(childNode.toString())) {
                childNode.setCyclic(true);
            }
            }
            break;
        }
        }
 
        for (int j = 0; j < profTreeNodes.length; j++) {
        if (name.equals(profTreeNodes[j].toString())) {
            childNode = new DblTreeNode((Object)profTreeNodes[j]);
 
            srcModel.insertNodeInto(childNode, profNode, i++);
            childNode.setEnabled(false);
            if (cyclic)
            childNode.setCyclic(true);
            else
            rebuildSubProfiles(childNode, topNode);
        }
        }
    }
 
    } // end rebuildSubProfiles
 
 
    public String updateRightSubProps(String rightObj) {
 
    if (isProfListOK) {
        Vector<String> vAssignedProfs = new Vector<String>();
 
        // Get the first generation children that are assigned to
        // current profile.
        DblTreeNode root = (DblTreeNode)dstModel.getRoot();
        Enumeration kids;
        kids = root.children();
 
        while (kids.hasMoreElements()) {
        DblTreeNode childNode;
        childNode = (DblTreeNode)kids.nextElement();
        vAssignedProfs.addElement(childNode.toString());
        }
 
        // ProfAttrObj profAttr = rightObj.getProfAttr();
        // profAttr.setProfNamesVector(vAssignedProfs);
    }
    return (rightObj);
 
    } // end updateRightSubProps
 
    public Vector<String> getAssignedProfs() {
    Vector<String> vAssignedProfs = new Vector<String>();
 
    if (!isProfListOK) {
        return vAssignedProfs;
    }
 
    // Get the first generation children that are assigned to
    // current profile.
    DblTreeNode root = (DblTreeNode)dstModel.getRoot();
    Enumeration kids;
    kids = root.children();
    while (kids.hasMoreElements()) {
        DblTreeNode childNode;
        childNode = (DblTreeNode)kids.nextElement();
        vAssignedProfs.addElement(childNode.toString());
    }
 
    return vAssignedProfs;
    }
 
    private Vector<String> getAvailableProfs() {
        Vector<String> rights = new Vector<String>();
    for (String s : panelDesc.getProfiles()) {
        rights.add(s);
 
    }
        return (rights);
    }
 
    public void setUser(UserManagedObject userObj) {
        this.userObj = userObj;
    loadProfiles();
    }
 
    public UserManagedObject updateUser() {
        Vector<String> vProfs = getAssignedProfs();
    String profStr;
 
    if (vProfs.size() > 0) {
        profStr = vProfs.elementAt(0);
    } else {
        profStr = "";
    }
 
    for (int i = 1; i < vProfs.size(); i++) {
        profStr = profStr.concat(",");
        profStr = profStr.concat(vProfs.elementAt(i));
        }
 
    userObj.getAuthRightsProperty().setValue(profStr);
 
    return (userObj);
    }
 
 
 
class ProfileRenderer extends DefaultTreeCellRenderer {
 
    private boolean selected;
    Icon warningIcon;
    AuthRightsPanel rightsPanel;
 
    public ProfileRenderer(AuthRightsPanel rightsPanel) {
 
    this.rightsPanel = rightsPanel;
    setClosedIcon(null);
    setOpenIcon(null);
    setLeafIcon(null);
        warningIcon = Finder.getIcon("images/warning.gif");
 
    }
 
 
    public Component getTreeCellRendererComponent(
    JTree tree,
    Object value,
    boolean selected,
    boolean expanded,
    boolean leaf,
    int row,
    boolean hasFocus) {
 
    FocusEvent e = null;
 
    this.selected = selected;
 
    DefaultTreeCellRenderer cr =
        (DefaultTreeCellRenderer)tree.getCellRenderer();
    cr.setLeafIcon(null);
    DblTreeNode node = (DblTreeNode)value;
 
    if (node.getParent() == null) {
        // maybe the same as row == 0
        setText(node.toString());
 
    } else if (node.getUserObject() instanceof ProfTreeNode) {
        ProfTreeNode profsdef = (ProfTreeNode)node.getUserObject();
        setText(profsdef.toString());
        // setIcon(actionIcon);
 
        if (node.isConflict())
        if (tree == rightsPanel.profPanel.dstTree)
            cr.setLeafIcon(warningIcon);
    }
 
    super.getTreeCellRendererComponent(tree, value, selected, expanded,
        leaf, row, hasFocus);
 
    return this;
 
    } // end getTreeCellRendererComponent
 
}
 
class ProfTreeNode {
 
    String name;
    String rightObj = null;
 
    public ProfTreeNode(String rightObj) {
    this.rightObj = rightObj;
    this.name = rightObj;
    }
 
    public String getRightObj() {
    return rightObj;
    }
 
    public String toString() {
    return name;
    }
 
}
 
class NodeCompare implements Compare {
 
    /**
     * The compare method compares two ProfTreeNode objects by comparing
     * their profile names.  The parameters are specified as Object class
     * objects for QuickSort.
     *
     * @param   a    The first Node.
     * @param   b    The second Node.
     *
     */
    public final int doCompare(Object a, Object b) {
 
    ProfTreeNode e1, e2;
    String e1Name, e2Name;
 
    e1 = (ProfTreeNode)a;
    e2 = (ProfTreeNode)b;
    e1Name = e1.toString();
    e2Name = e2.toString();
    return (e1Name.compareTo(e2Name));
 
    }
 
}
}