← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14340: use sharingService in core, still needs review/rewrite, but uses same code as SharingUtils did be...

 

------------------------------------------------------------
revno: 14340
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2014-03-21 12:57:24 +0100
message:
  use sharingService in core, still needs review/rewrite, but uses same code as SharingUtils did before
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/sharing/SharingService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/security/DefaultSecurityService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sharing/DefaultSharingService.java
  dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/SharingController.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/sharing/SharingService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/sharing/SharingService.java	2014-03-21 10:31:50 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/sharing/SharingService.java	2014-03-21 11:57:24 +0000
@@ -31,11 +31,16 @@
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.user.User;
 
+import java.util.Arrays;
+import java.util.List;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 public interface SharingService
 {
+    public static final List<String> SHARING_OVERRIDE_AUTHORITIES = Arrays.asList( "ALL", "F_METADATA_IMPORT" );
+
     boolean isSupported( String type );
 
     boolean isSupported( Class<?> klass );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.java	2014-03-21 09:35:30 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.java	2014-03-21 11:57:24 +0000
@@ -71,7 +71,15 @@
     @Override
     public Schema getSchema( Class<?> klass )
     {
-        return classSchemaMap.get( klass );
+        try
+        {
+            return classSchemaMap.get( klass );
+        }
+        catch ( NullPointerException ignored )
+        {
+        }
+
+        return null;
     }
 
     @Override

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/security/DefaultSecurityService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/security/DefaultSecurityService.java	2014-03-21 09:41:08 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/security/DefaultSecurityService.java	2014-03-21 11:57:24 +0000
@@ -32,10 +32,10 @@
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.common.CodeGenerator;
 import org.hisp.dhis.common.IdentifiableObject;
-import org.hisp.dhis.sharing.SharingUtils;
 import org.hisp.dhis.message.MessageSender;
 import org.hisp.dhis.period.Cal;
 import org.hisp.dhis.setting.SystemSettingManager;
+import org.hisp.dhis.sharing.SharingService;
 import org.hisp.dhis.system.util.ValidationUtils;
 import org.hisp.dhis.system.velocity.VelocityManager;
 import org.hisp.dhis.user.CurrentUserService;
@@ -103,6 +103,9 @@
     @Autowired
     private CurrentUserService currentUserService;
 
+    @Autowired
+    private SharingService sharingService;
+
     // -------------------------------------------------------------------------
     // SecurityService implementation
     // -------------------------------------------------------------------------
@@ -286,54 +289,67 @@
     @Override
     public boolean canCreatePublic( IdentifiableObject identifiableObject )
     {
-        return !SharingUtils.isSupported( identifiableObject.getClass() ) || SharingUtils.canCreatePublic( currentUserService.getCurrentUser(), identifiableObject );
+        return !sharingService.isSupported( identifiableObject.getClass() )
+            || sharingService.canCreatePublic( currentUserService.getCurrentUser(), identifiableObject.getClass() );
     }
 
     @Override
     public boolean canCreatePublic( String type )
     {
-        return !SharingUtils.isSupported( type ) || SharingUtils.canCreatePublic( currentUserService.getCurrentUser(), type );
+        Class<? extends IdentifiableObject> klass = sharingService.classForType( type );
+
+        return !sharingService.isSupported( klass )
+            || sharingService.canCreatePublic( currentUserService.getCurrentUser(), klass );
     }
 
     @Override
     public boolean canCreatePrivate( IdentifiableObject identifiableObject )
     {
-        return !SharingUtils.isSupported( identifiableObject.getClass() ) || SharingUtils.canCreatePrivate( currentUserService.getCurrentUser(), identifiableObject );
+        return !sharingService.isSupported( identifiableObject.getClass() )
+            || sharingService.canCreatePrivate( currentUserService.getCurrentUser(), identifiableObject.getClass() );
     }
 
     @Override
     public boolean canCreatePrivate( String type )
     {
-        return !SharingUtils.isSupported( type ) || SharingUtils.canCreatePrivate( currentUserService.getCurrentUser(), type );
+        Class<? extends IdentifiableObject> klass = sharingService.classForType( type );
+
+        return !sharingService.isSupported( klass )
+            || sharingService.canCreatePrivate( currentUserService.getCurrentUser(), klass );
     }
 
     @Override
     public boolean canRead( IdentifiableObject identifiableObject )
     {
-        return !SharingUtils.isSupported( identifiableObject.getClass() ) || SharingUtils.canRead( currentUserService.getCurrentUser(), identifiableObject );
+        return !sharingService.isSupported( identifiableObject.getClass() )
+            || sharingService.canRead( currentUserService.getCurrentUser(), identifiableObject );
     }
 
     @Override
     public boolean canWrite( IdentifiableObject identifiableObject )
     {
-        return !SharingUtils.isSupported( identifiableObject.getClass() ) || SharingUtils.canWrite( currentUserService.getCurrentUser(), identifiableObject );
+        return !sharingService.isSupported( identifiableObject.getClass() )
+            || sharingService.canWrite( currentUserService.getCurrentUser(), identifiableObject );
     }
 
     @Override
     public boolean canUpdate( IdentifiableObject identifiableObject )
     {
-        return !SharingUtils.isSupported( identifiableObject.getClass() ) || SharingUtils.canUpdate( currentUserService.getCurrentUser(), identifiableObject );
+        return !sharingService.isSupported( identifiableObject.getClass() )
+            || sharingService.canUpdate( currentUserService.getCurrentUser(), identifiableObject );
     }
 
     @Override
     public boolean canDelete( IdentifiableObject identifiableObject )
     {
-        return !SharingUtils.isSupported( identifiableObject.getClass() ) || SharingUtils.canDelete( currentUserService.getCurrentUser(), identifiableObject );
+        return !sharingService.isSupported( identifiableObject.getClass() )
+            || sharingService.canDelete( currentUserService.getCurrentUser(), identifiableObject );
     }
 
     @Override
     public boolean canManage( IdentifiableObject identifiableObject )
     {
-        return !SharingUtils.isSupported( identifiableObject.getClass() ) || SharingUtils.canManage( currentUserService.getCurrentUser(), identifiableObject );
+        return !sharingService.isSupported( identifiableObject.getClass() )
+            || sharingService.canManage( currentUserService.getCurrentUser(), identifiableObject );
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sharing/DefaultSharingService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sharing/DefaultSharingService.java	2014-03-21 11:01:40 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sharing/DefaultSharingService.java	2014-03-21 11:57:24 +0000
@@ -52,8 +52,6 @@
     @Autowired
     private SchemaService schemaService;
 
-    public static final List<String> SHARING_OVERRIDE_AUTHORITIES = Arrays.asList( "ALL", "F_METADATA_IMPORT" );
-
     @Override
     public boolean isSupported( String type )
     {

=== 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-03-21 09:41:08 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2014-03-21 11:57:24 +0000
@@ -36,18 +36,18 @@
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.criterion.Criterion;
-import org.hisp.dhis.sharing.AccessStringHelper;
 import org.hisp.dhis.common.AuditLogUtil;
 import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.GenericStore;
 import org.hisp.dhis.common.IdentifiableObject;
-import org.hisp.dhis.sharing.SharingUtils;
 import org.hisp.dhis.dashboard.Dashboard;
 import org.hisp.dhis.hibernate.exception.CreateAccessDeniedException;
 import org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException;
 import org.hisp.dhis.hibernate.exception.ReadAccessDeniedException;
 import org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException;
 import org.hisp.dhis.interpretation.Interpretation;
+import org.hisp.dhis.sharing.AccessStringHelper;
+import org.hisp.dhis.sharing.SharingService;
 import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.user.UserGroupAccess;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -84,6 +84,9 @@
     @Autowired
     protected CurrentUserService currentUserService;
 
+    @Autowired
+    protected SharingService sharingService;
+
     protected Class<T> clazz;
 
     /**
@@ -224,7 +227,7 @@
     @Override
     public int save( T object )
     {
-        if ( !Interpretation.class.isAssignableFrom( clazz ) && currentUserService.getCurrentUser() != null && SharingUtils.isSupported( clazz ) )
+        if ( !Interpretation.class.isAssignableFrom( clazz ) && currentUserService.getCurrentUser() != null && sharingService.isSupported( clazz ) )
         {
             BaseIdentifiableObject identifiableObject = (BaseIdentifiableObject) object;
 
@@ -237,9 +240,9 @@
                 identifiableObject.setUser( currentUserService.getCurrentUser() );
             }
 
-            if ( SharingUtils.canCreatePublic( currentUserService.getCurrentUser(), identifiableObject ) )
+            if ( sharingService.canCreatePublic( currentUserService.getCurrentUser(), identifiableObject.getClass() ) )
             {
-                if ( SharingUtils.defaultPublic( clazz ) )
+                if ( sharingService.defaultPublic( identifiableObject.getClass() ) )
                 {
                     String build = AccessStringHelper.newInstance()
                         .enable( AccessStringHelper.Permission.READ )
@@ -254,7 +257,7 @@
                     identifiableObject.setPublicAccess( build );
                 }
             }
-            else if ( SharingUtils.canCreatePrivate( currentUserService.getCurrentUser(), identifiableObject ) )
+            else if ( sharingService.canCreatePrivate( currentUserService.getCurrentUser(), identifiableObject.getClass() ) )
             {
                 identifiableObject.setPublicAccess( AccessStringHelper.newInstance().build() );
             }
@@ -397,9 +400,9 @@
 
     protected boolean sharingEnabled()
     {
-        boolean enabled = forceAcl() || ( SharingUtils.isSupported( clazz ) && !( currentUserService.getCurrentUser() == null ||
-            CollectionUtils.containsAny( currentUserService.getCurrentUser().getUserCredentials().getAllAuthorities(), SharingUtils.SHARING_OVERRIDE_AUTHORITIES ) ) );
-        
+        boolean enabled = forceAcl() || (sharingService.isSupported( clazz ) && !(currentUserService.getCurrentUser() == null ||
+            CollectionUtils.containsAny( currentUserService.getCurrentUser().getUserCredentials().getAllAuthorities(), SharingService.SHARING_OVERRIDE_AUTHORITIES )));
+
         return enabled;
     }
 
@@ -411,7 +414,7 @@
 
             if ( sharingEnabled() )
             {
-                return SharingUtils.canRead( currentUserService.getCurrentUser(), idObject );
+                return sharingService.canRead( currentUserService.getCurrentUser(), idObject );
             }
         }
 
@@ -426,7 +429,7 @@
 
             if ( sharingEnabled() )
             {
-                return SharingUtils.canWrite( currentUserService.getCurrentUser(), idObject );
+                return sharingService.canWrite( currentUserService.getCurrentUser(), idObject );
             }
         }
 
@@ -439,9 +442,9 @@
         {
             IdentifiableObject idObject = (IdentifiableObject) object;
 
-            if ( SharingUtils.isSupported( clazz ) )
+            if ( sharingService.isSupported( clazz ) )
             {
-                return SharingUtils.canUpdate( currentUserService.getCurrentUser(), idObject );
+                return sharingService.canUpdate( currentUserService.getCurrentUser(), idObject );
             }
         }
 
@@ -454,9 +457,9 @@
         {
             IdentifiableObject idObject = (IdentifiableObject) object;
 
-            if ( SharingUtils.isSupported( clazz ) )
+            if ( sharingService.isSupported( clazz ) )
             {
-                return SharingUtils.canDelete( currentUserService.getCurrentUser(), idObject );
+                return sharingService.canDelete( currentUserService.getCurrentUser(), idObject );
             }
         }
 

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/SharingController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/SharingController.java	2014-03-21 09:41:08 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/SharingController.java	2014-03-21 11:57:24 +0000
@@ -34,13 +34,12 @@
 import org.hisp.dhis.api.webdomain.sharing.Sharing;
 import org.hisp.dhis.api.webdomain.sharing.SharingUserGroupAccess;
 import org.hisp.dhis.api.webdomain.sharing.SharingUserGroups;
-import org.hisp.dhis.sharing.AccessStringHelper;
 import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObjectManager;
-import org.hisp.dhis.sharing.SharingUtils;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
-import org.hisp.dhis.security.SecurityService;
+import org.hisp.dhis.sharing.AccessStringHelper;
+import org.hisp.dhis.sharing.SharingService;
 import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.user.UserGroup;
 import org.hisp.dhis.user.UserGroupAccess;
@@ -79,21 +78,22 @@
     private UserGroupService userGroupService;
 
     @Autowired
-    private SecurityService securityService;
-
-    @Autowired
     private UserGroupAccessService userGroupAccessService;
 
+    @Autowired
+    private SharingService sharingService;
+
     @RequestMapping( value = "", produces = { "application/json", "text/*" } )
     public void getSharing( @RequestParam String type, @RequestParam String id, HttpServletResponse response ) throws IOException
     {
-        if ( !SharingUtils.isSupported( type ) )
+        if ( !sharingService.isSupported( type ) )
         {
             ContextUtils.notFoundResponse( response, "Type " + type + " is not supported." );
             return;
         }
 
-        IdentifiableObject object = manager.get( SharingUtils.classForType( type ), id );
+        Class<? extends IdentifiableObject> klass = sharingService.classForType( type );
+        IdentifiableObject object = manager.get( klass, id );
 
         if ( object == null )
         {
@@ -101,15 +101,15 @@
             return;
         }
 
-        if ( !securityService.canManage( object ) )
+        if ( !sharingService.canManage( currentUserService.getCurrentUser(), object ) )
         {
             throw new AccessDeniedException( "You do not have manage access to this object." );
         }
 
         Sharing sharing = new Sharing();
 
-        sharing.getMeta().setAllowPublicAccess( SharingUtils.canCreatePublic( currentUserService.getCurrentUser(), object ) );
-        sharing.getMeta().setAllowExternalAccess( SharingUtils.canExternalize( currentUserService.getCurrentUser(), object ) );
+        sharing.getMeta().setAllowPublicAccess( sharingService.canCreatePublic( currentUserService.getCurrentUser(), object.getClass() ) );
+        sharing.getMeta().setAllowExternalAccess( sharingService.canExternalize( currentUserService.getCurrentUser(), object.getClass() ) );
 
         sharing.getObject().setId( object.getUid() );
         sharing.getObject().setName( object.getDisplayName() );
@@ -119,7 +119,7 @@
         {
             String access;
 
-            if ( SharingUtils.canCreatePublic( currentUserService.getCurrentUser(), type ) )
+            if ( sharingService.canCreatePublic( currentUserService.getCurrentUser(), klass ) )
             {
                 access = AccessStringHelper.newInstance().enable( AccessStringHelper.Permission.READ ).enable( AccessStringHelper.Permission.WRITE ).build();
             }
@@ -157,7 +157,7 @@
     @RequestMapping( value = "", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = "application/json" )
     public void setSharing( @RequestParam String type, @RequestParam String id, HttpServletResponse response, HttpServletRequest request ) throws IOException
     {
-        BaseIdentifiableObject object = (BaseIdentifiableObject) manager.get( SharingUtils.classForType( type ), id );
+        BaseIdentifiableObject object = (BaseIdentifiableObject) manager.get( sharingService.classForType( type ), id );
 
         if ( object == null )
         {
@@ -165,7 +165,7 @@
             return;
         }
 
-        if ( !securityService.canManage( object ) )
+        if ( !sharingService.canManage( currentUserService.getCurrentUser(), object ) )
         {
             throw new AccessDeniedException( "You do not have manage access to this object." );
         }
@@ -173,13 +173,13 @@
         Sharing sharing = JacksonUtils.fromJson( request.getInputStream(), Sharing.class );
 
         // Ignore externalAccess if user is not allowed to make objects external
-        if ( SharingUtils.canExternalize( currentUserService.getCurrentUser(), object ) )
+        if ( sharingService.canExternalize( currentUserService.getCurrentUser(), object.getClass() ) )
         {
             object.setExternalAccess( sharing.getObject().hasExternalAccess() );
         }
 
         // Ignore publicAccess if user is not allowed to make objects public
-        if ( SharingUtils.canCreatePublic( currentUserService.getCurrentUser(), object ) )
+        if ( sharingService.canCreatePublic( currentUserService.getCurrentUser(), object.getClass() ) )
         {
             object.setPublicAccess( sharing.getObject().getPublicAccess() );
         }