Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVE] Add rooms property in user object, if the user has the permission, with rooms roles #12105

Merged
merged 5 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/rocketchat-api/server/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ RocketChat.API.v1.addRoute('users.getPresence', { authRequired: true }, {
RocketChat.API.v1.addRoute('users.info', { authRequired: true }, {
get() {
const { username } = this.getUserFromParams();

const { fields } = this.parseJsonQuery();
let user = {};
let result;
Meteor.runAsUser(this.userId, () => {
result = Meteor.call('getFullUserData', { username, limit: 1 });
Expand All @@ -123,8 +124,24 @@ RocketChat.API.v1.addRoute('users.info', { authRequired: true }, {
return RocketChat.API.v1.failure(`Failed to get the user data for the userId of "${ username }".`);
}

user = result[0];
if (fields.userRooms === 1 && RocketChat.authz.hasPermission(this.userId, 'view-other-user-channels')) {
user.rooms = RocketChat.models.Subscriptions.findByUserId(this.userId, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will add a lot of data to the response.. can we include this field only when requested (like with a fields param) ?

fields: {
rid: 1,
name: 1,
t: 1,
roles: 1,
},
sort: {
t: 1,
name: 1,
},
}).fetch();
}

return RocketChat.API.v1.success({
user: result[0],
user,
});
},
});
Expand Down
29 changes: 29 additions & 0 deletions tests/end-to-end/api/01-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,35 @@ describe('[Users]', function() {
})
.end(done);
});
it('should return "rooms" property when user request it and the user has the necessary permission (admin, "view-other-user-channels")', (done) => {
request.get(api('users.info'))
.set(credentials)
.query({
userId: targetUser._id,
fields: JSON.stringify({ userRooms: 1 }),
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.nested.property('user.rooms').and.to.be.an('array');
})
.end(done);
});
it('should NOT return "rooms" property when user NOT request it but the user has the necessary permission (admin, "view-other-user-channels")', (done) => {
request.get(api('users.info'))
.set(credentials)
.query({
userId: targetUser._id,
})
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.not.have.nested.property('user.rooms');
})
.end(done);
});
});

describe('[/users.getPresence]', () => {
Expand Down