@smplcty/auth 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +352 -0
- package/dist/create-session.d.ts +29 -0
- package/dist/create-session.d.ts.map +1 -0
- package/dist/create-session.js +108 -0
- package/dist/create-session.js.map +1 -0
- package/dist/errors.d.ts +44 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +54 -0
- package/dist/errors.js.map +1 -0
- package/dist/find-user-by-communication-method.d.ts +27 -0
- package/dist/find-user-by-communication-method.d.ts.map +1 -0
- package/dist/find-user-by-communication-method.js +47 -0
- package/dist/find-user-by-communication-method.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/hash-id.d.ts +8 -0
- package/dist/internal/hash-id.d.ts.map +1 -0
- package/dist/internal/hash-id.js +11 -0
- package/dist/internal/hash-id.js.map +1 -0
- package/dist/internal/noop-logger.d.ts +7 -0
- package/dist/internal/noop-logger.d.ts.map +1 -0
- package/dist/internal/noop-logger.js +11 -0
- package/dist/internal/noop-logger.js.map +1 -0
- package/dist/revoke-session.d.ts +16 -0
- package/dist/revoke-session.d.ts.map +1 -0
- package/dist/revoke-session.js +22 -0
- package/dist/revoke-session.js.map +1 -0
- package/dist/set-helpers.d.ts +78 -0
- package/dist/set-helpers.d.ts.map +1 -0
- package/dist/set-helpers.js +110 -0
- package/dist/set-helpers.js.map +1 -0
- package/dist/types.d.ts +118 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/validate-session.d.ts +20 -0
- package/dist/validate-session.d.ts.map +1 -0
- package/dist/validate-session.js +49 -0
- package/dist/validate-session.js.map +1 -0
- package/dist/with-session.d.ts +33 -0
- package/dist/with-session.d.ts.map +1 -0
- package/dist/with-session.js +134 -0
- package/dist/with-session.js.map +1 -0
- package/dist/with-transaction.d.ts +31 -0
- package/dist/with-transaction.d.ts.map +1 -0
- package/dist/with-transaction.js +49 -0
- package/dist/with-transaction.js.map +1 -0
- package/package.json +68 -0
- package/schema/tables/communication_channels.yaml +11 -0
- package/schema/tables/roles.yaml +29 -0
- package/schema/tables/sessions.yaml +30 -0
- package/schema/tables/tenants.yaml +11 -0
- package/schema/tables/user_communication_methods.yaml +23 -0
- package/schema/tables/user_roles.yaml +25 -0
- package/schema/tables/users.yaml +7 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { RoleNotAssignedError, SessionExpiredError, SessionNotFoundError, } from './errors.js';
|
|
2
|
+
import { hashId } from './internal/hash-id.js';
|
|
3
|
+
import { noopLogger } from './internal/noop-logger.js';
|
|
4
|
+
import { setSessionContext } from './set-helpers.js';
|
|
5
|
+
import { withTransaction } from './with-transaction.js';
|
|
6
|
+
/**
|
|
7
|
+
* Resolves the session and the user's roles in one query. Returns null
|
|
8
|
+
* if the session row doesn't exist or doesn't belong to a user with the
|
|
9
|
+
* requested role.
|
|
10
|
+
*
|
|
11
|
+
* The query joins through `sessions → user_communication_methods → users
|
|
12
|
+
* → user_roles → roles`, filtering by the requested role name. It also
|
|
13
|
+
* computes:
|
|
14
|
+
*
|
|
15
|
+
* - `tenantIds`: distinct non-null tenant_ids on user_roles rows
|
|
16
|
+
* - `allTenants`: true if any user_roles row has tenant_id IS NULL
|
|
17
|
+
* - `roles`: array of all distinct role names assigned to the user
|
|
18
|
+
*
|
|
19
|
+
* The roles array is sourced from a separate inner join so we get every
|
|
20
|
+
* role the user has, not just the one being requested. The `_hasRole`
|
|
21
|
+
* column tells us whether the requested role specifically was found.
|
|
22
|
+
*/
|
|
23
|
+
const RESOLVE_SESSION = `
|
|
24
|
+
WITH found_session AS (
|
|
25
|
+
SELECT
|
|
26
|
+
s.session_id,
|
|
27
|
+
s.expires_at,
|
|
28
|
+
ucm.user_id
|
|
29
|
+
FROM sessions s
|
|
30
|
+
JOIN user_communication_methods ucm
|
|
31
|
+
ON ucm.user_communication_method_id = s.user_communication_method_id
|
|
32
|
+
WHERE s.session_id = $1
|
|
33
|
+
),
|
|
34
|
+
user_role_data AS (
|
|
35
|
+
SELECT
|
|
36
|
+
ur.user_id,
|
|
37
|
+
ur.tenant_id,
|
|
38
|
+
r.name AS role_name
|
|
39
|
+
FROM found_session fs
|
|
40
|
+
JOIN user_roles ur ON ur.user_id = fs.user_id
|
|
41
|
+
JOIN roles r ON r.role_id = ur.role_id
|
|
42
|
+
)
|
|
43
|
+
SELECT
|
|
44
|
+
fs.user_id AS "userId",
|
|
45
|
+
fs.expires_at AS "expiresAt",
|
|
46
|
+
COALESCE(
|
|
47
|
+
array_agg(DISTINCT urd.tenant_id)
|
|
48
|
+
FILTER (WHERE urd.tenant_id IS NOT NULL),
|
|
49
|
+
'{}'
|
|
50
|
+
) AS "tenantIds",
|
|
51
|
+
COALESCE(bool_or(urd.tenant_id IS NULL), false) AS "allTenants",
|
|
52
|
+
COALESCE(
|
|
53
|
+
array_agg(DISTINCT urd.role_name)
|
|
54
|
+
FILTER (WHERE urd.role_name IS NOT NULL),
|
|
55
|
+
'{}'
|
|
56
|
+
) AS "roles",
|
|
57
|
+
bool_or(urd.role_name = $2) AS "hasRequestedRole"
|
|
58
|
+
FROM found_session fs
|
|
59
|
+
LEFT JOIN user_role_data urd ON urd.user_id = fs.user_id
|
|
60
|
+
GROUP BY fs.user_id, fs.expires_at
|
|
61
|
+
`;
|
|
62
|
+
/**
|
|
63
|
+
* The high-level "just make it work" entry point. Checks out a
|
|
64
|
+
* connection, opens a transaction, validates the session, validates the
|
|
65
|
+
* role, sets the four `app.*` GUC variables via parameterized
|
|
66
|
+
* `set_config(_, _, true)`, runs the callback, and commits (or rolls
|
|
67
|
+
* back on throw).
|
|
68
|
+
*
|
|
69
|
+
* The session variables use **transaction scope**, so they're discarded
|
|
70
|
+
* automatically when this function returns. Cross-request leakage is
|
|
71
|
+
* impossible.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* const widgets = await withSession(
|
|
76
|
+
* pool,
|
|
77
|
+
* { sessionId, roleName: 'user' },
|
|
78
|
+
* async (client, ctx) => {
|
|
79
|
+
* // ctx = { userId, tenantIds, allTenants, roles }
|
|
80
|
+
* const { rows } = await client.query('SELECT * FROM widgets');
|
|
81
|
+
* return rows;
|
|
82
|
+
* }
|
|
83
|
+
* );
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* @throws {SessionNotFoundError} If no session row matches `auth.sessionId`.
|
|
87
|
+
* @throws {SessionExpiredError} If the session has passed its `expires_at`.
|
|
88
|
+
* @throws {RoleNotAssignedError} If the user does not have `auth.roleName`.
|
|
89
|
+
* @throws {InvalidInputError} If `auth.sessionId` or `auth.roleName` is empty.
|
|
90
|
+
*/
|
|
91
|
+
export async function withSession(pool, auth, fn, options = {}) {
|
|
92
|
+
const log = options.logger ?? noopLogger;
|
|
93
|
+
const sessionHash = hashId(auth.sessionId);
|
|
94
|
+
return withTransaction(pool, async (client) => {
|
|
95
|
+
const { rows } = await client.query(RESOLVE_SESSION, [
|
|
96
|
+
auth.sessionId,
|
|
97
|
+
auth.roleName,
|
|
98
|
+
]);
|
|
99
|
+
const row = rows[0];
|
|
100
|
+
if (!row) {
|
|
101
|
+
log.warn({ sessionHash }, 'session not found');
|
|
102
|
+
throw new SessionNotFoundError();
|
|
103
|
+
}
|
|
104
|
+
if (row.expiresAt.getTime() <= Date.now()) {
|
|
105
|
+
log.warn({ sessionHash, expiresAt: row.expiresAt.toISOString() }, 'session expired');
|
|
106
|
+
throw new SessionExpiredError(row.expiresAt);
|
|
107
|
+
}
|
|
108
|
+
if (row.hasRequestedRole !== true) {
|
|
109
|
+
log.warn({ sessionHash, userId: row.userId, requestedRole: auth.roleName }, 'role not assigned');
|
|
110
|
+
throw new RoleNotAssignedError(auth.roleName);
|
|
111
|
+
}
|
|
112
|
+
const ctx = {
|
|
113
|
+
userId: row.userId,
|
|
114
|
+
tenantIds: row.tenantIds,
|
|
115
|
+
allTenants: row.allTenants,
|
|
116
|
+
roles: row.roles,
|
|
117
|
+
};
|
|
118
|
+
await setSessionContext(client, {
|
|
119
|
+
sessionId: auth.sessionId,
|
|
120
|
+
roleName: auth.roleName,
|
|
121
|
+
tenantIds: ctx.tenantIds,
|
|
122
|
+
allTenants: ctx.allTenants,
|
|
123
|
+
});
|
|
124
|
+
log.debug({
|
|
125
|
+
sessionHash,
|
|
126
|
+
userId: ctx.userId,
|
|
127
|
+
roleName: auth.roleName,
|
|
128
|
+
tenantCount: ctx.tenantIds.length,
|
|
129
|
+
allTenants: ctx.allTenants,
|
|
130
|
+
}, 'session context set');
|
|
131
|
+
return fn(client, ctx);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=with-session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with-session.js","sourceRoot":"","sources":["../src/with-session.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Pool, PoolClient } from 'pg';
|
|
2
|
+
/**
|
|
3
|
+
* Check out a connection from the pool, open a transaction, run `fn`,
|
|
4
|
+
* and either COMMIT (on success) or ROLLBACK (on throw). The connection
|
|
5
|
+
* is always released back to the pool.
|
|
6
|
+
*
|
|
7
|
+
* The session variables set inside `fn` via the `set*` helpers from this
|
|
8
|
+
* package use transaction scope, so they are discarded automatically when
|
|
9
|
+
* this function commits or rolls back. There is no risk of cross-request
|
|
10
|
+
* leakage.
|
|
11
|
+
*
|
|
12
|
+
* This is the lower-level companion to `withSession`. Use it when you
|
|
13
|
+
* need to set session variables manually (e.g. when migrating existing
|
|
14
|
+
* code that has its own session-extraction logic) and don't want the
|
|
15
|
+
* full session-and-role validation that `withSession` performs.
|
|
16
|
+
*
|
|
17
|
+
* @param pool - The pg connection pool.
|
|
18
|
+
* @param fn - Callback that receives the transaction-bound client.
|
|
19
|
+
* @returns The value returned by `fn`.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const result = await withTransaction(pool, async (client) => {
|
|
24
|
+
* await setSessionId(client, sessionId);
|
|
25
|
+
* await setRoleName(client, 'user');
|
|
26
|
+
* return client.query('SELECT * FROM widgets');
|
|
27
|
+
* });
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function withTransaction<T>(pool: Pool, fn: (client: PoolClient) => Promise<T>): Promise<T>;
|
|
31
|
+
//# sourceMappingURL=with-transaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with-transaction.d.ts","sourceRoot":"","sources":["../src/with-transaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAsB,eAAe,CAAC,CAAC,EACrC,IAAI,EAAE,IAAI,EACV,EAAE,EAAE,CAAC,MAAM,EAAE,UAAU,KAAK,OAAO,CAAC,CAAC,CAAC,GACrC,OAAO,CAAC,CAAC,CAAC,CAiBZ"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check out a connection from the pool, open a transaction, run `fn`,
|
|
3
|
+
* and either COMMIT (on success) or ROLLBACK (on throw). The connection
|
|
4
|
+
* is always released back to the pool.
|
|
5
|
+
*
|
|
6
|
+
* The session variables set inside `fn` via the `set*` helpers from this
|
|
7
|
+
* package use transaction scope, so they are discarded automatically when
|
|
8
|
+
* this function commits or rolls back. There is no risk of cross-request
|
|
9
|
+
* leakage.
|
|
10
|
+
*
|
|
11
|
+
* This is the lower-level companion to `withSession`. Use it when you
|
|
12
|
+
* need to set session variables manually (e.g. when migrating existing
|
|
13
|
+
* code that has its own session-extraction logic) and don't want the
|
|
14
|
+
* full session-and-role validation that `withSession` performs.
|
|
15
|
+
*
|
|
16
|
+
* @param pool - The pg connection pool.
|
|
17
|
+
* @param fn - Callback that receives the transaction-bound client.
|
|
18
|
+
* @returns The value returned by `fn`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const result = await withTransaction(pool, async (client) => {
|
|
23
|
+
* await setSessionId(client, sessionId);
|
|
24
|
+
* await setRoleName(client, 'user');
|
|
25
|
+
* return client.query('SELECT * FROM widgets');
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export async function withTransaction(pool, fn) {
|
|
30
|
+
const client = await pool.connect();
|
|
31
|
+
try {
|
|
32
|
+
await client.query('BEGIN');
|
|
33
|
+
try {
|
|
34
|
+
const result = await fn(client);
|
|
35
|
+
await client.query('COMMIT');
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
await client.query('ROLLBACK').catch(() => {
|
|
40
|
+
// Swallow rollback errors so the original error surfaces.
|
|
41
|
+
});
|
|
42
|
+
throw err;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
finally {
|
|
46
|
+
client.release();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=with-transaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"with-transaction.js","sourceRoot":"","sources":["../src/with-transaction.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,IAAU,EACV,EAAsC;IAEtC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;IACpC,IAAI,CAAC;QACH,MAAM,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC5B,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,MAAM,CAAC,CAAC;YAChC,MAAM,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;YAC7B,OAAO,MAAM,CAAC;QAChB,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE;gBACxC,0DAA0D;YAC5D,CAAC,CAAC,CAAC;YACH,MAAM,GAAG,CAAC;QACZ,CAAC;IACH,CAAC;YAAS,CAAC;QACT,MAAM,CAAC,OAAO,EAAE,CAAC;IACnB,CAAC;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@smplcty/auth",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Robust, type-safe session and role-based authentication primitives for PostgreSQL apps using Row-Level Security.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"schema",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public",
|
|
24
|
+
"registry": "https://registry.npmjs.org/"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/mabulu-inc/simplicity-auth.git"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/mabulu-inc/simplicity-auth#readme",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/mabulu-inc/simplicity-auth/issues"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"postgres",
|
|
36
|
+
"postgresql",
|
|
37
|
+
"rls",
|
|
38
|
+
"row-level-security",
|
|
39
|
+
"session",
|
|
40
|
+
"auth",
|
|
41
|
+
"authentication",
|
|
42
|
+
"authorization",
|
|
43
|
+
"multi-tenant"
|
|
44
|
+
],
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=20"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"pg": ">=8.0.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@smplcty/schema-flow": "^0.3.7",
|
|
53
|
+
"@types/node": "^25.5.2",
|
|
54
|
+
"@types/pg": "^8.20.0",
|
|
55
|
+
"eslint": "^10.2.0",
|
|
56
|
+
"pg": "^8.20.0",
|
|
57
|
+
"typescript": "^6.0.2",
|
|
58
|
+
"vitest": "^4.1.4"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "tsc -p tsconfig.build.json",
|
|
62
|
+
"clean": "rm -rf dist",
|
|
63
|
+
"lint": "eslint src tests",
|
|
64
|
+
"typecheck": "tsc --noEmit",
|
|
65
|
+
"test": "vitest run",
|
|
66
|
+
"test:watch": "vitest"
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
table: roles
|
|
2
|
+
columns:
|
|
3
|
+
- name: role_id
|
|
4
|
+
type: serial
|
|
5
|
+
primary_key: true
|
|
6
|
+
- name: name
|
|
7
|
+
type: text
|
|
8
|
+
nullable: false
|
|
9
|
+
unique_constraints:
|
|
10
|
+
- columns: [name]
|
|
11
|
+
name: roles_name_key
|
|
12
|
+
# The canonical role names @smplcty/auth's withSession() validates against.
|
|
13
|
+
#
|
|
14
|
+
# IDs 1-3 are reserved for these standard roles. Consumers adding their
|
|
15
|
+
# own roles should use IDs >= 100 to avoid colliding with future
|
|
16
|
+
# standard-role additions. The library never references these IDs
|
|
17
|
+
# directly — it always looks up by name — so the IDs are just a stable
|
|
18
|
+
# convention for migration-time conflict detection.
|
|
19
|
+
#
|
|
20
|
+
# Seeds use ON CONFLICT DO NOTHING via schema-flow, so re-running this
|
|
21
|
+
# migration against an already-seeded database is a no-op.
|
|
22
|
+
seeds:
|
|
23
|
+
- role_id: 1
|
|
24
|
+
name: 'user'
|
|
25
|
+
- role_id: 2
|
|
26
|
+
name: 'settings'
|
|
27
|
+
- role_id: 3
|
|
28
|
+
name: 'security'
|
|
29
|
+
seeds_on_conflict: 'DO NOTHING'
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
table: sessions
|
|
2
|
+
columns:
|
|
3
|
+
- name: session_id
|
|
4
|
+
type: text
|
|
5
|
+
primary_key: true
|
|
6
|
+
- name: user_communication_method_id
|
|
7
|
+
type: integer
|
|
8
|
+
nullable: false
|
|
9
|
+
references:
|
|
10
|
+
table: user_communication_methods
|
|
11
|
+
column: user_communication_method_id
|
|
12
|
+
- name: created_at
|
|
13
|
+
type: timestamptz
|
|
14
|
+
nullable: false
|
|
15
|
+
default: now()
|
|
16
|
+
- name: expires_at
|
|
17
|
+
type: timestamptz
|
|
18
|
+
nullable: false
|
|
19
|
+
- name: ip
|
|
20
|
+
type: text
|
|
21
|
+
- name: city
|
|
22
|
+
type: text
|
|
23
|
+
- name: region
|
|
24
|
+
type: text
|
|
25
|
+
- name: country
|
|
26
|
+
type: text
|
|
27
|
+
- name: latitude
|
|
28
|
+
type: text
|
|
29
|
+
- name: longitude
|
|
30
|
+
type: text
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
table: user_communication_methods
|
|
2
|
+
columns:
|
|
3
|
+
- name: user_communication_method_id
|
|
4
|
+
type: serial
|
|
5
|
+
primary_key: true
|
|
6
|
+
- name: user_id
|
|
7
|
+
type: integer
|
|
8
|
+
nullable: false
|
|
9
|
+
references:
|
|
10
|
+
table: users
|
|
11
|
+
column: user_id
|
|
12
|
+
- name: communication_channel_id
|
|
13
|
+
type: integer
|
|
14
|
+
nullable: false
|
|
15
|
+
references:
|
|
16
|
+
table: communication_channels
|
|
17
|
+
column: communication_channel_id
|
|
18
|
+
- name: code
|
|
19
|
+
type: text
|
|
20
|
+
nullable: false
|
|
21
|
+
unique_constraints:
|
|
22
|
+
- columns: [communication_channel_id, code]
|
|
23
|
+
name: user_communication_methods_channel_code_key
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
table: user_roles
|
|
2
|
+
columns:
|
|
3
|
+
- name: user_role_id
|
|
4
|
+
type: serial
|
|
5
|
+
primary_key: true
|
|
6
|
+
- name: user_id
|
|
7
|
+
type: integer
|
|
8
|
+
nullable: false
|
|
9
|
+
references:
|
|
10
|
+
table: users
|
|
11
|
+
column: user_id
|
|
12
|
+
- name: role_id
|
|
13
|
+
type: integer
|
|
14
|
+
nullable: false
|
|
15
|
+
references:
|
|
16
|
+
table: roles
|
|
17
|
+
column: role_id
|
|
18
|
+
# tenant_id IS NULL means a global role (applies to all tenants).
|
|
19
|
+
# The auth library reads this as `app.all_tenants = 'true'`.
|
|
20
|
+
- name: tenant_id
|
|
21
|
+
type: integer
|
|
22
|
+
nullable: true
|
|
23
|
+
references:
|
|
24
|
+
table: tenants
|
|
25
|
+
column: tenant_id
|