1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge branch 'fix-postgres_user_to_understand_pg_namespaces' of https://github.com/alanfairless/ansible into alanfairless-fix-postgres_user_to_understand_pg_namespaces

This commit is contained in:
James Cammarata 2014-02-17 10:04:12 -06:00
commit 1544c93bf2

View file

@ -265,16 +265,35 @@ def get_table_privileges(cursor, user, table):
return set([x[0] for x in cursor.fetchall()]) return set([x[0] for x in cursor.fetchall()])
def quote_pg_identifier(identifier):
"""
quote postgresql identifiers involving zero or more namespaces
"""
if '"' in identifier:
# the user has supplied their own quoting. we have to hope they're
# doing it right. Maybe they have an unfortunately named table
# containing a period in the name, such as: "public"."users.2013"
return identifier
tokens = identifier.strip().split(".")
quoted_tokens = []
for token in tokens:
quoted_tokens.append('"%s"' % (token, ))
return ".".join(quoted_tokens)
def grant_table_privilege(cursor, user, table, priv): def grant_table_privilege(cursor, user, table, priv):
prev_priv = get_table_privileges(cursor, user, table) prev_priv = get_table_privileges(cursor, user, table)
query = 'GRANT %s ON TABLE \"%s\" TO \"%s\"' % (priv, table, user) query = 'GRANT %s ON TABLE %s TO %s' % (
priv, quote_pg_identifier(table), quote_pg_identifier(user), )
cursor.execute(query) cursor.execute(query)
curr_priv = get_table_privileges(cursor, user, table) curr_priv = get_table_privileges(cursor, user, table)
return len(curr_priv) > len(prev_priv) return len(curr_priv) > len(prev_priv)
def revoke_table_privilege(cursor, user, table, priv): def revoke_table_privilege(cursor, user, table, priv):
prev_priv = get_table_privileges(cursor, user, table) prev_priv = get_table_privileges(cursor, user, table)
query = 'REVOKE %s ON TABLE \"%s\" FROM \"%s\"' % (priv, table, user) query = 'REVOKE %s ON TABLE %s FROM %s' % (
priv, quote_pg_identifier(table), quote_pg_identifier(user), )
cursor.execute(query) cursor.execute(query)
curr_priv = get_table_privileges(cursor, user, table) curr_priv = get_table_privileges(cursor, user, table)
return len(curr_priv) < len(prev_priv) return len(curr_priv) < len(prev_priv)