[gnome-shell/T29763: 168/249] customerSupport: Manage vendor-customer-support.ini from its own class



commit 58913a7a61fb19d33af3af48e7dccab3703d419e
Author: Mario Sanchez Prada <mario endlessm com>
Date:   Wed Apr 4 11:04:52 2018 +0100

    customerSupport: Manage vendor-customer-support.ini from its own class
    
    This commit does not implement any functional change, but it will allow
    extending and sharing the functionality of such class for other use cases
    we might have, such as Pay-as-You-Go.
    
    https://phabricator.endlessm.com/T21864

 js/gdm/authPrompt.js          | 74 ++++------------------------------
 js/js-resources.gresource.xml |  1 +
 js/misc/customerSupport.js    | 93 +++++++++++++++++++++++++++++++++++++++++++
 js/ui/main.js                 |  5 +++
 4 files changed, 106 insertions(+), 67 deletions(-)
---
diff --git a/js/gdm/authPrompt.js b/js/gdm/authPrompt.js
index 5089b244a1..4a0f56aeef 100644
--- a/js/gdm/authPrompt.js
+++ b/js/gdm/authPrompt.js
@@ -7,9 +7,9 @@ const ByteArray = imports.byteArray;
 
 const Animation = imports.ui.animation;
 const Batch = imports.gdm.batch;
-const Config = imports.misc.config;
 const GdmUtil = imports.gdm.util;
 const Util = imports.misc.util;
+const Main = imports.ui.main;
 const Params = imports.misc.params;
 const ShellEntry = imports.ui.shellEntry;
 const UserWidget = imports.ui.userWidget;
@@ -22,17 +22,6 @@ var MESSAGE_FADE_OUT_ANIMATION_TIME = 500;
 
 const _RESET_CODE_LENGTH = 7;
 
-const CUSTOMER_SUPPORT_FILENAME = 'vendor-customer-support.ini';
-const CUSTOMER_SUPPORT_LOCATIONS = [
-    Config.LOCALSTATEDIR + '/lib/eos-image-defaults/' + CUSTOMER_SUPPORT_FILENAME,
-    Config.PKGDATADIR + '/' + CUSTOMER_SUPPORT_FILENAME
-];
-
-const CUSTOMER_SUPPORT_GROUP_NAME = 'Customer Support';
-const CUSTOMER_SUPPORT_KEY_EMAIL = 'Email';
-const PASSWORD_RESET_GROUP_NAME = 'Password Reset';
-const PASSWORD_RESET_KEY_SALT = 'Salt';
-
 var AuthPromptMode = {
     UNLOCK_ONLY: 0,
     UNLOCK_OR_LOG_IN: 1,
@@ -174,7 +163,6 @@ var AuthPrompt = GObject.registerClass({
 
         this._displayingPasswordHint = false;
         this._customerSupportKeyFile = null;
-        this._customerSupportEmail = null;
         this._passwordResetCode = null;
     }
 
@@ -629,24 +617,6 @@ var AuthPrompt = GObject.registerClass({
         this.emit('cancelled');
     }
 
-    _ensureCustomerSupportFile() {
-        if (this._customerSupportKeyFile)
-            return this._customerSupportKeyFile;
-
-        this._customerSupportKeyFile = new GLib.KeyFile();
-
-        for (let path of CUSTOMER_SUPPORT_LOCATIONS) {
-            try {
-                this._customerSupportKeyFile.load_from_file(path, GLib.KeyFileFlags.NONE);
-                break;
-            } catch (e) {
-                logError(e, 'Failed to read customer support data from %s'.format(path));
-            }
-        }
-
-        return this._customerSupportKeyFile;
-    }
-
     _getUserLastLoginTime() {
         let userManager = AccountsService.UserManager.get_default();
         let user = userManager.get_user(this._username);
@@ -660,7 +630,7 @@ var AuthPrompt = GObject.registerClass({
 
         // The fist digit is fixed to "1" as version of the hash code (the zeroth
         // version had one less digit in the code).
-        let resetCode = this._getResetCodeSalt() ? '1' : '';
+        let resetCode = Main.customerSupport.passwordResetSalt ? '1' : '';
 
         let machineId = _getMachineId();
         let lastLoginTime = this._getUserLastLoginTime();
@@ -675,12 +645,11 @@ var AuthPrompt = GObject.registerClass({
     }
 
     _computeUnlockCode(resetCode) {
-        let salt = this._getResetCodeSalt();
         let checksum = new GLib.Checksum(GLib.ChecksumType.MD5);
         checksum.update(ByteArray.fromString(resetCode));
 
-        if (salt) {
-            checksum.update(ByteArray.fromString(salt));
+        if (Main.customerSupport.passwordResetSalt) {
+            checksum.update(ByteArray.fromString(Main.customerSupport.passwordResetSalt));
             checksum.update([0]);
         }
 
@@ -695,37 +664,8 @@ var AuthPrompt = GObject.registerClass({
         return unlockCode;
     }
 
-    _getCustomerSupportEmail() {
-        let keyFile = this._ensureCustomerSupportFile();
-
-        try {
-            return keyFile.get_locale_string(
-                CUSTOMER_SUPPORT_GROUP_NAME,
-                CUSTOMER_SUPPORT_KEY_EMAIL,
-                null);
-        } catch (e) {
-            logError(e, 'Failed to read customer support email');
-            return null;
-        }
-    }
-
-    _getResetCodeSalt() {
-        let keyFile = this._ensureCustomerSupportFile();
-
-        try {
-            return keyFile.get_locale_string(
-                PASSWORD_RESET_GROUP_NAME,
-                PASSWORD_RESET_KEY_SALT,
-                null);
-        } catch (e) {
-            logError(e, 'Failed to read password reset salt value');
-            return null;
-        }
-    }
-
     _showPasswordResetPrompt() {
-        let customerSupportEmail = this._getCustomerSupportEmail();
-        if (!customerSupportEmail)
+        if (!Main.customerSupport.customerSupportEmail)
             return;
 
         // Stop the normal gdm conversation so it doesn't interfere.
@@ -742,7 +682,7 @@ var AuthPrompt = GObject.registerClass({
             // Translators: Password reset. The first %s is a verification code and the second is an email.
             _('Please inform customer support of your verification code %s by emailing %s. Customer support 
will use the verification code to provide you with an unlock code, which you can enter here.').format(
                 this._passwordResetCode,
-                customerSupportEmail));
+                Main.customerSupport.customerSupportEmail));
     }
 
     _maybeShowPasswordResetButton() {
@@ -783,7 +723,7 @@ var AuthPrompt = GObject.registerClass({
             (obj, result) => {
                 try {
                     const permission = Polkit.Permission.new_finish(result);
-                    if (permission.get_allowed() && this._getCustomerSupportEmail())
+                    if (permission.get_allowed() && Main.customerSupport.customerSupportEmail)
                         this._passwordResetButton.show();
                 } catch (e) {
                     logError(e, 'Failed to determine if password reset is allowed');
diff --git a/js/js-resources.gresource.xml b/js/js-resources.gresource.xml
index f73f31836e..4fca81d025 100644
--- a/js/js-resources.gresource.xml
+++ b/js/js-resources.gresource.xml
@@ -141,6 +141,7 @@
 
     <!-- Endless-specific files beyond this point -->
 
+    <file>misc/customerSupport.js</file>
     <file>misc/parentalControlsManager.js</file>
     <file>misc/paygManager.js</file>
     <file>ui/appActivation.js</file>
diff --git a/js/misc/customerSupport.js b/js/misc/customerSupport.js
new file mode 100644
index 0000000000..6deaf21ff7
--- /dev/null
+++ b/js/misc/customerSupport.js
@@ -0,0 +1,93 @@
+// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
+//
+// Copyright (C) 2017, 2018 Endless Mobile, Inc.
+//
+// Licensed under the GNU General Public License Version 2
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+const { GLib, GObject } = imports.gi;
+
+const Config = imports.misc.config;
+
+const CUSTOMER_SUPPORT_FILENAME = 'vendor-customer-support.ini';
+const CUSTOMER_SUPPORT_LOCATIONS = [
+    Config.LOCALSTATEDIR + '/lib/eos-image-defaults',
+    Config.PKGDATADIR,
+];
+
+const CUSTOMER_SUPPORT_GROUP_NAME = 'Customer Support';
+const CUSTOMER_SUPPORT_KEY_EMAIL = 'Email';
+
+const PASSWORD_RESET_GROUP_NAME = 'Password Reset';
+const PASSWORD_RESET_KEY_SALT = 'Salt';
+
+var CustomerSupport = GObject.registerClass(
+class CustomerSupport extends GObject.Object {
+
+    _init() {
+        super._init();
+        this._customerSupportKeyFile = null;
+    }
+
+    _ensureCustomerSupportFile() {
+        if (this._customerSupportKeyFile)
+            return this._customerSupportKeyFile;
+
+        this._customerSupportKeyFile = new GLib.KeyFile();
+        try {
+            this._customerSupportKeyFile.load_from_dirs(CUSTOMER_SUPPORT_FILENAME,
+                                                        CUSTOMER_SUPPORT_LOCATIONS,
+                                                        GLib.KeyFileFlags.NONE);
+        } catch (e) {
+            logError(e, "Failed to read customer support data");
+        }
+    }
+
+    _getLocaleString(groupName, keyName) {
+        this._ensureCustomerSupportFile();
+
+        try {
+            return this._customerSupportKeyFile.get_locale_string(groupName, keyName, null);
+        } catch (e) {
+            if (e.matches(GLib.KeyFileError, GLib.KeyFileError.KEY_NOT_FOUND))
+                log("Key '" + keyName + "' from group '" + groupName + "' does not exist");
+            else if (e.matches(GLib.KeyFileError, GLib.KeyFileError.GROUP_NOT_FOUND))
+                log("Group '" + groupName + "' does not exist");
+            else
+                logError(e, "Failed to read key '" + keyName + "' from group '" + groupName + "'");
+
+            return null;
+        }
+    }
+
+    get customerSupportEmail() {
+        if (this._supportEmail === undefined) {
+            this._supportEmail = this._getLocaleString(CUSTOMER_SUPPORT_GROUP_NAME,
+                                                       CUSTOMER_SUPPORT_KEY_EMAIL);
+        }
+
+        return this._supportEmail;
+    }
+
+    get passwordResetSalt() {
+        if (this._passwordResetSalt === undefined) {
+            this._passwordResetSalt = this._getLocaleString(PASSWORD_RESET_GROUP_NAME,
+                                                            PASSWORD_RESET_KEY_SALT);
+        }
+
+        return this._passwordResetSalt;
+    }
+});
diff --git a/js/ui/main.js b/js/ui/main.js
index 6e9ba3d04c..0c9e3a4af0 100644
--- a/js/ui/main.js
+++ b/js/ui/main.js
@@ -30,6 +30,7 @@ const PadOsd = imports.ui.padOsd;
 const Panel = imports.ui.panel;
 const Params = imports.misc.params;
 const ParentalControlsManager = imports.misc.parentalControlsManager;
+const CustomerSupport = imports.misc.customerSupport;
 const PaygManager = imports.misc.paygManager;
 const RunDialog = imports.ui.runDialog;
 const Layout = imports.ui.layout;
@@ -94,6 +95,7 @@ var introspectService = null;
 var locatePointer = null;
 var discoveryFeed = null;
 var paygManager = null;
+var customerSupport = null;
 var workspaceMonitor = null;
 let _startDate;
 let _defaultCssStylesheet = null;
@@ -208,6 +210,9 @@ function _initializeUI() {
     // module needs to be initialized first.
     paygManager = new PaygManager.PaygManager();
 
+    // Centralized handling of things specific to customer support.
+    customerSupport = new CustomerSupport.CustomerSupport();
+
     if (LoginManager.canLock())
         screenShield = new ScreenShield.ScreenShield();
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]