Skip to content

Commit

Permalink
Refactor middleware to include current time as argument in after-hour…
Browse files Browse the repository at this point in the history
…s access denial message
  • Loading branch information
waveyboym committed Oct 13, 2024
1 parent e5e8b2b commit 0f75088
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 8 additions & 1 deletion occupi-backend/pkg/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,15 @@ func LimitRequestBodySize(maxSize int64) gin.HandlerFunc {
}

// block endpoint on weekends and after hours that is only allow access between Mon - Fri 08:00 - 17:00
func BlockAfterHours(now time.Time) gin.HandlerFunc {
func BlockAfterHours(arg ...time.Time) gin.HandlerFunc {
return func(ctx *gin.Context) {
var now time.Time
if len(arg) > 0 {
now = arg[0]
} else {
now = time.Now().In(time.Local)
}

// Check if the current time is outside working hours (08:00 - 17:00)
if now.Hour() < 8 || now.Hour() >= 17 {
ctx.JSON(http.StatusForbidden,
Expand Down
4 changes: 1 addition & 3 deletions occupi-backend/pkg/router/router.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package router

import (
"time"

"github.com/COS301-SE-2024/occupi/occupi-backend/pkg/constants"
"github.com/COS301-SE-2024/occupi/occupi-backend/pkg/handlers"
"github.com/COS301-SE-2024/occupi/occupi-backend/pkg/middleware"
Expand Down Expand Up @@ -60,7 +58,7 @@ func OccupiRouter(router *gin.Engine, appsession *models.AppSession) {
api.DELETE("/delete-room-image", middleware.ProtectedRoute, func(ctx *gin.Context) { middleware.VerifyMobileUser(ctx, appsession) }, middleware.AdminRoute, func(ctx *gin.Context) { handlers.DeleteRoomImage(ctx, appsession) })
api.PUT("/add-room", middleware.ProtectedRoute, func(ctx *gin.Context) { middleware.VerifyMobileUser(ctx, appsession) }, middleware.AdminRoute, func(ctx *gin.Context) { handlers.AddRoom(ctx, appsession) })
api.GET("/available-slots", middleware.ProtectedRoute, func(ctx *gin.Context) { middleware.VerifyMobileUser(ctx, appsession) }, func(ctx *gin.Context) { handlers.GetAvailableSlots(ctx, appsession) })
api.PUT("/toggle-onsite", middleware.ProtectedRoute, func(ctx *gin.Context) { middleware.VerifyMobileUser(ctx, appsession) }, middleware.BlockAfterHours(time.Now().In(time.Local)), func(ctx *gin.Context) { handlers.ToggleOnsite(ctx, appsession) })
api.PUT("/toggle-onsite", middleware.ProtectedRoute, func(ctx *gin.Context) { middleware.VerifyMobileUser(ctx, appsession) }, middleware.BlockAfterHours(), func(ctx *gin.Context) { handlers.ToggleOnsite(ctx, appsession) })
api.POST("/create-user", middleware.ProtectedRoute, func(ctx *gin.Context) { middleware.VerifyMobileUser(ctx, appsession) }, middleware.AdminRoute, func(ctx *gin.Context) { handlers.CreateUser(ctx, appsession) })
api.GET("/get-ip-info", middleware.ProtectedRoute, func(ctx *gin.Context) { middleware.VerifyMobileUser(ctx, appsession) }, middleware.AdminRoute, func(ctx *gin.Context) { handlers.GetIPInfo(ctx, appsession) })
api.POST("/add-ip", middleware.ProtectedRoute, func(ctx *gin.Context) { middleware.VerifyMobileUser(ctx, appsession) }, middleware.AdminRoute, func(ctx *gin.Context) { handlers.AddIP(ctx, appsession) })
Expand Down

0 comments on commit 0f75088

Please sign in to comment.