diff --git a/webapp/src/migrations/1.13.0.ts b/webapp/src/migrations/1.13.0.ts new file mode 100644 index 00000000..ae3a4e52 --- /dev/null +++ b/webapp/src/migrations/1.13.0.ts @@ -0,0 +1,30 @@ +import Permission from '@permission'; +import debug from 'debug'; +import { Binary } from 'mongodb'; +import { TEAM_MEMBER } from 'permissions/roles'; + +const log = debug('webapp:migration:1.13.0'); + +export default async function updateTeamMemberPermissions(db) { + log('add variable permissions to team members'); + const teamMemberPermissions = new Permission(); + teamMemberPermissions.setAll(TEAM_MEMBER.array); + const accounts = await db.collection('accounts').find().toArray(); + for (const account of accounts) { + const accountPermissions = new Permission(account.permissions.buffer); + const isTeamMember = TEAM_MEMBER.array.some(bit => accountPermissions.get(bit)); + if (!isTeamMember) { + continue; + } + const hasAllTeamMemberPermissions = TEAM_MEMBER.array.every(bit => accountPermissions.get(bit)); + if (!hasAllTeamMemberPermissions) { + accountPermissions.setAll(teamMemberPermissions.array); + await db + .collection('accounts') + .updateOne( + { _id: account._id }, + { $set: { permissions: new Binary(accountPermissions.array) } } + ); + } + } +}