LP#1526185 op-change supplies egNet.handlePermFailure
authorBill Erickson <berickxx@gmail.com>
Wed, 28 Dec 2016 15:59:33 +0000 (10:59 -0500)
committerKathy Lussier <klussier@masslnc.org>
Mon, 6 Mar 2017 03:42:55 +0000 (22:42 -0500)
Teach egOpChange to handle permission failure events via the new
egNet.handlePermFailure() handler.

This commit includes additional repairs to op-change promise
handling and minor formatting adjustments.

Op-change also now displays the username of the op-changed user
in the navbar immediately upon op-change.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Kathy Lussier <klussier@masslnc.org>
Open-ILS/src/templates/staff/share/t_opchange.tt2
Open-ILS/web/js/ui/default/staff/services/navbar.js
Open-ILS/web/js/ui/default/staff/services/op_change.js

index 2ca7fbe..584c296 100644 (file)
@@ -8,30 +8,32 @@
     <h4 class="modal-title alert alert-info">{{title}}</h4>
   </div>
   <div class="modal-body">
     <h4 class="modal-title alert alert-info">{{title}}</h4>
   </div>
   <div class="modal-body">
-    <div class="row" ng-if="message">
+    <div class="row pad-all-min" ng-if="message">
       <div class="col-md-12">
         <p>{{message}}</p>
       </div>
     </div>
       <div class="col-md-12">
         <p>{{message}}</p>
       </div>
     </div>
-    <div class="row">
+    <div class="row pad-all-min">
       <div class="col-md-4">
         [% l('Username:') %]
       </div>
       <div class="col-md-1"></div>
       <div class="col-md-7">
       <div class="col-md-4">
         [% l('Username:') %]
       </div>
       <div class="col-md-1"></div>
       <div class="col-md-7">
-        <input ng-keyup="$event.keyCode == 13 ? ok() : null" type='text' ng-model="args.username" class="form-control" focus-me="focus"/>
+        <input ng-keyup="$event.keyCode == 13 ? ok() : null" type='text'
+          ng-model="args.username" class="form-control" focus-me="focus"/>
       </div>
     </div>
       </div>
     </div>
-    <div class="row">
+    <div class="row pad-all-min">
       <div class="col-md-4">
         [% l('Password:') %]
       </div>
       <div class="col-md-1"></div>
       <div class="col-md-7">
       <div class="col-md-4">
         [% l('Password:') %]
       </div>
       <div class="col-md-1"></div>
       <div class="col-md-7">
-        <input ng-keyup="$event.keyCode == 13 ? ok() : null" type='password' ng-model="args.password" class="form-control"/>
+        <input ng-keyup="$event.keyCode == 13 ? ok() : null" type='password'
+          ng-model="args.password" class="form-control"/>
       </div>
     </div>
       </div>
     </div>
-    <div class="row" ng-if="displayTypeField == true">
+    <div class="row pad-all-min" ng-if="displayTypeField == true">
       <div class="col-md-4">
         [% l('Login Type:') %]
       </div>
       <div class="col-md-4">
         [% l('Login Type:') %]
       </div>
index 1c9f97c..e70d926 100644 (file)
@@ -26,10 +26,10 @@ angular.module('egCoreMod')
             inspect(element);
         },
 
             inspect(element);
         },
 
-        controller:['$scope','$window','$location','$timeout','hotkeys','egCore','$uibModal','ngToast',
-                    'egOpChange',
-            function($scope , $window , $location , $timeout , hotkeys , egCore , $uibModal , ngToast,
-                    egOpChange) {
+        controller:['$scope','$window','$location','$timeout','hotkeys',
+                    'egCore','$uibModal','ngToast','egOpChange',
+            function($scope , $window , $location , $timeout , hotkeys ,
+                     egCore , $uibModal , ngToast, egOpChange) {
 
                 function navTo(path) {                                           
                     // Strip the leading "./" if any.
 
                 function navTo(path) {                                           
                     // Strip the leading "./" if any.
@@ -75,11 +75,17 @@ angular.module('egCoreMod')
                 }
 
                 $scope.changeOperatorUndo = function() {
                 }
 
                 $scope.changeOperatorUndo = function() {
-                    $scope.op_changed = egOpChange.changeOperatorUndo();
+                    egOpChange.changeOperatorUndo().then(function() {
+                        $scope.op_changed = false;
+                        $scope.username = egCore.auth.user().usrname();
+                    });
                 }
 
                 $scope.changeOperator = function() {
                 }
 
                 $scope.changeOperator = function() {
-                    $scope.op_changed = egOpChange.changeOperator(true);
+                    egOpChange.changeOperator(true).then(function() {
+                        $scope.op_changed = true;
+                        $scope.username = egCore.auth.user().usrname();
+                    });
                 }
 
                 $scope.currentToken = function () {
                 }
 
                 $scope.currentToken = function () {
index 3f233a7..94d6690 100644 (file)
@@ -11,66 +11,87 @@ angular.module('egCoreMod')
        ['$uibModal','$interpolate', '$rootScope', '$q', 'egAuth', 'egStrings', 'egNet', 'ngToast',
 function($uibModal, $interpolate, $rootScope, $q, egAuth, egStrings, egNet, ngToast) {
 
        ['$uibModal','$interpolate', '$rootScope', '$q', 'egAuth', 'egStrings', 'egNet', 'ngToast',
 function($uibModal, $interpolate, $rootScope, $q, egAuth, egStrings, egNet, ngToast) {
 
-       var service = {};
+    var service = {};
 
 
-
-       service.changeOperator = function(calledFromNavbar, failedRequest) {
-               var _op_changed = false;
-        $uibModal.open({
+    // Returns a promise resolved upon successful op-change.
+    // Rejected otherwise.
+    service.changeOperator = function(showTypes, permEvt) {
+        return $uibModal.open({
             templateUrl: './share/t_opchange',
             controller:
                 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
                 $scope.args = {username : '', password : '', type : 'temp'};
             templateUrl: './share/t_opchange',
             controller:
                 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
                 $scope.args = {username : '', password : '', type : 'temp'};
-                $scope.displayTypeField = calledFromNavbar;
+                $scope.displayTypeField = showTypes;
                 $scope.title = egStrings.OP_CHANGE_TITLE;
                 $scope.title = egStrings.OP_CHANGE_TITLE;
-                if(failedRequest) {
-                    $scope.title = failedRequest.perm_evt.desc + ": "
-                        + failedRequest.perm_evt.ilsperm;
+                if (permEvt) {
+                    $scope.title = permEvt.desc + ": " + permEvt.ilsperm;
                     $scope.message = egStrings.OP_CHANGE_PERM_MESSAGE;
                     $scope.message = egStrings.OP_CHANGE_PERM_MESSAGE;
-                    console.log($scope.message);
                 }
                 $scope.focus = true;
                 $scope.ok = function() { $uibModalInstance.close($scope.args) }
                 $scope.cancel = function () { $uibModalInstance.dismiss() }
             }]
         }).result.then(function (args) {
                 }
                 $scope.focus = true;
                 $scope.ok = function() { $uibModalInstance.close($scope.args) }
                 $scope.cancel = function () { $uibModalInstance.dismiss() }
             }]
         }).result.then(function (args) {
-            if (!args || !args.username || !args.password) return;
+            if (!args || !args.username || !args.password)
+                return $q.reject();
+
             args.type = args.type || 'temp';
             args.workstation = egAuth.workstation();
             args.type = args.type || 'temp';
             args.workstation = egAuth.workstation();
-            egAuth.opChange(args).then(
+            return egAuth.opChange(args).then(
                 function() {
                 function() {
-                    _op_changed = true;
-                    if(failedRequest) {
-                        console.log(js2JSON(failedRequest));
-                        egNet.request(
-                            failedRequest.service,
-                            failedRequest.method,
-                            egAuth.token(),
-                            failedRequest.params[1]
-                        ).then(service.changeOperatorUndo());
-                    } else {
-                        ngToast.create(egStrings.OP_CHANGE_SUCCESS);
-                    }
+                    console.debug('op-change succeeded');
+                    ngToast.create(egStrings.OP_CHANGE_SUCCESS);
                 },
                 function() {
                 },
                 function() {
+                    console.debug('op-change failed');
                     ngToast.warning(egStrings.OP_CHANGE_FAILURE);
                 }
             );
         });
                     ngToast.warning(egStrings.OP_CHANGE_FAILURE);
                 }
             );
         });
-        return _op_changed;
     }
 
     }
 
+    // Returns a promise resolved on successful op-change undo.
     service.changeOperatorUndo = function() {
     service.changeOperatorUndo = function() {
-        egAuth.opChangeUndo();
-        var _op_changed = false;
-        ngToast.create(egStrings.OP_CHANGE_SUCCESS);
-        return _op_changed;
+        return egAuth.opChangeUndo().then(
+            function() {
+                console.debug('op-change undo succeeded');
+                ngToast.create(egStrings.OP_CHANGE_SUCCESS);
+            },
+            function() {
+                console.debug('op-change undo failed');
+                ngToast.warning(egStrings.OP_CHANGE_FAILURE);
+            }
+        );
     }
 
     }
 
-    //Check for any permission failure broadcasts. then call changeOperator and retry the action
-    $rootScope.$on('egNetPermFailure', function(args, request_info) {
-        var op_changed = service.changeOperator(false, request_info);
-    })
+    // Tell egNet to use our permission failure handler,
+    // since we know how to launch a login override dialog.
+    //
+    // 1. Launch the change-operator dialog
+    // 2. If op-change succeeds, re-do the failed request using the
+    //    op-change'd authtoken.
+    // 3. Undo the op-change.
+    //
+    // Returns a promise resolved along with the re-ran request.
+    egNet.handlePermFailure = function(request) {
+        console.debug("perm override required for "+request.method);
+
+        return service.changeOperator(false, request.evt).then(function() {
+
+            return egNet.requestWithParamList(
+                request.service,
+                request.method,
+                // original params, but replace the failed authtoken
+                // with the op-change'd authtoken
+                [egAuth.token()].concat(request.params.splice(1))
+
+            )['finally'](function() {
+                // always undo the operator change after a perm override.
+                console.debug("clearing op-change after perm override redo");
+                service.changeOperatorUndo();
+            });
+        });
+    }
 
 
-       return service;
+    return service;
 }])
 }])