@@ -5,15 +5,6 @@ import type { AlterationScript } from '../lib/types/alteration.js';
55
66const alteration : AlterationScript = {
77 up : async ( pool ) => {
8- // Unassign cloud scopes accidentally assigned to the admin Management API proxy
9- await pool . query ( sql `
10- delete from roles_scopes
11- using scopes
12- where roles_scopes.tenant_id = 'admin'
13- and roles_scopes.role_id = 'm-admin'
14- and roles_scopes.scope_id = scopes.id
15- and scopes.name in ('send:sms', 'send:email', 'create:affiliate', 'manage:affiliate');
16- ` ) ;
178 // Delete all legacy roles in the admin tenant
189 await pool . query ( sql `
1910 delete from roles
@@ -41,23 +32,22 @@ const alteration: AlterationScript = {
4132 } ,
4233 down : async ( pool ) => {
4334 console . log ( 'Add `manage:tenant` scope to the Cloud API resource' ) ;
44- // Add `manage:tenant` scope to the Cloud API resource
4535 await pool . query ( sql `
4636 insert into scopes (tenant_id, id, name, description, resource_id)
4737 values ('admin', 'manage:tenant', 'manage:tenant', 'Allow managing existing tenants, including create without limitation, update, and delete.', (
4838 select id from resources where tenant_id = 'admin' and indicator = 'https://cloud.logto.io/api'
4939 ));
5040 ` ) ;
41+
5142 console . log ( 'Update default role description' ) ;
52- // Update default role description
5343 await pool . query ( sql `
5444 update roles
5545 set description = 'Admin tenant admin role for Logto tenant default.'
5646 where tenant_id = 'admin'
5747 and name = 'default:admin';
5848 ` ) ;
49+
5950 console . log ( 'Add legacy roles in the admin tenant' ) ;
60- // Add legacy roles in the admin tenant
6151 const existingTenantIds = await pool . any < { id : string } > ( sql `
6252 select id from tenants where id != 'default';
6353 ` ) ;
@@ -77,8 +67,8 @@ const alteration: AlterationScript = {
7767 sql `, `
7868 ) } ;
7969 ` ) ;
70+
8071 console . log ( 'Restore assigned Management API scopes to the legacy roles' ) ;
81- // Restore assigned Management API scopes to the legacy roles
8272 await pool . query ( sql `
8373 insert into roles_scopes (tenant_id, id, role_id, scope_id)
8474 values ${ sql . join (
@@ -101,8 +91,8 @@ const alteration: AlterationScript = {
10191 sql `, `
10292 ) } ;
10393 ` ) ;
94+
10495 console . log ( 'Assign to legacy roles to users according to the tenant organization roles' ) ;
105- // Assign to legacy roles to users according to the tenant organization roles
10696 const adminUsersOrganizations = await pool . any < { userId : string ; organizationId : string } > ( sql `
10797 select user_id as "userId", organization_id as "organizationId"
10898 from organization_role_user_relations
@@ -127,26 +117,14 @@ const alteration: AlterationScript = {
127117 sql `, `
128118 ) } ;
129119 ` ) ;
130- console . log (
131- 'Assign back cloud scopes to the admin Management API proxy and the legacy admin user'
132- ) ;
133- // Assign back cloud scopes to the admin Management API proxy and the legacy admin user
120+
121+ console . log ( 'Assign back cloud scopes to the legacy admin user' ) ;
134122 await pool . query ( sql `
135123 insert into roles_scopes (tenant_id, id, role_id, scope_id)
136124 values ${ sql . join (
137125 [ 'send:sms' , 'send:email' , 'create:affiliate' , 'manage:affiliate' , 'manage:tenant' ] . map (
138126 ( scope ) => {
139127 return sql `
140- (
141- 'admin',
142- ${ generateStandardId ( ) } ,
143- 'm-admin',
144- (
145- select id from scopes
146- where tenant_id = 'admin'
147- and name = ${ scope }
148- )
149- ),
150128 (
151129 'admin',
152130 ${ generateStandardId ( ) } ,
0 commit comments