|
|
@ -129,6 +129,9 @@ else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def user_exists(cursor, user):
|
|
|
|
def user_exists(cursor, user):
|
|
|
|
|
|
|
|
# The PUBLIC user is a special case that is always there
|
|
|
|
|
|
|
|
if user == 'PUBLIC':
|
|
|
|
|
|
|
|
return True
|
|
|
|
query = "SELECT rolname FROM pg_roles WHERE rolname=%(user)s"
|
|
|
|
query = "SELECT rolname FROM pg_roles WHERE rolname=%(user)s"
|
|
|
|
cursor.execute(query, {'user': user})
|
|
|
|
cursor.execute(query, {'user': user})
|
|
|
|
return cursor.rowcount > 0
|
|
|
|
return cursor.rowcount > 0
|
|
|
@ -144,6 +147,14 @@ def user_alter(cursor, user, password, role_attr_flags):
|
|
|
|
"""Change user password"""
|
|
|
|
"""Change user password"""
|
|
|
|
changed = False
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if user == 'PUBLIC':
|
|
|
|
|
|
|
|
if password is not None:
|
|
|
|
|
|
|
|
module.fail_json(msg="cannot change the password for PUBLIC user")
|
|
|
|
|
|
|
|
elif role_attr_flags != '':
|
|
|
|
|
|
|
|
module.fail_json(msg="cannot change the role_attr_flags for PUBLIC user")
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
# Handle passwords.
|
|
|
|
# Handle passwords.
|
|
|
|
if password is not None or role_attr_flags is not None:
|
|
|
|
if password is not None or role_attr_flags is not None:
|
|
|
|
# Select password and all flag-like columns in order to verify changes.
|
|
|
|
# Select password and all flag-like columns in order to verify changes.
|
|
|
@ -241,6 +252,9 @@ def has_database_privilege(cursor, user, db, priv):
|
|
|
|
|
|
|
|
|
|
|
|
def grant_database_privilege(cursor, user, db, priv):
|
|
|
|
def grant_database_privilege(cursor, user, db, priv):
|
|
|
|
prev_priv = get_database_privileges(cursor, user, db)
|
|
|
|
prev_priv = get_database_privileges(cursor, user, db)
|
|
|
|
|
|
|
|
if user == "PUBLIC":
|
|
|
|
|
|
|
|
query = 'GRANT %s ON DATABASE \"%s\" TO PUBLIC' % (priv, db)
|
|
|
|
|
|
|
|
else:
|
|
|
|
query = 'GRANT %s ON DATABASE \"%s\" TO \"%s\"' % (priv, db, user)
|
|
|
|
query = 'GRANT %s ON DATABASE \"%s\" TO \"%s\"' % (priv, db, user)
|
|
|
|
cursor.execute(query)
|
|
|
|
cursor.execute(query)
|
|
|
|
curr_priv = get_database_privileges(cursor, user, db)
|
|
|
|
curr_priv = get_database_privileges(cursor, user, db)
|
|
|
@ -248,6 +262,9 @@ def grant_database_privilege(cursor, user, db, priv):
|
|
|
|
|
|
|
|
|
|
|
|
def revoke_database_privilege(cursor, user, db, priv):
|
|
|
|
def revoke_database_privilege(cursor, user, db, priv):
|
|
|
|
prev_priv = get_database_privileges(cursor, user, db)
|
|
|
|
prev_priv = get_database_privileges(cursor, user, db)
|
|
|
|
|
|
|
|
if user == "PUBLIC":
|
|
|
|
|
|
|
|
query = 'REVOKE %s ON DATABASE \"%s\" FROM PUBLIC' % (priv, db)
|
|
|
|
|
|
|
|
else:
|
|
|
|
query = 'REVOKE %s ON DATABASE \"%s\" FROM \"%s\"' % (priv, db, user)
|
|
|
|
query = 'REVOKE %s ON DATABASE \"%s\" FROM \"%s\"' % (priv, db, user)
|
|
|
|
cursor.execute(query)
|
|
|
|
cursor.execute(query)
|
|
|
|
curr_priv = get_database_privileges(cursor, user, db)
|
|
|
|
curr_priv = get_database_privileges(cursor, user, db)
|
|
|
|