Dépôt officiel du code source de l'ERP OpenConcerto
Rev 93 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.sql.users.rights;
import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.TM;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.element.SQLElementDirectory;
import org.openconcerto.sql.model.SQLInsert;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.sql.model.SQLTableEvent;
import org.openconcerto.sql.model.SQLTableEvent.Mode;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.request.ListSQLRequest;
import org.openconcerto.sql.sqlobject.IComboSelectionItem;
import org.openconcerto.sql.users.UserManager;
import org.openconcerto.sql.view.ListeModifyPanel;
import org.openconcerto.sql.view.list.RowAction;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.utils.ExceptionHandler;
import org.openconcerto.utils.Tuple2;
import org.openconcerto.utils.cc.IClosure;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
public class UserRightsPanel extends JPanel {
// Liste des utilisateurs
private final JListSQLTablePanel list;
private final ListeModifyPanel modifPanel;
public UserRightsPanel() {
this(Configuration.getInstance().getDirectory());
}
public UserRightsPanel(final SQLElementDirectory dir) {
super(new GridBagLayout());
// init the list before adding it, otherwise we see the first refresh from all lines to just
// these of undef
this.modifPanel = new ListeModifyPanel(dir.getElement(UserRightSQLElement.class)) {
@Override
protected void handleAction(JButton source, ActionEvent evt) {
super.handleAction(source, evt);
// if the user click add and the create frame is then hidden (not closed) and he
// later click add again the create frame will be brought to the front, its content
// unmodified even if he has selected another principal. So call reset() so that the
// principal in the create frame match the selected principal.
if (source == this.buttonAjouter) {
this.getAddComp().resetValue();
}
}
};
// don't listen to the list, as the behaviour differs if there's some rights for a principal
// or not. I.e. if the user clicks add the current principal is selected :
// - if the user select another principal with rights, the principal is updated
// - if the user select another principal without rights, no lines are available and the
// principal is not updated
this.modifPanel.setDeaf(true);
this.modifPanel.setSearchFullMode(false);
// order is important for rights
this.modifPanel.getListe().setSortingEnabled(false);
final SQLTable table = this.getTable().getForeignTable("ID_USER_COMMON");
final SQLElement usersElem = dir.getElement(table);
this.list = new JListSQLTablePanel(JListSQLTablePanel.createComboRequest(usersElem, true), TM.tr("rightsPanel.defaultRights"));
// only superusers can see superusers (that's how we prevent the setting of superuser
// rights)
if (!UserRightsManager.getCurrentUserRights().isSuperUser())
this.list.getModel().setWhere(new Where(table.getField("SUPERUSER"), "=", false));
this.list.getModel().setItemCustomizer(new IClosure<IComboSelectionItem>() {
@Override
public void executeChecked(IComboSelectionItem input) {
if (input.getId() == UserManager.getUserID())
input.setFlag(IComboSelectionItem.IMPORTANT_FLAG);
}
});
this.list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
updateListFromSelection();
}
});
this.updateListFromSelection();
// Liste des utilisateurs
JPanel listePanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new DefaultGridBagConstraints();
c.weightx = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
listePanel.add(new JLabel(TM.getInstance().trM("element.list", "element", usersElem.getName())), c);
c.weightx = 1;
c.weighty = 1;
c.gridy++;
c.fill = GridBagConstraints.BOTH;
listePanel.add(this.list, c);
// Droits
JPanel panelDroits = new JPanel(new GridBagLayout());
GridBagConstraints c2 = new DefaultGridBagConstraints();
c2.gridwidth = GridBagConstraints.REMAINDER;
panelDroits.add(new JLabel(TM.tr("rights")), c2);
c2.gridy++;
c2.weightx = 1;
c2.weighty = 0.7;
c2.fill = GridBagConstraints.BOTH;
panelDroits.add(new JScrollPane(this.modifPanel), c2);
// SplitPane
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listePanel, panelDroits);
GridBagConstraints c3 = new GridBagConstraints();
c3.weightx = 1;
c3.weighty = 1;
c3.fill = GridBagConstraints.BOTH;
this.add(pane, c3);
RowAction actionPaste = new RowAction(new AbstractAction("Coller") {
@Override
public void actionPerformed(ActionEvent e) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable clipboardContent = clipboard.getContents(this);
if (clipboardContent.isDataFlavorSupported(UserRightCopySelection.rightsFlavor)) {
JSONObject rights;
try {
rights = (JSONObject) clipboardContent.getTransferData(UserRightCopySelection.rightsFlavor);
addRights(rights);
} catch (UnsupportedFlavorException | IOException e1) {
e1.printStackTrace();
}
}
}
}, true) {
@Override
public boolean enabledFor(List<SQLRowValues> selection) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable clipboardContent = clipboard.getContents(this);
return clipboardContent.isDataFlavorSupported(UserRightCopySelection.rightsFlavor);
}
};
this.modifPanel.getListe().addIListeAction(actionPaste);
}
private void updateListFromSelection() {
final int selectedIndex = this.list.getSelectedIndex();
final boolean b = selectedIndex >= 0;
final ListSQLRequest req = this.modifPanel.getListe().getRequest();
final int userID;
if (b) {
userID = this.list.getModel().getRowAt(selectedIndex).getID();
} else {
// since we don't display user in the list (to avoid undef)
// we need to always display at most one user
userID = this.list.getModel().getTable().getUndefinedID();
}
req.setWhere(new Where(req.getPrimaryTable().getField("ID_USER_COMMON"), "=", userID));
// enforce the limitation
((UserRightSQLComponent) this.modifPanel.getModifComp()).setUserID(userID);
((UserRightSQLComponent) this.modifPanel.getAddComp()).setUserID(userID);
}
private void addRights(JSONObject rights) {
// Récupération des droits courants
final ListSQLRequest req = this.modifPanel.getListe().getRequest();
List<SQLRowValues> vals = req.getValues();
Map<Tuple2<Number, String>, SQLRowValues> currentUserRight = new HashMap<>();
for (SQLRowValues sqlRowValues : vals) {
currentUserRight.put(Tuple2.create(sqlRowValues.getForeignIDNumber("ID_RIGHT"), sqlRowValues.getString("OBJECT")), sqlRowValues);
}
try {
List<SQLInsert> inserts = new ArrayList<SQLInsert>();
final int selectedIndex = this.list.getSelectedIndex();
final boolean b = selectedIndex >= 0;
final int userID;
if (b) {
userID = this.list.getModel().getRowAt(selectedIndex).getID();
} else {
// since we don't display user in the list (to avoid undef)
// we need to always display at most one user
userID = this.list.getModel().getTable().getUndefinedID();
}
BigDecimal order = getTable().getMaxOrder().add(BigDecimal.ONE);
for (Object a : rights.values()) {
JSONArray array = (JSONArray) a;
Number right = (Number) array.get(0);
String obj = (String) array.get(1);
Boolean hasRight = (Boolean) array.get(2);
final Tuple2<Number, String> key = Tuple2.create(right, obj);
if (currentUserRight.containsKey(key)) {
SQLRowValues rowVals = currentUserRight.get(key);
if (!rowVals.getBoolean("HAVE_RIGHT").equals(hasRight)) {
rowVals.createEmptyUpdateRow().put("HAVE_RIGHT", hasRight).commit();
}
} else {
SQLInsert insert = new SQLInsert();
insert.add(getTable().getField("ID_RIGHT"), right);
insert.add(getTable().getField("HAVE_RIGHT"), hasRight);
insert.add(getTable().getField("OBJECT"), obj);
insert.add(getTable().getField("ID_USER_COMMON"), userID);
insert.add(getTable().getOrderField(), order);
inserts.add(insert);
order = order.add(BigDecimal.ONE);
}
}
if (!inserts.isEmpty()) {
SQLInsert.executeMultiple(getTable().getDBSystemRoot(), inserts);
getTable().fire(new SQLTableEvent(getTable(), SQLRow.NONEXISTANT_ID, Mode.ROW_ADDED));
}
} catch (SQLException e1) {
ExceptionHandler.handle("Erreur lors de la duplication des droits", e1);
}
}
public final SQLTable getTable() {
return this.modifPanel.getElement().getTable();
}
}