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] Groups endpoints permission validations #13994

Merged
merged 5 commits into from
Apr 5, 2019
Merged
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
53 changes: 40 additions & 13 deletions app/api/server/v1/groups.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,59 @@
import _ from 'underscore';

import { Meteor } from 'meteor/meteor';
import { Subscriptions, Rooms, Messages, Uploads, Integrations, Users } from '../../../models';
import { hasPermission } from '../../../authorization';
import { composeMessageObjectWithUser } from '../../../utils';

import { Subscriptions, Rooms, Messages, Uploads, Integrations, Users } from '../../../models/server';
import { hasPermission, canAccessRoom } from '../../../authorization/server';
import { composeMessageObjectWithUser } from '../../../utils/server';

import { API } from '../api';
import _ from 'underscore';

// Returns the private group subscription IF found otherwise it will return the failure of why it didn't. Check the `statusCode` property
function findPrivateGroupByIdOrName({ params, userId, checkedArchived = true }) {
if ((!params.roomId || !params.roomId.trim()) && (!params.roomName || !params.roomName.trim())) {
throw new Meteor.Error('error-room-param-not-provided', 'The parameter "roomId" or "roomName" is required');
}

let roomSub;
if (params.roomId) {
roomSub = Subscriptions.findOneByRoomIdAndUserId(params.roomId, userId);
} else if (params.roomName) {
roomSub = Subscriptions.findOneByRoomNameAndUserId(params.roomName, userId);
const roomOptions = {
fields: {
t: 1,
ro: 1,
name: 1,
fname: 1,
prid: 1,
archived: 1,
},
};
const room = params.roomId ?
Rooms.findOneById(params.roomId, roomOptions) :
Rooms.findOneByName(params.roomName, roomOptions);

if (!room || room.t !== 'p') {
throw new Meteor.Error('error-room-not-found', 'The required "roomId" or "roomName" param provided does not match any group');
}

if (!roomSub || roomSub.t !== 'p') {
const user = Users.findOneById(userId, { fields: { username: 1 } });

if (!canAccessRoom(room, user)) {
throw new Meteor.Error('error-room-not-found', 'The required "roomId" or "roomName" param provided does not match any group');
}

if (checkedArchived && roomSub.archived) {
throw new Meteor.Error('error-room-archived', `The private group, ${ roomSub.name }, is archived`);
// discussions have their names saved on `fname` property
const roomName = room.prid ? room.fname : room.name;

if (checkedArchived && room.archived) {
throw new Meteor.Error('error-room-archived', `The private group, ${ roomName }, is archived`);
}

return roomSub;
const sub = Subscriptions.findOneByRoomIdAndUserId(room._id, userId, { fields: { open: 1 } });

return {
rid: room._id,
open: sub && sub.open,
ro: room.ro,
t: room.t,
name: roomName,
};
}

API.v1.addRoute('groups.addAll', { authRequired: true }, {
Expand Down