-
Notifications
You must be signed in to change notification settings - Fork 176
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
4be8158
commit 45dc4e7
Showing
2 changed files
with
47 additions
and
23 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
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,22 +1,37 @@ | ||
exports.up = async (pgm) => { | ||
// test table privileges | ||
const { schema, table, role1, role2, tablePrivileges, schemaPrivilege } = require('./085_grant_tables_schemas_roles') | ||
|
||
const hasTablePrivileges = async (pgm, role, tableName, privileges) => { | ||
const rows = await pgm.db.select(` | ||
SELECT grantee, privilege_type | ||
FROM information_schema.role_table_grants | ||
WHERE table_name='table_for_bob' | ||
AND grantee = 'bob1' | ||
WHERE table_name='${tableName}' | ||
AND grantee = '${role}' | ||
`) | ||
const rows1 = await pgm.db.select(` | ||
SELECT has_schema_privilege('bob1', 'test_schema', 'USAGE'); | ||
const foundPrivileges = rows.map(({ privilegeType }) => privilegeType) | ||
return privileges.reduce((acc, privilege) => acc && foundPrivileges.includes(privilege), true) | ||
} | ||
|
||
const hasSchemaPrivilege = async (pgm, role, schemaName, privilege) => { | ||
const rows = await pgm.db.select(` | ||
SELECT has_schema_privilege('${role}', '${schemaName}', '${privilege}'); | ||
`) | ||
console.log({ rows, rows1 }) | ||
if (rows.length !== 7) { | ||
throw new Error('Incorrect number of priveleges') | ||
return rows.length && rows[0].has_schema_privilege | ||
} | ||
|
||
const checkGrantedPrivileges = async (pgm, role) => { | ||
const hasGrantedTablePrivileges = await hasTablePrivileges(pgm, role, table, tablePrivileges) | ||
if (!hasGrantedTablePrivileges) { | ||
throw new Error(`${role} misses granted table privileges`) | ||
} | ||
const hasSchemaPrivilege = rows1.length && rows1[0].has_schema_privilege | ||
if (!hasSchemaPrivilege) { | ||
throw new Error('Bob should have a USAGE schema privelege') | ||
const hasGrantedSchemaPrivilege = await hasSchemaPrivilege(pgm, role, schema, schemaPrivilege) | ||
if (!hasGrantedSchemaPrivilege) { | ||
throw new Error(`${role} misses USAGE schema privilege`) | ||
} | ||
} | ||
|
||
exports.up = async (pgm) => { | ||
await checkGrantedPrivileges(pgm, role1) | ||
await checkGrantedPrivileges(pgm, role2) | ||
} | ||
|
||
exports.down = () => null |