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

Added check if requester is admin functionality in 2 chat controllers. #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions backend/controllers/chatControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,15 @@ const renameGroup = asyncHandler(async (req, res) => {
const removeFromGroup = asyncHandler(async (req, res) => {
const { chatId, userId } = req.body;

// check if the requester is admin
const groupChatExists = await Chat.findOne({ _id: chatId }); // Find if group chat exists.

if (!groupChatExists) { // Error: No group chat with the given id exists.
return res.status(400).json({ error: "Invalid group chat Id." });
}

if (!groupChatExists.groupAdmin.equals(req.user._id)) { // Error: Requester is not the admin of this group.
return res.status(401).json({ error: "Only the admin can remove people from the group." });
}

const removed = await Chat.findByIdAndUpdate(
chatId,
Expand Down Expand Up @@ -171,7 +179,15 @@ const removeFromGroup = asyncHandler(async (req, res) => {
const addToGroup = asyncHandler(async (req, res) => {
const { chatId, userId } = req.body;

// check if the requester is admin
const groupChatExists = await Chat.findOne({ _id: chatId }); // Find if group chat exists.

if (!groupChatExists) { // Error: No group chat with the given id exists.
return res.status(400).json({ error: "Invalid group chat Id." });
}

if (!groupChatExists.groupAdmin.equals(req.user._id)) { // Error: Requester is not the admin of this group.
return res.status(401).json({ error: "Only the admin can add people to the group." });
}

const added = await Chat.findByIdAndUpdate(
chatId,
Expand Down