← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17795: User group managed by testing

 

------------------------------------------------------------
revno: 17795
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-12-25 15:10:43 +0100
message:
  User group managed by testing
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserGroupService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserGroupService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserServiceTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java	2014-12-22 09:30:23 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java	2014-12-25 14:10:43 +0000
@@ -248,10 +248,9 @@
     {
         return userCredentials != null && userCredentials.isSuper();
     }
-    
+        
     /**
-     * Indicates whether this user can manage the given user group. This is derived
-     * from which user groups are managed by the given group.
+     * Indicates whether this user can manage the given user group.
      * 
      * @param userGroup the user group to test.
      * @return true if the given user group can be managed by this user, false if not.
@@ -260,6 +259,65 @@
     {
         return userGroup != null && CollectionUtils.containsAny( groups, userGroup.getManagedByGroups() );
     }
+    
+    /**
+     * Indicates whether this user can manage the given user.
+     * 
+     * @param user the user to test.
+     * @return true if the given user can be managed by this user, false if not.
+     */
+    public boolean canManage( User user )
+    {
+        if ( user == null || user.getGroups() == null )
+        {
+            return false;
+        }
+        
+        for ( UserGroup group : user.getGroups() )
+        {
+            if ( canManage( group ) )
+            {
+                return true;
+            }
+        }
+        
+        return false;
+    }
+    
+    /**
+     * Indicates whether this user is managed by the given user group.
+     * 
+     * @param userGroup the user group to test.
+     * @return true if the given user group is managed by this user, false if not.
+     */
+    public boolean isManagedBy( UserGroup userGroup )
+    {
+        return userGroup != null && CollectionUtils.containsAny( groups, userGroup.getManagedGroups() );
+    }
+
+    /**
+     * Indicates whether this user is managed by the given user.
+     * 
+     * @param userGroup the user  to test.
+     * @return true if the given user is managed by this user, false if not.
+     */
+    public boolean isManagedBy( User user )
+    {
+        if ( user == null || user.getGroups() == null )
+        {
+            return false;
+        }
+        
+        for ( UserGroup group : user.getGroups() )
+        {
+            if ( isManagedBy( group ) )
+            {
+                return true;
+            }
+        }
+        
+        return false;
+    }
 
     // -------------------------------------------------------------------------
     // Getters and setters

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserGroupService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserGroupService.java	2014-12-24 14:44:09 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserGroupService.java	2014-12-25 14:10:43 +0000
@@ -35,7 +35,7 @@
 {
     String ID = UserGroupService.class.getName();
 
-    void addUserGroup( UserGroup userGroup );
+    int addUserGroup( UserGroup userGroup );
     
     void updateUserGroup( UserGroup userGroup );
 

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserGroupService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserGroupService.java	2014-12-25 10:43:54 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserGroupService.java	2014-12-25 14:10:43 +0000
@@ -72,9 +72,9 @@
     // -------------------------------------------------------------------------
 
     @Override
-    public void addUserGroup( UserGroup userGroup )
+    public int addUserGroup( UserGroup userGroup )
     {
-        userGroupStore.save( userGroup );
+        return userGroupStore.save( userGroup );
     }
 
     @Override

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserServiceTest.java	2014-12-25 10:43:54 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserServiceTest.java	2014-12-25 14:10:43 +0000
@@ -31,6 +31,8 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
 
 import java.util.HashSet;
 import java.util.Set;
@@ -41,6 +43,8 @@
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import com.google.common.collect.Sets;
+
 /**
  * @author Lars Helge Overland
  */
@@ -49,9 +53,9 @@
 {
     @Autowired
     private UserService userService;
-
+    
     @Autowired
-    private UserCredentialsStore userCredentialsStore;
+    private UserGroupService userGroupService;
 
     @Autowired
     private OrganisationUnitService organisationUnitService;
@@ -130,20 +134,60 @@
         assertNull( userService.getUser( idA ) );
         assertNotNull( userService.getUser( idB ) );
     }
-
+    
     @Test
-    public void testAddGetUserCredentials()
+    public void testManagedGroups()
     {
         User userA = createUser( 'A' );
         User userB = createUser( 'B' );
-        
-        UserCredentials credentialsA = createUserCredentials( 'A', userA );
-        UserCredentials credentialsB = createUserCredentials( 'B', userB );
-        
-        int idA = userCredentialsStore.addUserCredentials( credentialsA );
-        int idB = userCredentialsStore.addUserCredentials( credentialsB );
-        
-        assertEquals( credentialsA, userCredentialsStore.getUserCredentials( idA ) );
-        assertEquals( credentialsB, userCredentialsStore.getUserCredentials( idB ) );
+        User userC = createUser( 'C' );
+        User userD = createUser( 'D' );
+        
+        userService.addUser( userA );
+        userService.addUser( userB );
+        userService.addUser( userC );
+        userService.addUser( userD );
+        
+        UserGroup userGroup1 = createUserGroup( 'A', Sets.newHashSet( userA, userB ) );
+        UserGroup userGroup2 = createUserGroup( 'B', Sets.newHashSet( userC, userD ) );
+        userA.getGroups().add( userGroup1 );
+        userB.getGroups().add( userGroup1 );
+        userC.getGroups().add( userGroup2 );
+        userD.getGroups().add( userGroup2 );
+        
+        userGroup1.setManagedGroups( Sets.newHashSet( userGroup2 ) );
+        userGroup2.setManagedByGroups( Sets.newHashSet( userGroup1 ) );
+        
+        int group1 = userGroupService.addUserGroup( userGroup1 );
+        int group2 = userGroupService.addUserGroup( userGroup2 );
+
+        assertEquals( 1, userGroupService.getUserGroup( group1 ).getManagedGroups().size() );
+        assertTrue( userGroupService.getUserGroup( group1 ).getManagedGroups().contains( userGroup2 ) );
+        assertEquals( 1, userGroupService.getUserGroup( group2 ).getManagedByGroups().size() );
+        assertTrue( userGroupService.getUserGroup( group2 ).getManagedByGroups().contains( userGroup1 ) );
+        
+        assertTrue( userA.canManage( userGroup2 ) );
+        assertTrue( userB.canManage( userGroup2 ) );
+        assertFalse( userC.canManage( userGroup1 ) );
+        assertFalse( userD.canManage( userGroup1 ) );
+
+        assertTrue( userA.canManage( userC ) );
+        assertTrue( userA.canManage( userD ) );
+        assertTrue( userB.canManage( userC ) );
+        assertTrue( userA.canManage( userD ) );
+        assertFalse( userC.canManage( userA ) );
+        assertFalse( userC.canManage( userB ) );
+        
+        assertTrue( userC.isManagedBy( userGroup1 ) );
+        assertTrue( userD.isManagedBy( userGroup1 ) );
+        assertFalse( userA.isManagedBy( userGroup2 ) );
+        assertFalse( userB.isManagedBy( userGroup2 ) );
+
+        assertTrue( userC.isManagedBy( userA ) );
+        assertTrue( userC.isManagedBy( userB ) );
+        assertTrue( userD.isManagedBy( userA ) );
+        assertTrue( userD.isManagedBy( userB ) );
+        assertFalse( userA.isManagedBy( userC ) );
+        assertFalse( userA.isManagedBy( userD ) );
     }
 }