Skip to content

Commit

Permalink
Expand authorization validation for Project CRUD operations
Browse files Browse the repository at this point in the history
  • Loading branch information
nclairesays committed Mar 31, 2020
1 parent beb16a7 commit 0241a87
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 157 deletions.
38 changes: 0 additions & 38 deletions app/controllers/playground.controller.js

This file was deleted.

21 changes: 20 additions & 1 deletion app/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const getById = async (req, res) => {

const post = async (req, res) => {
try {
if (!isAuthorizedUser(req) && !isAdmin(req)) {
res.status(403).send("You can only create your own projects.");
}

const response = await projectService.post(req.body);
res.status(201).json(response);
} catch (err) {
Expand All @@ -33,9 +37,10 @@ const post = async (req, res) => {

const put = async (req, res) => {
try {
if (req.user.id !== req.body.loginId) {
if (!isAuthorizedUser(req) && !isAdmin(req)) {
res.status(403).send("You can only make changes to your own projects.");
}

await projectService.put(req.body);
res.sendStatus(200);
} catch (err) {
Expand All @@ -45,13 +50,27 @@ const put = async (req, res) => {

const del = async (req, res) => {
try {
if (!isAuthorizedUser(req) && !isAdmin(req)) {
res.status(403).send("You can only delete your own projects.");
}

await projectService.del(req.user.id, req.params.id);
res.sendStatus(200);
} catch (err) {
res.status(500).send(err);
}
};

// HELPER FUNCTIONS:
const isAdmin = req => (req.user.isAdmin ? true : false);

const isAuthorizedUser = req => {
const loginIdOfCurrentUser = req.user.id;
const loginIdInRequestBody = req.body.loginId;

return loginIdOfCurrentUser === loginIdInRequestBody ? true : false;
};

module.exports = {
getAll,
getById,
Expand Down
13 changes: 0 additions & 13 deletions app/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const calculationRoutes = require("./calculation.routes");
const ruleRoutes = require("./rule.routes");
const accountRoutes = require("./account.routes");
const faqRoutes = require("./faq.routes");
const playgroundRoutes = require("./playground.routes");
const projectRoutes = require("./project.routes");

module.exports = router;
Expand All @@ -14,15 +13,3 @@ router.use("/calculations", calculationRoutes);
router.use("/projects", projectRoutes);
router.use("/rules", ruleRoutes);
router.use("/faq", faqRoutes);

router.use("/playground", playgroundRoutes);

// router.use(authChecker);

//function authChecker(req, res, next) {
// if (req.session.auth || req.path === "/auth") {
// next();
// } else {
// res.redirect("/auth");
// }
//}
32 changes: 0 additions & 32 deletions app/routes/playground.routes.js

This file was deleted.

2 changes: 1 addition & 1 deletion app/routes/project.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ router.get("/:id", jwtSession.validateUser, projectController.getById);
router.get("/", jwtSession.validateUser, projectController.getAll);
router.post("/", jwtSession.validateUser, projectController.post);
router.put("/:id", jwtSession.validateUser, projectController.put);
router.delete("/", jwtSession.validateUser, projectController.del);
router.delete("/:id", jwtSession.validateUser, projectController.del);
30 changes: 0 additions & 30 deletions app/services/auth.service.js

This file was deleted.

42 changes: 0 additions & 42 deletions app/services/playground.service.js

This file was deleted.

0 comments on commit 0241a87

Please sign in to comment.