diff --git a/core/server/data/migrations/versions/3.39/01-add-user-id-to-api-keys-table.js b/core/server/data/migrations/versions/3.39/01-add-user-id-to-api-keys-table.js new file mode 100644 index 0000000000..6b58a27bdf --- /dev/null +++ b/core/server/data/migrations/versions/3.39/01-add-user-id-to-api-keys-table.js @@ -0,0 +1,7 @@ +const {createAddColumnMigration} = require('../../utils'); + +module.exports = createAddColumnMigration('api_keys', 'user_id', { + type: 'string', + maxlength: 24, + nullable: true +}); diff --git a/core/server/data/schema/schema.js b/core/server/data/schema/schema.js index d9bf13a936..a752897fb9 100644 --- a/core/server/data/schema/schema.js +++ b/core/server/data/schema/schema.js @@ -355,6 +355,7 @@ module.exports = { role_id: {type: 'string', maxlength: 24, nullable: true}, // integration_id is nullable to allow "internal" API keys that don't show in the UI integration_id: {type: 'string', maxlength: 24, nullable: true}, + user_id: {type: 'string', maxlength: 24, nullable: true}, last_seen_at: {type: 'dateTime', nullable: true}, last_seen_version: {type: 'string', maxlength: 50, nullable: true}, created_at: {type: 'dateTime', nullable: false}, diff --git a/core/server/models/api-key.js b/core/server/models/api-key.js index 1a7703afab..f412e81722 100644 --- a/core/server/models/api-key.js +++ b/core/server/models/api-key.js @@ -92,6 +92,10 @@ const ApiKey = ghostBookshelf.Model.extend({ return this.belongsTo('Integration'); }, + user() { + return this.belongsTo('User'); + }, + format(attrs) { return omit(attrs, 'role'); }, diff --git a/core/server/models/user.js b/core/server/models/user.js index 0de6969119..e962fdcfe4 100644 --- a/core/server/models/user.js +++ b/core/server/models/user.js @@ -249,6 +249,10 @@ User = ghostBookshelf.Model.extend({ return this.belongsToMany('Permission'); }, + apiKeys() { + return this.hasMany('ApiKey', 'user_id'); + }, + hasRole: function hasRole(roleName) { const roles = this.related('roles'); diff --git a/test/unit/data/schema/integrity_spec.js b/test/unit/data/schema/integrity_spec.js index f7e086253e..69b20da0f1 100644 --- a/test/unit/data/schema/integrity_spec.js +++ b/test/unit/data/schema/integrity_spec.js @@ -32,7 +32,7 @@ const defaultSettings = require('../../../../core/server/data/schema/default-set */ describe('DB version integrity', function () { // Only these variables should need updating - const currentSchemaHash = '102b04bbd38cd2451fbf0957ffc35b30'; + const currentSchemaHash = 'cd8820283c865acf610f97252addfa99'; const currentFixturesHash = 'd46d696c94d03e41a5903500547fea77'; const currentSettingsHash = 'b7c0c2c6a4c61561dfefe642470d30f8'; const currentRoutesHash = '3d180d52c663d173a6be791ef411ed01';