Skip to content

Commit

Permalink
Merge pull request #29 from Killerrekt/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Emerald-Wolf authored Mar 14, 2024
2 parents abf37ab + b7da847 commit d9d1761
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
golang.org/x/oauth2 v0.15.0
golang.org/x/text v0.14.0
google.golang.org/api v0.153.0
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
)

require (
Expand Down Expand Up @@ -51,5 +52,4 @@ require (
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
)
21 changes: 21 additions & 0 deletions internal/middleware/check_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/redis/go-redis/v9"

"github.com/CodeChefVIT/devsoc-backend-24/internal/database"
"github.com/CodeChefVIT/devsoc-backend-24/internal/models"
services "github.com/CodeChefVIT/devsoc-backend-24/internal/services/user"
)

Expand Down Expand Up @@ -77,6 +78,13 @@ func AuthUser(next echo.HandlerFunc) echo.HandlerFunc {
}
}

if user.IsBanned {
return c.JSON(http.StatusFailedDependency, map[string]string{
"message": "user is banned",
"status": "fail",
})
}

if !user.IsVerified {
return c.JSON(http.StatusForbidden, map[string]string{
"message": "not verified",
Expand All @@ -96,3 +104,16 @@ func AuthUser(next echo.HandlerFunc) echo.HandlerFunc {
return next(c)
}
}

func CheckAdmin(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
user := c.Get("user").(*models.User)
if user.Role != "admin" {
return c.JSON(http.StatusUnauthorized, map[string]string{
"message": "the user is not an admin",
"status": "fail",
})
}
return next(c)
}
}
6 changes: 4 additions & 2 deletions internal/routes/admin_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package routes

import (
"github.com/CodeChefVIT/devsoc-backend-24/internal/controllers"
"github.com/CodeChefVIT/devsoc-backend-24/internal/middleware"
"github.com/labstack/echo/v4"
)

func AdminRoutes(incomingRoutes *echo.Echo) {
admin := incomingRoutes.Group("/admin")
// admin.Use(middleware.Protected())
// admin.Use(middleware.AuthUser)
admin.Use(middleware.Protected())
admin.Use(middleware.AuthUser)
admin.Use(middleware.CheckAdmin)

admin.GET("/team/all", controllers.GetTeams)
admin.GET("/team/:id", controllers.GetTeamsByID)
Expand Down
6 changes: 3 additions & 3 deletions internal/services/team/get_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ func GetAllTeams() ([]models.GetTeam, error) {
var teams []models.GetTeam

query := `SELECT teams.name,teams.code, teams.leader_id, teams.round ,
users.first_name, users.last_name, users.email, users.reg_no,
users.first_name, users.last_name, users.id, users.reg_no,
ideas.title, ideas.description, ideas.track, ideas.github, ideas.figma, ideas.others ,
projects.name, projects.description, projects.github, projects.figma, projects.track, projects.others
FROM teams
INNER JOIN users ON users.team_id = teams.id
LEFT JOIN projects ON teams.projectid = projects.id
LEFT JOIN ideas ON teams.ideaid = ideas.id`
LEFT JOIN projects ON teams.id = projects.teamid
LEFT JOIN ideas ON teams.id = ideas.teamid`

rows, err := database.DB.Query(query)
if err != nil {
Expand Down

0 comments on commit d9d1761

Please sign in to comment.