Skip to content

Commit 841a9d0

Browse files
authored
Merge pull request #7 from KunalKumar-1/ctx
auth: added context for auth check if the user has permission
2 parents 057c3ce + 9f893e9 commit 841a9d0

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-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: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ func (app *application) createEvent(c *gin.Context) {
2222
return
2323
}
2424

25+
user := app.GetUserFromContext(c)
26+
event.OwnerId = user.Id
27+
2528
// Insert into db
2629
err := app.models.Events.Insert(&event)
2730

@@ -85,6 +88,7 @@ func (app *application) updateEvent(c *gin.Context) {
8588
return
8689
}
8790

91+
user := app.GetUserFromContext(c) // get user from context
8892
existingEvent, err := app.models.Events.Get(id)
8993

9094
if err != nil {
@@ -101,10 +105,17 @@ func (app *application) updateEvent(c *gin.Context) {
101105
return
102106
}
103107

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

106117
fmt.Println("Existing Event:", existingEvent)
107-
fmt.Println("upadtedEvent:", updatedEvent)
118+
fmt.Println("UpadtedEvent:", updatedEvent)
108119

109120
if err := c.ShouldBindJSON(updatedEvent); err != nil {
110121
c.JSON(http.StatusBadGateway, gin.H{
@@ -136,13 +147,35 @@ func (app *application) deleteEvent(c *gin.Context) {
136147
return
137148
}
138149

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

145177
c.JSON(http.StatusNoContent, nil)
178+
146179
}
147180

148181
func (app *application) addAttendeeToEvent(c *gin.Context) {
@@ -190,6 +223,15 @@ func (app *application) addAttendeeToEvent(c *gin.Context) {
190223
return
191224
}
192225

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

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