OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Rev 73 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.sql.users.rights;
15
 
16
import org.openconcerto.sql.model.SQLTable;
17
import org.openconcerto.sql.model.Where;
18
import org.openconcerto.sql.request.ListSQLRequest;
19
import org.openconcerto.sql.sqlobject.IComboSelectionItem;
20
import org.openconcerto.sql.users.UserManager;
21
import org.openconcerto.sql.users.rights.UserRightSQLElement.UserRightComp;
22
import org.openconcerto.sql.view.ListeModifyPanel;
23
import org.openconcerto.ui.DefaultGridBagConstraints;
24
import org.openconcerto.utils.cc.IClosure;
25
 
26
import java.awt.GridBagConstraints;
27
import java.awt.GridBagLayout;
28
import java.util.Arrays;
29
 
30
import javax.swing.JLabel;
31
import javax.swing.JPanel;
32
import javax.swing.JScrollPane;
33
import javax.swing.JSplitPane;
34
import javax.swing.event.ListSelectionEvent;
35
import javax.swing.event.ListSelectionListener;
36
 
37
public class UserRightsPanel extends JPanel {
38
 
39
    // Liste des utilisateurs
40
    private final JListSQLTablePanel list;
41
    private final ListeModifyPanel modifPanel;
42
 
43
    public UserRightsPanel() {
44
        super(new GridBagLayout());
45
 
46
        // init the list before adding it, otherwise we see the first refresh from all lines to just
47
        // these of undef
48
        // instantiate our own element to be safe
49
        this.modifPanel = new ListeModifyPanel(new UserRightSQLElement());
50
        this.modifPanel.setSearchFullMode(false);
51
        final SQLTable table = this.getTable().getForeignTable("ID_USER_COMMON");
65 ilm 52
 
53
        this.list = new JListSQLTablePanel(JListSQLTablePanel.createComboRequest(table, true), "Droits par défaut");
17 ilm 54
        // only superusers can see superusers (that's how we prevent the setting of superuser
55
        // rights)
56
        if (!UserRightsManager.getCurrentUserRights().isSuperUser())
57
            this.list.getModel().setWhere(new Where(table.getField("SUPERUSER"), "=", false));
58
        this.list.getModel().setItemCustomizer(new IClosure<IComboSelectionItem>() {
59
            @Override
60
            public void executeChecked(IComboSelectionItem input) {
61
                if (input.getId() == UserManager.getUserID())
62
                    input.setFlag(IComboSelectionItem.IMPORTANT_FLAG);
63
            }
64
        });
65
        this.list.addListSelectionListener(new ListSelectionListener() {
66
            @Override
67
            public void valueChanged(ListSelectionEvent e) {
68
                updateListFromSelection();
69
            }
70
        });
71
        this.updateListFromSelection();
72
 
73
        // Liste des utilisateurs
74
        JPanel listePanel = new JPanel(new GridBagLayout());
75
        GridBagConstraints c = new DefaultGridBagConstraints();
76
        c.weightx = 1;
77
        c.gridwidth = GridBagConstraints.REMAINDER;
78
        listePanel.add(new JLabel("Liste des utilisateurs"), c);
79
 
80
        c.weightx = 1;
81
        c.weighty = 1;
82
        c.gridy++;
83
        c.fill = GridBagConstraints.BOTH;
84
        listePanel.add(this.list, c);
85
 
86
        // Droits
87
        JPanel panelDroits = new JPanel(new GridBagLayout());
88
        GridBagConstraints c2 = new DefaultGridBagConstraints();
89
        c2.gridwidth = GridBagConstraints.REMAINDER;
90
        panelDroits.add(new JLabel("Droits"), c2);
91
        c2.gridy++;
92
        c2.weightx = 1;
93
        c2.weighty = 0.7;
94
        c2.fill = GridBagConstraints.BOTH;
95
 
96
        panelDroits.add(new JScrollPane(this.modifPanel), c2);
97
 
98
        // SplitPane
99
        JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listePanel, panelDroits);
100
        GridBagConstraints c3 = new GridBagConstraints();
101
        c3.weightx = 1;
102
        c3.weighty = 1;
103
        c3.fill = GridBagConstraints.BOTH;
104
        this.add(pane, c3);
105
    }
106
 
107
    private void updateListFromSelection() {
108
        final int selectedIndex = this.list.getSelectedIndex();
109
 
110
        final boolean b = selectedIndex >= 0;
111
 
112
        final ListSQLRequest req = this.modifPanel.getListe().getRequest();
113
        final int userID;
114
        if (b) {
115
            userID = this.list.getModel().getRowAt(selectedIndex).getID();
116
        } else {
117
            // since we don't display user in the list (to avoid undef)
118
            // we need to always display at most one user
119
            userID = this.list.getModel().getTable().getUndefinedID();
120
        }
121
        req.setWhere(new Where(req.getPrimaryTable().getField("ID_USER_COMMON"), "=", userID));
122
 
123
        // enforce the limitation
124
        ((UserRightComp) this.modifPanel.getModifComp()).setUserID(userID);
125
        ((UserRightComp) this.modifPanel.getAddComp()).setUserID(userID);
126
    }
127
 
128
    public final SQLTable getTable() {
129
        return this.modifPanel.getElement().getTable();
130
    }
131
}