Skip to content

Commit

Permalink
Stop execution when verify admin fails
Browse files Browse the repository at this point in the history
  • Loading branch information
sondreb committed Oct 2, 2023
1 parent f1b62ba commit 3c56483
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 28 deletions.
20 changes: 16 additions & 4 deletions srv/routes/category.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ router.get("/:id", async (req, res) => {

// Add a new document to the collection
router.post("/", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
let collection = await db.collection(collectionName);
let newDocument = req.body;
Expand All @@ -71,7 +74,10 @@ router.post("/", async (req, res) => {

// Add a new document to the collection
router.put("/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
let collection = await db.collection(collectionName);

Expand Down Expand Up @@ -106,7 +112,10 @@ router.put("/:id", async (req, res) => {

// Update the post with a new comment
router.patch("/item/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

const query = { _id: MUUID.from(req.params.id) };
// const query = { _id: ObjectId(req.params.id) };

Expand All @@ -122,7 +131,10 @@ router.patch("/item/:id", async (req, res) => {

// Delete an entry
router.delete("/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
if (req.params.id.length > 32) {
const query = { _id: MUUID.from(req.params.id) };
Expand Down
21 changes: 16 additions & 5 deletions srv/routes/collection.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { verifyAdmin } from "../services/verifyAdmin.mjs";

// Get a list of 50 posts
router.get("/", async (req, res) => {

try {
let collection = await db.collection(collectionName);
let results = await collection.find({}).limit(50).toArray();
Expand All @@ -29,7 +28,10 @@ router.get("/latest", async (req, res) => {

// Add a new document to the collection
router.post("/", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
let collection = await db.collection(collectionName);
let newDocument = req.body;
Expand Down Expand Up @@ -73,7 +75,10 @@ router.get("/:id", async (req, res) => {

// Add a new document to the collection
router.put("/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
let collection = await db.collection(collectionName);

Expand Down Expand Up @@ -108,7 +113,10 @@ router.put("/:id", async (req, res) => {

// Update the post with a new comment
router.patch("/item/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

const query = { _id: MUUID.from(req.params.id) };
// const query = { _id: ObjectId(req.params.id) };

Expand All @@ -124,7 +132,10 @@ router.patch("/item/:id", async (req, res) => {

// Delete an entry
router.delete("/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

const query = { _id: MUUID.from(req.params.id) };
// const query = { _id: ObjectId(req.params.id) };

Expand Down
20 changes: 16 additions & 4 deletions srv/routes/permission.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ router.get("/:id", async (req, res) => {

// Add a new document to the collection
router.post("/", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
let collection = await db.collection(collectionName);
let newDocument = req.body;
Expand All @@ -71,7 +74,10 @@ router.post("/", async (req, res) => {

// Add a new document to the collection
router.put("/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
let collection = await db.collection(collectionName);

Expand Down Expand Up @@ -106,7 +112,10 @@ router.put("/:id", async (req, res) => {

// Update the post with a new comment
router.patch("/item/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

const query = { _id: MUUID.from(req.params.id) };
// const query = { _id: ObjectId(req.params.id) };

Expand All @@ -122,7 +131,10 @@ router.patch("/item/:id", async (req, res) => {

// Delete an entry
router.delete("/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
if (req.params.id.length > 32) {
const query = { _id: MUUID.from(req.params.id) };
Expand Down
20 changes: 16 additions & 4 deletions srv/routes/profile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ router.get("/:id", async (req, res) => {

// Add a new document to the collection
router.post("/", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
let collection = await db.collection(collectionName);
let newDocument = req.body;
Expand All @@ -71,7 +74,10 @@ router.post("/", async (req, res) => {

// Add a new document to the collection
router.put("/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
let collection = await db.collection(collectionName);

Expand Down Expand Up @@ -106,7 +112,10 @@ router.put("/:id", async (req, res) => {

// Update the post with a new comment
router.patch("/item/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

const query = { _id: MUUID.from(req.params.id) };
// const query = { _id: ObjectId(req.params.id) };

Expand All @@ -122,7 +131,10 @@ router.patch("/item/:id", async (req, res) => {

// Delete an entry
router.delete("/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
if (req.params.id.length > 32) {
const query = { _id: MUUID.from(req.params.id) };
Expand Down
20 changes: 16 additions & 4 deletions srv/routes/project.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ router.get("/:id", async (req, res) => {

// Add a new document to the collection
router.post("/", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
let collection = await db.collection(collectionName);
let newDocument = req.body;
Expand All @@ -71,7 +74,10 @@ router.post("/", async (req, res) => {

// Add a new document to the collection
router.put("/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
let collection = await db.collection(collectionName);

Expand Down Expand Up @@ -106,7 +112,10 @@ router.put("/:id", async (req, res) => {

// Update the post with a new comment
router.patch("/item/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

const query = { _id: MUUID.from(req.params.id) };
// const query = { _id: ObjectId(req.params.id) };

Expand All @@ -122,7 +131,10 @@ router.patch("/item/:id", async (req, res) => {

// Delete an entry
router.delete("/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
if (req.params.id.length > 32) {
const query = { _id: MUUID.from(req.params.id) };
Expand Down
32 changes: 25 additions & 7 deletions srv/routes/user.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const router = express.Router();
const collectionName = "user";

router.get("/", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
let collection = await db.collection(collectionName);
Expand All @@ -24,7 +26,9 @@ router.get("/", async (req, res) => {

// Fetches the root categories
router.get("/root", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

let collection = await db.collection(collectionName);
let results = await collection.aggregate([{ $project: { name: 1, icon: 1, slug: 1, parent: 1, sort: 1 } }, { $sort: { sort: 1 } }, { $limit: 50 }]).toArray();
Expand All @@ -33,7 +37,9 @@ router.get("/root", async (req, res) => {

// Fetches the latest posts
router.get("/latest", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

let collection = await db.collection(collectionName);
let results = await collection.aggregate([{ $project: { author: 1, title: 1, tags: 1, date: 1 } }, { $sort: { date: -1 } }, { $limit: 3 }]).toArray();
Expand All @@ -42,7 +48,9 @@ router.get("/latest", async (req, res) => {

// Get a single post
router.get("/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

let collection = await db.collection(collectionName);

Expand All @@ -56,7 +64,9 @@ router.get("/:id", async (req, res) => {

// Add a new document to the collection
router.post("/", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
let collection = await db.collection(collectionName);
Expand All @@ -82,6 +92,10 @@ router.post("/", async (req, res) => {
router.put("/:id", async (req, res) => {
const user = verifyAdmin(req, res);

if (!user) {
return;
}

try {
let collection = await db.collection(collectionName);

Expand Down Expand Up @@ -143,7 +157,9 @@ router.put("/:id", async (req, res) => {

// Update the post with a new comment
router.patch("/item/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

const query = { _id: MUUID.from(req.params.id) };
// const query = { _id: ObjectId(req.params.id) };
Expand All @@ -160,7 +176,9 @@ router.patch("/item/:id", async (req, res) => {

// Delete an entry
router.delete("/:id", async (req, res) => {
verifyAdmin(req, res);
if (!verifyAdmin(req, res)) {
return;
}

try {
if (req.params.id.length > 32) {
Expand Down

0 comments on commit 3c56483

Please sign in to comment.