Merge pull request #3480 from jaswilli/issue-3466

Update user roles in store after owner transfer
This commit is contained in:
Hannah Wolfe 2014-07-31 15:39:48 +01:00
commit 194d45fac6
4 changed files with 27 additions and 8 deletions

View File

@ -14,7 +14,18 @@ var TransferOwnerController = Ember.Controller.extend({
'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.showSuccess('Ownership successfully transferred to ' + user.get('name'));
}).catch(function (error) {

View File

@ -337,8 +337,8 @@ users = {
return canThis(options.context).assign.role(ownerRole);
}).then(function () {
return utils.checkObject(object, 'owner').then(function (checkedOwnerTransfer) {
return dataProvider.User.transferOwnership(checkedOwnerTransfer.owner[0], options).then(function () {
return when.resolve({owner: [{message: 'Ownership transferred successfully.'}]});
return dataProvider.User.transferOwnership(checkedOwnerTransfer.owner[0], options).then(function (updatedUsers) {
return when.resolve({ users: updatedUsers });
}).catch(function (error) {
return when.reject(new errors.ValidationError(error.message));
});

View File

@ -764,6 +764,12 @@ User = ghostBookshelf.Model.extend({
}).then(function () {
// assign owner role to a new user
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'] });
});
},

View File

@ -942,11 +942,13 @@ describe('Users API', function () {
{id: userIdFor.admin}
]}, context.owner
).then(function (response) {
should.exist(response);
should.exist(response.owner);
response.owner.should.have.length(1);
response.owner[0].message.should.eql('Ownership transferred successfully.');
done();
should.exist(response);
response.users.should.have.length(2);
testUtils.API.checkResponse(response.users[0], 'user', ['roles']);
testUtils.API.checkResponse(response.users[1], 'user', ['roles']);
response.users[0].roles[0].id.should.equal(1);
response.users[1].roles[0].id.should.equal(4);
done();
}).catch(done);
});