Skip to content

Commit 57e8ccd

Browse files
committed
auth: added context for auth check if the user has permission
1 parent 057c3ce commit 57e8ccd

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

cmd/api/contex.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/kunalkumar-1/Evently/internals/database"
6+
)
7+
8+
func(app *application) GetUserFromContext(c *gin.Context) *database.User {
9+
contextUser, exist := c.Get("user")
10+
if !exist {
11+
return &database.User{}
12+
}
13+
user, ok := contextUser.(*database.User)
14+
if !ok {
15+
return &database.User{}
16+
}
17+
return user
18+
}

cmd/api/events.go

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func (app *application) createEvent(c *gin.Context) {
2222
return
2323
}
2424

25+
user := app.GetUserFromContext(c)
26+
event.OwnerId = user.Id
27+
28+
2529
// Insert into db
2630
err := app.models.Events.Insert(&event)
2731

@@ -85,6 +89,7 @@ func (app *application) updateEvent(c *gin.Context) {
8589
return
8690
}
8791

92+
user := app.GetUserFromContext(c) // get user from context
8893
existingEvent, err := app.models.Events.Get(id)
8994

9095
if err != nil {
@@ -101,10 +106,17 @@ func (app *application) updateEvent(c *gin.Context) {
101106
return
102107
}
103108

109+
if existingEvent.OwnerId != user.Id {
110+
c.JSON(http.StatusForbidden, gin.H{
111+
"erorr": "You are not authorized to update this event",
112+
})
113+
return
114+
}
115+
104116
updatedEvent := &database.Event{}
105117

106118
fmt.Println("Existing Event:", existingEvent)
107-
fmt.Println("upadtedEvent:", updatedEvent)
119+
fmt.Println("UpadtedEvent:", updatedEvent)
108120

109121
if err := c.ShouldBindJSON(updatedEvent); err != nil {
110122
c.JSON(http.StatusBadGateway, gin.H{
@@ -136,13 +148,35 @@ func (app *application) deleteEvent(c *gin.Context) {
136148
return
137149
}
138150

151+
user := app.GetUserFromContext(c) // get user from context
152+
existingEvent, err := app.models.Events.Get(id)
153+
if err != nil {
154+
c.JSON(http.StatusInternalServerError, gin.H{
155+
"error": "Failed to retireve event",
156+
})
157+
}
158+
if existingEvent == nil {
159+
c.JSON(http.StatusNotFound, gin.H{
160+
"erorr": "Event not found",
161+
})
162+
return
163+
}
164+
165+
if existingEvent.OwnerId != user.Id {
166+
c.JSON(http.StatusForbidden, gin.H{
167+
"erorr": "You are not authorized to delete this event",
168+
})
169+
return
170+
}
171+
139172
if err := app.models.Events.Delete(id); err != nil {
140173
c.JSON(http.StatusInternalServerError, gin.H{
141174
"error": "Failed to delete event",
142175
})
143176
}
144177

145178
c.JSON(http.StatusNoContent, nil)
179+
146180
}
147181

148182
func (app *application) addAttendeeToEvent(c *gin.Context) {
@@ -190,6 +224,15 @@ func (app *application) addAttendeeToEvent(c *gin.Context) {
190224
return
191225
}
192226

227+
user := app.GetUserFromContext(c)
228+
229+
if event.OwnerId != user.Id {
230+
c.JSON(http.StatusForbidden, gin.H{
231+
"erorr": "You are not authorized to add attendees to this event",
232+
})
233+
return
234+
}
235+
193236
existingAttendee, err := app.models.Attendees.GetByEventAndAttendee(event.Id, userToAdd.Id) //get user by id
194237
if err != nil { // if error in getting user
195238
c.JSON(http.StatusInternalServerError, gin.H{
@@ -255,6 +298,28 @@ func (app *application) deleteAttendeeFromEvent(c *gin.Context) {
255298
})
256299
}
257300

301+
event, err := app.models.Events.Get(id)
302+
if err != nil {
303+
c.JSON(http.StatusInternalServerError, gin.H{
304+
"error": "Failed to retireve event",
305+
})
306+
return
307+
}
308+
if event == nil {
309+
c.JSON(http.StatusNotFound, gin.H{
310+
"erorr": "Event not found",
311+
})
312+
return
313+
}
314+
315+
user := app.GetUserFromContext(c)
316+
if event.OwnerId != user.Id {
317+
c.JSON(http.StatusForbidden, gin.H{
318+
"error": "You are not authorized to delete attendees from this event",
319+
})
320+
return
321+
}
322+
258323
err = app.models.Attendees.Delete(userId, id)
259324
if err != nil {
260325
c.JSON(http.StatusInternalServerError, gin.H{

internals/database/event.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type EventModel struct {
1313

1414
type Event struct {
1515
Id int `json:"id"`
16-
OwnerId int `json:"ownerId" binding:"required"`
16+
OwnerId int `json:"ownerId"`
1717
Name string `json:"name" binding:"required,min=3,max=50"`
1818
Description string `json:"description" binding:"required,min=3,max=500"`
1919
Date string `json:"date" binding:"required"`

0 commit comments

Comments
 (0)