This is an automated email from the git hooks/post-receive script. New commit to branch feature/7549 in repository observe. See http://git.codelutin.com/observe.git commit bfcd27c09e6ba89f6879d500b611f953fbe88ba4 Author: Sylvain Bavencoff <bavencoff@codelutin.com> Date: Wed Sep 30 09:50:39 2015 +0200 migration des tâches administratives, mise ren place de la Sécurité (refs #7549) --- .../main/java/fr/ird/observe/ObserveCLAction.java | 6 - .../ird/observe/business/util/SecurityModel.java | 217 ---------- .../fr/ird/observe/db/ObserveSwingDataSource.java | 23 +- .../ird/observe/ui/storage/ObstunaAdminAction.java | 458 ++++++++++++--------- .../ird/observe/ui/storage/RemoteUILauncher.java | 48 +-- .../ird/observe/ui/storage/StorageUIHandler.java | 96 +++-- .../fr/ird/observe/ui/storage/StorageUIModel.java | 18 +- .../observe/ui/storage/tabs/RolesTableModel.java | 62 ++- .../fr/ird/observe/ui/storage/tabs/RolesUI.css | 2 +- .../fr/ird/observe/ui/storage/tabs/RolesUI.jaxx | 2 +- .../ird/observe/ui/storage/tabs/SecurityModel.java | 149 +++++++ 11 files changed, 565 insertions(+), 516 deletions(-) diff --git a/observe-application-swing/src/main/java/fr/ird/observe/ObserveCLAction.java b/observe-application-swing/src/main/java/fr/ird/observe/ObserveCLAction.java index a8c6130..d9c78c6 100644 --- a/observe-application-swing/src/main/java/fr/ird/observe/ObserveCLAction.java +++ b/observe-application-swing/src/main/java/fr/ird/observe/ObserveCLAction.java @@ -174,12 +174,6 @@ public class ObserveCLAction { public ObserveCLAction() { } - public void useJMX() throws Exception { - - // just set the useJMX flag - ObserveServiceHelper.setUseJMX(true); - } - /** Désactiver la possiblite de lancer l'ui principale. */ public void disableMainUI() { if (log.isDebugEnabled()) { diff --git a/observe-application-swing/src/main/java/fr/ird/observe/business/util/SecurityModel.java b/observe-application-swing/src/main/java/fr/ird/observe/business/util/SecurityModel.java deleted file mode 100644 index 878c1ff..0000000 --- a/observe-application-swing/src/main/java/fr/ird/observe/business/util/SecurityModel.java +++ /dev/null @@ -1,217 +0,0 @@ -/* - * #%L - * ObServe :: Business - * %% - * Copyright (C) 2008 - 2010 IRD, Codelutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ -package fr.ird.observe.business.util; - -import com.google.common.base.Function; -import com.google.common.collect.Iterables; -import com.google.common.collect.Sets; - -import java.beans.PropertyChangeListener; -import java.beans.PropertyChangeSupport; -import java.io.Serializable; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.SortedMap; -import java.util.TreeMap; - -/** - * Le modèle la sécurité à appliquer sur une base postgres - * - * @author Tony Chemit - chemit@codelutin.com - * @since 1.4 - */ -public class SecurityModel implements Serializable { - - public static final String PROPERTY_ADMINISTRATEUR = "administrateur"; - - public static final String PROPERTY_ROLE = "role"; - - public static final String PROPERTY_ASSIGNED = "assigned"; - - private static final long serialVersionUID = 1L; - - public static final Function<String, String> ESCAPE_STRING = new Function<String, String>() { - @Override - public String apply(String input) { - return "\"" + input + "\""; - } - }; - - public enum Role { - unused, - referentiel, - utilisateur, - technicien - } - - protected String administrateur; - - protected SortedMap<String, Role> map; - - protected PropertyChangeSupport pcs = new PropertyChangeSupport(this); - - public SecurityModel() { - map = new TreeMap<String, Role>(); - } - - public String getAdministrateur() { - return administrateur; - } - - public Role getRole(String role) { - return map.get(role); - } - - public Set<String> getRole(Role role) { - Set<String> roles = new HashSet<String>(); - for (Map.Entry<String, Role> entry : map.entrySet()) { - if (role.equals(entry.getValue())) { - roles.add(entry.getKey()); - } - } - return roles; - } - - public Set<String> getRole() { - return map.keySet(); - } - - public Set<String> getTechnicien() { - return getRole(Role.technicien); - } - - public Set<String> getUtilisateur() { - return getRole(Role.utilisateur); - } - - public Set<String> getReferentiel() { - return getRole(Role.referentiel); - } - - public Set<String> getEscapedReferentiel() { - Set<String> result = Sets.newHashSet( - Iterables.transform(getReferentiel(), ESCAPE_STRING)); - return result; - } - - public Set<String> getEscapedTechnicien() { - Set<String> result = Sets.newHashSet( - Iterables.transform(getTechnicien(), ESCAPE_STRING)); - return result; - } - - public Set<String> getEscapedUtilisateur() { - Set<String> result = Sets.newHashSet( - Iterables.transform(getUtilisateur(), ESCAPE_STRING)); - return result; - } - - public String getEscapedAdministrateur() { - return ESCAPE_STRING.apply(getAdministrateur()); - } - - public Set<String> getUnused() { - return getRole(Role.unused); - } - - public Set<String> getEscapedUnused() { - Set<String> result = Sets.newHashSet( - Iterables.transform(getUnused(), ESCAPE_STRING)); - return result; - } - - public boolean isRole(Role r, String role) { - return r.equals(getRole(role)); - } - - public void setAdministrateur(String administrateur) { - Object oldvalue = this.administrateur; - this.administrateur = administrateur; - firePropertyChange(PROPERTY_ADMINISTRATEUR, oldvalue, administrateur); - } - - public void init(Iterable<String> role) { - map.clear(); - if (role != null) { - for (String r : role) { - // by default let's say that any role is not used - // (make it quicker then to only give correct access) - map.put(r, Role.unused); - } - } - firePropertyChange(PROPERTY_ROLE, null, getRole()); - firePropertyChange(PROPERTY_ASSIGNED, null, getAssigned()); - } - - public void setRole(String name, Role role, boolean fire) { - map.put(name, role); - if (fire) { - if (role != null) { - firePropertyChange(role.name(), null, getRole(role)); - } - firePropertyChange(PROPERTY_ASSIGNED, null, getAssigned()); - } - } - - public void setRole(Iterable<String> roles, Role role) { - for (String r : roles) { - setRole(r, role, false); - } - firePropertyChange(role.name(), null, getRole(role)); - firePropertyChange(PROPERTY_ASSIGNED, null, getAssigned()); - } - - public int getAssigned() { - int count = 0; - for (Map.Entry<String, Role> entry : map.entrySet()) { - if (entry.getValue() != null) { - count++; - } - } - return count; - } - - public void addPropertyChangeListener(PropertyChangeListener listener) { - pcs.addPropertyChangeListener(listener); - } - - public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { - pcs.addPropertyChangeListener(propertyName, listener); - } - - public void removePropertyChangeListener(PropertyChangeListener listener) { - pcs.removePropertyChangeListener(listener); - } - - public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { - pcs.removePropertyChangeListener(propertyName, listener); - } - - protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { - pcs.firePropertyChange(propertyName, oldValue, newValue); - } - - protected void firePropertyChange(String propertyName, Object newValue) { - firePropertyChange(propertyName, null, newValue); - } -} diff --git a/observe-application-swing/src/main/java/fr/ird/observe/db/ObserveSwingDataSource.java b/observe-application-swing/src/main/java/fr/ird/observe/db/ObserveSwingDataSource.java index 7906baa..b49a5b4 100644 --- a/observe-application-swing/src/main/java/fr/ird/observe/db/ObserveSwingDataSource.java +++ b/observe-application-swing/src/main/java/fr/ird/observe/db/ObserveSwingDataSource.java @@ -17,6 +17,7 @@ import fr.ird.observe.services.configuration.ObserveDataSourceConnection; import fr.ird.observe.services.dto.DataSourceCreateConfigurationDto; import fr.ird.observe.services.dto.DataSourceCreateWithNoReferentialImportException; import fr.ird.observe.services.dto.IncompatibleDataSourceCreateConfigurationException; +import fr.ird.observe.services.dto.ObserveDbUserDto; import fr.ird.observe.services.dto.constants.ReferentialLocale; import fr.ird.observe.services.service.DataSourceService; import fr.ird.observe.services.service.DatabaseConnexionNotAuthorizedException; @@ -29,6 +30,7 @@ import org.nuiton.util.version.Version; import javax.swing.event.EventListenerList; import java.io.File; import java.util.Locale; +import java.util.Set; import static org.nuiton.i18n.I18n.t; @@ -168,6 +170,25 @@ public class ObserveSwingDataSource extends AbstractSerializableBean { } + public Set<ObserveDbUserDto> getUsers() { + Preconditions.checkState(isOpen(), "Connection is not open"); + + DataSourceService dataSourceService = newService(DataSourceService.class); + + Set<ObserveDbUserDto> users = dataSourceService.getUsers(); + + return users; + } + + + public void applySecurity(Set<ObserveDbUserDto> users) { + Preconditions.checkState(isOpen(), "Connection is not open"); + + DataSourceService dataSourceService = newService(DataSourceService.class); + + dataSourceService.applySecurity(users); + } + public boolean isOpen() { return connection != null; } @@ -300,6 +321,4 @@ public class ObserveSwingDataSource extends AbstractSerializableBean { listener.onClosed(evt); } } - - } diff --git a/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/ObstunaAdminAction.java b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/ObstunaAdminAction.java index d3a23f3..4f68ed6 100644 --- a/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/ObstunaAdminAction.java +++ b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/ObstunaAdminAction.java @@ -21,6 +21,20 @@ */ package fr.ird.observe.ui.storage; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import fr.ird.observe.db.ObserveSwingDataSource; +import fr.ird.observe.db.constantes.DbMode; +import fr.ird.observe.services.configuration.ObserveDataSourceConfiguration; +import fr.ird.observe.services.configuration.ObserveDataSourceConfigurationRest; +import fr.ird.observe.services.configuration.ObserveDataSourceConfigurationTopiaPG; +import fr.ird.observe.services.dto.DataSourceCreateConfigurationDto; +import fr.ird.observe.services.dto.IdDtos; +import fr.ird.observe.services.dto.ObserveDbUserDto; +import fr.ird.observe.services.service.DatabaseConnexionNotAuthorizedException; +import fr.ird.observe.services.service.DatabaseNotFoundException; +import fr.ird.observe.ui.storage.tabs.DataSelectionModel; import jaxx.runtime.JAXXContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -28,8 +42,10 @@ import org.nuiton.util.version.Version; import org.nuiton.util.version.Versions; import java.awt.Window; +import java.util.Set; import static org.nuiton.i18n.I18n.n; +import static org.nuiton.i18n.I18n.t; /** * Les différents types d'actions d'administration possible sur une base @@ -50,128 +66,139 @@ public enum ObstunaAdminAction { null) { @Override public RemoteUILauncher newLauncher(JAXXContext context, Window frame) { - //FIXME -// return new RemoteUILauncher(this, context, frame, t(getLabel())) { -// @Override -// protected void init(StorageUI ui) { -// StorageUIModel model = ui.getModel(); -// -// // on autorise le mode de creation import referentiel -// // depuis une source distante -// model.getPgConfig().setCreationMode(CreationMode.IMPORT_REMOTE_STORAGE); -// model.getPgConfig().setCanMigrate(true); -// super.init(ui); -// } -// -// protected void checkImportDbVersion(StorageUIModel model, DataSourceConfig dataSourceConfig) throws DataSourceException { -// -// DataSource importDatasource = DataSourceFactory.newService(dataSourceConfig); -// -// try { -// importDatasource.doOpen(); -// -// model.checkImportDbVersion(importDatasource); -// -// } finally { -// importDatasource.doClose(false); -// } -// -// } -// -// protected void initTask(StorageUIModel model, -// PGInstall task) throws Exception { -// -// -// PGDataSourceConfig pgConfig = model.getPgConfig(); -// -// // pas autorise a migrer automatiquement -// pgConfig.setCanMigrate(false); -// -// // references import -// pgConfig.setCreationMode(model.getReferentielImportMode()); -// -// DataSourceConfig importReferenceConfig = model.toImportReferentielSourceConfig(); -// pgConfig.setImportConfig(importReferenceConfig); -// -// if (importReferenceConfig != null) { -// -// if (log.isInfoEnabled()) { -// log.info("Use referentiel import data source " + importReferenceConfig.getLabel()); -// } -// -// checkImportDbVersion(model, importReferenceConfig); -// -// } -// -// // data import -// -// DataSourceConfig importDataConfig = model.toImportDataSourceConfig(); -// pgConfig.setImportDataConfig(importDataConfig); -// -// Set<String> importDataIds = null; -// -// if (importDataConfig != null) { -// -// if (log.isInfoEnabled()) { -// log.info("Use data import data source " + importDataConfig.getLabel()); -// } -// -// checkImportDbVersion(model, importDataConfig); -// -// DataSelectionModel dataModel = model.getSelectDataModel(); -// -// if (!dataModel.isDataFull()) { -// // on renseigne les marees a importer uniquement si -// // on en a selectionner, de plus si on a selectionne -// // toutes les marees, on le les passe pas : car c un dump -// // complet de la base. -// importDataIds = dataModel.getSelectedData(); -// } -// pgConfig.setImportDataIds(importDataIds); -// } -// pgConfig.setImportDataIds(importDataIds); -// -// super.initTask(model, task); -// } -// -// @Override -// protected String getPgLabel() { -// return t("observe.storage.label.db.to.create"); -// } -// -// @Override -// protected void execute(PGInstall task) throws Exception { -// if (log.isInfoEnabled()) { -// log.info("Create db..."); -// } -// -// // on commence par supprimer toute table gérée par ObServe -// task.dropDb(); -// -// // ici on ne fait qu'ouvrir la base (qui va créer le schema -// // puis importer le référentiel) -// PGDataSourceConfig config = task.getDataSourceConfig(); -// PGDataSource service = DataSourceFactory.newService(config); -// if (log.isInfoEnabled()) { -// log.info("Open [" + service.getLabel() + "] and create it."); -// } -// -// try { -// service.doOpen(); -// } finally { -// service.doClose(false); -// } -// } -// -// @Override -// protected void applySecurity(PGInstall task) throws SQLException { -// if (log.isInfoEnabled()) { -// log.info("Apply security..."); -// } -// task.applySecurity(); -// } -// }; - return null; + return new RemoteUILauncher(this, context, frame, t(getLabel())) { + + protected DataSourceCreateConfigurationDto createConfigurationDto; + protected ObserveSwingDataSource dataSource; + protected Set<ObserveDbUserDto> users; + + @Override + protected void init(StorageUI ui) { + StorageUIModel model = ui.getModel(); + + // on autorise le mode de creation import referentiel + // depuis une source distante + model.getPgConfig().setCanMigrate(true); + super.init(ui); + } + + protected void checkImportDbVersion(StorageUIModel model, ObserveDataSourceConfiguration dataSourceConfig) throws DatabaseConnexionNotAuthorizedException, DatabaseNotFoundException { + + ObserveSwingDataSource importDataSource = new ObserveSwingDataSource(dataSourceConfig); + + Preconditions.checkState(importDataSource != null, "Can't select data on a null dataSource"); + + try { + + importDataSource.open(); + + model.checkImportDbVersion(importDataSource); + + } finally { + + importDataSource.close(); + } + + } + + protected void initTask(StorageUIModel model) throws Exception { + + if (DbMode.USE_REMOTE.equals(model.getDbMode())) { + ObserveDataSourceConfigurationTopiaPG pgConfig = model.getPgConfig(); + + // pas autorise a migrer automatiquement + pgConfig.setCanMigrate(false); + + dataSource = new ObserveSwingDataSource(pgConfig); + } else { + ObserveDataSourceConfigurationRest restConfig = model.getRestConfig(); + + dataSource = new ObserveSwingDataSource(restConfig); + } + + // references import + model.setCreationMode(model.getReferentielImportMode()); + + createConfigurationDto = model.toImportReferentielSourceConfig(); + + ObserveDataSourceConfiguration importReferentialConfig = createConfigurationDto.getImportReferentialDataSourceConfiguration(); + + + if (importReferentialConfig != null) { + + if (log.isInfoEnabled()) { + log.info("Use referentiel import data source " + importReferentialConfig.getLabel()); + } + + checkImportDbVersion(model, importReferentialConfig); + + } + + // data import + + ObserveSwingDataSource importDataConfig = model.toImportDataSourceConfig(); + + + ImmutableSet<String> importDataIds = null; + + if (importDataConfig != null) { + + if (log.isInfoEnabled()) { + log.info("Use data import data source " + importDataConfig.getLabel()); + } + + checkImportDbVersion(model, importDataConfig.getConfiguration()); + + DataSelectionModel dataModel = model.getSelectDataModel(); + + if (!dataModel.isDataFull()) { + // on renseigne les marees a importer uniquement si + // on en a selectionner, de plus si on a selectionne + // toutes les marees, on le les passe pas : car c un dump + // complet de la base. + importDataIds = ImmutableSet.copyOf(Iterables.transform(dataModel.getSelectedData(), IdDtos.getIdFunction())); + } + + createConfigurationDto.setImportDataConfiguration(importDataConfig.getConfiguration(), importDataIds); + } + + + super.initTask(model); + } + + @Override + protected String getPgLabel() { + return t("observe.storage.label.db.to.create"); + } + + @Override + protected void execute() throws Exception { + if (log.isInfoEnabled()) { + log.info("Create db..."); + } + + try { + dataSource.create(createConfigurationDto); + + if (log.isInfoEnabled()) { + log.info("Open [" + dataSource.getLabel() + "] and create it."); + } + + } finally { + if (dataSource.isOpen()) { + dataSource.close(); + } + } + } + + @Override + protected void applySecurity() throws Exception { + if (log.isInfoEnabled()) { + log.info("Apply security..."); + } + dataSource.applySecurity(users); + } + }; } }, /** @@ -184,40 +211,67 @@ public enum ObstunaAdminAction { Versions.valueOf("1.0")) { @Override public RemoteUILauncher newLauncher(JAXXContext context, Window frame) { - //FIXME -// return new RemoteUILauncher(this, context, frame, t(getLabel())) { -// -// @Override -// protected void execute(PGInstall task) throws Exception { -// -// // ici on ne fait qu'ouvrir la base (qui va faire les mise à jour) -// PGDataSourceConfig config = task.getDataSourceConfig(); -// PGDataSource service = DataSourceFactory.newService(config); -// if (log.isInfoEnabled()) { -// log.info("Open [" + service.getLabel() + "] and update it."); -// } -// -// try { -// service.doOpen(); -// } finally { -// service.doClose(false); -// } -// } -// -// @Override -// protected String getPgLabel() { -// return t("observe.storage.label.db.to.update"); -// } -// -// @Override -// protected void applySecurity(PGInstall task) throws SQLException { -// if (log.isInfoEnabled()) { -// log.info("Apply security..."); -// } -// task.applySecurity(); -// } -// }; - return null; + return new RemoteUILauncher(this, context, frame, t(getLabel())) { + + protected ObserveSwingDataSource dataSource; + protected Version targetVersion; + protected Set<ObserveDbUserDto> users; + + @Override + protected void initTask(StorageUIModel model) throws Exception { + + ObserveDataSourceConfigurationTopiaPG pgConfig = model.getPgConfig(); + + // pas autorise a migrer automatiquement + pgConfig.setCanMigrate(false); + + dataSource = new ObserveSwingDataSource(pgConfig); + + targetVersion = model.getConfig().getModelVersion(); + + users = model.getSecurityModel().getUsers(); + + } + + @Override + protected void execute() throws Exception { + + try { + + dataSource.open(); + + Version dataSourceVersion = dataSource.getVersion(); + + if (dataSourceVersion.before(targetVersion)) { + + if (log.isInfoEnabled()) { + log.info("Migrate data source " + dataSource.getLabel() + " in " + dataSourceVersion + " to " + targetVersion); + } + + // todo + + } + + } finally { + if (dataSource.isOpen()) { + dataSource.close(); + } + } + } + + @Override + protected String getPgLabel() { + return t("observe.storage.label.db.to.update"); + } + + @Override + protected void applySecurity() throws Exception { + if (log.isInfoEnabled()) { + log.info("Apply security..."); + } + dataSource.applySecurity(users); + } + }; } }, /** @@ -230,53 +284,53 @@ public enum ObstunaAdminAction { Versions.valueOf("1.4")) { @Override public RemoteUILauncher newLauncher(JAXXContext context, Window frame) { - //FIXME -// return new RemoteUILauncher(this, context, frame, t(getLabel())) { -// -// @Override -// protected String getPgLabel() { -// return t("observe.storage.label.db.to.update.security"); -// } -// -// @Override -// protected void applySecurity(PGInstall task) throws SQLException { -// if (log.isInfoEnabled()) { -// log.info("Apply security..."); -// } -// task.applySecurity(); -// } -// }; - return null; - } - }, - /** - * pour supprimer une base obstuna. - * <p/> - */ - DROP(n("observe.obstuna.action.drop"), - n("observe.obstuna.action.drop.description"), - null) { - @Override - public RemoteUILauncher newLauncher(JAXXContext context, Window frame) { - //FIXME -// return new RemoteUILauncher(this, context, frame, t(getLabel())) { -// -// @Override -// protected String getPgLabel() { -// return t("observe.storage.label.db.to.drop"); -// } -// -// @Override -// protected void execute(PGInstall task) throws Exception { -// if (log.isInfoEnabled()) { -// log.info("Drop db..."); -// } -// task.dropDb(); -// } -// }; - return null; + return new RemoteUILauncher(this, context, frame, t(getLabel())) { + + protected ObserveSwingDataSource dataSource; + protected Set<ObserveDbUserDto> users; + + @Override + protected void initTask(StorageUIModel model) throws Exception { + + if (DbMode.USE_REMOTE.equals(model.getDbMode())) { + ObserveDataSourceConfigurationTopiaPG pgConfig = model.getPgConfig(); + + // pas autorise a migrer automatiquement + pgConfig.setCanMigrate(false); + + dataSource = new ObserveSwingDataSource(pgConfig); + } else { + ObserveDataSourceConfigurationRest restConfig = model.getRestConfig(); + + dataSource = new ObserveSwingDataSource(restConfig); + } + + users = model.getSecurityModel().getUsers(); + + } + + @Override + protected String getPgLabel() { + return t("observe.storage.label.db.to.update.security"); + } + + @Override + protected void applySecurity() throws Exception { + if (log.isInfoEnabled()) { + log.info("Apply security..."); + } + try { + dataSource.open(); + dataSource.applySecurity(users); + } finally { + if (dataSource.isOpen()) { + dataSource.close(); + } + } + } + }; } - },; + }; /** Logger */ static private Log log = LogFactory.getLog(ObstunaAdminAction.class); diff --git a/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/RemoteUILauncher.java b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/RemoteUILauncher.java index a3aedde..efd349d 100644 --- a/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/RemoteUILauncher.java +++ b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/RemoteUILauncher.java @@ -22,7 +22,7 @@ package fr.ird.observe.ui.storage; import fr.ird.observe.db.constantes.DbMode; -import fr.ird.observe.business.util.SecurityModel; +import fr.ird.observe.ui.storage.tabs.SecurityModel; import jaxx.runtime.JAXXContext; import jaxx.runtime.swing.ErrorDialogUI; import org.apache.commons.logging.Log; @@ -65,14 +65,9 @@ public abstract class RemoteUILauncher extends StorageUILauncher { model.setCanUseServerService(true); model.setDbMode(DbMode.USE_REMOTE); model.setAdminAction(action); -// model.setPgInstall(new PGInstall() { -// @Override -// public void execute() throws Exception { -// RemoteUILauncher.this.execute(this); -// } -// }); List<StorageStep> steps = new ArrayList<StorageStep>(); + steps.add(StorageStep.CHOOSE_DB_MODE); steps.add(StorageStep.CONFIG); if (action == ObstunaAdminAction.CREATE) { @@ -103,11 +98,9 @@ public abstract class RemoteUILauncher extends StorageUILauncher { log.info("Will use security model " + securityModel); } -// PGInstall install = model.getPgInstall(); - try { -//FIXME -// initTask(model, install); + + initTask(model); } catch (Exception e) { if (log.isErrorEnabled()) { log.error("Could not init task.", e); @@ -117,8 +110,7 @@ public abstract class RemoteUILauncher extends StorageUILauncher { } try { - //FIXME -// execute(install); + execute(); } catch (Exception e) { if (log.isErrorEnabled()) { @@ -129,8 +121,7 @@ public abstract class RemoteUILauncher extends StorageUILauncher { } try { - //FIXME -// applySecurity(install); + applySecurity(); } catch (Exception e) { if (log.isErrorEnabled()) { log.error("Could not apply security to db.", e); @@ -147,20 +138,15 @@ public abstract class RemoteUILauncher extends StorageUILauncher { // } protected abstract String getPgLabel(); -//FIXME -// protected void initTask(StorageUIModel model, -// PGInstall task) throws Exception { -// -// task.init(model.toPostgresStorageConfig(getPgLabel()), model.getSecurityModel(), false); -// } -//FIXME -// protected void execute(PGInstall task) throws Exception { -// } -////FIXME -// protected void applySecurity(PGInstall task) throws Exception { -// } -////FIXME -// protected DataService getDataService() { -// return ObserveContext.get().getDataService(); -// } + + protected void initTask(StorageUIModel model) throws Exception { + // FIXME + // task.init(model.toPostgresStorageConfig(getPgLabel()), model.getSecurityModel(), false); + } + + protected void execute() throws Exception { + } + + protected void applySecurity() throws Exception { + } } diff --git a/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/StorageUIHandler.java b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/StorageUIHandler.java index 03530f4..c1463a1 100644 --- a/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/StorageUIHandler.java +++ b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/StorageUIHandler.java @@ -31,7 +31,6 @@ import fr.ird.observe.ObserveSwingApplicationContext; import fr.ird.observe.ObserveTextGenerator; import fr.ird.observe.business.db.DataSource; import fr.ird.observe.business.db.DataSourceException; -import fr.ird.observe.business.util.SecurityModel; import fr.ird.observe.configuration.ObserveSwingApplicationConfig; import fr.ird.observe.db.ObserveSwingDataSource; import fr.ird.observe.db.constantes.ConnexionStatus; @@ -40,17 +39,20 @@ import fr.ird.observe.db.constantes.DbMode; import fr.ird.observe.db.event.ObserveSwingDataSourceEvent; import fr.ird.observe.db.event.ObserveSwingDataSourceListenerAdapter; import fr.ird.observe.services.configuration.ObserveDataSourceConfiguration; +import fr.ird.observe.services.configuration.ObserveDataSourceConfigurationRest; import fr.ird.observe.services.configuration.ObserveDataSourceConfigurationTopiaH2; import fr.ird.observe.services.configuration.ObserveDataSourceConfigurationTopiaPG; import fr.ird.observe.services.dto.DataSourceCreateConfigurationDto; import fr.ird.observe.services.dto.IdDto; import fr.ird.observe.services.dto.IdDtos; +import fr.ird.observe.services.dto.ObserveDbUserDto; import fr.ird.observe.services.service.DataSourceDumpProducerService; import fr.ird.observe.ui.DecoratorService; import fr.ird.observe.ui.ObserveMainUI; import fr.ird.observe.ui.UIHelper; import fr.ird.observe.ui.storage.tabs.DataSelectionModel; import fr.ird.observe.ui.storage.tabs.RolesTableModel; +import fr.ird.observe.ui.storage.tabs.SecurityModel; import fr.ird.observe.ui.storage.tabs.SelectDataUI; import fr.ird.observe.ui.storage.tabs.StorageTabUI; import jaxx.runtime.JAXXContext; @@ -870,6 +872,36 @@ public class StorageUIHandler { addCloseTag("ul", sb); } + protected void computeCanMigrateAction(StorageUIModel model, StringBuilder sb) { + addTag("h3", sb, t("observe.storage.report.action.migrate") + " :"); + addOpenTag("ul", sb); + if (model.isCanMigrate()) { + Version version = model.getModelVersion(); + addTag("li", sb, t("observe.storage.report.can.migrate", version)); + if (model.isShowMigrationProgression()) { + addTag("li", sb, t("observe.storage.showMigrationProgression")); + } + if (model.isShowMigrationSql()) { + addTag("li", sb, t("observe.storage.showMigrationSql")); + } + } else { + addTag("li", sb, t("observe.storage.report.can.not.migrate")); + } + addCloseTag("ul", sb); + } + + protected void computeSecurityreport(StorageUIModel model, StringBuilder sb) { + + addTag("h3", sb, t("observe.storage.report.selected.security")); + addOpenTag("ul", sb); + SecurityModel security = model.getSecurityModel(); + addTag("li", sb, t("observe.storage.report.security.owner", security.getAdministrateur())); + addTag("li", sb, t("observe.storage.report.security.techniciens", security.getTechnicien())); + addTag("li", sb, t("observe.storage.report.security.readers", security.getUtilisateur())); + addTag("li", sb, t("observe.storage.report.security.referentiels", security.getReferentiel())); + addCloseTag("ul", sb); + } + public static void addTag(String tag, StringBuilder sb, String message) { sb.append('<').append(tag).append('>'); sb.append(message); @@ -966,31 +998,43 @@ public class StorageUIHandler { protected void updateSecurity(StorageUIModel model, RolesTableModel roleModel) { SecurityModel security = model.getSecurityModel(); - //FIXME -// PGInstall pgInstall = model.getPgInstall(); -// PGDataSourceConfig config = model.getPgConfig(); -// pgInstall.init(config, security); -// security.setAdministrateur(config.getLogin()); -// try { -// Set<String> roles = pgInstall.getRoles(); -// -// String ownerRole = security.getAdministrateur(); -// if (!roles.contains(ownerRole)) { -// if (log.isWarnEnabled()) { -// log.warn("Could not find owner role : " + ownerRole); -// } -// } else { -// roles.remove(ownerRole); -// } -// if (log.isInfoEnabled()) { -// log.info("Db roles : " + roles); -// } -// security.init(roles); -// roleModel.init(security); -// -// } catch (SQLException e) { -// throw new RuntimeException("Could not obtain db roles", e); -// } + + ObserveSwingDataSource dataSource = null; + + switch (model.getDbMode()) { + case USE_REMOTE: + ObserveDataSourceConfigurationTopiaPG pgConfig = model.getPgConfig(); + + dataSource = new ObserveSwingDataSource(pgConfig); + break; + case USE_SERVER: + ObserveDataSourceConfigurationRest restConfig = model.getRestConfig(); + dataSource = new ObserveSwingDataSource(restConfig); + + } + + if (dataSource != null) { + try { + + dataSource.open(); + + Set<ObserveDbUserDto> users = dataSource.getUsers(); + + if (log.isInfoEnabled()) { + log.info("Db roles : " + users); + } + + security.init(users); + roleModel.init(security); + + } catch (Exception e) { + throw new RuntimeException("Could not obtain db roles", e); + } finally { + if (dataSource.isOpen()) { + dataSource.close(); + } + } + } } diff --git a/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/StorageUIModel.java b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/StorageUIModel.java index b4fe772..ec2e4d6 100644 --- a/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/StorageUIModel.java +++ b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/StorageUIModel.java @@ -22,7 +22,6 @@ package fr.ird.observe.ui.storage; import fr.ird.observe.ObserveSwingApplicationContext; -import fr.ird.observe.business.util.SecurityModel; import fr.ird.observe.configuration.ObserveSwingApplicationConfig; import fr.ird.observe.db.ObserveSwingDataSource; import fr.ird.observe.db.constantes.ConnexionStatus; @@ -35,11 +34,13 @@ import fr.ird.observe.services.configuration.ObserveDataSourceConfigurationTopia import fr.ird.observe.services.configuration.ObserveDataSourceConfigurationTopiaPG; import fr.ird.observe.services.configuration.ObserveDataSourceConnection; import fr.ird.observe.services.dto.DataSourceCreateConfigurationDto; +import fr.ird.observe.services.dto.ObserveDbUserDto; import fr.ird.observe.services.service.DataSourceDumpProducerService; import fr.ird.observe.services.service.DatabaseConnexionNotAuthorizedException; import fr.ird.observe.services.service.DatabaseNotFoundException; import fr.ird.observe.services.service.PingService; import fr.ird.observe.ui.storage.tabs.DataSelectionModel; +import fr.ird.observe.ui.storage.tabs.SecurityModel; import jaxx.runtime.JAXXContext; import jaxx.runtime.context.JAXXContextEntryDef; import jaxx.runtime.swing.wizard.WizardModel; @@ -279,6 +280,7 @@ public class StorageUIModel extends WizardModel<StorageStep> { @Override public void propertyChange(PropertyChangeEvent evt) { setConnexionStatus(ConnexionStatus.UNTESTED); + connection = null; } }; @@ -808,11 +810,7 @@ public class StorageUIModel extends WizardModel<StorageStep> { return; } List<StorageStep> universe = new ArrayList<StorageStep>(); - if (adminAction == null) { - - // when doing an admin mode we do not choose db mode, we always work on remote - universe.add(StorageStep.CHOOSE_DB_MODE); - } + universe.add(StorageStep.CHOOSE_DB_MODE); boolean canBackup = localStorageExist; switch (dbMode) { @@ -869,9 +867,7 @@ public class StorageUIModel extends WizardModel<StorageStep> { universe.add(StorageStep.SELECT_DATA); } } - if (adminAction != ObstunaAdminAction.DROP) { - universe.add(StorageStep.ROLES); - } + universe.add(StorageStep.ROLES); } universe.add(StorageStep.CONFIRM); if (excludeSteps != null) { @@ -1017,7 +1013,7 @@ public class StorageUIModel extends WizardModel<StorageStep> { break; case ROLES: - Set<String> role = getSecurityModel().getRole(); + Set<ObserveDbUserDto> role = getSecurityModel().getUsers(); int assigned = getSecurityModel().getAssigned(); @@ -1683,6 +1679,8 @@ public class StorageUIModel extends WizardModel<StorageStep> { dataSource.open(); + connection = dataSource.getConnection(); + Version versionDataSource = dataSource.getVersion(); if (getModelVersion().equals(versionDataSource)) { diff --git a/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/tabs/RolesTableModel.java b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/tabs/RolesTableModel.java index 9272a74..ed6c9d3 100644 --- a/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/tabs/RolesTableModel.java +++ b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/tabs/RolesTableModel.java @@ -21,11 +21,13 @@ */ package fr.ird.observe.ui.storage.tabs; -import fr.ird.observe.business.util.SecurityModel; + +import com.google.common.collect.Lists; +import fr.ird.observe.services.dto.ObserveDbUserDto; +import fr.ird.observe.services.dto.ObserveDbUserDtos; +import fr.ird.observe.services.dto.constants.ObserveDbRole; import javax.swing.table.AbstractTableModel; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; import static org.nuiton.i18n.I18n.n; @@ -67,7 +69,7 @@ public class RolesTableModel extends AbstractTableModel { protected SecurityModel model; - protected String[] roles; + protected List<ObserveDbUserDto> roles; public RolesTableModel() { } @@ -88,9 +90,8 @@ public class RolesTableModel extends AbstractTableModel { public void init(SecurityModel model) { this.model = model; - List<String> roles = new ArrayList<String>(model.getRole()); - Collections.sort(roles); - this.roles = roles.toArray(new String[roles.size()]); + this.roles = Lists.newArrayList(model.getUsersWithoutAdministrator()); + this.roles.sort(ObserveDbUserDtos.getUserDtoComparator()); fireTableDataChanged(); } @@ -102,7 +103,7 @@ public class RolesTableModel extends AbstractTableModel { @Override public int getRowCount() { - return roles == null ? 0 : roles.length; + return roles == null ? 0 : roles.size(); } @Override @@ -114,19 +115,24 @@ public class RolesTableModel extends AbstractTableModel { public Object getValueAt(int rowIndex, int columnIndex) { Object value = null; - String p = getRole(rowIndex); - if (p != null) { + ObserveDbUserDto user = getUser(rowIndex); + if (user != null) { switch (columnIndex) { case 0: - value = p; + value = user.getName(); break; case 1: + value = ObserveDbRole.UNUSED.equals(user.getRole()); + break; case 2: + value = ObserveDbRole.REFERENTIAL.equals(user.getRole()); + break; case 3: + value = ObserveDbRole.USER.equals(user.getRole()); + break; case 4: - SecurityModel.Role r = SecurityModel.Role.values()[columnIndex - 1]; - value = model.isRole(r, p); + value = ObserveDbRole.TECHNICAL.equals(user.getRole()); break; default: throw new IllegalStateException( @@ -147,21 +153,37 @@ public class RolesTableModel extends AbstractTableModel { } Boolean value = (Boolean) aValue; - String role = getRole(rowIndex); - SecurityModel.Role r = SecurityModel.Role.values()[columnIndex - 1]; + ObserveDbUserDto userDto = getUser(rowIndex); + + ObserveDbRole role; if (!value) { - r = null; + role = null; + } else { + switch (columnIndex) { + case 2: + role = ObserveDbRole.REFERENTIAL; + break; + case 3: + role = ObserveDbRole.USER; + break; + case 4: + role = ObserveDbRole.TECHNICAL; + break; + default: + role = ObserveDbRole.UNUSED; + } } - model.setRole(role, r, true); + + model.setRole(userDto, role, true); // toute la ligne (sauf le role change) fireTableRowsUpdated(rowIndex, rowIndex); } - protected String getRole(int rowIndex) { - String role = roles == null ? null : roles[rowIndex]; - return role; + protected ObserveDbUserDto getUser(int rowIndex) { + ObserveDbUserDto user = roles == null ? null : roles.get(rowIndex); + return user; } } diff --git a/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/tabs/RolesUI.css b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/tabs/RolesUI.css index b03b72c..f16ae5d 100644 --- a/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/tabs/RolesUI.css +++ b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/tabs/RolesUI.css @@ -30,7 +30,7 @@ } #administrateur { - text:{securityModel.getAdministrateur()}; + text:{securityModel.getAdministrateur().getName()}; } #roles { diff --git a/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/tabs/RolesUI.jaxx b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/tabs/RolesUI.jaxx index 50c1ee3..1df8560 100644 --- a/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/tabs/RolesUI.jaxx +++ b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/tabs/RolesUI.jaxx @@ -27,7 +27,7 @@ <StorageTabUI> <import> - fr.ird.observe.business.util.SecurityModel + fr.ird.observe.ui.storage.tabs.SecurityModel fr.ird.observe.ui.storage.StorageStep fr.ird.observe.ui.storage.StorageUIModel </import> diff --git a/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/tabs/SecurityModel.java b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/tabs/SecurityModel.java new file mode 100644 index 0000000..2b4aa4d --- /dev/null +++ b/observe-application-swing/src/main/java/fr/ird/observe/ui/storage/tabs/SecurityModel.java @@ -0,0 +1,149 @@ +/* + * #%L + * ObServe :: Business + * %% + * Copyright (C) 2008 - 2010 IRD, Codelutin, Tony Chemit + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ +package fr.ird.observe.ui.storage.tabs; + +import com.google.common.base.Function; +import com.google.common.base.Predicates; +import com.google.common.collect.Iterables; +import com.google.common.collect.Sets; +import fr.ird.observe.services.dto.ObserveDbUserDto; +import fr.ird.observe.services.dto.ObserveDbUserDtos; +import fr.ird.observe.services.dto.constants.ObserveDbRole; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.io.Serializable; +import java.util.Collection; +import java.util.Set; + +/** + * Le modèle la sécurité à appliquer sur une base postgres + * + * @author Tony Chemit - chemit@codelutin.com + * @since 1.4 + */ +public class SecurityModel implements Serializable { + + public static final String PROPERTY_ADMINISTRATEUR = "administrateur"; + + public static final String PROPERTY_ROLE = "role"; + + public static final String PROPERTY_ASSIGNED = "assigned"; + + private static final long serialVersionUID = 1L; + + public static final Function<String, String> ESCAPE_STRING = new Function<String, String>() { + @Override + public String apply(String input) { + return "\"" + input + "\""; + } + }; + + protected ObserveDbUserDto administrateur; + + protected Set<ObserveDbUserDto> users; + + protected PropertyChangeSupport pcs = new PropertyChangeSupport(this); + + public SecurityModel() { + users = Sets.newHashSet(); + } + + public ObserveDbUserDto getAdministrateur() { + return administrateur; + } + + public Set<ObserveDbUserDto> getUsersByRole(ObserveDbRole role) { + Set<ObserveDbUserDto> roles = Sets.newHashSet(Iterables.filter(users, ObserveDbUserDtos.newRolePredicate(role))); + return roles; + } + + public Set<ObserveDbUserDto> getUsers() { + return Sets.newHashSet(users); + } + + public Set<ObserveDbUserDto> getUsersWithoutAdministrator() { + return Sets.newHashSet(Iterables.filter(users, Predicates.not(ObserveDbUserDtos.newRolePredicate(ObserveDbRole.ADMINISTRATOR)))); + } + + public boolean isRole(ObserveDbRole r, ObserveDbUserDto user) { + return r.equals(user.getRole()); + } + + public void init(Collection<ObserveDbUserDto> users) { + this.users.clear(); + this.users.addAll(users); + + this.administrateur = Iterables.find(users, ObserveDbUserDtos.newRolePredicate(ObserveDbRole.ADMINISTRATOR)); + firePropertyChange(PROPERTY_ROLE, null, getUsers()); + firePropertyChange(PROPERTY_ADMINISTRATEUR, null, getAdministrateur()); + firePropertyChange(PROPERTY_ASSIGNED, null, getAssigned()); + } + + public void setRole(ObserveDbUserDto user, ObserveDbRole role, boolean fire) { + user.setRole(role); + if (fire) { + if (role != null) { + firePropertyChange(role.name(), null, getUsersByRole(role)); + } + firePropertyChange(PROPERTY_ASSIGNED, null, getAssigned()); + } + } + + public void setRole(Iterable<ObserveDbUserDto> users, ObserveDbRole role) { + for (ObserveDbUserDto user : users) { + setRole(user, role, false); + } + firePropertyChange(role.name(), null, getUsersByRole(role)); + firePropertyChange(PROPERTY_ASSIGNED, null, getAssigned()); + } + + public int getAssigned() { + int countNotAssigned = Iterables.size(Iterables.filter(users, ObserveDbUserDtos.newRolePredicate(null))); + int count = users.size() - countNotAssigned; + return count; + } + + public void addPropertyChangeListener(PropertyChangeListener listener) { + pcs.addPropertyChangeListener(listener); + } + + public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { + pcs.addPropertyChangeListener(propertyName, listener); + } + + public void removePropertyChangeListener(PropertyChangeListener listener) { + pcs.removePropertyChangeListener(listener); + } + + public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { + pcs.removePropertyChangeListener(propertyName, listener); + } + + protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { + pcs.firePropertyChange(propertyName, oldValue, newValue); + } + + protected void firePropertyChange(String propertyName, Object newValue) { + firePropertyChange(propertyName, null, newValue); + } +} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.