From ed01274f7b95caeba4943d550792abf265fea7f8 Mon Sep 17 00:00:00 2001
From: Akshay Joshi <akshay.joshi@enterprisedb.com>
Date: Fri, 12 Jul 2019 10:16:18 +0100
Subject: [PATCH] Fix an error that could be seen when editing column
 privileges. Fixes #4389

---
 docs/en_US/release_notes_4_11.rst             |  1 +
 .../databases/schemas/tables/__init__.py      | 16 +++-
 .../templates/columns/sql/10_plus/update.sql  | 16 ++--
 .../templates/columns/sql/9.2_plus/update.sql | 16 ++--
 .../templates/tables/sql/10_plus/create.sql   |  2 +-
 .../templates/tables/sql/12_plus/create.sql   |  2 +-
 .../tests/test_column_privileges_put.py       | 96 +++++++++++++++++++
 .../servers/databases/schemas/tables/utils.py |  7 --
 8 files changed, 127 insertions(+), 29 deletions(-)
 create mode 100644 web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_column_privileges_put.py

#diff --git a/docs/en_US/release_notes_4_11.rst b/docs/en_US/release_notes_4_11.rst
#index 30746e937..bac325440 100644
#--- a/docs/en_US/release_notes_4_11.rst
#+++ b/docs/en_US/release_notes_4_11.rst
#@@ -29,6 +29,7 @@ Bug fixes
# 
# | `Issue #3919 <https://redmine.postgresql.org/issues/3919>`_ -  Allow keyboard navigation of all controls on subnode grids.
# | `Issue #4224 <https://redmine.postgresql.org/issues/4224>`_ -  Prevent flickering of large tooltips on the Graphical EXPLAIN canvas.
#+| `Issue #4389 <https://redmine.postgresql.org/issues/4389>`_ -  Fix an error that could be seen when editing column privileges.
# | `Issue #4393 <https://redmine.postgresql.org/issues/4393>`_ -  Ensure parameter values are quoted when needed when editing roles.
# | `Issue #4395 <https://redmine.postgresql.org/issues/4395>`_ -  EXPLAIN options should be Query Tool instance-specific.
# | `Issue #4429 <https://redmine.postgresql.org/issues/4429>`_ -  Ensure drag/drop from the treeview works as expected on Firefox.
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/__init__.py
index d2354f375..f694396be 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/__init__.py
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/__init__.py
@@ -905,10 +905,18 @@ def _parse_format_columns(self, data, mode=None):
 
                     for c in final_columns:
                         if 'attacl' in c:
-                            c['attacl'] = parse_priv_to_db(
-                                c['attacl'], self.column_acl
-                            )
-
+                            if 'added' in c['attacl']:
+                                c['attacl']['added'] = parse_priv_to_db(
+                                    c['attacl']['added'], self.column_acl
+                                )
+                            elif 'changed' in c['attacl']:
+                                c['attacl']['changed'] = parse_priv_to_db(
+                                    c['attacl']['changed'], self.column_acl
+                                )
+                            elif 'deleted' in c['attacl']:
+                                c['attacl']['deleted'] = parse_priv_to_db(
+                                    c['attacl']['deleted'], self.column_acl
+                                )
                         if 'cltype' in c:
                             # check type for '[]' in it
                             c['cltype'], c['hasSqrBracket'] = \
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/columns/sql/10_plus/update.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/columns/sql/10_plus/update.sql
index eea328db7..7cd40fc96 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/columns/sql/10_plus/update.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/columns/sql/10_plus/update.sql
@@ -131,29 +131,29 @@ ALTER TABLE {{conn|qtIdent(data.schema, data.table)}}
 {% if 'deleted' in data.attacl %}
 {% for priv in data.attacl.deleted %}
 {% if data.name %}
-    {{ PRIVILEGE.RESETALL(conn, data.schema, data.table, data.name, priv.grantee) }}
+{{ PRIVILEGE.RESETALL(conn, data.schema, data.table, data.name, priv.grantee) }}
 {% else %}
-    {{ PRIVILEGE.RESETALL(conn, data.schema, data.table, o_data.name, priv.grantee) }}
+{{ PRIVILEGE.RESETALL(conn, data.schema, data.table, o_data.name, priv.grantee) }}
 {% endif %}
 {% endfor %}
 {% endif %}
 {% if 'changed' in data.attacl %}
 {% for priv in data.attacl.changed %}
 {% if data.name %}
-    {{ PRIVILEGE.RESETALL(conn, data.schema, data.table, data.name, priv.grantee) }}
-    {{ PRIVILEGE.APPLY(conn, data.schema, data.table, data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
+{{ PRIVILEGE.RESETALL(conn, data.schema, data.table, data.name, priv.grantee) }}
+{{ PRIVILEGE.APPLY(conn, data.schema, data.table, data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
 {% else %}
-    {{ PRIVILEGE.RESETALL(conn, data.schema, data.table, o_data.name, priv.grantee) }}
-    {{ PRIVILEGE.APPLY(conn, data.schema, data.table, o_data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
+{{ PRIVILEGE.RESETALL(conn, data.schema, data.table, o_data.name, priv.grantee) }}
+{{ PRIVILEGE.APPLY(conn, data.schema, data.table, o_data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
 {% endif %}
 {% endfor %}
 {% endif %}
 {% if 'added' in data.attacl %}
 {% for priv in data.attacl.added %}
 {% if data.name %}
-    {{ PRIVILEGE.APPLY(conn, data.schema, data.table, data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
+{{ PRIVILEGE.APPLY(conn, data.schema, data.table, data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
 {% else %}
-    {{ PRIVILEGE.APPLY(conn, data.schema, data.table, o_data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
+{{ PRIVILEGE.APPLY(conn, data.schema, data.table, o_data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
 {% endif %}
 {% endfor %}
 {% endif %}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/columns/sql/9.2_plus/update.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/columns/sql/9.2_plus/update.sql
index 806fb37ed..d92d01121 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/columns/sql/9.2_plus/update.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/columns/sql/9.2_plus/update.sql
@@ -89,29 +89,29 @@ ALTER TABLE {{conn|qtIdent(data.schema, data.table)}}
 {% if 'deleted' in data.attacl %}
 {% for priv in data.attacl.deleted %}
 {% if data.name %}
-    {{ PRIVILEGE.RESETALL(conn, data.schema, data.table, data.name, priv.grantee) }}
+{{ PRIVILEGE.RESETALL(conn, data.schema, data.table, data.name, priv.grantee) }}
 {% else %}
-    {{ PRIVILEGE.RESETALL(conn, data.schema, data.table, o_data.name, priv.grantee) }}
+{{ PRIVILEGE.RESETALL(conn, data.schema, data.table, o_data.name, priv.grantee) }}
 {% endif %}
 {% endfor %}
 {% endif %}
 {% if 'changed' in data.attacl %}
 {% for priv in data.attacl.changed %}
 {% if data.name %}
-    {{ PRIVILEGE.RESETALL(conn, data.schema, data.table, data.name, priv.grantee) }}
-    {{ PRIVILEGE.APPLY(conn, data.schema, data.table, data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
+{{ PRIVILEGE.RESETALL(conn, data.schema, data.table, data.name, priv.grantee) }}
+{{ PRIVILEGE.APPLY(conn, data.schema, data.table, data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
 {% else %}
-    {{ PRIVILEGE.RESETALL(conn, data.schema, data.table, o_data.name, priv.grantee) }}
-    {{ PRIVILEGE.APPLY(conn, data.schema, data.table, o_data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
+{{ PRIVILEGE.RESETALL(conn, data.schema, data.table, o_data.name, priv.grantee) }}
+{{ PRIVILEGE.APPLY(conn, data.schema, data.table, o_data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
 {% endif %}
 {% endfor %}
 {% endif %}
 {% if 'added' in data.attacl %}
 {% for priv in data.attacl.added %}
 {% if data.name %}
-    {{ PRIVILEGE.APPLY(conn, data.schema, data.table, data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
+{{ PRIVILEGE.APPLY(conn, data.schema, data.table, data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
 {% else %}
-    {{ PRIVILEGE.APPLY(conn, data.schema, data.table, o_data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
+{{ PRIVILEGE.APPLY(conn, data.schema, data.table, o_data.name, priv.grantee, priv.without_grant, priv.with_grant) }}
 {% endif %}
 {% endfor %}
 {% endif %}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/tables/sql/10_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/tables/sql/10_plus/create.sql
index ab79d2e6c..1f4c83877 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/tables/sql/10_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/tables/sql/10_plus/create.sql
@@ -145,7 +145,7 @@ ALTER TABLE {{conn|qtIdent(data.schema, data.name)}}
 {% if c.attacl and c.attacl|length > 0 %}
 
 {% for priv in c.attacl %}
-    {{ COLUMN_PRIVILEGE.APPLY(conn, data.schema, data.name, c.name, priv.grantee, priv.without_grant, priv.with_grant) }}
+{{ COLUMN_PRIVILEGE.APPLY(conn, data.schema, data.name, c.name, priv.grantee, priv.without_grant, priv.with_grant) }}
 {% endfor %}
 {% endif %}
 {###  Security Lables ###}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/tables/sql/12_plus/create.sql b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/tables/sql/12_plus/create.sql
index f4daa2ccd..7acbd0083 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/tables/sql/12_plus/create.sql
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/tables/sql/12_plus/create.sql
@@ -157,7 +157,7 @@ ALTER TABLE {{conn|qtIdent(data.schema, data.name)}}
 {% if c.attacl and c.attacl|length > 0 %}
 
 {% for priv in c.attacl %}
-    {{ COLUMN_PRIVILEGE.APPLY(conn, data.schema, data.name, c.name, priv.grantee, priv.without_grant, priv.with_grant) }}
+{{ COLUMN_PRIVILEGE.APPLY(conn, data.schema, data.name, c.name, priv.grantee, priv.without_grant, priv.with_grant) }}
 {% endfor %}
 {% endif %}
 {###  Security Lables ###}
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_column_privileges_put.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_column_privileges_put.py
new file mode 100644
index 000000000..1c5877861
--- /dev/null
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_column_privileges_put.py
@@ -0,0 +1,96 @@
+##########################################################################
+#
+# pgAdmin 4 - PostgreSQL Tools
+#
+# Copyright (C) 2013 - 2019, The pgAdmin Development Team
+# This software is released under the PostgreSQL Licence
+#
+##########################################################################
+
+import json
+import uuid
+
+from pgadmin.browser.server_groups.servers.databases.schemas.tests import \
+    utils as schema_utils
+from pgadmin.browser.server_groups.servers.databases.tests import utils as \
+    database_utils
+from pgadmin.utils.route import BaseTestGenerator
+from regression import parent_node_dict
+from regression.python_test_utils import test_utils as utils
+from . import utils as tables_utils
+
+
+class TableUpdateColumnTestCase(BaseTestGenerator):
+    """This class will update the column node from table"""
+    scenarios = [
+        # Fetching default URL for table node.
+        ('Add privileges for existing column',
+         dict(url='/browser/table/obj/')
+         )
+    ]
+
+    def setUp(self):
+        self.db_name = parent_node_dict["database"][-1]["db_name"]
+        schema_info = parent_node_dict["schema"][-1]
+        self.server_id = schema_info["server_id"]
+        self.db_id = schema_info["db_id"]
+        db_con = database_utils.connect_database(self, utils.SERVER_GROUP,
+                                                 self.server_id, self.db_id)
+        if not db_con['data']["connected"]:
+            raise Exception("Could not connect to database to add a table.")
+        self.schema_id = schema_info["schema_id"]
+        self.schema_name = schema_info["schema_name"]
+        schema_response = schema_utils.verify_schemas(self.server,
+                                                      self.db_name,
+                                                      self.schema_name)
+        if not schema_response:
+            raise Exception("Could not find the schema to add a table.")
+        self.table_name = "test_table_column_put_%s" % (str(uuid.uuid4())[1:8])
+
+        self.table_id = tables_utils.create_table(
+            self.server, self.db_name,
+            self.schema_name,
+            self.table_name)
+
+    def runTest(self):
+        """This function will fetch added table under schema node."""
+        table_response = tables_utils.verify_table(self.server, self.db_name,
+                                                   self.table_id)
+        if not table_response:
+            raise Exception("Could not find the table to update.")
+
+        data = {
+            "columns": {
+                "changed": [{
+                    "attnum": 1,
+                    "attacl": {
+                        "added": [{
+                            "grantee": self.server["username"],
+                            "grantor": self.server["username"],
+                            "privileges": [
+                                {"privilege_type": "a", "privilege": True,
+                                 "with_grant": True},
+                                {"privilege_type": "r", "privilege": True,
+                                 "with_grant": True},
+                                {"privilege_type": "w", "privilege": True,
+                                 "with_grant": True},
+                                {"privilege_type": "x", "privilege": True,
+                                 "with_grant": True
+                                 }
+                            ]
+                        }]
+                    }
+                }]
+            }
+        }
+
+        response = self.tester.put(
+            self.url + str(utils.SERVER_GROUP) + '/' +
+            str(self.server_id) + '/' + str(self.db_id) + '/' +
+            str(self.schema_id) + '/' + str(self.table_id),
+            data=json.dumps(data), follow_redirects=True)
+        self.assertEquals(response.status_code, 200)
+
+    def tearDown(self):
+        # Disconnect the database
+        database_utils.disconnect_database(self, self.server_id, self.db_id)
diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py
index bd3ba21e8..078ba281e 100644
--- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py
+++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/utils.py
@@ -1750,9 +1750,6 @@ def get_sql(self, did, scid, tid, data, res):
                     for c in columns['changed']:
                         c['schema'] = data['schema']
                         c['table'] = data['name']
-                        if 'attacl' in c:
-                            c['attacl'] = parse_priv_to_db(c['attacl'],
-                                                           self.column_acl)
 
                         properties_sql = render_template(
                             "/".join([self.column_template_path,
@@ -1856,10 +1853,6 @@ def get_type_attr(key, data):
                     for c in columns['added']:
                         c['schema'] = data['schema']
                         c['table'] = data['name']
-                        # Sql for create column
-                        if 'attacl' in c:
-                            c['attacl'] = parse_priv_to_db(c['attacl'],
-                                                           self.column_acl)
 
                         c = BaseTableView.convert_length_precision_to_string(c)
 
