From 8b97cfe9b0a78c031e0086fe43f3b00242b3826e Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Tue, 8 Feb 2011 19:37:27 +0100 Subject: [PATCH] move admin user and group configuration from plugins to core --- .../sonia/scm/config/ScmConfiguration.java | 61 ++++++++++ .../src/main/java/sonia/scm/util/Util.java | 28 +++++ .../sonia/scm/xml/XmlSetStringAdapter.java | 107 ++++++++++++++++++ .../web/security/BasicSecurityContext.java | 25 ++++ .../main/webapp/resources/js/sonia.config.js | 10 ++ 5 files changed, 231 insertions(+) create mode 100644 scm-core/src/main/java/sonia/scm/xml/XmlSetStringAdapter.java diff --git a/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java b/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java index e03448fd29..4f3f4acce9 100644 --- a/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java +++ b/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java @@ -37,14 +37,19 @@ package sonia.scm.config; import com.google.inject.Singleton; +import sonia.scm.xml.XmlSetStringAdapter; + //~--- JDK imports ------------------------------------------------------------ import java.io.File; +import java.util.Set; + import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; /** * @@ -80,10 +85,34 @@ public class ScmConfiguration this.enableSSL = other.enableSSL; this.port = other.port; this.anonymousAccessEnabled = other.anonymousAccessEnabled; + this.adminUsers = other.adminUsers; + this.adminGroups = other.adminGroups; } //~--- get methods ---------------------------------------------------------- + /** + * Method description + * + * + * @return + */ + public Set getAdminGroups() + { + return adminGroups; + } + + /** + * Method description + * + * + * @return + */ + public Set getAdminUsers() + { + return adminUsers; + } + /** * Method description * @@ -152,6 +181,28 @@ public class ScmConfiguration //~--- set methods ---------------------------------------------------------- + /** + * Method description + * + * + * @param adminGroups + */ + public void setAdminGroups(Set adminGroups) + { + this.adminGroups = adminGroups; + } + + /** + * Method description + * + * + * @param adminUsers + */ + public void setAdminUsers(Set adminUsers) + { + this.adminUsers = adminUsers; + } + /** * Method description * @@ -220,6 +271,16 @@ public class ScmConfiguration //~--- fields --------------------------------------------------------------- + /** Field description */ + @XmlElement(name = "admin-groups") + @XmlJavaTypeAdapter(XmlSetStringAdapter.class) + private Set adminGroups; + + /** Field description */ + @XmlElement(name = "admin-users") + @XmlJavaTypeAdapter(XmlSetStringAdapter.class) + private Set adminUsers; + /** Field description */ @XmlElement(name = "plugin-url") private String pluginUrl = DEFAULT_PLUGINURL; diff --git a/scm-core/src/main/java/sonia/scm/util/Util.java b/scm-core/src/main/java/sonia/scm/util/Util.java index e9e7852da5..4e5f0bec01 100644 --- a/scm-core/src/main/java/sonia/scm/util/Util.java +++ b/scm-core/src/main/java/sonia/scm/util/Util.java @@ -84,6 +84,34 @@ public class Util return result; } + /** + * Method description + * + * + * @param collection + * @param other + * @param + * + * @return + */ + public static boolean containsOne(Collection collection, + Collection other) + { + boolean result = false; + + for (T item : collection) + { + if (other.contains(item)) + { + result = true; + + break; + } + } + + return result; + } + /** * Method description * diff --git a/scm-core/src/main/java/sonia/scm/xml/XmlSetStringAdapter.java b/scm-core/src/main/java/sonia/scm/xml/XmlSetStringAdapter.java new file mode 100644 index 0000000000..708159bfac --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/xml/XmlSetStringAdapter.java @@ -0,0 +1,107 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.xml; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import javax.xml.bind.annotation.adapters.XmlAdapter; + +/** + * + * @author Sebastian Sdorra + */ +public class XmlSetStringAdapter extends XmlAdapter> +{ + + /** + * Method description + * + * + * @param value + * + * @return + * + * @throws Exception + */ + @Override + public String marshal(Set value) throws Exception + { + StringBuilder buffer = new StringBuilder(); + Iterator it = value.iterator(); + + while (it.hasNext()) + { + buffer.append(it.next()); + + if (it.hasNext()) + { + buffer.append(","); + } + } + + return buffer.toString(); + } + + /** + * Method description + * + * + * @param rawString + * + * @return + * + * @throws Exception + */ + @Override + public Set unmarshal(String rawString) throws Exception + { + Set tokens = new HashSet(); + + for (String token : rawString.split(",")) + { + token = token.trim(); + + if (token.length() > 0) + { + tokens.add(token); + } + } + + return tokens; + } +} diff --git a/scm-webapp/src/main/java/sonia/scm/web/security/BasicSecurityContext.java b/scm-webapp/src/main/java/sonia/scm/web/security/BasicSecurityContext.java index 642f9560c9..b14674de93 100644 --- a/scm-webapp/src/main/java/sonia/scm/web/security/BasicSecurityContext.java +++ b/scm-webapp/src/main/java/sonia/scm/web/security/BasicSecurityContext.java @@ -145,6 +145,11 @@ public class BasicSecurityContext implements WebSecurityContext loadGroups(); + if (!user.isAdmin()) + { + user.setAdmin(isAdmin()); + } + if (logger.isDebugEnabled()) { logGroups(); @@ -283,6 +288,26 @@ public class BasicSecurityContext implements WebSecurityContext logger.debug(msg.toString()); } + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + private boolean isAdmin() + { + boolean result = configuration.getAdminUsers().contains(user.getName()); + + if (!result) + { + result = Util.containsOne(configuration.getAdminGroups(), groups); + } + + return result; + } + //~--- fields --------------------------------------------------------------- /** Field description */ diff --git a/scm-webapp/src/main/webapp/resources/js/sonia.config.js b/scm-webapp/src/main/webapp/resources/js/sonia.config.js index 3490731de1..4208686a43 100644 --- a/scm-webapp/src/main/webapp/resources/js/sonia.config.js +++ b/scm-webapp/src/main/webapp/resources/js/sonia.config.js @@ -124,6 +124,16 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{ fieldLabel: 'SSL Port', name: 'sslPort', allowBlank: false + },{ + xtype : 'textfield', + fieldLabel : 'Admin Groups', + name : 'admin-groups', + allowBlank : true + },{ + xtype : 'textfield', + fieldLabel : 'Admin Users', + name : 'admin-users', + allowBlank : true }], onSubmit: function(values){