Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 1x 1x 1x 1x 1x 1x 147x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 4x 3x 4x 3x 3x 4x 1x 4x 1x 4x 1x 4x 4x 4x 4x 4x 18x 1x 17x 5x 12x 12x 9x 97x 97x 9x 9x 3x 2x 1x 1x 7x 7x 6x 6x 6x 6x 5x 5x 5x 8x 5x 6x 6x 6x 6x 18x 18x 1x 1x 2x 2x 4x 4x 11x | import _ from 'lodash'; import DefaultHandler, { order } from './default'; import { calculateChanges } from '../../calculateChanges'; import log from '../../../logger'; import { Asset, Assets, CalculatedChanges } from '../../../types'; export const schema = { type: 'array', items: { type: 'object', properties: { name: { type: 'string' }, display_name: { type: 'string' }, branding: { type: 'object' }, metadata: { type: 'object' }, connections: { type: 'array', items: { type: 'object', properties: { connection_id: { type: 'string' }, assign_membership_on_login: { type: 'boolean' }, }, }, }, }, required: ['name'], }, }; export default class OrganizationsHandler extends DefaultHandler { existing: Asset[]; constructor(config: DefaultHandler) { super({ ...config, type: 'organizations', id: 'id', }); } async deleteOrganization(org): Promise<void> { await this.client.organizations.delete({ id: org.id }); } async deleteOrganizations(data): Promise<void> { Eif ( this.config('AUTH0_ALLOW_DELETE') === 'true' || this.config('AUTH0_ALLOW_DELETE') === true ) { await this.client.pool .addEachTask({ data: data || [], generator: (item) => this.deleteOrganization(item) .then(() => { this.didDelete(item); this.deleted += 1; }) .catch((err) => { throw new Error(`Problem deleting ${this.type} ${this.objString(item)}\n${err}`); }), }) .promise(); } else { log.warn(`Detected the following organizations should be deleted. Doing so may be destructive.\nYou can enable deletes by setting 'AUTH0_ALLOW_DELETE' to true in the config \n${data.map((i) => this.objString(i)).join('\n')}`); } } async createOrganization(org): Promise<Asset> { const organization = { ...org }; delete organization.connections; const created = await this.client.organizations.create(organization); if (typeof org.connections !== 'undefined' && org.connections.length > 0) { await Promise.all( org.connections.map((conn) => this.client.organizations.addEnabledConnection({ id: created.id }, conn) ) ); } return created; } async createOrganizations(creates: CalculatedChanges['create']) { await this.client.pool .addEachTask({ data: creates || [], generator: (item) => this.createOrganization(item) .then((data) => { this.didCreate(data); this.created += 1; }) .catch((err) => { throw new Error(`Problem creating ${this.type} ${this.objString(item)}\n${err}`); }), }) .promise(); } async updateOrganization(org, organizations) { const { connections: existingConnections } = await organizations.find( (orgToUpdate) => orgToUpdate.name === org.name ); const params = { id: org.id }; const { connections } = org; delete org.connections; delete org.name; delete org.id; await this.client.organizations.update(params, org); const connectionsToRemove = existingConnections.filter( (c) => !connections.find((x) => x.connection_id === c.connection_id) ); const connectionsToAdd = connections.filter( (c) => !existingConnections.find((x) => x.connection_id === c.connection_id) ); const connectionsToUpdate = connections.filter((c) => existingConnections.find( (x) => x.connection_id === c.connection_id && x.assign_membership_on_login !== c.assign_membership_on_login ) ); // Handle updates first await Promise.all( connectionsToUpdate.map((conn) => this.client.organizations .updateEnabledConnection( { connection_id: conn.connection_id, ...params }, { assign_membership_on_login: conn.assign_membership_on_login } ) .catch(() => { throw new Error( `Problem updating Enabled Connection ${conn.connection_id} for organizations ${params.id}` ); }) ) ); await Promise.all( connectionsToAdd.map((conn) => this.client.organizations .addEnabledConnection(params, _.omit(conn, 'connection')) .catch(() => { throw new Error( `Problem adding Enabled Connection ${conn.connection_id} for organizations ${params.id}` ); }) ) ); await Promise.all( connectionsToRemove.map((conn) => this.client.organizations .removeEnabledConnection({ connection_id: conn.connection_id, ...params }) .catch(() => { throw new Error( `Problem removing Enabled Connection ${conn.connection_id} for organizations ${params.id}` ); }) ) ); return params; } async updateOrganizations(updates: CalculatedChanges['update'], orgs: Asset[]): Promise<void> { await this.client.pool .addEachTask({ data: updates || [], generator: (item) => this.updateOrganization(item, orgs) .then((data) => { this.didUpdate(data); this.updated += 1; }) .catch((err) => { throw new Error(`Problem updating ${this.type} ${this.objString(item)}\n${err}`); }), }) .promise(); } async getType(): Promise<Asset[]> { if (this.existing) { return this.existing; } if (!this.client.organizations || typeof this.client.organizations.getAll !== 'function') { return []; } try { const organizations = await this.client.organizations.getAll({ checkpoint: true, include_totals: true, }); for (let index = 0; index < organizations.length; index++) { const connections = await this.client.organizations.connections.get({ id: organizations[index].id, }); organizations[index].connections = connections; } this.existing = organizations; return this.existing; } catch (err) { if (err.statusCode === 404 || err.statusCode === 501) { return []; } throw err; } } // Run after connections @order('70') async processChanges(assets: Assets): Promise<void> { const { organizations } = assets; // Do nothing if not set if (!organizations) return; // Gets organizations from destination tenant const existing = await this.getType(); const existingConnections = await this.client.connections.getAll({ paginate: true, include_totals: true, }); // We need to get the connection ids for the names configured so we can link them together organizations.forEach((org) => { org.connections = (org.connections || []) .map((connection) => { const { name } = connection; delete connection.name; return { ...connection, connection_id: (existingConnections.find((c) => c.name === name) || {}).id, }; }) .filter((connection) => !!connection.connection_id); }); const changes = calculateChanges({ handler: this, assets: organizations, existing, identifiers: this.identifiers, allowDelete: !!this.config('AUTH0_ALLOW_DELETE'), }); log.debug( `Start processChanges for organizations [delete:${changes.del.length}] [update:${changes.update.length}], [create:${changes.create.length}]` ); const myChanges = [ { del: changes.del }, { create: changes.create }, { update: changes.update }, ]; await Promise.all( myChanges.map(async (change) => { switch (true) { case change.del && change.del.length > 0: await this.deleteOrganizations(change.del); break; case change.create && change.create.length > 0: await this.createOrganizations(changes.create); break; case change.update && change.update.length > 0: Eif (change.update) await this.updateOrganizations(change.update, existing); break; default: break; } }) ); } } |