qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From oru...@apache.org
Subject svn commit: r1529362 - in /qpid/trunk/qpid/java: broker-core/src/main/java/org/apache/qpid/server/model/ broker-core/src/main/java/org/apache/qpid/server/model/adapter/ broker-core/src/test/java/org/apache/qpid/server/model/adapter/ broker-plugins/mana...
Date Sat, 05 Oct 2013 00:28:57 GMT
Author: orudyy
Date: Sat Oct  5 00:28:57 2013
New Revision: 1529362

URL: http://svn.apache.org/r1529362
Log:
QPID-5138: Change preferences provider to allow deletion of preferences for multiple users

Modified:
    qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/PreferencesProvider.java
    qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/User.java
    qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/adapter/AuthenticationProviderAdapter.java
    qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/adapter/FileSystemPreferencesProvider.java
    qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/adapter/FileSystemPreferencesProviderTest.java
    qpid/trunk/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/UserPreferencesServlet.java
    qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/UserPreferencesRestTest.java

Modified: qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/PreferencesProvider.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/PreferencesProvider.java?rev=1529362&r1=1529361&r2=1529362&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/PreferencesProvider.java
(original)
+++ qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/PreferencesProvider.java
Sat Oct  5 00:28:57 2013
@@ -69,11 +69,11 @@ public interface PreferencesProvider ext
     Map<String, Object> setPreferences(String userId, Map<String, Object> preferences);
 
     /**
-     * Delete preferences for a given user ID
-     * @param userId user ID to delete preferences for
-     * @return user preferences before the deletion
+     * Delete preferences for given user IDs
+     * @param userIDs user IDs to delete preferences for
+     * @return user an array with user IDs having preferences deleted
      */
-    Map<String, Object> deletePreferences(String userId);
+    String[] deletePreferences(String... userIDs);
 
     /**
      * Returns set of the user IDs having preferences set

Modified: qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/User.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/User.java?rev=1529362&r1=1529361&r2=1529362&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/User.java
(original)
+++ qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/User.java
Sat Oct  5 00:28:57 2013
@@ -61,5 +61,5 @@ public interface User extends Configured
 
     public Map<String, Object> setPreferences(Map<String, Object> preferences);
 
-    public Map<String, Object> replacePreferences(Map<String, Object> newPreferences);
+    public boolean deletePreferences();
 }

Modified: qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/adapter/AuthenticationProviderAdapter.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/adapter/AuthenticationProviderAdapter.java?rev=1529362&r1=1529361&r2=1529362&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/adapter/AuthenticationProviderAdapter.java
(original)
+++ qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/adapter/AuthenticationProviderAdapter.java
Sat Oct  5 00:28:57 2013
@@ -803,16 +803,15 @@ public abstract class AuthenticationProv
             }
 
             @Override
-            public Map<String, Object> replacePreferences(Map<String, Object>
newPreferences)
+            public boolean deletePreferences()
             {
                 PreferencesProvider preferencesProvider = getPreferencesProvider();
                 if (preferencesProvider == null)
                 {
-                    return null;
+                    return false;
                 }
-                Map<String, Object> preferences = preferencesProvider.deletePreferences(this.getName());
-                preferencesProvider.setPreferences(this.getName(), newPreferences);
-                return preferences;
+                String[] deleted = preferencesProvider.deletePreferences(this.getName());
+                return deleted.length == 1;
             }
 
             private PreferencesProvider getPreferencesProvider()

Modified: qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/adapter/FileSystemPreferencesProvider.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/adapter/FileSystemPreferencesProvider.java?rev=1529362&r1=1529361&r2=1529362&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/adapter/FileSystemPreferencesProvider.java
(original)
+++ qpid/trunk/qpid/java/broker-core/src/main/java/org/apache/qpid/server/model/adapter/FileSystemPreferencesProvider.java
Sat Oct  5 00:28:57 2013
@@ -35,6 +35,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
@@ -301,9 +302,9 @@ public class FileSystemPreferencesProvid
     }
 
     @Override
-    public Map<String, Object> deletePreferences(String userId)
+    public String[] deletePreferences(String... userIDs)
     {
-        return _store.deletePreferences(userId);
+        return _store.deletePreferences(userIDs);
     }
 
     @Override
@@ -566,19 +567,26 @@ public class FileSystemPreferencesProvid
             return userPreferences;
         }
 
-        public Map<String, Object> deletePreferences(String userId)
+        public String[] deletePreferences(String... userIDs)
         {
             checkStoreOpened();
-            Map<String, Object> userPreferences = null;
+            Set<String> deletedUsers = new HashSet<String>();
             synchronized (_preferences)
             {
-                if (_preferences.containsKey(userId))
+                for (String id : userIDs)
+                {
+                    if (_preferences.containsKey(id))
+                    {
+                        _preferences.remove(id);
+                        deletedUsers.add(id);
+                    }
+                }
+                if (!deletedUsers.isEmpty())
                 {
-                    userPreferences = _preferences.remove(userId);
                     save();
                 }
             }
-            return userPreferences;
+            return deletedUsers.toArray(new String[deletedUsers.size()]);
         }
 
         public Set<String> listUserIDs()

Modified: qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/adapter/FileSystemPreferencesProviderTest.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/adapter/FileSystemPreferencesProviderTest.java?rev=1529362&r1=1529361&r2=1529362&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/adapter/FileSystemPreferencesProviderTest.java
(original)
+++ qpid/trunk/qpid/java/broker-core/src/test/java/org/apache/qpid/server/model/adapter/FileSystemPreferencesProviderTest.java
Sat Oct  5 00:28:57 2013
@@ -210,7 +210,7 @@ public class FileSystemPreferencesProvid
 
         String user3 = "user3";
         Map<String, Object> preferences3 = _preferencesProvider.getPreferences(user3);
-        assertTrue("No preference found for user3", preferences3.isEmpty());
+        assertTrue("Unexpected preferences found for user3", preferences3.isEmpty());
     }
 
     public void testDeletePrefernces()
@@ -218,6 +218,9 @@ public class FileSystemPreferencesProvid
         _preferencesProvider = createPreferencesProvider();
         _preferencesProvider.setDesiredState(State.INITIALISING, State.ACTIVE);
 
+        assertUser1Preferences(_preferencesProvider.getPreferences(_user1));
+        assertUser2Preferences(_preferencesProvider.getPreferences(_user2));
+
         _preferencesProvider.deletePreferences(_user1);
         _preferencesProvider.setDesiredState(State.ACTIVE, State.STOPPED);
 
@@ -231,6 +234,30 @@ public class FileSystemPreferencesProvid
 
         String user3 = "user3";
         Map<String, Object> preferences3 = _preferencesProvider.getPreferences(user3);
+        assertTrue("Unexpected preferences found for user3", preferences3.isEmpty());
+    }
+
+    public void testDeleteMultipleUsersPrefernces()
+    {
+        _preferencesProvider = createPreferencesProvider();
+        _preferencesProvider.setDesiredState(State.INITIALISING, State.ACTIVE);
+
+        assertUser1Preferences(_preferencesProvider.getPreferences(_user1));
+        assertUser2Preferences(_preferencesProvider.getPreferences(_user2));
+
+        _preferencesProvider.deletePreferences(_user1, _user2);
+        _preferencesProvider.setDesiredState(State.ACTIVE, State.STOPPED);
+
+        _preferencesProvider = createPreferencesProvider();
+        _preferencesProvider.setDesiredState(State.INITIALISING, State.ACTIVE);
+        Map<String, Object> preferences1 = _preferencesProvider.getPreferences(_user1);
+        assertTrue("Preferences should not be set for user 1", preferences1.isEmpty());
+
+        Map<String, Object> preferences2 = _preferencesProvider.getPreferences(_user2);
+        assertTrue("Preferences should not be set for user 2", preferences2.isEmpty());
+
+        String user3 = "user3";
+        Map<String, Object> preferences3 = _preferencesProvider.getPreferences(user3);
         assertTrue("No preference found for user3", preferences3.isEmpty());
     }
 

Modified: qpid/trunk/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/UserPreferencesServlet.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/UserPreferencesServlet.java?rev=1529362&r1=1529361&r2=1529362&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/UserPreferencesServlet.java
(original)
+++ qpid/trunk/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/UserPreferencesServlet.java
Sat Oct  5 00:28:57 2013
@@ -25,7 +25,7 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.Iterator;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -157,53 +157,75 @@ public class UserPreferencesServlet exte
     @Override
     protected void doDeleteWithSubjectAndActor(HttpServletRequest request, HttpServletResponse
response) throws IOException
     {
-        final List<String[]> userData = new ArrayList<String[]>();
-        for (String name : request.getParameterValues("user"))
+        Broker broker = getBroker();
+        Collection<AuthenticationProvider> authenticationProviders = broker.getAuthenticationProviders();
+        Map<String, Set<String>> providerUsers = new HashMap<String, Set<String>>();
+        Map<String, AuthenticationProvider> requestProviders = new HashMap<String,
AuthenticationProvider>();
+        for (String path : request.getParameterValues("user"))
         {
-            String[] elements = name.split("/");
+            String[] elements = path.split("/");
             if (elements.length != 2)
             {
-                throw new IllegalArgumentException("Illegal parameter");
+                throw new IllegalArgumentException("Illegal user parameter " + path);
+            }
+
+            String userId = elements[1];
+
+            if (!userPreferencesOperationAuthorized(userId))
+            {
+                response.sendError(HttpServletResponse.SC_FORBIDDEN, "Deletion of preferences
is not allowed");
+                return;
             }
-            userData.add(elements);
+            String providerName =  elements[0];
+            Set<String> users = providerUsers.get(providerName);
+
+            if (users == null)
+            {
+                AuthenticationProvider provider = findAuthenticationProviderByName(providerName,
authenticationProviders);
+                if (provider == null)
+                {
+                    throw new IllegalArgumentException("Cannot find provider with name '"
+ providerName + "'");
+                }
+                users = new HashSet<String>();
+                providerUsers.put(providerName, users);
+                requestProviders.put(providerName, provider);
+            }
+            users.add(userId);
         }
 
-        if (!userData.isEmpty())
+        if (!providerUsers.isEmpty())
         {
-            Broker broker = getBroker();
-            Collection<AuthenticationProvider> authenticationProviders = broker.getAuthenticationProviders();
-            for (Iterator<String[]> it = userData.iterator(); it.hasNext();)
+            for (Map.Entry<String, Set<String>> entry : providerUsers.entrySet())
             {
-                String[] data = (String[]) it.next();
-                String authenticationProviderName = data[0];
-                String userId = data[1];
+                String providerName = entry.getKey();
+                AuthenticationProvider provider = requestProviders.get(providerName);
+                Set<String> usersToDelete = entry.getValue();
+                PreferencesProvider preferencesProvider = provider.getPreferencesProvider();
 
-                for (AuthenticationProvider authenticationProvider : authenticationProviders)
+                if (preferencesProvider != null && !usersToDelete.isEmpty())
                 {
-                    if (authenticationProviderName.equals(authenticationProvider.getName()))
-                    {
-                        PreferencesProvider preferencesProvider = authenticationProvider.getPreferencesProvider();
-                        if (preferencesProvider != null)
-                        {
-                            Set<String> usernames = preferencesProvider.listUserIDs();
-                            if (usernames.contains(userId))
-                            {
-                                if (!userPreferencesOperationAuthorized(userId))
-                                {
-                                    response.sendError(HttpServletResponse.SC_FORBIDDEN,
"Deletion of preferences is not allowed");
-                                    return;
-                                }
-                                preferencesProvider.deletePreferences(userId);
-                            }
-                        }
-                        break;
-                    }
+                    String[] users = usersToDelete.toArray(new String[usersToDelete.size()]);
+                    preferencesProvider.deletePreferences(users);
                 }
             }
         }
 
     }
 
+    protected AuthenticationProvider findAuthenticationProviderByName(String providerName,
Collection<AuthenticationProvider> authenticationProviders)
+    {
+        AuthenticationProvider provider = null;
+        for (AuthenticationProvider authenticationProvider : authenticationProviders)
+        {
+            if(authenticationProvider.getName().equals(providerName))
+            {
+                provider = authenticationProvider;
+                break;
+            }
+        }
+        return provider;
+    }
+
     private boolean userPreferencesOperationAuthorized(String userId)
     {
         return getBroker().getSecurityManager().authoriseUserOperation(Operation.UPDATE,
userId);

Modified: qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/UserPreferencesRestTest.java
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/UserPreferencesRestTest.java?rev=1529362&r1=1529361&r2=1529362&view=diff
==============================================================================
--- qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/UserPreferencesRestTest.java
(original)
+++ qpid/trunk/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/UserPreferencesRestTest.java
Sat Oct  5 00:28:57 2013
@@ -125,6 +125,17 @@ public class UserPreferencesRestTest ext
         assertNull("User webadmin is found", findUser("webadmin", users));
     }
 
+    public void testDeleteMultipleUser() throws Exception
+    {
+        int status = getRestTestHelper().submitRequest("/rest/userpreferences?user="
+                + URLEncoder.encode(TestBrokerConfiguration.ENTRY_NAME_AUTHENTICATION_PROVIDER
+ "/webadmin", "UTF-8")
+                + "&user=" + URLEncoder.encode(TestBrokerConfiguration.ENTRY_NAME_AUTHENTICATION_PROVIDER
+ "/admin", "UTF-8"),
+                "DELETE", null);
+        assertEquals("Unexpected status ", 200, status);
+        List<Map<String, Object>> users = getRestTestHelper().getJsonAsList("/rest/userpreferences");
+        assertEquals("Unexpected number of users", 0, users.size());
+    }
+
     private Map<String, Object> findUser(String userName, List<Map<String, Object>>
users)
     {
         for (Map<String, Object> map : users)



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message