← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18725: apply user roles when accessing programs - WIP

 

------------------------------------------------------------
revno: 18725
committer: Abyot Asalefew Gizaw <abyota@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2015-03-28 10:24:30 +0100
message:
  apply user roles when accessing programs - WIP
modified:
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/controllers.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/event-capture.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/services.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/scripts/services.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/scripts/tracker-capture.js
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.angular.services.js


--
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-apps/src/main/webapp/dhis-web-event-capture/scripts/controllers.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/controllers.js	2015-03-27 14:31:13 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/controllers.js	2015-03-28 09:24:30 +0000
@@ -58,7 +58,7 @@
     
     //notes
     $scope.note = {};
-    $scope.today = DateUtils.getToday();
+    $scope.today = DateUtils.getToday();    
     
     var userProfile = SessionStorageService.get('USER_PROFILE');
     var storedBy = userProfile && userProfile.username ? userProfile.username : '';
@@ -116,12 +116,7 @@
         if (angular.isObject($scope.selectedOrgUnit)) {    
             
             ProgramFactory.getAll().then(function(programs){
-                $scope.programs = [];
-                angular.forEach(programs, function(program){                            
-                    if(program.organisationUnits.hasOwnProperty($scope.selectedOrgUnit.id)){                                
-                        $scope.programs.push(program);
-                    }
-                });
+                $scope.programs = programs;                
                 
                 if(angular.isObject($scope.programs) && $scope.programs.length === 1){
                     $scope.selectedProgram = $scope.programs[0];

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/event-capture.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/event-capture.js	2015-03-27 12:43:44 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/event-capture.js	2015-03-28 09:24:30 +0000
@@ -143,7 +143,8 @@
     var def = $.Deferred();
     var promise = def.promise();
     
-    promise = promise.then( dhis2.ec.store.open );
+    promise = promise.then( dhis2.ec.store.open );    
+    promise = promise.then( getUserRoles );
     promise = promise.then( getCalendarSetting );
     promise = promise.then( getOrgUnitLevels );    
     promise = promise.then( getMetaPrograms );     
@@ -165,6 +166,29 @@
     def.resolve();
 }
 
+function getUserRoles()
+{
+    var SessionStorageService = angular.element('body').injector().get('SessionStorageService');
+    
+    if( SessionStorageService.get('USER_ROLES') ){
+       return; 
+    }
+    
+    var def = $.Deferred();
+
+    $.ajax({
+        url: '../api/me.json?fields=id,name,userCredentials[userRoles[id]]',
+        type: 'GET'
+    }).done(function(response) {
+        SessionStorageService.set('USER_ROLES', response);
+        def.resolve();
+    }).fail(function(){
+        def.resolve();
+    });
+
+    return def.promise();
+}
+
 function getCalendarSetting()
 {
     var SessionStorageService = angular.element('body').injector().get('SessionStorageService');
@@ -288,7 +312,7 @@
 {
     return function() {
         return $.ajax( {
-            url: '../api/programs.json?filter=id:eq:' + id +'&fields=id,name,type,version,dataEntryMethod,dateOfEnrollmentDescription,dateOfIncidentDescription,displayIncidentDate,ignoreOverdueEvents,organisationUnits[id,name],programStages[id,name,version]',
+            url: '../api/programs.json?filter=id:eq:' + id +'&fields=id,name,type,version,dataEntryMethod,dateOfEnrollmentDescription,dateOfIncidentDescription,displayIncidentDate,ignoreOverdueEvents,organisationUnits[id,name],programStages[id,name,version],userRoles[id,name]',
             type: 'GET'
         }).done( function( response ){
             

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/services.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/services.js	2015-03-04 11:34:55 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/services.js	2015-03-28 09:24:30 +0000
@@ -1,3 +1,5 @@
+/* global angular */
+
 'use strict';
 
 /* Services */
@@ -158,19 +160,37 @@
 })
 
 /* Factory to fetch programs */
-.factory('ProgramFactory', function($q, $rootScope, ECStorageService) {  
+.factory('ProgramFactory', function($q, $rootScope, SessionStorageService, ECStorageService) {  
+    
+    var userHasValidRole = function(program, userRoles){
         
+        var hasRole = false;
+
+        if($.isEmptyObject(program.userRoles)){
+            return !hasRole;
+        }
+
+        for(var i=0; i < userRoles.length && !hasRole; i++){
+            if( program.userRoles.hasOwnProperty( userRoles[i].id ) ){
+                hasRole = true;
+            }
+        }        
+        return hasRole;        
+    };
+    
     return {
         
         getAll: function(){
-            
+            var roles = SessionStorageService.get('USER_ROLES');
+            var userRoles = roles && roles.userCredentials && roles.userCredentials.userRoles ? roles.userCredentials.userRoles : [];
+            var ou = SessionStorageService.get('SELECTED_OU');
             var def = $q.defer();
             
             ECStorageService.currentStore.open().done(function(){
                 ECStorageService.currentStore.getAll('programs').done(function(prs){
-                    var programs = [];
+                    var programs = [];                    
                     angular.forEach(prs, function(pr){
-                        if(pr.type === 3){
+                        if(pr.organisationUnits.hasOwnProperty( ou.id ) && userHasValidRole(pr, userRoles)){
                             programs.push(pr);
                         }
                     });

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/scripts/services.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/scripts/services.js	2015-03-26 14:17:29 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/scripts/services.js	2015-03-28 09:24:30 +0000
@@ -199,17 +199,38 @@
 })
 
 /* Factory to fetch programs */
-.factory('ProgramFactory', function($q, $rootScope, TCStorageService) { 
-    return {
+.factory('ProgramFactory', function($q, $rootScope, SessionStorageService, TCStorageService) { 
+    
+    var userHasValidRole = function(program, userRoles){
+        
+        var hasRole = false;
+
+        if($.isEmptyObject(program.userRoles)){
+            return !hasRole;
+        }
+
+        for(var i=0; i < userRoles.length && !hasRole; i++){
+            if( program.userRoles.hasOwnProperty( userRoles[i].id ) ){
+                hasRole = true;
+            }
+        }        
+        return hasRole;        
+    };
+    
+    return {        
+        
         getAll: function(){
             
+            var roles = SessionStorageService.get('USER_ROLES');
+            var userRoles = roles && roles.userCredentials && roles.userCredentials.userRoles ? roles.userCredentials.userRoles : [];
+            var ou = SessionStorageService.get('SELECTED_OU');
             var def = $q.defer();
             
             TCStorageService.currentStore.open().done(function(){
                 TCStorageService.currentStore.getAll('programs').done(function(prs){
                     var programs = [];
                     angular.forEach(prs, function(pr){
-                        if(pr.type === 1){
+                        if(pr.organisationUnits.hasOwnProperty( ou.id ) && userHasValidRole(pr, userRoles)){
                             programs.push(pr);
                         }
                     });
@@ -235,13 +256,15 @@
             return def.promise;            
         },
         getProgramsByOu: function(ou, selectedProgram){
+            var roles = SessionStorageService.get('USER_ROLES');
+            var userRoles = roles && roles.userCredentials && roles.userCredentials.userRoles ? roles.userCredentials.userRoles : [];
             var def = $q.defer();
             
             TCStorageService.currentStore.open().done(function(){
                 TCStorageService.currentStore.getAll('programs').done(function(prs){
                     var programs = [];
                     angular.forEach(prs, function(pr){                            
-                        if(pr.organisationUnits.hasOwnProperty(ou.id)){                                
+                        if(pr.organisationUnits.hasOwnProperty( ou.id ) && userHasValidRole(pr, userRoles)){
                             programs.push(pr);
                         }
                     });

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/scripts/tracker-capture.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/scripts/tracker-capture.js	2015-03-27 12:43:44 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-tracker-capture/scripts/tracker-capture.js	2015-03-28 09:24:30 +0000
@@ -128,7 +128,8 @@
     var def = $.Deferred();
     var promise = def.promise();
 
-    promise = promise.then( dhis2.tc.store.open );    
+    promise = promise.then( dhis2.tc.store.open );
+    promise = promise.then( getUserRoles );
     promise = promise.then( getCalendarSetting );
     promise = promise.then( getRelationships );       
     promise = promise.then( getTrackedEntities );
@@ -156,6 +157,29 @@
     
 }
 
+function getUserRoles()
+{
+    var SessionStorageService = angular.element('body').injector().get('SessionStorageService');
+    
+    if( SessionStorageService.get('USER_ROLES') ){
+       return; 
+    }
+    
+    var def = $.Deferred();
+
+    $.ajax({
+        url: '../api/me.json?fields=id,name,userCredentials[userRoles[id]]',
+        type: 'GET'
+    }).done(function(response) {
+        SessionStorageService.set('USER_ROLES', response);
+        def.resolve();
+    }).fail(function(){
+        def.resolve();
+    });
+
+    return def.promise();
+}
+
 function getCalendarSetting()
 {
     var SessionStorageService = angular.element('body').injector().get('SessionStorageService');
@@ -299,7 +323,7 @@
         return $.ajax( {
             url: '../api/programs.json',
             type: 'GET',
-            data: 'paging=false&filter=id:eq:' + id +'&fields=id,name,type,version,dataEntryMethod,dateOfEnrollmentDescription,dateOfIncidentDescription,displayIncidentDate,ignoreOverdueEvents,selectEnrollmentDatesInFuture,selectIncidentDatesInFuture,onlyEnrollOnce,externalAccess,displayOnAllOrgunit,registration,relationshipText,relationshipFromA,relatedProgram[id,name],relationshipType[id,name],trackedEntity[id,name,description],userRoles[id,name],organisationUnits[id,name],programStages[id,name,version,minDaysFromStart,standardInterval,periodType,generatedByEnrollmentDate,reportDateDescription,repeatable,autoGenerateEvent,openAfterEnrollment,reportDateToUse],programTrackedEntityAttributes[displayInList,mandatory,allowFutureDate,trackedEntityAttribute[id,unique]]'
+            data: 'paging=false&filter=id:eq:' + id +'&fields=id,name,type,version,dataEntryMethod,dateOfEnrollmentDescription,dateOfIncidentDescription,displayIncidentDate,ignoreOverdueEvents,selectEnrollmentDatesInFuture,selectIncidentDatesInFuture,onlyEnrollOnce,externalAccess,displayOnAllOrgunit,registration,relationshipText,relationshipFromA,relatedProgram[id,name],relationshipType[id,name],trackedEntity[id,name,description],userRoles[id,name],organisationUnits[id,name],userRoles[id,name],programStages[id,name,version,minDaysFromStart,standardInterval,periodType,generatedByEnrollmentDate,reportDateDescription,repeatable,autoGenerateEvent,openAfterEnrollment,reportDateToUse],programTrackedEntityAttributes[displayInList,mandatory,allowFutureDate,trackedEntityAttribute[id,unique]]'
         }).done( function( response ){
             
             _.each( _.values( response.programs ), function ( program ) { 

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.angular.services.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.angular.services.js	2015-03-27 12:43:44 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/dhis2/dhis2.angular.services.js	2015-03-28 09:24:30 +0000
@@ -93,6 +93,11 @@
         },
         set: function(key, obj) {            
             $window.sessionStorage.setItem( key, JSON.stringify( obj ) );
+        },
+        clearAll: function(){
+        	for(var key in $window.sessionStorage){
+	            $window.sessionStorage.removeItem(key);
+	        }
         }
     };
 })