← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14880: Analytics, updated user org unit logic to use data view org unit with fallback to data capture or...

 

------------------------------------------------------------
revno: 14880
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-04-16 16:01:54 +0200
message:
  Analytics, updated user org unit logic to use data view org unit with fallback to data capture org unit
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java
  dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/security/DefaultAnalyticsSecurityManager.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/common/BaseAnalyticalObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java	2014-04-14 14:17:32 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseAnalyticalObject.java	2014-04-16 14:01:54 +0000
@@ -298,19 +298,19 @@
             items.addAll( organisationUnits );
             items.addAll( transientOrganisationUnits );
             
-            if ( userOrganisationUnit && user != null && user.hasOrganisationUnit() )
-            {
-                items.add( user.getOrganisationUnit() );
-            }
-            
-            if ( userOrganisationUnitChildren && user != null && user.hasOrganisationUnit() )
-            {
-                items.addAll( user.getOrganisationUnit().getSortedChildren() );
-            }
-            
-            if ( userOrganisationUnitGrandChildren && user != null && user.hasOrganisationUnit() )
-            {
-                items.addAll( user.getOrganisationUnit().getSortedGrandChildren() );
+            if ( userOrganisationUnit && user != null && user.hasDataViewOrganisationUnitWithFallback() )
+            {
+                items.add( user.getDataViewOrganisationUnitWithFallback() );
+            }
+            
+            if ( userOrganisationUnitChildren && user != null && user.hasDataViewOrganisationUnitWithFallback() )
+            {
+                items.addAll( user.getDataViewOrganisationUnitWithFallback().getSortedChildren() );
+            }
+            
+            if ( userOrganisationUnitGrandChildren && user != null && user.hasDataViewOrganisationUnitWithFallback() )
+            {
+                items.addAll( user.getDataViewOrganisationUnitWithFallback().getSortedGrandChildren() );
             }
             
             if ( organisationUnitLevels != null && !organisationUnitLevels.isEmpty() && organisationUnitsAtLevel != null )

=== 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-04-05 20:08:37 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java	2014-04-16 14:01:54 +0000
@@ -190,8 +190,6 @@
      * Null is returned if the user has no organisation units. Which
      * organisation unit to return is undefined if the user has multiple
      * organisation units.
-     *
-     * @return an organisation unit associated with the user.
      */
     public OrganisationUnit getOrganisationUnit()
     {
@@ -208,6 +206,27 @@
         return !CollectionUtils.isEmpty( dataViewOrganisationUnits );
     }
 
+    public OrganisationUnit getDataViewOrganisationUnit()
+    {
+        return CollectionUtils.isEmpty( dataViewOrganisationUnits ) ? null : dataViewOrganisationUnits.iterator().next();
+    }
+    
+    public boolean hasDataViewOrganisationUnitWithFallback()
+    {
+        return hasDataViewOrganisationUnit() || hasOrganisationUnit();
+    }
+    
+    /**
+     * Returns the first of the data view organisation units associated with the
+     * user. If none, returns the first of the data capture organisation units.
+     * If none, return nulls.
+     */
+    public OrganisationUnit getDataViewOrganisationUnitWithFallback()
+    {
+        OrganisationUnit dataViewOrgUnit = getDataViewOrganisationUnit();
+        return dataViewOrgUnit != null ? dataViewOrgUnit : getOrganisationUnit();
+    }
+    
     public String getOrganisationUnitsName()
     {
         return IdentifiableObjectUtils.join( organisationUnits );

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2014-04-14 11:23:57 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/data/DefaultAnalyticsService.java	2014-04-16 14:01:54 +0000
@@ -909,17 +909,17 @@
             
             for ( String ou : items )
             {
-                if ( KEY_USER_ORGUNIT.equals( ou ) && user != null && user.getOrganisationUnit() != null )
-                {
-                    ous.add( user.getOrganisationUnit() );
-                }
-                else if ( KEY_USER_ORGUNIT_CHILDREN.equals( ou ) && user != null && user.getOrganisationUnit() != null )
-                {
-                    ous.addAll( user.getOrganisationUnit().getSortedChildren() );
-                }
-                else if ( KEY_USER_ORGUNIT_GRANDCHILDREN.equals( ou ) && user != null && user.getOrganisationUnit() != null )
-                {
-                    ous.addAll( user.getOrganisationUnit().getSortedGrandChildren() );
+                if ( KEY_USER_ORGUNIT.equals( ou ) && user != null && user.hasDataViewOrganisationUnitWithFallback() )
+                {
+                    ous.add( user.getDataViewOrganisationUnitWithFallback() );
+                }
+                else if ( KEY_USER_ORGUNIT_CHILDREN.equals( ou ) && user != null && user.hasDataViewOrganisationUnitWithFallback() )
+                {
+                    ous.addAll( user.getDataViewOrganisationUnitWithFallback().getSortedChildren() );
+                }
+                else if ( KEY_USER_ORGUNIT_GRANDCHILDREN.equals( ou ) && user != null && user.hasDataViewOrganisationUnitWithFallback() )
+                {
+                    ous.addAll( user.getDataViewOrganisationUnitWithFallback().getSortedGrandChildren() );
                 }
                 else if ( ou != null && ou.startsWith( KEY_LEVEL ) )
                 {

=== modified file 'dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/security/DefaultAnalyticsSecurityManager.java'
--- dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/security/DefaultAnalyticsSecurityManager.java	2014-04-14 11:23:57 +0000
+++ dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/security/DefaultAnalyticsSecurityManager.java	2014-04-16 14:01:54 +0000
@@ -88,20 +88,20 @@
         
         List<NameableObject> queryOrgUnits = params.getDimensionOrFilter( DimensionalObject.ORGUNIT_DIM_ID );
         
-        if ( queryOrgUnits == null || user == null || !user.hasDataViewOrganisationUnit() )
+        if ( queryOrgUnits == null || queryOrgUnits.isEmpty() || user == null || !user.hasDataViewOrganisationUnit() )
         {
-            return;
+            return; // Allow if no 
         }
         
         Set<OrganisationUnit> viewOrgUnits = user.getDataViewOrganisationUnits();
-        
+                
         for ( NameableObject object : queryOrgUnits )
         {
             OrganisationUnit queryOrgUnit = (OrganisationUnit) object;
             
             if ( !queryOrgUnit.isEqualOrChildOf( viewOrgUnits ) )
             {
-                throw new IllegalQueryException( "Org unit is not viewable for current user: " + queryOrgUnit.getUid() );
+                throw new IllegalQueryException( "User: " + user.getUsername() + " is not allowed to view org unit: " + queryOrgUnit.getUid() );
             }
         }
     }