@logto/schemas 1.13.1 → 1.14.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.
@@ -0,0 +1,47 @@
1
+ import { sql } from 'slonik';
2
+
3
+ import type { AlterationScript } from '../lib/types/alteration.js';
4
+
5
+ enum RoleType {
6
+ User = 'User',
7
+ }
8
+
9
+ const getManagementApiResourceIndicator = (tenantId: string) => `https://${tenantId}.logto.app/api`;
10
+
11
+ // Remove management API scopes assigned to user roles, in case they were assigned by management API and bypassed the constraints in admin console.
12
+ const alteration: AlterationScript = {
13
+ up: async (pool) => {
14
+ const { rows } = await pool.query<{
15
+ rolesScopesId: string;
16
+ indicator: string;
17
+ tenantId: string;
18
+ }>(sql`
19
+ select
20
+ roles_scopes.id as "rolesScopesId",
21
+ roles_scopes.tenant_id as "tenantId",
22
+ resources.indicator as indicator from roles_scopes
23
+ join roles
24
+ on roles_scopes.role_id = roles.id and roles_scopes.tenant_id = roles.tenant_id
25
+ join scopes on
26
+ roles_scopes.scope_id = scopes.id and roles_scopes.tenant_id = scopes.tenant_id
27
+ join resources on
28
+ scopes.resource_id = resources.id and scopes.tenant_id = resources.tenant_id
29
+ where roles.type = ${RoleType.User};
30
+ `);
31
+ const rolesScopesIdsToRemove = rows
32
+ .filter(
33
+ ({ indicator, tenantId }) => indicator === getManagementApiResourceIndicator(tenantId)
34
+ )
35
+ .map(({ rolesScopesId }) => rolesScopesId);
36
+ if (rolesScopesIdsToRemove.length > 0) {
37
+ await pool.query(sql`
38
+ delete from roles_scopes where id in (${sql.join(rolesScopesIdsToRemove, sql`, `)});
39
+ `);
40
+ }
41
+ },
42
+ down: async (pool) => {
43
+ // It cannot be reverted automatically.
44
+ },
45
+ };
46
+
47
+ export default alteration;
@@ -0,0 +1,18 @@
1
+ import { sql } from 'slonik';
2
+
3
+ import type { AlterationScript } from '../lib/types/alteration.js';
4
+
5
+ const alteration: AlterationScript = {
6
+ up: async (pool) => {
7
+ await pool.query(sql`
8
+ alter table daily_active_users alter column date set default now();
9
+ `);
10
+ },
11
+ down: async (pool) => {
12
+ await pool.query(sql`
13
+ alter table daily_active_users alter column date drop default;
14
+ `);
15
+ },
16
+ };
17
+
18
+ export default alteration;
@@ -0,0 +1,3 @@
1
+ import type { AlterationScript } from '../lib/types/alteration.js';
2
+ declare const alteration: AlterationScript;
3
+ export default alteration;
@@ -0,0 +1,36 @@
1
+ import { sql } from 'slonik';
2
+ var RoleType;
3
+ (function (RoleType) {
4
+ RoleType["User"] = "User";
5
+ })(RoleType || (RoleType = {}));
6
+ const getManagementApiResourceIndicator = (tenantId) => `https://${tenantId}.logto.app/api`;
7
+ // Remove management API scopes assigned to user roles, in case they were assigned by management API and bypassed the constraints in admin console.
8
+ const alteration = {
9
+ up: async (pool) => {
10
+ const { rows } = await pool.query(sql `
11
+ select
12
+ roles_scopes.id as "rolesScopesId",
13
+ roles_scopes.tenant_id as "tenantId",
14
+ resources.indicator as indicator from roles_scopes
15
+ join roles
16
+ on roles_scopes.role_id = roles.id and roles_scopes.tenant_id = roles.tenant_id
17
+ join scopes on
18
+ roles_scopes.scope_id = scopes.id and roles_scopes.tenant_id = scopes.tenant_id
19
+ join resources on
20
+ scopes.resource_id = resources.id and scopes.tenant_id = resources.tenant_id
21
+ where roles.type = ${RoleType.User};
22
+ `);
23
+ const rolesScopesIdsToRemove = rows
24
+ .filter(({ indicator, tenantId }) => indicator === getManagementApiResourceIndicator(tenantId))
25
+ .map(({ rolesScopesId }) => rolesScopesId);
26
+ if (rolesScopesIdsToRemove.length > 0) {
27
+ await pool.query(sql `
28
+ delete from roles_scopes where id in (${sql.join(rolesScopesIdsToRemove, sql `, `)});
29
+ `);
30
+ }
31
+ },
32
+ down: async (pool) => {
33
+ // It cannot be reverted automatically.
34
+ },
35
+ };
36
+ export default alteration;
@@ -0,0 +1,3 @@
1
+ import type { AlterationScript } from '../lib/types/alteration.js';
2
+ declare const alteration: AlterationScript;
3
+ export default alteration;
@@ -0,0 +1,14 @@
1
+ import { sql } from 'slonik';
2
+ const alteration = {
3
+ up: async (pool) => {
4
+ await pool.query(sql `
5
+ alter table daily_active_users alter column date set default now();
6
+ `);
7
+ },
8
+ down: async (pool) => {
9
+ await pool.query(sql `
10
+ alter table daily_active_users alter column date drop default;
11
+ `);
12
+ },
13
+ };
14
+ export default alteration;
@@ -8,7 +8,7 @@ export type CreateDailyActiveUser = {
8
8
  id: string;
9
9
  tenantId?: string;
10
10
  userId: string;
11
- date: number;
11
+ date?: number;
12
12
  };
13
13
  export type DailyActiveUser = {
14
14
  id: string;
@@ -4,7 +4,7 @@ const createGuard = z.object({
4
4
  id: z.string().min(1).max(21),
5
5
  tenantId: z.string().max(21).optional(),
6
6
  userId: z.string().min(1).max(21),
7
- date: z.number(),
7
+ date: z.number().optional(),
8
8
  });
9
9
  const guard = z.object({
10
10
  id: z.string().min(1).max(21),
@@ -13,8 +13,8 @@ export declare enum CloudScope {
13
13
  ManageAffiliate = "manage:affiliate",
14
14
  /** The user can create new affiliates and logs. */
15
15
  CreateAffiliate = "create:affiliate",
16
- /** The user can cleanup outdated logs. */
17
- CleanupOutdatedLogs = "cleanup:outdated-logs"
16
+ /** The user can prune logs which are expired. */
17
+ PruneLogs = "prune:logs"
18
18
  }
19
19
  export declare const createCloudApi: () => readonly [UpdateAdminData, ...CreateScope[]];
20
20
  export declare const createTenantApplicationRole: () => Readonly<Role>;
@@ -16,8 +16,8 @@ export var CloudScope;
16
16
  CloudScope["ManageAffiliate"] = "manage:affiliate";
17
17
  /** The user can create new affiliates and logs. */
18
18
  CloudScope["CreateAffiliate"] = "create:affiliate";
19
- /** The user can cleanup outdated logs. */
20
- CloudScope["CleanupOutdatedLogs"] = "cleanup:outdated-logs";
19
+ /** The user can prune logs which are expired. */
20
+ CloudScope["PruneLogs"] = "prune:logs";
21
21
  })(CloudScope || (CloudScope = {}));
22
22
  export const createCloudApi = () => {
23
23
  const resourceId = generateStandardId();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logto/schemas",
3
- "version": "1.13.1",
3
+ "version": "1.14.0",
4
4
  "author": "Silverhand Inc. <contact@silverhand.io>",
5
5
  "license": "MPL-2.0",
6
6
  "type": "module",
@@ -1,3 +1,3 @@
1
1
  /* This SQL will run before all other queries. */
2
2
 
3
- create role logto_tenant_${database} noinherit;
3
+ create role logto_tenant_${database} password '${password}' noinherit;
@@ -3,7 +3,7 @@ create table daily_active_users (
3
3
  tenant_id varchar(21) not null
4
4
  references tenants (id) on update cascade on delete cascade,
5
5
  user_id varchar(21) not null,
6
- date timestamptz not null,
6
+ date timestamptz not null default (now()),
7
7
  primary key (id),
8
8
  constraint daily_active_users__user_id_date
9
9
  unique (user_id, date)