🎨 take the latest attached role when importing (#8765)

refs #8756

- there was a bug in one of the last LTS releases, which produced duplicated attached roles to users
- we want to prevent that on import and take the latest created based on the autoincrement id
This commit is contained in:
Katharina Irrgang 2017-07-27 13:25:01 +04:00 committed by Kevin Ansfield
parent 7060fa5e35
commit 3462f07f58
3 changed files with 1652 additions and 1 deletions

View File

@ -30,7 +30,7 @@ class UsersImporter extends BaseImporter {
beforeImport() {
debug('beforeImport');
let self = this, role;
let self = this, role, lookup = {};
// Remove legacy field language
this.dataToImport = _.filter(this.dataToImport, function (data) {
@ -44,6 +44,19 @@ class UsersImporter extends BaseImporter {
model.status = 'locked';
});
// NOTE: sort out duplicated roles based on incremental id
_.each(this.roles_users, function (attachedRole) {
if (lookup.hasOwnProperty(attachedRole.user_id)) {
if (lookup[attachedRole.user_id].id < attachedRole.id) {
lookup[attachedRole.user_id] = attachedRole;
}
} else {
lookup[attachedRole.user_id] = attachedRole;
}
});
this.roles_users = _.toArray(lookup);
_.each(this.roles_users, function (attachedRole) {
role = _.find(self.roles, function (role) {
if (attachedRole.role_id === role.id) {

View File

@ -1604,4 +1604,58 @@ describe('Import (new test structure)', function () {
});
});
});
describe('lts: multiple roles attached', function () {
var exportData;
before(function doImport(done) {
// initialise the blog with some data
testUtils.initFixtures('roles', 'owner', 'settings').then(function () {
return testUtils.fixtures.loadExportFixture('export-lts-multiple-roles',
{lts: true}
);
}).then(function (exported) {
exportData = exported;
return dataImporter.doImport(exportData);
}).then(function () {
done();
}).catch(done);
});
after(testUtils.teardown);
it('takes the latest role attached', function (done) {
var fetchImported = Promise.join(
knex('users').select(),
knex('roles_users').select(),
knex('roles').select()
);
fetchImported
.then(function (importedData) {
should.exist(importedData);
importedData.length.should.equal(3);
var users = importedData[0],
rolesUsers = importedData[1],
roles = importedData[2];
users.length.should.equal(4);
// original owner, search the owner by slug
_.find(rolesUsers, {user_id: _.find(users, {slug: testUtils.DataGenerator.Content.users[0].slug}).id}).role_id.should.eql(_.find(roles, {name: 'Owner'}).id);
// first imported user, which was the original owner, now administator
_.find(rolesUsers, {user_id: _.find(users, {slug: exportData.data.users[0].slug}).id}).role_id.should.eql(_.find(roles, {name: 'Administrator'}).id);
// second imported users, which has two roles attached, but the last role is the owner role, now administraotr
_.find(rolesUsers, {user_id: _.find(users, {slug: exportData.data.users[1].slug}).id}).role_id.should.eql(_.find(roles, {name: 'Administrator'}).id);
// second imported users, which has two roles attached, but the last role is the editor role
_.find(rolesUsers, {user_id: _.find(users, {slug: exportData.data.users[2].slug}).id}).role_id.should.eql(_.find(roles, {name: 'Editor'}).id);
done();
}).catch(done);
});
});
});

File diff suppressed because it is too large Load Diff