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 3 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
19 changes: 18 additions & 1 deletion packages/rocketchat-api/server/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ RocketChat.API.v1.addRoute('users.getPresence', { authRequired: true }, {
RocketChat.API.v1.addRoute('users.info', { authRequired: true }, {
get() {
const { username } = this.getUserFromParams();
let user = {};

let result;
Meteor.runAsUser(this.userId, () => {
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 (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
14 changes: 14 additions & 0 deletions tests/end-to-end/api/01-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ describe('[Users]', function() {
})
.end(done);
});
it('should return "rooms" property when 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.have.nested.property('user.rooms').and.to.be.an('array');
})
.end(done);
});
});

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