← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17772: User controller. Improved logic for invites. Made it possible to add users to groups to which you...

 

------------------------------------------------------------
revno: 17772
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-12-22 11:31:50 +0100
message:
  User controller. Improved logic for invites. Made it possible to add users to groups to which you have read-only access to if you can manage the group (read-write not required in that case). Pushed code to service layer.
modified:
  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/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/UserController.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/UserGroupService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserGroupService.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserGroupService.java	2014-12-22 10:31:50 +0000
@@ -36,7 +36,7 @@
     String ID = UserGroupService.class.getName();
 
     void addUserGroup( UserGroup userGroup );
-
+    
     void updateUserGroup( UserGroup userGroup );
 
     void deleteUserGroup( UserGroup userGroup );
@@ -45,6 +45,12 @@
 
     UserGroup getUserGroup( String uid );
 
+    boolean canAddOrRemove( User user, Collection<String> uids );
+    
+    void addUserToGroups( User user, Collection<String> uids );
+    
+    void removeUserFromGroups( User user, Collection<String> uids );
+
     Collection<UserGroup> getAllUserGroups();
 
     List<UserGroup> getUserGroupByName( String name );

=== 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-09-08 13:02:43 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/DefaultUserGroupService.java	2014-12-22 10:31:50 +0000
@@ -28,12 +28,14 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Collection;
+import java.util.List;
+
+import org.hisp.dhis.acl.AclService;
 import org.hisp.dhis.common.GenericIdentifiableObjectStore;
+import org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.Collection;
-import java.util.List;
-
 @Transactional
 public class DefaultUserGroupService
     implements UserGroupService
@@ -49,6 +51,20 @@
         this.userGroupStore = userGroupStore;
     }
 
+    private CurrentUserService currentUserService;
+
+    public void setCurrentUserService( CurrentUserService currentUserService )
+    {
+        this.currentUserService = currentUserService;
+    }
+
+    private AclService aclService;
+
+    public void setAclService( AclService aclService )
+    {
+        this.aclService = aclService;
+    }
+    
     // -------------------------------------------------------------------------
     // UserGroup
     // -------------------------------------------------------------------------
@@ -90,6 +106,66 @@
     }
 
     @Override
+    public boolean canAddOrRemove( User user, Collection<String> uids )
+    {
+        User currentUser = currentUserService.getCurrentUser();
+        
+        for ( String uid : uids )
+        {
+            UserGroup userGroup = getUserGroup( uid );
+            
+            if ( userGroup == null )
+            {
+                return false;
+            }
+            
+            boolean canUpdate = aclService.canUpdate( currentUser, userGroup );
+            boolean canManage = currentUser.canManage( userGroup );
+            
+            if ( !canUpdate && !canManage )
+            {
+                return false;
+            }
+        }
+        
+        return true;
+    }
+    
+    @Override
+    public void addUserToGroups( User user, Collection<String> uids )
+    {
+        if ( !canAddOrRemove( user, uids ) )
+        {
+            throw new UpdateAccessDeniedException( user.toString() );
+        }
+        
+        for ( String uid : uids )
+        {
+            UserGroup userGroup = getUserGroup( uid );
+            user.getGroups().add( userGroup );
+            userGroup.getMembers().add( user );
+            userGroupStore.updateNoAcl( userGroup );
+        }
+    }
+
+    @Override
+    public void removeUserFromGroups( User user, Collection<String> uids )
+    {
+        if ( !canAddOrRemove( user, uids ) )
+        {
+            throw new UpdateAccessDeniedException( user.toString() );
+        }
+        
+        for ( String uid : uids )
+        {
+            UserGroup userGroup = getUserGroup( uid );
+            user.getGroups().remove( userGroup );
+            userGroup.getMembers().remove( user );
+            userGroupStore.updateNoAcl( userGroup );
+        }        
+    }
+
+    @Override
     public List<UserGroup> getUserGroupByName( String name )
     {
         return userGroupStore.getAllEqName( name );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2014-12-21 17:57:33 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/META-INF/dhis/beans.xml	2014-12-22 10:31:50 +0000
@@ -601,6 +601,8 @@
 
   <bean id="org.hisp.dhis.user.UserGroupService" class="org.hisp.dhis.user.DefaultUserGroupService">
     <property name="userGroupStore" ref="org.hisp.dhis.user.UserGroupStore" />
+    <property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
+    <property name="aclService" ref="org.hisp.dhis.acl.AclService" />
   </bean>
 
   <bean id="org.hisp.dhis.user.UserGroupAccessService" class="org.hisp.dhis.user.DefaultUserGroupAccessService">

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/UserController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/UserController.java	2014-12-22 09:30:23 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/UserController.java	2014-12-22 10:31:50 +0000
@@ -38,6 +38,7 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.common.Pager;
 import org.hisp.dhis.dxf2.metadata.ImportTypeSummary;
 import org.hisp.dhis.hibernate.exception.CreateAccessDeniedException;
@@ -383,7 +384,7 @@
 
         renderService.toJson( response.getOutputStream(), summary );
         
-        addUserGroups( user );
+        userGroupService.addUserToGroups( user, IdentifiableObjectUtils.getUids( user.getGroups() ) );
     }
 
     /**
@@ -426,24 +427,4 @@
             }
         }
     }
-
-    /**
-     * Adds user groups (if any) to the newly-created user
-     *
-     * @param user user object (including user groups) parsed from the POST request
-     */
-    private void addUserGroups( User user )
-    {        
-        if ( user.getGroups() != null )
-        {
-            for ( UserGroup ug : new ArrayList<>( user.getGroups() ) )
-            {
-                UserGroup group = userGroupService.getUserGroup( ug.getUid() );
-
-                group.addUser( user );
-
-                userGroupService.updateUserGroup( group );
-            }
-        }
-    }
 }