Skip to content

Commit f801344

Browse files
committed
Only authorized user can edit/delete a campground in YelpCamp project
1 parent 1681ba3 commit f801344

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

YelpCamp/routes/campgrounds.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,14 @@ router.post("/", isLoggedIn, function(req, res) {
6262
});
6363

6464
// EDIT Campground route
65-
router.get("/:id/edit", function(req, res) {
65+
router.get("/:id/edit", checkCampgroundOwnership, function(req, res) {
6666
Campground.findById(req.params.id, function(err, foundCampground) {
67-
if(err) {
68-
res.redirect("/campgrounds");
69-
} else {
70-
res.render("campgrounds/edit", {campground: foundCampground});
71-
}
67+
res.render("campgrounds/edit", {campground: foundCampground});
7268
});
7369
});
7470

7571
// UPDATE Campground route
76-
router.put("/:id", function(req, res) {
72+
router.put("/:id", checkCampgroundOwnership, function(req, res) {
7773
// find and update the correct campground
7874
Campground.findByIdAndUpdate(req.params.id, req.body.campground, function(err, updatedCamground) {
7975
if(err) {
@@ -86,7 +82,7 @@ router.put("/:id", function(req, res) {
8682
});
8783

8884
// DESTROY Campground Route
89-
router.delete("/:id/", function(req, res) {
85+
router.delete("/:id/", checkCampgroundOwnership, function(req, res) {
9086
Campground.findByIdAndRemove(req.params.id, function(err) {
9187
if(err) {
9288
res.redirect("/campgrounds");
@@ -96,7 +92,27 @@ router.delete("/:id/", function(req, res) {
9692
});
9793
});
9894

99-
// middleware function to check if user is logged in
95+
// middleware
96+
function checkCampgroundOwnership(req, res, next) {
97+
if(req.isAuthenticated()) {
98+
Campground.findById(req.params.id, function(err, foundCampground) {
99+
if(err) {
100+
res.redirect("/campgrounds");
101+
} else {
102+
// does user own the camground?
103+
if(foundCampground.author.id.equals(req.user._id)) {
104+
next();
105+
} else {
106+
res.redirect("back");
107+
}
108+
}
109+
});
110+
} else {
111+
res.redirect("back");
112+
}
113+
}
114+
115+
// middleware
100116
function isLoggedIn(req, res, next){
101117
if(req.isAuthenticated()){
102118
return next();

YelpCamp/views/campgrounds/show.ejs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
<p>
2121
<em>Submitted By <%= campground.author.username%> </em>
2222
</p>
23-
<a class="btn btn-warning" href="/campgrounds/<%=campground._id%>/edit">Edit</a>
24-
<form id="delete-form" action="/campgrounds/<%=campground._id%>?_method=DELETE" method="POST">
25-
<button class="btn btn-danger">Delete</button>
26-
</form>
23+
<% if (currentUser && campground.author.id.equals(currentUser._id)) { %>
24+
<a class="btn btn-warning" href="/campgrounds/<%=campground._id%>/edit">Edit</a>
25+
<form id="delete-form" action="/campgrounds/<%=campground._id%>?_method=DELETE" method="POST">
26+
<button class="btn btn-danger">Delete</button>
27+
</form>
28+
<% } %>
29+
2730
</div>
2831
</div>
2932
<div class="well">

0 commit comments

Comments
 (0)