Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add migration script to update team member permissions #606 #608

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion webapp/src/lib/permissions/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ TEAM_OWNER.setAll(ORG_BITS);
const TEAM_ADMIN = new Permission();
TEAM_ADMIN.setAll(TEAM_BITS);

const TEAM_MEMBER = new Permission();
export const TEAM_MEMBER = new Permission();
TEAM_MEMBER.setAll([
Permissions.CREATE_APP,
Permissions.EDIT_APP,
Expand Down
35 changes: 35 additions & 0 deletions webapp/src/migrations/1.12.0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Permission from '@permission';
import debug from 'debug';
import { Binary } from 'mongodb';
import { TEAM_MEMBER } from 'permissions/roles';

const log = debug('webapp:migration:1.10.0');

async function updateTeamMemberPermissions(db) {
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) } }
);
}
}
}

export default async function (db) {
log('Starting migration to update TEAM_MEMBER permissions');
await updateTeamMemberPermissions(db);
log('Migration to update TEAM_MEMBER permissions completed');
}
Loading