diff --git a/core/server/data/migrations/versions/2.28/1-add-db-backup-content-permission.js b/core/server/data/migrations/versions/2.28/1-add-db-backup-content-permission.js new file mode 100644 index 0000000000..f87bd9a8d3 --- /dev/null +++ b/core/server/data/migrations/versions/2.28/1-add-db-backup-content-permission.js @@ -0,0 +1,61 @@ +const logging = require('../../../../lib/common/logging'); +const models = require('../../../../models'); +const utils = require('../../../schema/fixtures/utils'); + +const fixtureBackupContentPerm = utils.findModelFixtureEntry('Permission', { + object_type: 'db', + action_type: 'backupContent' +}); + +module.exports = { + config: { + transaction: true + }, + + async up(options) { + try { + const existingBackupContentPerm = await models.Permission.findOne( + fixtureBackupContentPerm, + options + ); + + if (existingBackupContentPerm) { + return logging.warn('Issue adding db.backupContent (already exists)'); + } + + const result = await utils.addFixturesForModel({ + name: 'Permission', + entries: [fixtureBackupContentPerm] + }, options); + + const success = result.done === result.expected; + + if (!success) { + return logging.warn('Issue adding db.backupContent permission (did not insert)'); + } + + return logging.info('Completed adding db.backupContent permission'); + } catch (err) { + return logging.error('Errored when adding db.backupContent permission'); + } + }, + + async down(options) { + try { + const existingBackupContentPerm = await models.Permission.findOne( + fixtureBackupContentPerm, + options + ); + + if (!existingBackupContentPerm) { + return logging.warn('Issue removing db.backupContent (already removed)'); + } + + await existingBackupContentPerm.destroy(options); + + return logging.info('Completed removing db.backupContent permission'); + } catch (err) { + return logging.error('Errored when removing db.backupContent permission'); + } + } +}; diff --git a/core/server/data/migrations/versions/2.28/2-add-db-backup-content-permission-to-roles.js b/core/server/data/migrations/versions/2.28/2-add-db-backup-content-permission-to-roles.js new file mode 100644 index 0000000000..f30e0d4d72 --- /dev/null +++ b/core/server/data/migrations/versions/2.28/2-add-db-backup-content-permission-to-roles.js @@ -0,0 +1,46 @@ +const logging = require('../../../../lib/common/logging'); +const utils = require('../../../schema/fixtures/utils'); + +const relationFixtures = { + from: { + model: 'Role', + match: 'name', + relation: 'permissions' + }, + to: { + model: 'Permission', + match: ['object_type', 'action_type'] + }, + entries: { + Administrator: { + db: 'backupContent' + }, + 'DB Backup Integration': { + db: 'backupContent' + } + } +}; + +module.exports = { + config: { + transaction: true + }, + + async up(options) { + try { + await utils.addFixturesForRelation(relationFixtures, options); + return logging.info('Completed adding db.backupContent permission to roles'); + } catch (err) { + return logging.warn('Issue adding db.backupContent permission to roles'); + } + }, + + async down(options) { + try { + await utils.removeFixturesForRelation(relationFixtures, options); + return logging.info('Completed removing db.backupContent permission from roles'); + } catch (err) { + return logging.warn('Issue removing db.backupContent permission from roles'); + } + } +};