← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19590: Centralized code for checking if org unit is descendant

 

------------------------------------------------------------
revno: 19590
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-07-08 11:56:56 +0200
message:
  Centralized code for checking if org unit is descendant
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.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	2015-06-01 19:49:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java	2015-07-08 09:56:56 +0000
@@ -240,46 +240,6 @@
     {
         return IdentifiableObjectUtils.join( organisationUnits );
     }
-
-    /**
-     * Indicates whether the given organisation unit is part of the hierarchy
-     * of the organisation units of this user.
-     * 
-     * @param organisationUnit the organisation unit.
-     * @param the user organisation units.
-     * @return true if the given organisation unit is part of the hierarchy.
-     */
-    public static boolean isInUserHierarchy( OrganisationUnit organisationUnit, Set<OrganisationUnit> organisationUnits )
-    {
-        if ( organisationUnits == null )
-        {
-            return false;
-        }
-        
-        while ( organisationUnit != null )
-        {
-            if ( organisationUnits.contains( organisationUnit ) )
-            {
-                return true;
-            }
-            
-            organisationUnit = organisationUnit.getParent();
-        }
-        
-        return false;
-    }
-    
-    /**
-     * Indicates whether the given organisation unit is part of the hierarchy
-     * of the organisation units of this user.
-     * 
-     * @param organisationUnit the organisation unit.
-     * @return true if the given organisation unit is part of the hierarchy.
-     */
-    public boolean isInUserHierarchy( OrganisationUnit organisationUnit )
-    {
-        return User.isInUserHierarchy( organisationUnit, organisationUnits );
-    }
     
     public String getUsername()
     {

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2015-07-07 14:54:50 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2015-07-08 09:56:56 +0000
@@ -796,16 +796,21 @@
     public boolean isInUserHierarchy( OrganisationUnit organisationUnit )
     {
         User user = currentUserService.getCurrentUser();
+        
+        if ( user == null || user.getOrganisationUnits() == null || user.getOrganisationUnits().isEmpty() )
+        {
+            return false;
+        }
 
-        return user != null ? user.isInUserHierarchy( organisationUnit ) : false;
+        return organisationUnit.isDescendant( user.getOrganisationUnits() );
     }
 
     @Override
     public boolean isInUserHierarchy( String uid, Set<OrganisationUnit> organisationUnits )
     {
         OrganisationUnit organisationUnit = organisationUnitStore.getByUid( uid );
-
-        return User.isInUserHierarchy( organisationUnit, organisationUnits );
+        
+        return organisationUnit != null ? organisationUnit.isDescendant( organisationUnits ) : false;
     }
 
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java	2015-06-16 05:11:29 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java	2015-07-08 09:56:56 +0000
@@ -983,15 +983,6 @@
         User user = createUser( 'A' );
         Set<OrganisationUnit> organisationUnits = Sets.newHashSet( ouB );
         user.setOrganisationUnits( organisationUnits );
-
-        assertTrue( user.isInUserHierarchy( ouB ) );
-        assertTrue( user.isInUserHierarchy( ouD ) );
-        assertTrue( user.isInUserHierarchy( ouE ) );
-        
-        assertFalse( user.isInUserHierarchy( ouA ) );
-        assertFalse( user.isInUserHierarchy( ouC ) );
-        assertFalse( user.isInUserHierarchy( ouF ) );
-        assertFalse( user.isInUserHierarchy( ouG ) );
         
         assertTrue( organisationUnitService.isInUserHierarchy( ouB.getUid(), organisationUnits ) );
         assertTrue( organisationUnitService.isInUserHierarchy( ouD.getUid(), organisationUnits ) );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java	2015-06-09 08:37:13 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java	2015-07-08 09:56:56 +0000
@@ -53,6 +53,8 @@
     
     void validate( DataExportParams params );
     
+    void decideAccess( DataExportParams params );
+    
     void writeDataValueSetXml( DataExportParams params, OutputStream out );
 
     void writeDataValueSetJson( DataExportParams params, OutputStream out );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2015-07-07 13:16:25 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2015-07-08 09:56:56 +0000
@@ -56,6 +56,7 @@
 import org.hisp.dhis.common.DxfNamespaces;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.IdentifiableProperty;
+import org.hisp.dhis.common.IllegalQueryException;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
@@ -228,14 +229,6 @@
             violation = "At least one valid organisation unit must be specified";
         }
         
-        for ( OrganisationUnit unit : params.getRequestOrganisationUnits() )
-        {
-            if ( !organisationUnitService.isInUserHierarchy( unit ) )
-            {
-                violation = "Organisation unit is not inside hierarchy of current user: " + unit.getUid();
-            }
-        }
-
         if ( params.hasLimit() && params.getLimit() < 0 )
         {
             violation = "Limit cannot be less than zero: " + params.getLimit();
@@ -248,6 +241,18 @@
             throw new IllegalArgumentException( violation );
         }
     }
+
+    @Override
+    public void decideAccess( DataExportParams params )
+    {
+        for ( OrganisationUnit unit : params.getRequestOrganisationUnits() )
+        {
+            if ( !organisationUnitService.isInUserHierarchy( unit ) )
+            {
+                throw new IllegalQueryException( "User is not allowed to view org unit: " + unit.getUid() );
+            }
+        }
+    }
     
     //--------------------------------------------------------------------------
     // Write
@@ -256,6 +261,7 @@
     @Override
     public void writeDataValueSetXml( DataExportParams params, OutputStream out )
     {
+        decideAccess( params );
         validate( params );
 
         dataValueSetStore.writeDataValueSetXml( params, getCompleteDate( params ), out );
@@ -264,6 +270,7 @@
     @Override
     public void writeDataValueSetJson( DataExportParams params, OutputStream out )
     {
+        decideAccess( params );
         validate( params );
 
         dataValueSetStore.writeDataValueSetJson( params, getCompleteDate( params ), out );
@@ -278,6 +285,7 @@
     @Override
     public void writeDataValueSetCsv( DataExportParams params, Writer writer )
     {
+        decideAccess( params );
         validate( params );
         
         dataValueSetStore.writeDataValueSetCsv( params, getCompleteDate( params ), writer );