public inbox for [email protected]  
help / color / mirror / Atom feed
From: Murtuza Zabuawala <[email protected]>
To: Dave Page <[email protected]>
Cc: pgadmin-hackers <[email protected]>
Subject: Re: [pgAdmin4][RM#3037] Allow user to disable Gravatar image
Date: Wed, 7 Mar 2018 20:47:58 +0530
Message-ID: <CAKKotZQDE-HhywR8Ky0v6_nA3TWVhPqB8Fc034+WScckBPs1KA@mail.gmail.com> (raw)
In-Reply-To: <CA+OCxowXxPpwZPvmGW3iDyjXE_6Na6LKk8XbNwVOKWN8iwobkA@mail.gmail.com>
References: <CAKKotZQynh9AcUpyVPj0aVDOBjvUH7mYRJb5Cc1=Spzg_LBD_g@mail.gmail.com>
	<CA+OCxowXxPpwZPvmGW3iDyjXE_6Na6LKk8XbNwVOKWN8iwobkA@mail.gmail.com>

Hi Dave,

PFA updated patch.

--
Regards,
Murtuza Zabuawala
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


On Wed, Mar 7, 2018 at 7:20 PM, Dave Page <[email protected]> wrote:

> Hi
>
> On Mon, Mar 5, 2018 at 8:12 AM, Murtuza Zabuawala <murtuza.zabuawala@
> enterprisedb.com> wrote:
>
>> Hi,
>>
>> PFA patch to disable Gravatar image in Server mode.
>>
>> Requirments & Issues:
>> - For security reasons.
>> - For systems which do not have internet access.
>> - Hangs pgAdmin4 while loading the page if connection has no internet
>> access (as described in the ticket)
>>
>
> If I run with the Gravatar disabled, then I get the following regression
> failure:
>
> ======================================================================
> FAIL: runTest (pgadmin.browser.tests.test_login.LoginTestCase)
> Valid_Credentials
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>   File "/Users/dpage/git/pgadmin4/web/pgadmin/browser/tests/test_login.py",
> line 93, in runTest
>     self.assertTrue(self.respdata in response.data.decode('utf8'))
> AssertionError: False is not true
>
> ----------------------------------------------------------------------
>
> It's fine when Gravatar is enabled (and the rest of the patch does too).
>
> --
> Dave Page
> Blog: http://pgsnake.blogspot.com
> Twitter: @pgsnake
>
> EnterpriseDB UK: http://www.enterprisedb.com
> The Enterprise PostgreSQL Company
>


Attachments:

  [application/octet-stream] RM_3037_v1.diff (8.7K, 3-RM_3037_v1.diff)
  download | inline diff:
diff --git a/web/config.py b/web/config.py
index a728334..d4ddd9d 100644
--- a/web/config.py
+++ b/web/config.py
@@ -357,6 +357,11 @@ SQLALCHEMY_TRACK_MODIFICATIONS = False
 ON_DEMAND_RECORD_COUNT = 1000
 
 ##########################################################################
+# Allow users to display Gravatar image for their username in Server mode
+##########################################################################
+SHOW_GRAVATAR_IMAGE = True
+
+##########################################################################
 # Local config settings
 ##########################################################################
 
diff --git a/web/pgadmin/browser/__init__.py b/web/pgadmin/browser/__init__.py
index 1589677..dd2542f 100644
--- a/web/pgadmin/browser/__init__.py
+++ b/web/pgadmin/browser/__init__.py
@@ -730,18 +730,18 @@ class BrowserPluginModule(PgAdminModule):
 @login_required
 def index():
     """Render and process the main browser window."""
-    # Get the Gravatar
-    Gravatar(
-        current_app,
-        size=100,
-        rating='g',
-        default='retro',
-        force_default=False,
-        use_ssl=True,
-        base_url=None
-    )
+    # Register Gravatar module with the app only if required
+    if config.SHOW_GRAVATAR_IMAGE:
+        Gravatar(
+            current_app,
+            size=100,
+            rating='g',
+            default='retro',
+            force_default=False,
+            use_ssl=True,
+            base_url=None
+        )
 
-    msg = None
     # Get the current version info from the website, and flash a message if
     # the user is out of date, and the check is enabled.
     if config.UPGRADE_CHECK_ENABLED:
@@ -761,7 +761,7 @@ def index():
             if response.getcode() == 200:
                 data = json.loads(response.read().decode('utf-8'))
                 current_app.logger.debug('Response data: %s' % data)
-        except Exception as e:
+        except Exception:
             current_app.logger.exception('Exception when checking for update')
 
         if data is not None:
diff --git a/web/pgadmin/browser/static/css/browser.css b/web/pgadmin/browser/static/css/browser.css
index 2fffd64..3ba330d 100644
--- a/web/pgadmin/browser/static/css/browser.css
+++ b/web/pgadmin/browser/static/css/browser.css
@@ -62,3 +62,7 @@ samp,
 .sql-editor-grid-container {
   font-family: 'Open Sans' !important;
 }
+
+.pg-login-icon {
+  font-size: 16px;
+}
diff --git a/web/pgadmin/browser/templates/browser/index.html b/web/pgadmin/browser/templates/browser/index.html
index 58ff43f..76c3e4c 100644
--- a/web/pgadmin/browser/templates/browser/index.html
+++ b/web/pgadmin/browser/templates/browser/index.html
@@ -1,5 +1,13 @@
 {% extends "base.html" %}
+
+{% if config.SERVER_MODE and config.SHOW_GRAVATAR_IMAGE -%}
+{% import 'browser/macros/gravatar_icon.macro' as IMG with context %}
+{% elif config.SERVER_MODE %}
+{% import 'browser/macros/static_user_icon.macro' as IMG with context %}
+{% endif %}
+
 {% block title %}{{ config.APP_NAME }}{% endblock %}
+
 {% block init_script %}
 try {
 require(
@@ -66,9 +74,11 @@ require.onResourceLoad = function (context, map, depMaps) {
     }, 400)
   }
 };
+
+{% if config.SERVER_MODE %}
 window.onload = function(e){
  setTimeout(function() {
-   var gravatarImg = '<img src="{{ username | gravatar }}" width="18" height="18" alt="Gravatar image for {{ username }}"> {{ username }} <span class="caret"></span>';
+   var gravatarImg = {{ IMG.PREPARE_HTML()|safe }}
    //$('#navbar-menu .navbar-right > li > a').html(gravatarImg);
    var navbarRight = document.getElementById("navbar-menu").getElementsByClassName("navbar-right")[0];
    if (navbarRight) {
@@ -77,8 +87,9 @@ window.onload = function(e){
    }
  }, 1000);
 };
-
+{% endif %}
 {% endblock %}
+
 {% block body %}
 <style>
 #pg-spinner {
diff --git a/web/pgadmin/browser/templates/browser/macros/gravatar_icon.macro b/web/pgadmin/browser/templates/browser/macros/gravatar_icon.macro
new file mode 100644
index 0000000..72ec97e
--- /dev/null
+++ b/web/pgadmin/browser/templates/browser/macros/gravatar_icon.macro
@@ -0,0 +1,8 @@
+{##########################################################################
+We wrote separate macro because if user choose to disable Gravatar then
+we will not associate our application with Gravatar module which will make
+'gravatar' filter unavailable in Jinja templates
+###########################################################################}
+{% macro PREPARE_HTML() -%}
+'<img src = "{{ username | gravatar }}" width = "18" height = "18" alt = "Gravatar image for {{ username }}" > {{ username }} <span class="caret"></span>';
+{%- endmacro %}
diff --git a/web/pgadmin/browser/templates/browser/macros/static_user_icon.macro b/web/pgadmin/browser/templates/browser/macros/static_user_icon.macro
new file mode 100644
index 0000000..6b72844
--- /dev/null
+++ b/web/pgadmin/browser/templates/browser/macros/static_user_icon.macro
@@ -0,0 +1,3 @@
+{% macro PREPARE_HTML() -%}
+'<i class="fa fa-user-circle pg-login-icon" aria-hidden="true"></i> {{ username }} <span class="caret"></span>';
+{%- endmacro %}
diff --git a/web/pgadmin/browser/tests/test_gravatar_image_display.py b/web/pgadmin/browser/tests/test_gravatar_image_display.py
new file mode 100644
index 0000000..a4526c9
--- /dev/null
+++ b/web/pgadmin/browser/tests/test_gravatar_image_display.py
@@ -0,0 +1,68 @@
+##########################################################################
+#
+# pgAdmin 4 - PostgreSQL Tools
+#
+# Copyright (C) 2013 - 2018, The pgAdmin Development Team
+# This software is released under the PostgreSQL Licence
+#
+##########################################################################
+
+import config
+from pgadmin.utils.route import BaseTestGenerator
+from regression.python_test_utils import test_utils as utils
+from regression.test_setup import config_data as tconfig
+
+
+class TestLoginUserImage(BaseTestGenerator):
+    """
+    This class checks for user image after successful login.
+    - If SHOW_GRAVATAR_IMAGE config option is set to True then we will show
+    Gravatar on the Page.
+    - If SHOW_GRAVATAR_IMAGE config option is set to False then we will show
+    Static image on the Page.
+    """
+
+    scenarios = [
+        (
+            'Verify gravatar image on the page', dict(
+                email=(
+                    tconfig['pgAdmin4_login_credentials']['login_username']
+                ),
+                password=(
+                    tconfig['pgAdmin4_login_credentials']['login_password']
+                ),
+                respdata='Gravatar image for %s' %
+                         tconfig['pgAdmin4_login_credentials']
+                         ['login_username'],
+            )
+        )
+    ]
+
+    @classmethod
+    def setUpClass(cls):
+        "Logout first if already logged in"
+        utils.logout_tester_account(cls.tester)
+
+    def runTest(self):
+        # Login and check type of image in response
+        response = self.tester.post(
+            '/login', data=dict(
+                email=self.email,
+                password=self.password
+            ),
+            follow_redirects=True
+        )
+        # Should have gravatar image
+        if config.SHOW_GRAVATAR_IMAGE:
+            self.assertIn(self.respdata, response.data.decode('utf8'))
+        # Should not have gravatar image
+        else:
+            self.assertNotIn(self.respdata, response.data.decode('utf8'))
+
+    @classmethod
+    def tearDownClass(cls):
+        """
+        We need to again login the test client as soon as test scenarios
+        finishes.
+        """
+        utils.login_tester_account(cls.tester)
diff --git a/web/pgadmin/browser/tests/test_login.py b/web/pgadmin/browser/tests/test_login.py
index 3331b68..0f0a30e 100644
--- a/web/pgadmin/browser/tests/test_login.py
+++ b/web/pgadmin/browser/tests/test_login.py
@@ -62,13 +62,15 @@ class LoginTestCase(BaseTestGenerator):
         # This test case validates the valid/correct credentials and allow user
         # to login pgAdmin 4
         ('Valid_Credentials', dict(
-            email=(config_data['pgAdmin4_login_credentials']
-                   ['login_username']),
-            password=(config_data['pgAdmin4_login_credentials']
-                      ['login_password']),
-            respdata='Gravatar image for %s' %
-                     config_data['pgAdmin4_login_credentials']
-                     ['login_username']))
+            email=(config_data[
+                'pgAdmin4_login_credentials'
+            ]['login_username']),
+            password=(config_data[
+                'pgAdmin4_login_credentials'
+            ]['login_password']),
+            respdata='%s' % config_data['pgAdmin4_login_credentials']
+            ['login_username'])
+         )
     ]
 
     @classmethod


view thread (11+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: [pgAdmin4][RM#3037] Allow user to disable Gravatar image
  In-Reply-To: <CAKKotZQDE-HhywR8Ky0v6_nA3TWVhPqB8Fc034+WScckBPs1KA@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox