From 98fd5bd22d6172e7f36294bf2440e5701a8ccb0b Mon Sep 17 00:00:00 2001
From: blavenie <benoit.lavenier@e-is.pro>
Date: Wed, 11 Oct 2017 17:50:39 +0200
Subject: [PATCH] [fix] Only member can send invitation to certify - fix #561

---
 www/plugins/es/i18n/locale-en-GB.json               |  2 +-
 www/plugins/es/i18n/locale-en.json                  |  2 +-
 www/plugins/es/i18n/locale-es-ES.json               |  2 +-
 www/plugins/es/i18n/locale-fr-FR.json               |  2 +-
 www/plugins/es/js/controllers/app-controllers.js    |  2 --
 .../es/js/controllers/invitation-controllers.js     |  5 +++++
 .../es/js/controllers/message-controllers.js        | 13 +++++++++++--
 www/plugins/es/js/entities/invitation.js            |  1 -
 www/plugins/es/js/services/invitation-services.js   |  9 +++++++++
 www/plugins/es/js/services/settings-services.js     | 11 +++++++++--
 10 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/www/plugins/es/i18n/locale-en-GB.json b/www/plugins/es/i18n/locale-en-GB.json
index 6ab370203..6f3ae1c34 100644
--- a/www/plugins/es/i18n/locale-en-GB.json
+++ b/www/plugins/es/i18n/locale-en-GB.json
@@ -99,7 +99,7 @@
       "REMOVE_INVITATION_FAILED": "Error while deleting the invitation",
       "REMOVE_ALL_INVITATIONS_FAILED": "Error while deleting invitations",
       "SEND_INVITATION_FAILED": "Error while sending invitation",
-      "BAD_INVITATION_FORMAT": "<span class=\"assertive\"><i class=\"ion-close-circled\"></i> Invitation unreadable (format unknown)</span> - sent by <a href=\"#/app/wot/{{pubkey}}/{{::uid}}\">{{::name||uid||pubkey}}</a>"
+      "BAD_INVITATION_FORMAT": "<span class=\"assertive\"><i class=\"ion-close-circled\"></i> Invitation unreadable (format unknown)</span> - sent by <a ui-sref=\"app.wot_identity({pubkey: '{{::pubkey}}', uid: '{{::uid}}' })\">{{::name||uid}}</a>"
     }
   },
   "COMMENTS": {
diff --git a/www/plugins/es/i18n/locale-en.json b/www/plugins/es/i18n/locale-en.json
index 6ab370203..6f3ae1c34 100644
--- a/www/plugins/es/i18n/locale-en.json
+++ b/www/plugins/es/i18n/locale-en.json
@@ -99,7 +99,7 @@
       "REMOVE_INVITATION_FAILED": "Error while deleting the invitation",
       "REMOVE_ALL_INVITATIONS_FAILED": "Error while deleting invitations",
       "SEND_INVITATION_FAILED": "Error while sending invitation",
-      "BAD_INVITATION_FORMAT": "<span class=\"assertive\"><i class=\"ion-close-circled\"></i> Invitation unreadable (format unknown)</span> - sent by <a href=\"#/app/wot/{{pubkey}}/{{::uid}}\">{{::name||uid||pubkey}}</a>"
+      "BAD_INVITATION_FORMAT": "<span class=\"assertive\"><i class=\"ion-close-circled\"></i> Invitation unreadable (format unknown)</span> - sent by <a ui-sref=\"app.wot_identity({pubkey: '{{::pubkey}}', uid: '{{::uid}}' })\">{{::name||uid}}</a>"
     }
   },
   "COMMENTS": {
diff --git a/www/plugins/es/i18n/locale-es-ES.json b/www/plugins/es/i18n/locale-es-ES.json
index 4e39c2fc6..2192a1268 100644
--- a/www/plugins/es/i18n/locale-es-ES.json
+++ b/www/plugins/es/i18n/locale-es-ES.json
@@ -93,7 +93,7 @@
       "REMOVE_INVITATION_FAILED": "Fracaso durante la supresión de la invitación",
       "REMOVE_ALL_INVITATIONS_FAILED": "Fracaso durante la supresión de las invitaciónes",
       "SEND_INVITATION_FAILED": "Fracaso durante el envío de la invitación",
-      "BAD_INVITATION_FORMAT": "<span class=\"assertive\"><i class=\"ion-close-circled\"></i> Invitación ilegible (formato desconocido)</span> - mandada por <a href=\"#/app/wot/{{pubkey}}/{{::uid}}\">{{::name||uid}}</a>"
+      "BAD_INVITATION_FORMAT": "<span class=\"assertive\"><i class=\"ion-close-circled\"></i> Invitación ilegible (formato desconocido)</span> - mandada por <a ui-sref=\"app.wot_identity({pubkey: '{{::pubkey}}', uid: '{{::uid}}' })\">{{::name||uid}}</a>"
     }
   },
   "COMMENTS": {
diff --git a/www/plugins/es/i18n/locale-fr-FR.json b/www/plugins/es/i18n/locale-fr-FR.json
index 561c25407..9df790aa5 100644
--- a/www/plugins/es/i18n/locale-fr-FR.json
+++ b/www/plugins/es/i18n/locale-fr-FR.json
@@ -99,7 +99,7 @@
       "REMOVE_INVITATION_FAILED": "Erreur lors de la suppression de l'invitation",
       "REMOVE_ALL_INVITATIONS_FAILED": "Erreur lors de la suppression des invitations",
       "SEND_INVITATION_FAILED": "Erreur lors de l'envoi de l'invitation",
-      "BAD_INVITATION_FORMAT": "<span class=\"assertive\"><i class=\"ion-close-circled\"></i> Invitation illisible (format inconnu)</span> - envoyée par <a href=\"#/app/wot/{{pubkey}}/{{::uid}}\">{{::name||uid}}</a>"
+      "BAD_INVITATION_FORMAT": "<span class=\"assertive\"><i class=\"ion-close-circled\"></i> Invitation illisible (format inconnu)</span> - envoyée par <a ui-sref=\"app.wot_identity({pubkey: '{{::pubkey}}', uid: '{{::uid}}' })\">{{::name||uid}}</a>"
     }
   },
   "COMMENTS": {
diff --git a/www/plugins/es/js/controllers/app-controllers.js b/www/plugins/es/js/controllers/app-controllers.js
index ba52ffc29..fa4fd0f95 100644
--- a/www/plugins/es/js/controllers/app-controllers.js
+++ b/www/plugins/es/js/controllers/app-controllers.js
@@ -125,8 +125,6 @@ function ESMenuExtendController($scope, $state, PluginService, esSettings, UIUti
           popover.scope.closePopover();
         });
       }
-    })
-      .then(function() {
     });
   };
 
diff --git a/www/plugins/es/js/controllers/invitation-controllers.js b/www/plugins/es/js/controllers/invitation-controllers.js
index 1f1392c17..fc4c8ec4d 100644
--- a/www/plugins/es/js/controllers/invitation-controllers.js
+++ b/www/plugins/es/js/controllers/invitation-controllers.js
@@ -194,6 +194,11 @@ function InvitationsController($scope, $q, $ionicPopover, $state, $timeout, UIUt
   $scope.showNewInvitationModal = function() {
     $scope.hideActionsPopover();
 
+    // Not allow for non-member - issue #561
+    if (!csWallet.data.isMember) {
+      return UIUtils.alert.error('ERROR.ONLY_MEMBER_CAN_EXECUTE_THIS_ACTION');
+    }
+
     esModals.showNewInvitation({});
   };
 
diff --git a/www/plugins/es/js/controllers/message-controllers.js b/www/plugins/es/js/controllers/message-controllers.js
index 11d769af2..9279da555 100644
--- a/www/plugins/es/js/controllers/message-controllers.js
+++ b/www/plugins/es/js/controllers/message-controllers.js
@@ -61,7 +61,7 @@ angular.module('cesium.es.message.controllers', ['cesium.es.services'])
 ;
 
 function ESMessageListController($scope, $state, $translate, $ionicHistory, $ionicPopover, $timeout,
-                                 esModals, UIUtils, esMessage) {
+                                 csWallet, esModals, UIUtils, esMessage) {
   'ngInject';
 
   $scope.loading = true;
@@ -267,7 +267,7 @@ function ESMessageListController($scope, $state, $translate, $ionicHistory, $ion
 
   // Watch received message
   $scope.onNewInboxMessage = function(notification) {
-    if ($scope.type != 'inbox') return;
+    if ($scope.type != 'inbox' || !$scope.entered) return;
     // Add message sent to list
     $scope.loading = true;
     // Load the the message
@@ -283,6 +283,15 @@ function ESMessageListController($scope, $state, $translate, $ionicHistory, $ion
   };
   esMessage.api.data.on.new($scope, $scope.onNewInboxMessage);
 
+  // Watch unauth
+  $scope.onUnauth = function() {
+    // Reset all data
+    $scope.messages = undefined;
+    $scope.loading = false;
+    $scope.entered = false;
+  };
+  csWallet.api.data.on.unauth($scope, $scope.onUnauth);
+
   // for DEV only
   /*$timeout(function() {
     $scope.showNewMessageModal();
diff --git a/www/plugins/es/js/entities/invitation.js b/www/plugins/es/js/entities/invitation.js
index 906de99f0..1bba5d2d9 100644
--- a/www/plugins/es/js/entities/invitation.js
+++ b/www/plugins/es/js/entities/invitation.js
@@ -63,7 +63,6 @@ function Invitation(json) {
       that.uid = identity.uid;
     }
 
-
   }
 
 }
diff --git a/www/plugins/es/js/services/invitation-services.js b/www/plugins/es/js/services/invitation-services.js
index 99ec02914..c267967c8 100644
--- a/www/plugins/es/js/services/invitation-services.js
+++ b/www/plugins/es/js/services/invitation-services.js
@@ -94,6 +94,15 @@ angular.module('cesium.es.invitation.services', ['cesium.platform',
   function onNewInvitationEvent(event) {
     console.debug("[ES] [invitation] detected new invitation (from notification service)");
 
+    // If user not auth: simply increment counter
+    if (!csWallet.isAuth()) {
+      $rootScope.$apply(function() {
+        csWallet.data.invitations = csWallet.data.invitations || {};
+        csWallet.data.invitations.unreadCount++;
+      });
+      return;
+    }
+
     getInvitationById(event.reference.id, event.reference.type)
       .then(function(invitation){
         csWallet.data.invitations = csWallet.data.invitations || {};
diff --git a/www/plugins/es/js/services/settings-services.js b/www/plugins/es/js/services/settings-services.js
index 98462476a..7fad3ccc9 100644
--- a/www/plugins/es/js/services/settings-services.js
+++ b/www/plugins/es/js/services/settings-services.js
@@ -236,6 +236,10 @@ angular.module('cesium.es.settings.services', ['cesium.services', 'cesium.es.htt
         var boxKeypair = res[0];
         var nonce = res[1];
 
+        // Make sure user has not disconnect
+        // This can occur, when auth + disabling ES plugin in settings
+        if (!boxKeypair.boxPk || !boxKeypair.boxSk) return;
+
         var record = {
           issuer: csWallet.data.pubkey,
           nonce: CryptoUtils.util.encode_base58(nonce),
@@ -243,7 +247,6 @@ angular.module('cesium.es.settings.services', ['cesium.services', 'cesium.es.htt
         };
 
         //console.debug("Will store settings remotely: ", filteredData);
-
         var json = JSON.stringify(filteredData);
 
         return CryptoUtils.box.pack(json, nonce, boxKeypair.boxPk, boxKeypair.boxSk)
@@ -253,9 +256,13 @@ angular.module('cesium.es.settings.services', ['cesium.services', 'cesium.es.htt
             return !data.time ?
               that.add(record) :
               that.update(record, {id: record.issuer});
+          })
+          .then(function() {
+            return true;
           });
       })
-      .then(function() {
+      .then(function(saved) {
+        if (!saved) return;
         // Update settings version, then store (on local store only)
         csSettings.data.time = time;
         previousRemoteData = filteredData;
-- 
GitLab