← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16608: minor fix/opt, include user arg in sharing criteria (default to currentUser)

 

------------------------------------------------------------
revno: 16608
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-09-02 13:24:44 +0700
message:
  minor fix/opt, include user arg in sharing criteria (default to currentUser)
modified:
  dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.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-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java'
--- dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2014-09-02 05:23:24 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2014-09-02 06:24:44 +0000
@@ -54,10 +54,12 @@
 import org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException;
 import org.hisp.dhis.interpretation.Interpretation;
 import org.hisp.dhis.user.CurrentUserService;
+import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserGroupAccess;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Required;
 import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.util.Assert;
 import org.springframework.util.CollectionUtils;
 
 import java.util.HashSet;
@@ -170,6 +172,8 @@
     /**
      * Creates a Criteria for the implementation Class type.
      *
+     * Please note that sharing is not considered.
+     *
      * @return a Criteria instance.
      */
     protected final Criteria getCriteria()
@@ -179,18 +183,25 @@
 
     protected final Disjunction getSharingDisjunction()
     {
+        return getSharingDisjunction( currentUserService.getCurrentUser() );
+    }
+
+    protected final Disjunction getSharingDisjunction( User user )
+    {
+        Assert.notNull( user, "User argument can't be null." );
+
         Disjunction disjunction = Restrictions.disjunction();
 
         disjunction.add( Restrictions.like( "publicAccess", "r%" ) );
         disjunction.add( Restrictions.isNull( "user" ) );
-        disjunction.add( Restrictions.eq( "user", currentUserService.getCurrentUser() ) );
+        disjunction.add( Restrictions.eq( "user", user ) );
 
         DetachedCriteria detachedCriteria = DetachedCriteria.forClass( UserGroupAccess.class, "uga" );
         detachedCriteria.createAlias( "uga.userGroup", "ug" );
         detachedCriteria.createAlias( "ug.members", "ugm" );
 
         detachedCriteria.add( Restrictions.like( "uga.access", "r%" ) );
-        detachedCriteria.add( Restrictions.eq( "ugm.id", currentUserService.getCurrentUser().getId() ) );
+        detachedCriteria.add( Restrictions.eq( "ugm.id", user.getId() ) );
 
         detachedCriteria.setProjection( Projections.id() );
 
@@ -199,13 +210,13 @@
         return disjunction;
     }
 
-    /**
-     * Creates a Criteria for the implementation Class type.
-     *
-     * @return a Criteria instance.
-     */
     protected final Criteria getSharingCriteria()
     {
+        return getSharingCriteria( currentUserService.getCurrentUser() );
+    }
+
+    protected final Criteria getSharingCriteria( User user )
+    {
         Criteria criteria = getCriteria();
 
         if ( !sharingEnabled() )
@@ -213,7 +224,9 @@
             return criteria;
         }
 
-        criteria.add( getSharingDisjunction() );
+        Assert.notNull( user, "User argument can't be null." );
+
+        criteria.add( getSharingDisjunction( user ) );
 
         return criteria;
     }