|
| 1 | +import { ident, literal } from 'pg-format' |
| 2 | +import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js' |
| 3 | +import { filterByList } from './helpers.js' |
| 4 | +import { tablePrivilegesSql } from './sql/index.js' |
| 5 | +import { |
| 6 | + PostgresMetaResult, |
| 7 | + PostgresTablePrivileges, |
| 8 | + PostgresTablePrivilegesGrant, |
| 9 | + PostgresTablePrivilegesRevoke, |
| 10 | +} from './types.js' |
| 11 | + |
| 12 | +export default class PostgresMetaTablePrivileges { |
| 13 | + query: (sql: string) => Promise<PostgresMetaResult<any>> |
| 14 | + |
| 15 | + constructor(query: (sql: string) => Promise<PostgresMetaResult<any>>) { |
| 16 | + this.query = query |
| 17 | + } |
| 18 | + |
| 19 | + async list({ |
| 20 | + includeSystemSchemas = false, |
| 21 | + includedSchemas, |
| 22 | + excludedSchemas, |
| 23 | + limit, |
| 24 | + offset, |
| 25 | + }: { |
| 26 | + includeSystemSchemas?: boolean |
| 27 | + includedSchemas?: string[] |
| 28 | + excludedSchemas?: string[] |
| 29 | + limit?: number |
| 30 | + offset?: number |
| 31 | + } = {}): Promise<PostgresMetaResult<PostgresTablePrivileges[]>> { |
| 32 | + let sql = ` |
| 33 | +with table_privileges as (${tablePrivilegesSql}) |
| 34 | +select * |
| 35 | +from table_privileges |
| 36 | +` |
| 37 | + const filter = filterByList( |
| 38 | + includedSchemas, |
| 39 | + excludedSchemas, |
| 40 | + !includeSystemSchemas ? DEFAULT_SYSTEM_SCHEMAS : undefined |
| 41 | + ) |
| 42 | + if (filter) { |
| 43 | + sql += ` where schema ${filter}` |
| 44 | + } |
| 45 | + if (limit) { |
| 46 | + sql += ` limit ${limit}` |
| 47 | + } |
| 48 | + if (offset) { |
| 49 | + sql += ` offset ${offset}` |
| 50 | + } |
| 51 | + return await this.query(sql) |
| 52 | + } |
| 53 | + |
| 54 | + async retrieve({ id }: { id: number }): Promise<PostgresMetaResult<PostgresTablePrivileges>> |
| 55 | + async retrieve({ |
| 56 | + name, |
| 57 | + schema, |
| 58 | + }: { |
| 59 | + name: string |
| 60 | + schema: string |
| 61 | + }): Promise<PostgresMetaResult<PostgresTablePrivileges>> |
| 62 | + async retrieve({ |
| 63 | + id, |
| 64 | + name, |
| 65 | + schema = 'public', |
| 66 | + }: { |
| 67 | + id?: number |
| 68 | + name?: string |
| 69 | + schema?: string |
| 70 | + }): Promise<PostgresMetaResult<PostgresTablePrivileges>> { |
| 71 | + if (id) { |
| 72 | + const sql = ` |
| 73 | +with table_privileges as (${tablePrivilegesSql}) |
| 74 | +select * |
| 75 | +from table_privileges |
| 76 | +where table_privileges.relation_id = ${literal(id)};` |
| 77 | + const { data, error } = await this.query(sql) |
| 78 | + if (error) { |
| 79 | + return { data, error } |
| 80 | + } else if (data.length === 0) { |
| 81 | + return { data: null, error: { message: `Cannot find a relation with ID ${id}` } } |
| 82 | + } else { |
| 83 | + return { data: data[0], error } |
| 84 | + } |
| 85 | + } else if (name) { |
| 86 | + const sql = ` |
| 87 | +with table_privileges as (${tablePrivilegesSql}) |
| 88 | +select * |
| 89 | +from table_privileges |
| 90 | +where table_privileges.schema = ${literal(schema)} |
| 91 | + and table_privileges.name = ${literal(name)} |
| 92 | +` |
| 93 | + const { data, error } = await this.query(sql) |
| 94 | + if (error) { |
| 95 | + return { data, error } |
| 96 | + } else if (data.length === 0) { |
| 97 | + return { |
| 98 | + data: null, |
| 99 | + error: { message: `Cannot find a relation named ${name} in schema ${schema}` }, |
| 100 | + } |
| 101 | + } else { |
| 102 | + return { data: data[0], error } |
| 103 | + } |
| 104 | + } else { |
| 105 | + return { data: null, error: { message: 'Invalid parameters on retrieving table privileges' } } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + async grant( |
| 110 | + grants: PostgresTablePrivilegesGrant[] |
| 111 | + ): Promise<PostgresMetaResult<PostgresTablePrivileges[]>> { |
| 112 | + let sql = ` |
| 113 | +do $$ |
| 114 | +begin |
| 115 | +${grants |
| 116 | + .map( |
| 117 | + ({ privilege_type, relation_id, grantee, is_grantable }) => |
| 118 | + `execute format('grant ${privilege_type} on table %I to ${ |
| 119 | + grantee.toLowerCase() === 'public' ? 'public' : ident(grantee) |
| 120 | + } ${is_grantable ? 'with grant option' : ''}', ${relation_id}::regclass);` |
| 121 | + ) |
| 122 | + .join('\n')} |
| 123 | +end $$; |
| 124 | +` |
| 125 | + const { data, error } = await this.query(sql) |
| 126 | + if (error) { |
| 127 | + return { data, error } |
| 128 | + } |
| 129 | + |
| 130 | + // Return the updated table privileges for modified relations. |
| 131 | + const relationIds = [...new Set(grants.map(({ relation_id }) => relation_id))] |
| 132 | + sql = ` |
| 133 | +with table_privileges as (${tablePrivilegesSql}) |
| 134 | +select * |
| 135 | +from table_privileges |
| 136 | +where relation_id in (${relationIds.map(literal).join(',')}) |
| 137 | +` |
| 138 | + return await this.query(sql) |
| 139 | + } |
| 140 | + |
| 141 | + async revoke( |
| 142 | + revokes: PostgresTablePrivilegesRevoke[] |
| 143 | + ): Promise<PostgresMetaResult<PostgresTablePrivileges[]>> { |
| 144 | + let sql = ` |
| 145 | +do $$ |
| 146 | +begin |
| 147 | +${revokes |
| 148 | + .map( |
| 149 | + (revoke) => |
| 150 | + `execute format('revoke ${revoke.privilege_type} on table %I from ${revoke.grantee}', ${revoke.relation_id}::regclass);` |
| 151 | + ) |
| 152 | + .join('\n')} |
| 153 | +end $$; |
| 154 | +` |
| 155 | + const { data, error } = await this.query(sql) |
| 156 | + if (error) { |
| 157 | + return { data, error } |
| 158 | + } |
| 159 | + |
| 160 | + // Return the updated table privileges for modified relations. |
| 161 | + const relationIds = [...new Set(revokes.map(({ relation_id }) => relation_id))] |
| 162 | + sql = ` |
| 163 | +with table_privileges as (${tablePrivilegesSql}) |
| 164 | +select * |
| 165 | +from table_privileges |
| 166 | +where relation_id in (${relationIds.map(literal).join(',')}) |
| 167 | +` |
| 168 | + return await this.query(sql) |
| 169 | + } |
| 170 | +} |
0 commit comments