Commits

Sebastian Sdorra  committed 4d9e5e3

move admin user and group configuration from plugins to core

  • Participants
  • Parent commits 6a7e608

Comments (0)

Files changed (5)

File scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java

 
 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;
 
 /**
  *
     this.enableSSL = other.enableSSL;
     this.port = other.port;
     this.anonymousAccessEnabled = other.anonymousAccessEnabled;
+    this.adminUsers = other.adminUsers;
+    this.adminGroups = other.adminGroups;
   }
 
   //~--- get methods ----------------------------------------------------------
    *
    * @return
    */
+  public Set<String> getAdminGroups()
+  {
+    return adminGroups;
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @return
+   */
+  public Set<String> getAdminUsers()
+  {
+    return adminUsers;
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @return
+   */
   public String getPluginUrl()
   {
     return pluginUrl;
    * Method description
    *
    *
+   * @param adminGroups
+   */
+  public void setAdminGroups(Set<String> adminGroups)
+  {
+    this.adminGroups = adminGroups;
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param adminUsers
+   */
+  public void setAdminUsers(Set<String> adminUsers)
+  {
+    this.adminUsers = adminUsers;
+  }
+
+  /**
+   * Method description
+   *
+   *
    * @param anonymousAccessEnabled
    */
   public void setAnonymousAccessEnabled(boolean anonymousAccessEnabled)
   //~--- fields ---------------------------------------------------------------
 
   /** Field description */
+  @XmlElement(name = "admin-groups")
+  @XmlJavaTypeAdapter(XmlSetStringAdapter.class)
+  private Set<String> adminGroups;
+
+  /** Field description */
+  @XmlElement(name = "admin-users")
+  @XmlJavaTypeAdapter(XmlSetStringAdapter.class)
+  private Set<String> adminUsers;
+
+  /** Field description */
   @XmlElement(name = "plugin-url")
   private String pluginUrl = DEFAULT_PLUGINURL;
 

File scm-core/src/main/java/sonia/scm/util/Util.java

    * Method description
    *
    *
+   * @param collection
+   * @param other
+   * @param <T>
+   *
+   * @return
+   */
+  public static <T> boolean containsOne(Collection<T> collection,
+          Collection<T> other)
+  {
+    boolean result = false;
+
+    for (T item : collection)
+    {
+      if (other.contains(item))
+      {
+        result = true;
+
+        break;
+      }
+    }
+
+    return result;
+  }
+
+  /**
+   * Method description
+   *
+   *
    * @param date
    * @param tz
    *

File scm-core/src/main/java/sonia/scm/xml/XmlSetStringAdapter.java

+/**
+ * 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<String, Set<String>>
+{
+
+  /**
+   * Method description
+   *
+   *
+   * @param value
+   *
+   * @return
+   *
+   * @throws Exception
+   */
+  @Override
+  public String marshal(Set<String> value) throws Exception
+  {
+    StringBuilder buffer = new StringBuilder();
+    Iterator<String> 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<String> unmarshal(String rawString) throws Exception
+  {
+    Set<String> tokens = new HashSet<String>();
+
+    for (String token : rawString.split(","))
+    {
+      token = token.trim();
+
+      if (token.length() > 0)
+      {
+        tokens.add(token);
+      }
+    }
+
+    return tokens;
+  }
+}

File scm-webapp/src/main/java/sonia/scm/web/security/BasicSecurityContext.java

 
         loadGroups();
 
+        if (!user.isAdmin())
+        {
+          user.setAdmin(isAdmin());
+        }
+
         if (logger.isDebugEnabled())
         {
           logGroups();
     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 */

File scm-webapp/src/main/webapp/resources/js/sonia.config.js

           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){