-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate-default-groups.js
More file actions
337 lines (293 loc) · 8.97 KB
/
Copy pathcreate-default-groups.js
File metadata and controls
337 lines (293 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
const { createClient } = require('@supabase/supabase-js');
const commandLineArgs = require('command-line-args');
const fs = require('fs');
const stream = require('stream');
const main = async (options) => {
let supabase = createClient(
process.env.SUPABASE_HOST,
process.env.SUPABASE_SERVICE_KEY,
{
auth: {
persistSession: true,
autoRefreshToken: true,
detectSessionInUrl: false,
},
}
);
/*
const {data, error } = await supabase.from('policies').select();
console.table(data);
*/
let config;
try {
const configStr = fs.readFileSync(options.file, 'utf8');
config = JSON.parse(configStr);
} catch (err) {
console.error(err);
return;
}
// console.info(JSON.stringify(config, null, 2));
// Create the policies
let policiesInserts = [];
config.policies.forEach((policy) => {
policiesInserts.push({
id: policy.id,
table_name: policy.table_name,
operation: policy.operation,
});
});
const policiesResponse = await supabase
.from('policies')
.upsert(policiesInserts)
.select();
console.info('Policies:');
console.table(policiesResponse.data);
// Create the roles
let rolesInsert = [];
let rolePoliciesInsert = [];
config.roles.forEach((role) => {
rolesInsert.push({
id: role.id,
name: role.name,
description: role.description,
});
role.policies.forEach((rolePolicyId) => {
rolePoliciesInsert.push({
role_id: role.id,
policy_id: rolePolicyId,
});
});
});
const rolesResponse = await supabase
.from('roles')
.upsert(rolesInsert)
.select();
const rolePolicesResponse = await supabase
.from('role_policies')
.upsert(rolePoliciesInsert, { onConflict: 'role_id, policy_id' })
.select();
console.info('Roles:');
console.table(rolesResponse.data);
console.info('Role Policies:');
console.table(rolePolicesResponse.data);
// Create Organization Groups
let organizationGroupInserts = [];
config.org_groups.forEach((orgGroup) => {
organizationGroupInserts.push({
id: orgGroup.id,
role_id: orgGroup.role_id,
name: orgGroup.name,
description: orgGroup.description,
is_admin: orgGroup.is_admin,
is_default: orgGroup.is_default,
is_read_only: orgGroup.is_read_only
});
});
const orgGroupsResponse = await supabase
.from('organization_groups')
.upsert(organizationGroupInserts, {
onConflict: 'role_id',
ignoreDuplicates: false,
});
const orgAdminGroup = organizationGroupInserts.find(
(g) => g.is_admin === true
);
console.log('Org group: ', orgAdminGroup)
const getOrgAdminResponse = await supabase
.from('organization_groups')
.select()
.eq('id', orgAdminGroup.id);
console.info('Organization Admin Group: ');
console.table(getOrgAdminResponse.data);
// Retrieve the Admin Profile
let orgAdminProfile = await supabase
.from('profiles')
.select()
.eq('email', config.admin.admin_email);
// if no Admin found, create one
if (orgAdminProfile.data.length === 0) {
supabase = createClient(
process.env.SUPABASE_HOST,
process.env.SUPABASE_SERVICE_KEY,
{
auth: {
persistSession: true,
autoRefreshToken: true,
detectSessionInUrl: false,
},
}
);
// Create the Admin user
const createAdminUserResponse = await supabase.auth.admin.createUser({
email: config.admin.admin_email,
password: process.env.ORG_ADMIN_PW,
email_confirm: true,
});
if (createAdminUserResponse.error) {
console.log('Error creating org admin: ', createAdminUserResponse.error);
}
supabase = createClient(
process.env.SUPABASE_HOST,
process.env.SUPABASE_SERVICE_KEY,
{
auth: {
persistSession: true,
autoRefreshToken: true,
detectSessionInUrl: false,
},
}
);
orgAdminProfile = await supabase
.from('profiles')
.select()
.eq('email', config.admin.admin_email);
}
console.info('Organization Admin:');
console.info(JSON.stringify(orgAdminProfile.data[0], null, 2));
const adminId = orgAdminProfile.data[0].id;
const adminGroupId = getOrgAdminResponse.data[0].id;
// Add the Admin to the specified groups
for (let i = 0; i < config.admin.admin_groups.length; i++) {
const groupAddResponse = await supabase
.from('group_users')
.update({
group_type: 'organization',
type_id: adminGroupId,
})
.eq('user_id', adminId)
.select();
if (groupAddResponse.error) {
console.error('Failed to add Admin to group: ', orgGroupsResponse.name);
console.error(groupAddResponse.error);
}
}
// Create the default groups table
let defaultGroups = [];
config.project_groups.forEach((group) => {
defaultGroups.push({
id: group.id,
group_type: 'project',
name: group.name,
description: group.description,
role_id: group.role_id,
is_admin: !!group.is_admin,
is_default: !!group.is_default,
is_read_only: !!group.is_read_only
});
});
config.layer_groups.forEach((group) => {
defaultGroups.push({
id: group.id,
group_type: 'layer',
name: group.name,
description: group.description,
role_id: group.role_id,
is_admin: !!group.is_admin,
is_default: !!group.is_default,
is_read_only: !!group.is_read_only
});
});
const defaultGroupsResponse = await supabase
.from('default_groups')
.upsert(defaultGroups, { ignoreDuplicates: false, onConflict: 'id' })
.select();
if (defaultGroupsResponse.error) {
console.error('Failed to create default_groups');
} else {
console.info('Default Groups:');
console.table(defaultGroupsResponse.data);
}
// Now check for deletions
// rolePoliciesInsert
const remoteRolesPolicies = await supabase.from('role_policies').select();
if (remoteRolesPolicies.error) {
console.error('Failed to retrieve Roles');
} else {
for (let i = 0; i < remoteRolesPolicies.data.length; i++) {
const rolePolicy = remoteRolesPolicies.data[i];
const findIdx = rolePoliciesInsert.findIndex(
(r) =>
r.role_id === rolePolicy.role_id &&
r.policy_id === rolePolicy.policy_id
);
if (findIdx === -1) {
const deleteRolePolicyResp = await supabase
.from('role_policies')
.delete()
.eq('role_id', rolePolicy.role_id)
.eq('policy_id', rolePolicy.policy_id);
if (deleteRolePolicyResp.error) {
console.error('Failed to delete role_policy');
}
}
}
}
// rolesInsert
const remoteRoles = await supabase.from('roles').select();
if (remoteRoles.error) {
console.error('Failed to retrieve Roles');
} else {
for (let i = 0; i < remoteRoles.data.length; i++) {
const role = remoteRoles.data[i];
const findIdx = rolesInsert.findIndex((r) => r.id === role.id);
if (findIdx === -1) {
const deleteRoleResp = await supabase
.from('roles')
.delete()
.eq('id', role.id);
if (deleteRoleResp.error) {
console.error('Failed to role');
}
}
}
}
// organizationGroupInserts
const remoteOrgGroups = await supabase.from('organization_groups').select();
if (remoteOrgGroups.error) {
console.error('Failed to retrieve Org Groups');
} else {
for (let i = 0; i < remoteOrgGroups.data.length; i++) {
const group = remoteOrgGroups.data[i];
const findIdx = organizationGroupInserts.findIndex(
(og) => og.role_id === group.role_id
);
if (findIdx === -1) {
const deleteGroupResp = await supabase
.from('organization_groups')
.delete()
.eq('role_id', group.role_id);
if (deleteGroupResp.error) {
console.error('Failed to delete Org Group');
}
}
}
}
// Make sure we have a 'documents' bucket
const documentsBucketResp = await supabase.storage.getBucket('documents');
if (documentsBucketResp.error || documentsBucketResp.data.length === 0) {
const { data, error } = await supabase.storage.createBucket('documents', {
public: false
});
}
// Make sure we have a 'jobs' bucket
const jobsBucketResp = await supabase.storage.getBucket('jobs');
if (jobsBucketResp.error || jobsBucketResp.data.length === 0) {
const { data, error } = await supabase.storage.createBucket('jobs', {
public: false
});
}
// Make sure we have a DEFAULT_CONTEXT tag_definition
const tagCreateResp = await supabase.from('tag_definitions').upsert({
id: process.env.DEFAULT_CONTEXT_ID,
name: 'DEFAULT_CONTEXT',
target_type: 'context',
scope: 'system',
});
};
const optionDefinitions = [
{ name: 'file', alias: 'f', type: String },
{ name: 'admin-id', alias: 'i', type: String },
{ name: 'admin-group-id', alias: 'g', type: String },
];
const options = commandLineArgs(optionDefinitions);
main(options);