0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

🐛Deleted associated roles_users rows when user is destroyed (#10243)

closes #8843
This commit is contained in:
Dexter Leng 2019-01-21 19:36:13 +08:00 committed by Katharina Irrgang
parent a927aecc3f
commit da71b61bcf
2 changed files with 51 additions and 4 deletions

View file

@ -37,6 +37,18 @@ User = ghostBookshelf.Model.extend({
ghostBookshelf.Model.prototype.emitChange.bind(this)(this, eventToTrigger, options); ghostBookshelf.Model.prototype.emitChange.bind(this)(this, eventToTrigger, options);
}, },
/**
* @TODO:
*
* The user model does not use bookshelf-relations yet.
* Therefor we have to remove the relations manually.
*/
onDestroying(model, options) {
return (options.transacting || ghostBookshelf.knex)('roles_users')
.where('user_id', model.id)
.del();
},
onDestroyed: function onDestroyed(model, options) { onDestroyed: function onDestroyed(model, options) {
if (_.includes(activeStates, model.previous('status'))) { if (_.includes(activeStates, model.previous('status'))) {
model.emitChange('deactivated', options); model.emitChange('deactivated', options);
@ -556,6 +568,23 @@ User = ghostBookshelf.Model.extend({
}); });
}, },
destroy: function destroy(unfilteredOptions) {
const options = this.filterOptions(unfilteredOptions, 'destroy', {extraAllowedProperties: ['id']});
const destroyUser = () => {
return ghostBookshelf.Model.destroy.call(this, options);
};
if (!options.transacting) {
return ghostBookshelf.transaction((transacting) => {
options.transacting = transacting;
return destroyUser();
});
}
return destroyUser();
},
/** /**
* We override the owner! * We override the owner!
* Owner already has a slug -> force setting a new one by setting slug to null * Owner already has a slug -> force setting a new one by setting slug to null

View file

@ -7,6 +7,7 @@ const ObjectId = require('bson-objectid');
const testUtils = require('../../../../utils'); const testUtils = require('../../../../utils');
const localUtils = require('./utils'); const localUtils = require('./utils');
const config = require('../../../../../../core/server/config'); const config = require('../../../../../../core/server/config');
const db = require('../../../../../../core/server/data/db');
const models = require('../../../../../../core/server/models'); const models = require('../../../../../../core/server/models');
const ghost = testUtils.startGhost; const ghost = testUtils.startGhost;
let request; let request;
@ -332,32 +333,49 @@ describe('User API V2', function () {
describe('Destroy', function () { describe('Destroy', function () {
it('[success] Destroy active user', function () { it('[success] Destroy active user', function () {
const userId = testUtils.existingData.users[1].id;
return request return request
.get(localUtils.API.getApiQuery(`posts/?filter=author_id:${testUtils.existingData.users[1].id}`)) .get(localUtils.API.getApiQuery(`posts/?filter=author_id:${userId}`))
.set('Origin', config.get('url')) .set('Origin', config.get('url'))
.expect(200) .expect(200)
.then((res) => { .then((res) => {
res.body.posts.length.should.eql(7); res.body.posts.length.should.eql(7);
return request return request
.delete(localUtils.API.getApiQuery(`users/${testUtils.existingData.users[1].id}`)) .delete(localUtils.API.getApiQuery(`users/${userId}`))
.set('Origin', config.get('url')) .set('Origin', config.get('url'))
.expect(204); .expect(204);
}) })
.then(() => { .then(() => {
return request return request
.get(localUtils.API.getApiQuery(`users/${testUtils.existingData.users[1].id}/`)) .get(localUtils.API.getApiQuery(`users/${userId}/`))
.set('Origin', config.get('url')) .set('Origin', config.get('url'))
.expect(404); .expect(404);
}) })
.then(() => { .then(() => {
return request return request
.get(localUtils.API.getApiQuery(`posts/?filter=author_id:${testUtils.existingData.users[1].id}`)) .get(localUtils.API.getApiQuery(`posts/?filter=author_id:${userId}`))
.set('Origin', config.get('url')) .set('Origin', config.get('url'))
.expect(200); .expect(200);
}) })
.then((res) => { .then((res) => {
res.body.posts.length.should.eql(0); res.body.posts.length.should.eql(0);
return db.knex('roles_users')
.where({
user_id: userId
})
.select();
})
.then((models) => {
models.length.should.eql(0);
return db.knex('roles_users')
.select();
})
.then((models) => {
models.length.should.greaterThan(0);
}); });
}); });