Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions handler/fiber-middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package handler

import (
"errors"
"fmt"

"github.com/FrosTiK-SD/auth/constants"
"github.com/FrosTiK-SD/auth/controller"
"github.com/FrosTiK-SD/auth/interfaces"
"github.com/FrosTiK-SD/auth/model"
"github.com/FrosTiK-SD/auth/util"
"github.com/gofiber/fiber/v2"
)
Expand All @@ -29,9 +31,37 @@ func (h *Handler) FiberVerifyStudent(ctx *fiber.Ctx) error {
}

ctx.Locals(constants.SESSION, student)
ctx.Next()

return nil
return ctx.Next()
}

// To be used only after FiberVerifyStudent
func (h *Handler) FiberGetRoleCheckHandlerForStudent(roles ...string) func(ctx *fiber.Ctx) error {
return func(ctx *fiber.Ctx) error {
student, ok := ctx.Locals(constants.SESSION).(*model.StudentPopulated)

if !ok {
return ctx.Status(fiber.StatusForbidden).JSON(
map[string]any{
"message": constants.ERROR_ROLE_CHECK_FAILED,
"error": "Student does not exist",
},
)
}

for _, role := range roles {
if !util.CheckRoleExists(&student.GroupDetails, role) {
return ctx.Status(fiber.StatusForbidden).JSON(
map[string]any{
"message": constants.ERROR_ROLE_CHECK_FAILED,
"error": fmt.Sprintf("Student Does not have Role '%s'", role),
},
)
}
}

return ctx.Next()
}
}

func (h *RoleCheckerHandler) FiberVerifyRole(ctx *fiber.Ctx) error {
Expand Down
2 changes: 1 addition & 1 deletion handler/gin-middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (h *Handler) GinVerifyStudent(ctx *gin.Context) {
}

// To be Used only after GinVerifyStudent
func (h *Handler) GetRoleCheckHandlerForStudent(roles ...string) func(ctx *gin.Context) {
func (h *Handler) GinGetRoleCheckHandlerForStudent(roles ...string) func(ctx *gin.Context) {
return func(ctx *gin.Context) {
value, exists := ctx.Get(constants.SESSION)
student, ok := value.(*model.StudentPopulated)
Expand Down
28 changes: 14 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func main() {

student := r.Group("/api/student")
{
student.GET("", handler.GinVerifyStudent, handler.GetRoleCheckHandlerForStudent(constants.ROLE_ADMIN), handler.GetAllStudents)
student.GET("", handler.GinVerifyStudent, handler.GinGetRoleCheckHandlerForStudent(constants.ROLE_ADMIN), handler.GetAllStudents)
student.GET("/id", handler.GinVerifyStudent, handler.GetStudentById)
student.GET("/tpr/all", handler.GinVerifyStudent, handler.GetRoleCheckHandlerForStudent(constants.ROLE_ADMIN), handler.GetAllTprs)
student.GET("/tprLogin", handler.GinVerifyStudent, handler.GetRoleCheckHandlerForStudent(constants.ROLE_TPR), handler.HandlerTprLogin)
student.GET("/tpr/all", handler.GinVerifyStudent, handler.GinGetRoleCheckHandlerForStudent(constants.ROLE_ADMIN), handler.GetAllTprs)
student.GET("/tprLogin", handler.GinVerifyStudent, handler.GinGetRoleCheckHandlerForStudent(constants.ROLE_TPR), handler.HandlerTprLogin)
student.PUT("/update", handler.GinVerifyStudent, handler.HandlerUpdateStudentDetails)

student.GET("/profile", handler.GinVerifyStudent, handler.HandlerGetStudentProfile)
Expand All @@ -70,25 +70,25 @@ func main() {

group := r.Group("/api/group", handler.GinVerifyStudent)
{
group.GET("", handler.GetRoleCheckHandlerForStudent(constants.ROLE_GROUP_READ), handler.GetAllGroups)
group.POST("/batch", handler.GetRoleCheckHandlerForStudent(constants.ROLE_GROUP_CREATE), handler.BatchCreateGroup)
group.PUT("/batch/edit", handler.GetRoleCheckHandlerForStudent(constants.ROLE_GROUP_EDIT), handler.BatchEditGroup)
group.DELETE("/batch/delete", handler.GetRoleCheckHandlerForStudent(constants.ROLE_GROUP_DELETE), handler.BatchDeleteGroup)
group.POST("/batch/assign", handler.GetRoleCheckHandlerForStudent(constants.ROLE_GROUP_ASSIGN), handler.BatchAssignGroup)
group.GET("", handler.GinGetRoleCheckHandlerForStudent(constants.ROLE_GROUP_READ), handler.GetAllGroups)
group.POST("/batch", handler.GinGetRoleCheckHandlerForStudent(constants.ROLE_GROUP_CREATE), handler.BatchCreateGroup)
group.PUT("/batch/edit", handler.GinGetRoleCheckHandlerForStudent(constants.ROLE_GROUP_EDIT), handler.BatchEditGroup)
group.DELETE("/batch/delete", handler.GinGetRoleCheckHandlerForStudent(constants.ROLE_GROUP_DELETE), handler.BatchDeleteGroup)
group.POST("/batch/assign", handler.GinGetRoleCheckHandlerForStudent(constants.ROLE_GROUP_ASSIGN), handler.BatchAssignGroup)
}

domain := r.Group("/api/domain", handler.GinVerifyStudent)
{
domain.GET("", handler.GetRoleCheckHandlerForStudent(constants.ROLE_DOMAIN_ALL_READ), handler.GetAllDomains)
domain.GET("/id", handler.GetRoleCheckHandlerForStudent(constants.ROLE_DOMAIN_ALL_READ), handler.GetDomainById)
domain.POST("/batch", handler.GetRoleCheckHandlerForStudent(constants.ROLE_DOMAIN_CREATE), handler.BatchCreateDomain)
domain.PUT("/id", handler.GetRoleCheckHandlerForStudent(constants.ROLE_DOMAIN_EDIT), handler.EditDomainById)
domain.DELETE("/id", handler.GetRoleCheckHandlerForStudent(constants.ROLE_DOMAIN_DELETE), handler.DeleteDomainById)
domain.GET("", handler.GinGetRoleCheckHandlerForStudent(constants.ROLE_DOMAIN_ALL_READ), handler.GetAllDomains)
domain.GET("/id", handler.GinGetRoleCheckHandlerForStudent(constants.ROLE_DOMAIN_ALL_READ), handler.GetDomainById)
domain.POST("/batch", handler.GinGetRoleCheckHandlerForStudent(constants.ROLE_DOMAIN_CREATE), handler.BatchCreateDomain)
domain.PUT("/id", handler.GinGetRoleCheckHandlerForStudent(constants.ROLE_DOMAIN_EDIT), handler.EditDomainById)
domain.DELETE("/id", handler.GinGetRoleCheckHandlerForStudent(constants.ROLE_DOMAIN_DELETE), handler.DeleteDomainById)
}

companies := r.Group("/api/company", handler.GinVerifyStudent)
{
companies.GET("/all", handler.GetRoleCheckHandlerForStudent(constants.ROLE_COMPANY_ALL_READ), handler.GetAllCompanies)
companies.GET("/all", handler.GinGetRoleCheckHandlerForStudent(constants.ROLE_COMPANY_ALL_READ), handler.GetAllCompanies)
}

port := "" + os.Getenv("PORT")
Expand Down