You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@milagro.apache.org by sa...@apache.org on 2016/08/05 14:19:44 UTC

[17/37] incubator-milagro-mfa-js-lib git commit: Fix addToUser function

Fix addToUser function


Project: http://git-wip-us.apache.org/repos/asf/incubator-milagro-mfa-js-lib/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-milagro-mfa-js-lib/commit/d4defcc4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-milagro-mfa-js-lib/tree/d4defcc4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-milagro-mfa-js-lib/diff/d4defcc4

Branch: refs/heads/add-documentation
Commit: d4defcc40b14b524179ba3f65bff187eb569c2a1
Parents: 9b77bac
Author: Boyan Bakov <bo...@certivox.com>
Authored: Wed Feb 3 15:42:08 2016 +0200
Committer: Boyan Bakov <bo...@certivox.com>
Committed: Wed Feb 10 13:39:56 2016 +0200

----------------------------------------------------------------------
 lib/mpin.js | 47 +++++++++++++++++++++++++++++++----------------
 1 file changed, 31 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-milagro-mfa-js-lib/blob/d4defcc4/lib/mpin.js
----------------------------------------------------------------------
diff --git a/lib/mpin.js b/lib/mpin.js
index 3d09851..0ae1be3 100644
--- a/lib/mpin.js
+++ b/lib/mpin.js
@@ -661,23 +661,23 @@ var mpinjs = (function () {
   };
 
   Mpin.prototype.addToUser = function (userId, userProps, skipSave) {
-    var _save;
-    if (!this.checkUser(userId) && !userProps.userId) {
-      return false;
-    }
-    _save = !skipSave;
-
-    //create
-    if (userProps.userId) {
+    if (!this.checkUser(userId)) {
+      //create
       Users[userId] = {};
-    }
+    }    
 
+    //If mpinId has changed, we need to delete the object withthe previous one
+    if (Users[userId].mpinId && userProps.mpinId && Users[userId].mpinId != userProps.mpinId) {
+       this.deleteData(userId);
+    }
+    
     for (var uKey in userProps) {
       if (userProps[uKey]) {
         Users[userId][uKey] = userProps[uKey];
       }
     }
 
+    var _save = !skipSave;
     _save && this.setData(userId, userProps);
   };
 
@@ -685,6 +685,19 @@ var mpinjs = (function () {
     Users = {};
   };
 
+  Mpin.prototype.deleteData = function (userId) {
+    var mpinData = this.getData();
+
+    var mpinId = Users[userId].mpinId;
+    if (!mpinData || !mpinData.accounts[mpinId]) {
+      return;
+    }
+    
+    delete mpinData.accounts[mpinId];
+    
+    this.storeData(mpinData);    
+  };
+  
   Mpin.prototype.setData = function (userId, upData) {
     var mpinData = this.getData();
 
@@ -695,33 +708,35 @@ var mpinjs = (function () {
       };
     }
 
+    var mpinId = upData.mpinId || Users[userId].mpinId;
+    
+    if (!mpinId) {
+      return false;
+    }
+    
     //update Default Identity
-    if (upData.mpinId) {
-      mpinData.accounts[upData.mpinId] = {};
+    if (!mpinData.accounts[mpinId]) {
+      mpinData.accounts[mpinId] = {};
     }
 
     if (upData.regOTT) {
-      mpinData.accounts[upData.mpinId].regOTT = upData.regOTT;
+      mpinData.accounts[mpinId].regOTT = upData.regOTT;
     }
 
     if (upData.timePermitHex) {
-      var mpinId = Users[userId].mpinId;
       mpinData.accounts[mpinId].MPinPermit = upData.timePermitHex;
     }
 
     if (upData.token) {
-      var mpinId = Users[userId].mpinId;
       mpinData.accounts[mpinId].token = upData.token;
     }
 
     if (upData.state && Users[userId].mpinId) {
-      var mpinId = Users[userId].mpinId;
       mpinData.accounts[mpinId].state = upData.state;
     }
 
     //cache cache
     if (upData.timePermitCache) {
-      var mpinId = Users[userId].mpinId;
       mpinData.accounts[mpinId].timePermitCache = upData.timePermitCache;
     }