forked from TryGhost/Ghost
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added migrations for scheduler integration, permission and role
refs TryGhost#10060 - Modification of https://github.com/TryGhost/Ghost/pull/10974/files - Added publish permission migrations for all roles having "post": "all" permission
- Loading branch information
Showing
8 changed files
with
220 additions
and
9 deletions.
There are no files selected for viewing
File renamed without changes.
84 changes: 84 additions & 0 deletions
84
core/server/data/migrations/versions/2.28/3-insert-ghost-scheduler-role.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const logging = require('../../../../lib/common/logging'); | ||
const merge = require('lodash/merge'); | ||
const models = require('../../../../models'); | ||
const utils = require('../../../schema/fixtures/utils'); | ||
|
||
const resource = 'post'; | ||
const _private = {}; | ||
|
||
_private.printResult = function printResult(result, message) { | ||
if (result.done === result.expected) { | ||
logging.info(message); | ||
} else { | ||
logging.warn(`(${result.done}/${result.expected}) ${message}`); | ||
} | ||
}; | ||
|
||
_private.addSchedulerRole = (options) => { | ||
const message = 'Adding "Scheduler Integration" role to roles table'; | ||
const apiKeyRole = utils.findModelFixtureEntry('Role', {name: 'Scheduler Integration'}); | ||
|
||
return models.Role.findOne({name: apiKeyRole.name}, options) | ||
.then((role) => { | ||
if (!role) { | ||
return utils.addFixturesForModel({ | ||
name: 'Role', | ||
entries: [apiKeyRole] | ||
}, options).then(result => _private.printResult(result, message)); | ||
} | ||
|
||
logging.warn(message); | ||
}); | ||
}; | ||
|
||
_private.addPublishPermission = (options) => { | ||
const modelToAdd = utils.findModelFixtures('Permission', {object_type: resource, action_type: 'publish'}); | ||
|
||
return utils.addFixturesForModel(modelToAdd, options) | ||
.then(result => _private.printResult(result, `Adding "publish" permissions fixtures for ${resource}s`)); | ||
}; | ||
|
||
_private.removeApiKeyPermissionsAndRole = (options) => { | ||
const message = 'Rollback: Removing "Scheduler Integration" role and permissions'; | ||
|
||
const modelToRemove = utils.findModelFixtures('Permission', {object_type: resource, action_type: 'publish'}); | ||
|
||
// permission model automatically cleans up permissions_roles on .destroy() | ||
return utils.removeFixturesForModel(modelToRemove, options) | ||
.then(result => _private.printResult(result, `Removing "publish" permissions fixtures for ${resource}s`)) | ||
.then(() => models.Role.findOne({name: 'Scheduler Integration'}, options)) | ||
.then((role) => { | ||
if (!role) { | ||
logging.warn(message); | ||
return; | ||
} | ||
|
||
return role.destroy(options); | ||
}) | ||
.then(() => { | ||
logging.info(message); | ||
}); | ||
}; | ||
|
||
module.exports.config = { | ||
transaction: true | ||
}; | ||
|
||
module.exports.up = (options) => { | ||
const localOptions = merge({ | ||
context: {internal: true}, | ||
migrating: true | ||
}, options); | ||
|
||
return _private.addSchedulerRole(localOptions) | ||
.then(() => _private.addPublishPermission(localOptions)); | ||
}; | ||
|
||
module.exports.down = (options) => { | ||
const localOptions = merge({ | ||
context: {internal: true}, | ||
migrating: true | ||
}, options); | ||
|
||
return _private.removeApiKeyPermissionsAndRole(localOptions); | ||
}; |
69 changes: 69 additions & 0 deletions
69
core/server/data/migrations/versions/2.28/4-insert-scheduler-integration.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const logging = require('../../../../lib/common/logging'); | ||
const merge = require('lodash/merge'); | ||
const models = require('../../../../models'); | ||
const utils = require('../../../schema/fixtures/utils'); | ||
|
||
const _private = {}; | ||
|
||
_private.printResult = function printResult(result, message) { | ||
if (result.done === result.expected) { | ||
logging.info(message); | ||
} else { | ||
logging.warn(`(${result.done}/${result.expected}) ${message}`); | ||
} | ||
}; | ||
|
||
_private.addGhostSchedulerIntegration = (options) => { | ||
const message = 'Adding "Ghost Scheduler" integration'; | ||
const fixtureIntegration = utils.findModelFixtureEntry('Integration', {slug: 'ghost-scheduler'}); | ||
|
||
return models.Integration.findOne({slug: fixtureIntegration.slug}, options) | ||
.then((integration) => { | ||
if (!integration) { | ||
return utils.addFixturesForModel({ | ||
name: 'Integration', | ||
entries: [fixtureIntegration] | ||
}, options).then(result => _private.printResult(result, message)); | ||
} | ||
|
||
logging.warn(message); | ||
}); | ||
}; | ||
|
||
_private.removeGhostSchedulerIntegration = (options) => { | ||
const message = 'Rollback: Removing "Ghost Scheduler" integration'; | ||
|
||
return models.Integration.findOne({slug: 'ghost-scheduler'}, options) | ||
.then((integration) => { | ||
if (!integration) { | ||
logging.warn(message); | ||
return; | ||
} | ||
|
||
return integration.destroy().then(() => { | ||
logging.info(message); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports.config = { | ||
transaction: true | ||
}; | ||
|
||
module.exports.up = (options) => { | ||
const localOptions = merge({ | ||
context: {internal: true}, | ||
migrating: true | ||
}, options); | ||
|
||
return _private.addGhostSchedulerIntegration(localOptions); | ||
}; | ||
|
||
module.exports.down = (options) => { | ||
const localOptions = merge({ | ||
context: {internal: true}, | ||
migrating: true | ||
}, options); | ||
|
||
return _private.removeGhostSchedulerIntegration(localOptions); | ||
}; |
52 changes: 52 additions & 0 deletions
52
core/server/data/migrations/versions/2.28/5-add-scheduler-permission-to-roles.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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: { | ||
post: 'publish' | ||
}, | ||
'Admin Integration': { | ||
post: 'publish' | ||
}, | ||
Editor: { | ||
post: 'publish' | ||
}, | ||
'Scheduler Integration': { | ||
post: 'publish' | ||
} | ||
} | ||
}; | ||
|
||
module.exports = { | ||
config: { | ||
transaction: true | ||
}, | ||
|
||
async up(options) { | ||
try { | ||
await utils.addFixturesForRelation(relationFixtures, options); | ||
return logging.info('Completed adding post.publish permission to roles'); | ||
} catch (err) { | ||
return logging.warn('Issue adding post.publish permission to roles'); | ||
} | ||
}, | ||
|
||
async down(options) { | ||
try { | ||
await utils.removeFixturesForRelation(relationFixtures, options); | ||
return logging.info('Completed removing post.publish permission from roles'); | ||
} catch (err) { | ||
return logging.warn('Issue removing post.publish permission from roles'); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters