mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
e0ce8995a7
- Swapped instances of createSecret for security.secret.create Co-authored-by: Renovate Bot <bot@renovateapp.com> Co-authored-by: Hannah Wolfe <github.erisds@gmail.com>
89 lines
2.4 KiB
JavaScript
89 lines
2.4 KiB
JavaScript
const omit = require('lodash/omit');
|
|
const security = require('@tryghost/security');
|
|
const ghostBookshelf = require('./base');
|
|
const {Role} = require('./role');
|
|
|
|
const ApiKey = ghostBookshelf.Model.extend({
|
|
tableName: 'api_keys',
|
|
|
|
defaults() {
|
|
const secret = security.secret.create(this.get('type'));
|
|
|
|
return {
|
|
secret
|
|
};
|
|
},
|
|
|
|
role() {
|
|
return this.belongsTo('Role');
|
|
},
|
|
|
|
integration() {
|
|
return this.belongsTo('Integration');
|
|
},
|
|
|
|
user() {
|
|
return this.belongsTo('User');
|
|
},
|
|
|
|
format(attrs) {
|
|
return omit(attrs, 'role');
|
|
},
|
|
|
|
onSaving(model, attrs, options) {
|
|
ghostBookshelf.Model.prototype.onSaving.apply(this, arguments);
|
|
|
|
// enforce roles which are currently hardcoded
|
|
// - admin key = Adminstrator role
|
|
// - content key = no role
|
|
if (this.hasChanged('type') || this.hasChanged('role_id')) {
|
|
if (this.get('type') === 'admin') {
|
|
return Role.findOne({name: attrs.role || 'Admin Integration'}, Object.assign({}, options, {columns: ['id']}))
|
|
.then((role) => {
|
|
this.set('role_id', role.get('id'));
|
|
});
|
|
}
|
|
|
|
if (this.get('type') === 'content') {
|
|
this.set('role_id', null);
|
|
}
|
|
}
|
|
},
|
|
onUpdated(model, options) {
|
|
if (this.previous('secret') !== this.get('secret')) {
|
|
this.addAction(model, 'refreshed', options);
|
|
}
|
|
},
|
|
|
|
getAction(event, options) {
|
|
const actor = this.getActor(options);
|
|
|
|
// @NOTE: we ignore internal updates (`options.context.internal`) for now
|
|
if (!actor) {
|
|
return;
|
|
}
|
|
|
|
// @TODO: implement context
|
|
return {
|
|
event: event,
|
|
resource_id: this.id || this.previous('id'),
|
|
resource_type: 'api_key',
|
|
actor_id: actor.id,
|
|
actor_type: actor.type
|
|
};
|
|
}
|
|
}, {
|
|
refreshSecret(data, options) {
|
|
const secret = security.secret.create(data.type);
|
|
return this.edit(Object.assign({}, data, {secret}), options);
|
|
}
|
|
});
|
|
|
|
const ApiKeys = ghostBookshelf.Collection.extend({
|
|
model: ApiKey
|
|
});
|
|
|
|
module.exports = {
|
|
ApiKey: ghostBookshelf.model('ApiKey', ApiKey),
|
|
ApiKeys: ghostBookshelf.collection('ApiKeys', ApiKeys)
|
|
};
|