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

[FIX] Added helper for testing if the current user matches the params #6845

Merged
merged 1 commit into from
May 2, 2017
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
1 change: 1 addition & 0 deletions packages/rocketchat-api/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Package.onUse(function(api) {
//Register v1 helpers
api.addFiles('server/v1/helpers/getPaginationItems.js', 'server');
api.addFiles('server/v1/helpers/getUserFromParams.js', 'server');
api.addFiles('server/v1/helpers/isUserFromParams.js', 'server');
api.addFiles('server/v1/helpers/parseJsonQuery.js', 'server');
api.addFiles('server/v1/helpers/getLoggedInUser.js', 'server');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//Convience method, almost need to turn it into a middleware of sorts
//Convenience method, almost need to turn it into a middleware of sorts
RocketChat.API.v1.helperMethods.set('getUserFromParams', function _getUserFromParams() {
const doesntExist = { _doesntExist: true };
let user;
Expand Down
5 changes: 5 additions & 0 deletions packages/rocketchat-api/server/v1/helpers/isUserFromParams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RocketChat.API.v1.helperMethods.set('isUserFromParams', function _isUserFromParams() {
return (this.queryParams.userId && this.userId === this.queryParams.userId) ||
(this.queryParams.username && this.user.username === this.queryParams.username) ||
(this.queryParams.user && this.user.username === this.queryParams.user);
});
27 changes: 14 additions & 13 deletions packages/rocketchat-api/server/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,19 @@ RocketChat.API.v1.addRoute('users.getAvatar', { authRequired: false }, {

RocketChat.API.v1.addRoute('users.getPresence', { authRequired: true }, {
get() {
//BLAHHHHHHHHHH :'(
Copy link
Contributor

Choose a reason for hiding this comment

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

Ahhhhhh so nice to have this removed ;)

if ((this.queryParams.userId && this.userId !== this.queryParams.userId) || (this.queryParams.username && this.user.username !== this.queryParams.username) || (this.queryParams.user && this.user.username !== this.queryParams.user)) {
const user = this.getUserFromParams();

if (this.isUserFromParams()) {
const user = RocketChat.models.Users.findOneById(this.userId);
return RocketChat.API.v1.success({
presence: user.status
presence: user.status,
connectionStatus: user.statusConnection,
lastLogin: user.lastLogin
});
}

const user = RocketChat.models.Users.findOneById(this.userId);
const user = this.getUserFromParams();

return RocketChat.API.v1.success({
presence: user.status,
connectionStatus: user.statusConnection,
lastLogin: user.lastLogin
presence: user.status
});
}
});
Expand Down Expand Up @@ -185,17 +184,19 @@ RocketChat.API.v1.addRoute('users.resetAvatar', { authRequired: true }, {
}
});

//TODO: Make this route work with support for usernames
RocketChat.API.v1.addRoute('users.setAvatar', { authRequired: true }, {
post() {
check(this.bodyParams, { avatarUrl: Match.Maybe(String), userId: Match.Maybe(String) });

if (typeof this.bodyParams.userId !== 'undefined' && this.userId !== this.bodyParams.userId && !RocketChat.authz.hasPermission(this.userId, 'edit-other-user-info')) {
let user;
if (this.isUserFromParams()) {
user = Meteor.users.findOne(this.userId);
} else if (RocketChat.authz.hasPermission(this.userId, 'edit-other-user-info')) {
user = this.getUserFromParams();
} else {
return RocketChat.API.v1.unauthorized();
}

const user = Meteor.users.findOne(this.bodyParams.userId ? this.bodyParams.userId : this.userId);

if (this.bodyParams.avatarUrl) {
RocketChat.setUserAvatar(user, this.bodyParams.avatarUrl, '', 'url');
} else {
Expand Down