← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17201: Added join/leave user group from context menu in user group management. New webapi methods for ad...

 

Merge authors:
  Halvdan Hoem Grelland (halvdanhg)
------------------------------------------------------------
revno: 17201 [merge]
committer: Halvdan Hoem Grelland <halvdanhg@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-10-22 00:44:53 +0200
message:
  Added join/leave user group from context menu in user group management. New webapi methods for adding or removing user groups for current user.
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/CurrentUserController.java
  dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/java/org/hisp/dhis/dashboard/usergroup/action/GetUserGroupListAction.java
  dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/resources/org/hisp/dhis/dashboard/i18n_module.properties
  dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/javascript/usergroup.js
  dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/userGroupList.vm


--
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-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/CurrentUserController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/CurrentUserController.java	2014-09-29 18:38:31 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/CurrentUserController.java	2014-10-21 22:42:27 +0000
@@ -37,6 +37,7 @@
 import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -55,6 +56,7 @@
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dataset.DataSetService;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
+import org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException;
 import org.hisp.dhis.i18n.I18nService;
 import org.hisp.dhis.interpretation.Interpretation;
 import org.hisp.dhis.interpretation.InterpretationService;
@@ -67,6 +69,7 @@
 import org.hisp.dhis.system.util.TextUtils;
 import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserGroup;
 import org.hisp.dhis.user.UserGroupService;
 import org.hisp.dhis.user.UserService;
 import org.hisp.dhis.user.UserSettingService;
@@ -693,4 +696,83 @@
         response.setContentType( MediaType.APPLICATION_JSON_VALUE );
         JacksonUtils.toJson( response.getOutputStream(), forms );
     }
+
+    @RequestMapping( method = RequestMethod.POST, value = "/groups" )
+    public void joinUserGroup(
+        HttpServletResponse response, @RequestParam( value = "groupUid", required = true ) String groupUid )
+        throws NotAuthenticatedException
+    {
+        User currentUser = currentUserService.getCurrentUser();
+
+        if ( currentUser == null )
+        {
+            throw new NotAuthenticatedException();
+        }
+
+        UserGroup group = manager.get( UserGroup.class, groupUid );
+
+        if ( group == null )
+        {
+            ContextUtils.notFoundResponse( response, "UserGroup does not exist: " + groupUid );
+            return;
+        }
+
+        Collection<UserGroup> userGroups = currentUser.getGroups();
+
+        if ( userGroups.contains( group ) )
+        {
+            ContextUtils.okResponse( response, "Already a member of this group." );
+            return;
+        }
+
+        if ( !aclService.canUpdate( currentUser, group ) )
+        {
+            throw new UpdateAccessDeniedException( "You don't have permissions modify this group." );
+        }
+
+        group.addUser( currentUser );
+
+        manager.update( group );
+
+        ContextUtils.okResponse( response, "Joined group." );
+    }
+
+    @RequestMapping( method = RequestMethod.DELETE, value = "/groups/{uid}" )
+    public void leaveUserGroup( HttpServletResponse response, @PathVariable( "uid" ) String groupUid )
+        throws NotAuthenticatedException
+    {
+        User currentUser = currentUserService.getCurrentUser();
+
+        if ( currentUser == null )
+        {
+            throw new NotAuthenticatedException();
+        }
+
+        UserGroup group = manager.get( UserGroup.class, groupUid );
+
+        if ( group == null )
+        {
+            ContextUtils.notFoundResponse( response, "UserGroup does not exist: " + groupUid );
+            return;
+        }
+
+        Collection<UserGroup> userGroups = currentUser.getGroups();
+
+        if ( !userGroups.contains( group ) )
+        {
+            ContextUtils.okResponse( response, "Not a member of this UserGroup." );
+            return;
+        }
+
+        if ( !aclService.canUpdate( currentUser, group ) )
+        {
+            throw new UpdateAccessDeniedException( "You don't have permissions modify this group." );
+        }
+
+        group.removeUser( currentUser );
+
+        manager.update( group );
+
+        ContextUtils.okResponse( response, "Left group." );
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/java/org/hisp/dhis/dashboard/usergroup/action/GetUserGroupListAction.java'
--- dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/java/org/hisp/dhis/dashboard/usergroup/action/GetUserGroupListAction.java	2014-10-16 06:17:19 +0000
+++ dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/java/org/hisp/dhis/dashboard/usergroup/action/GetUserGroupListAction.java	2014-10-21 16:15:25 +0000
@@ -31,9 +31,13 @@
 import static org.apache.commons.lang.StringUtils.isNotBlank;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.hisp.dhis.paging.ActionPagingSupport;
+import org.hisp.dhis.user.CurrentUserService;
+import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserGroup;
 import org.hisp.dhis.user.UserGroupService;
 
@@ -51,6 +55,13 @@
         this.userGroupService = userGroupService;
     }
 
+    private CurrentUserService currentUserService;
+
+    public void setCurrentUserService( CurrentUserService currentUserService )
+    {
+        this.currentUserService = currentUserService;
+    }
+
     // -------------------------------------------------------------------------
     // Parameters
     // -------------------------------------------------------------------------
@@ -62,6 +73,13 @@
         return userGroupList;
     }
 
+    private Map<UserGroup, Boolean> isCurrentUserMemberMap;
+
+    public Map<UserGroup, Boolean> getIsCurrentUserMemberMap()
+    {
+        return isCurrentUserMemberMap;
+    }
+
     private String key;
     
     public String getKey()
@@ -93,7 +111,28 @@
             
             userGroupList = new ArrayList<>( userGroupService.getUserGroupsBetween( paging.getStartPos(), paging.getPageSize() ) );
         }
+
+        isCurrentUserMemberMap = populateMemberShipMap( userGroupList );
         
         return SUCCESS;
     }
+
+    // -------------------------------------------------------------------------
+    // Supportive methods
+    // -------------------------------------------------------------------------
+
+    private Map<UserGroup, Boolean> populateMemberShipMap( List<UserGroup> userGroups )
+    {
+        User currentUser = currentUserService.getCurrentUser();
+
+        Map<UserGroup, Boolean> map = new HashMap<>();
+
+        for( UserGroup ug : userGroups )
+        {
+            map.put( ug, ug.getMembers().contains( currentUser ) );
+        }
+
+        return map;
+    }
 }
+

=== modified file 'dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/resources/META-INF/dhis/beans.xml	2014-06-12 16:30:25 +0000
+++ dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/resources/META-INF/dhis/beans.xml	2014-10-21 16:15:25 +0000
@@ -69,7 +69,7 @@
     scope="prototype"/>
     
   <!-- User groups -->
-  
+
   <bean id="org.hisp.dhis.dashboard.usergroup.action.AddUserGroupAction" class="org.hisp.dhis.dashboard.usergroup.action.AddUserGroupAction"
     scope="prototype">
     <property name="userService" ref="org.hisp.dhis.user.UserService" />
@@ -85,6 +85,7 @@
   <bean id="org.hisp.dhis.dashboard.usergroup.action.GetUserGroupListAction" class="org.hisp.dhis.dashboard.usergroup.action.GetUserGroupListAction"
     scope="prototype">
     <property name="userGroupService" ref="org.hisp.dhis.user.UserGroupService" />
+    <property name="currentUserService" ref="org.hisp.dhis.user.CurrentUserService" />
   </bean>
   
   <bean id="org.hisp.dhis.dashboard.usergroup.action.EditUserGroupFormAction" class="org.hisp.dhis.dashboard.usergroup.action.EditUserGroupFormAction"

=== modified file 'dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/resources/org/hisp/dhis/dashboard/i18n_module.properties'
--- dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/resources/org/hisp/dhis/dashboard/i18n_module.properties	2014-08-11 14:16:43 +0000
+++ dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/resources/org/hisp/dhis/dashboard/i18n_module.properties	2014-10-21 16:15:25 +0000
@@ -126,4 +126,6 @@
 add_items_to_current_dashboard=Add items to current dashboard
 could_not_delete_interpretation=Could not delete interpretation, please try again later
 could_not_delete_interpretation_comment=Could not delete interpretation comment, please try again later
-could_not_save_interpretation=Could not save interpretation, please try again later
\ No newline at end of file
+could_not_save_interpretation=Could not save interpretation, please try again later
+join_group=Join group
+leave_group=Leave group

=== modified file 'dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/javascript/usergroup.js'
--- dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/javascript/usergroup.js	2014-06-13 08:17:44 +0000
+++ dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/javascript/usergroup.js	2014-10-21 22:37:18 +0000
@@ -10,7 +10,7 @@
 }
 
 // -----------------------------------------------------------------------------
-// Usergroup functionality
+// User group functionality
 // -----------------------------------------------------------------------------
 
 function showUserGroupDetails( context ) {
@@ -27,3 +27,36 @@
 function removeUserGroup( context ) {
   removeItem(context.id, context.name, i18n_confirm_delete, 'removeUserGroup.action');
 }
+
+function joinUserGroup( context ) {
+  $.ajax( {
+    type: 'POST',
+    url: '../api/me/groups',
+    data: { 'groupUid'  : context.uid },
+    success: function() {
+      var $userGroup = $( "#tr" + context.id );
+      $userGroup.find( ".memberIcon" ).show();
+      $userGroup.data( "can-join", false );
+      $userGroup.data( "can-leave", true );
+    },
+    error: function( jqXHR, textStatus, errorThrown ) {
+      console.log( "Failed to join user group: " + jqXHR.responseText );
+    }
+  });
+}
+
+function leaveUserGroup( context ) {
+  $.ajax( {
+    type: 'DELETE',
+    url: '../api/me/groups/' + context.uid,
+    success: function( data ) {
+      var $userGroup = $( "#tr" + context.id );
+      $userGroup.find( ".memberIcon" ).hide();
+      $userGroup.data( "can-join", true );
+      $userGroup.data( "can-leave", false );
+    },
+    error: function( jqXHR, textStatus, errorThrown ) {
+      console.log( "Failed to leave user group: " + jqXHR.responseText );
+    }
+  });
+}
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/userGroupList.vm'
--- dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/userGroupList.vm	2014-06-13 08:17:44 +0000
+++ dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/userGroupList.vm	2014-10-20 15:42:46 +0000
@@ -9,10 +9,24 @@
 
 <div id="contextMenu" class="contextMenu">
   <ul id="contextMenuItems" class="contextMenuItems">
-    <li data-enabled="canManage"><a data-target-fn="showSharingDialogWithContext"><i class="fa fa-share"></i>&nbsp;&nbsp;$i18n.getString( "sharing_settings" )</a></li>
-    <li data-enabled="canUpdate"><a data-target-fn="editUserGroupForm"><i class="fa fa-edit"></i>&nbsp;&nbsp;$i18n.getString( "edit" )</a></li>
-    <li data-enabled="canDelete"><a data-target-fn="removeUserGroup"><i class="fa fa-trash-o"></i>&nbsp;&nbsp;$i18n.getString( "remove" )</a></li>
-    <li><a data-target-fn="showUserGroupDetails"><i class="fa fa-info-circle"></i>&nbsp;&nbsp;$i18n.getString( "show_details" )</a></li>
+    <li data-enabled="canJoin">
+      <a data-target-fn="joinUserGroup"><i class="fa fa-check-circle"></i>&nbsp;&nbsp;&nbsp;$i18n.getString( "join_group" )</a>
+    </li>
+    <li data-enabled="canLeave">
+      <a data-target-fn="leaveUserGroup"><i class="fa fa-times-circle"></i>&nbsp;&nbsp;&nbsp;$i18n.getString( "leave_group" )</a>
+    </li>
+    <li data-enabled="canManage">
+       <a data-target-fn="showSharingDialogWithContext"><i class="fa fa-share"></i>&nbsp;&nbsp;$i18n.getString( "sharing_settings" )</a>
+    </li>
+    <li data-enabled="canUpdate">
+       <a data-target-fn="editUserGroupForm"><i class="fa fa-edit"></i>&nbsp;&nbsp;$i18n.getString( "edit" )</a>
+    </li>
+    <li data-enabled="canDelete">
+      <a data-target-fn="removeUserGroup"><i class="fa fa-trash-o"></i>&nbsp;&nbsp;&nbsp;$i18n.getString( "remove" )</a>
+    </li>
+    <li>
+      <a data-target-fn="showUserGroupDetails"><i class="fa fa-info-circle"></i>&nbsp;&nbsp;&nbsp;$i18n.getString( "show_details" )</a>
+    </li>
   </ul>
 </div>
 
@@ -33,15 +47,38 @@
           <thead>
           <tr>
             <th>$i18n.getString( "name" )</th>
+            <th></th>
           </tr>
           </thead>
           <tbody id="list">
             #foreach( $userGroup in $userGroupList )
-              <tr id="tr${userGroup.id}" data-id="$!userGroup.id" data-uid="$!userGroup.uid" data-type="UserGroup" data-name="$encoder.htmlEncode( $!userGroup.displayName )"
+              <tr id="tr${userGroup.id}"
+                  data-id="$!userGroup.id"
+                  data-uid="$!userGroup.uid"
+                  data-type="UserGroup"
+                  data-name="$encoder.htmlEncode( $!userGroup.displayName )"
                   data-can-manage="$security.canManage( $userGroup )"
                   data-can-update="$security.canUpdate( $userGroup )"
-                  data-can-delete="$security.canDelete( $userGroup )">
-                  <td>$encoder.htmlEncode( $!userGroup.displayName )</td>
+                  data-can-delete="$security.canDelete( $userGroup )"
+                  #if( $isCurrentUserMemberMap.get( $userGroup ) && $security.canUpdate( $userGroup ) )
+                    data-can-join="false"
+                    data-can-leave="true"
+                  #elseif ( $security.canUpdate( $userGroup ) )
+                    data-can-join="true"
+                    data-can-leave="false"
+                  #else
+                     data-can-join="false"
+                     data-can-leave="false"
+                  #end >
+                  <td>$encoder.htmlEncode( $!userGroup.displayName )
+                  <i class="memberIcon fa fa-user"
+                  #if( not $isCurrentUserMemberMap.get( $userGroup ) )
+                     style="margin-left: 5px; display: none;"
+                  #else
+                     style="margin-left: 5px;"
+                  #end
+                  ></i>
+                  </td>
               </tr>
             #end
           </tbody>
@@ -50,7 +87,6 @@
         #parse( "/dhis-web-commons/paging/paging.vm" )
         </div>
       </td>
-
       <td id="detailsData">
         <div id="detailsArea">
           <div id="hideDetailsArea">