mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Update user roles in store after owner transfer
Closes #3466 - Transferring the owner role is now done via a separate endpoint and not through Ember-Data. As a result the user role data needs to be updated manually. - Updated the owner endpoint to return a response body containing the updated user objects. - Updated tests.
This commit is contained in:
parent
3de308dc20
commit
baf9138b27
4 changed files with 27 additions and 8 deletions
|
@ -14,7 +14,18 @@ var TransferOwnerController = Ember.Controller.extend({
|
||||||
'id': user.get('id')
|
'id': user.get('id')
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
}).then(function () {
|
}).then(function (response) {
|
||||||
|
// manually update the roles for the users that just changed roles
|
||||||
|
// because store.pushPayload is not working with embedded relations
|
||||||
|
if (response && Ember.isArray(response.users)) {
|
||||||
|
response.users.forEach(function (userJSON) {
|
||||||
|
var user = self.store.getById('user', userJSON.id),
|
||||||
|
role = self.store.getById('role', userJSON.roles[0].id);
|
||||||
|
|
||||||
|
user.set('role', role);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
self.notifications.closePassive();
|
self.notifications.closePassive();
|
||||||
self.notifications.showSuccess('Ownership successfully transferred to ' + user.get('name'));
|
self.notifications.showSuccess('Ownership successfully transferred to ' + user.get('name'));
|
||||||
}).catch(function (error) {
|
}).catch(function (error) {
|
||||||
|
|
|
@ -296,8 +296,8 @@ users = {
|
||||||
return canThis(options.context).assign.role(ownerRole);
|
return canThis(options.context).assign.role(ownerRole);
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
return utils.checkObject(object, 'owner').then(function (checkedOwnerTransfer) {
|
return utils.checkObject(object, 'owner').then(function (checkedOwnerTransfer) {
|
||||||
return dataProvider.User.transferOwnership(checkedOwnerTransfer.owner[0], options).then(function () {
|
return dataProvider.User.transferOwnership(checkedOwnerTransfer.owner[0], options).then(function (updatedUsers) {
|
||||||
return when.resolve({owner: [{message: 'Ownership transferred successfully.'}]});
|
return when.resolve({ users: updatedUsers });
|
||||||
}).catch(function (error) {
|
}).catch(function (error) {
|
||||||
return when.reject(new errors.ValidationError(error.message));
|
return when.reject(new errors.ValidationError(error.message));
|
||||||
});
|
});
|
||||||
|
|
|
@ -757,6 +757,12 @@ User = ghostBookshelf.Model.extend({
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
// assign owner role to a new user
|
// assign owner role to a new user
|
||||||
return assignUser.roles().updatePivot({role_id: ownerRole.id});
|
return assignUser.roles().updatePivot({role_id: ownerRole.id});
|
||||||
|
}).then(function () {
|
||||||
|
return Users.forge()
|
||||||
|
.query('whereIn', 'id', [contextUser.id, assignUser.id])
|
||||||
|
.fetch({ withRelated: ['roles'] });
|
||||||
|
}).then(function (users) {
|
||||||
|
return users.toJSON({ include: ['roles'] });
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -898,11 +898,13 @@ describe('Users API', function () {
|
||||||
{id: userIdFor.admin}
|
{id: userIdFor.admin}
|
||||||
]}, context.owner
|
]}, context.owner
|
||||||
).then(function (response) {
|
).then(function (response) {
|
||||||
should.exist(response);
|
should.exist(response);
|
||||||
should.exist(response.owner);
|
response.users.should.have.length(2);
|
||||||
response.owner.should.have.length(1);
|
testUtils.API.checkResponse(response.users[0], 'user', ['roles']);
|
||||||
response.owner[0].message.should.eql('Ownership transferred successfully.');
|
testUtils.API.checkResponse(response.users[1], 'user', ['roles']);
|
||||||
done();
|
response.users[0].roles[0].id.should.equal(1);
|
||||||
|
response.users[1].roles[0].id.should.equal(4);
|
||||||
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue