2019-08-02 17:28:02 +08:00
|
|
|
const omit = require('lodash/omit');
|
2022-05-06 17:53:10 +01:00
|
|
|
const security = require('@tryghost/security');
|
2018-10-02 17:46:38 +01:00
|
|
|
const ghostBookshelf = require('./base');
|
|
|
|
const {Role} = require('./role');
|
|
|
|
|
|
|
|
const ApiKey = ghostBookshelf.Model.extend({
|
|
|
|
tableName: 'api_keys',
|
|
|
|
|
|
|
|
defaults() {
|
2022-05-06 17:53:10 +01:00
|
|
|
const secret = security.secret.create(this.get('type'));
|
2018-10-02 17:46:38 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
secret
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
role() {
|
|
|
|
return this.belongsTo('Role');
|
|
|
|
},
|
|
|
|
|
|
|
|
integration() {
|
|
|
|
return this.belongsTo('Integration');
|
|
|
|
},
|
|
|
|
|
2020-10-23 13:01:14 +02:00
|
|
|
user() {
|
|
|
|
return this.belongsTo('User');
|
|
|
|
},
|
|
|
|
|
2019-08-02 17:28:02 +08:00
|
|
|
format(attrs) {
|
|
|
|
return omit(attrs, 'role');
|
|
|
|
},
|
|
|
|
|
2018-10-14 16:54:10 +07:00
|
|
|
onSaving(model, attrs, options) {
|
2018-10-02 17:46:38 +01:00
|
|
|
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') {
|
2019-08-02 17:28:02 +08:00
|
|
|
return Role.findOne({name: attrs.role || 'Admin Integration'}, Object.assign({}, options, {columns: ['id']}))
|
2018-10-02 17:46:38 +01:00
|
|
|
.then((role) => {
|
|
|
|
this.set('role_id', role.get('id'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get('type') === 'content') {
|
|
|
|
this.set('role_id', null);
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 23:36:21 +05:30
|
|
|
},
|
2021-08-25 12:44:34 +02:00
|
|
|
onUpdated(model, options) {
|
2020-05-05 23:36:21 +05:30
|
|
|
if (this.previous('secret') !== this.get('secret')) {
|
2021-06-16 12:18:58 +01:00
|
|
|
this.addAction(model, 'refreshed', options);
|
2020-05-05 23:36:21 +05:30
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
2018-10-02 17:46:38 +01:00
|
|
|
}
|
2018-10-05 15:51:13 +07:00
|
|
|
}, {
|
|
|
|
refreshSecret(data, options) {
|
2022-05-06 17:53:10 +01:00
|
|
|
const secret = security.secret.create(data.type);
|
2018-10-05 15:51:13 +07:00
|
|
|
return this.edit(Object.assign({}, data, {secret}), options);
|
|
|
|
}
|
2018-10-02 17:46:38 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const ApiKeys = ghostBookshelf.Collection.extend({
|
|
|
|
model: ApiKey
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
ApiKey: ghostBookshelf.model('ApiKey', ApiKey),
|
|
|
|
ApiKeys: ghostBookshelf.collection('ApiKeys', ApiKeys)
|
|
|
|
};
|