Skip to content

Commit

Permalink
added permission require for creating user
Browse files Browse the repository at this point in the history
  • Loading branch information
emirhanyagci committed Jun 9, 2024
1 parent 0f7d30b commit dde3648
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions backend/controllers/userControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ const asyncHandler = require("express-async-handler");
// @route GET /users
// @access Private
exports.getAllUser = asyncHandler(async (req, res, next) => {
const user = res.user;
const isEmployee = user.roles.length === 1 && user.roles.includes("Employee");

const users = await User.find(isEmployee ? { username: user.username } : null)
const users = await User.find(
isEmployee(res.user) ? { username: res.user.username } : null
)
.select("-password")
.lean();
if (!users.length) {
Expand All @@ -27,6 +26,11 @@ exports.getAllUser = asyncHandler(async (req, res, next) => {
// @route POST /users
// @access Private
exports.createNewUser = asyncHandler(async (req, res, next) => {
if (isEmployee(res.user)) {
return res.status(401).json({
message: "Forbidden",
});
}
const { username, password, roles } = req.body;
if (!username || !password || !Array.isArray(roles) || !roles.length) {
return res.status(400).json({ message: "All field are required" });
Expand Down Expand Up @@ -127,3 +131,6 @@ exports.deleteUser = asyncHandler(async (req, res, next) => {
message: `Username ${deletedUser.username} Id ${deletedUser._id} deleted`,
});
});
function isEmployee(user) {
return user.roles.length === 1 && user.roles.includes("Employee");
}

0 comments on commit dde3648

Please sign in to comment.