-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a41df68
commit cc13039
Showing
1 changed file
with
30 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,35 @@ | ||
{% macro redshift__get_show_grant_sql(relation) %} | ||
|
||
with privileges as ( | ||
{% macro get_users() %} | ||
{% call statement('get_users_list', fetch_result=True) -%} | ||
select | ||
distinct user_name | ||
from svv_user_info | ||
where | ||
user_name != current_user | ||
and superuser = false | ||
{% endcall %} | ||
|
||
-- valid options per https://docs.aws.amazon.com/redshift/latest/dg/r_HAS_TABLE_PRIVILEGE.html | ||
select 'select' as privilege_type | ||
union all | ||
select 'insert' as privilege_type | ||
union all | ||
select 'update' as privilege_type | ||
union all | ||
select 'delete' as privilege_type | ||
union all | ||
select 'references' as privilege_type | ||
{{ return(load_result('get_users_list').table) }} | ||
{% endmacro %} | ||
|
||
) | ||
{% macro redshift__get_show_grant_sql(relation) %} | ||
{% set users_list = get_users() %} | ||
{%- set users_list = users_list.columns[0].values() -%} | ||
{%- set user_privilege_list = [] -%} | ||
{% for username in users_list %} | ||
{{ user_privilege_list.append((username, 'select')) }} | ||
{{ user_privilege_list.append((username, 'insert')) }} | ||
{{ user_privilege_list.append((username, 'update')) }} | ||
{{ user_privilege_list.append((username, 'delete')) }} | ||
{{ user_privilege_list.append((username, 'references')) }} | ||
{% endfor %} | ||
|
||
select | ||
u.usename as grantee, | ||
p.privilege_type | ||
from pg_user u | ||
cross join privileges p | ||
where has_table_privilege(u.usename, '{{ relation }}', privilege_type) | ||
and u.usename != current_user | ||
and not u.usesuper | ||
{% for username, privilege in (user_privilege_list) %} | ||
select '{{ username }}' as grantee, | ||
'{{ privilege }}' as privilege_type | ||
where has_table_privilege('{{ username }}', '{{ relation }}', '{{ privilege }}') | ||
{% if not loop.last %} | ||
union all | ||
{% endif %} | ||
{% endfor %} | ||
|
||
{% endmacro %} |