diff --git a/go.mod b/go.mod index d936b6d..3e2ad2d 100644 --- a/go.mod +++ b/go.mod @@ -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 ( @@ -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 ) diff --git a/internal/middleware/check_user.go b/internal/middleware/check_user.go index ab7401b..c014c07 100644 --- a/internal/middleware/check_user.go +++ b/internal/middleware/check_user.go @@ -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" ) @@ -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", @@ -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) + } +} diff --git a/internal/routes/admin_routes.go b/internal/routes/admin_routes.go index 05daa82..46ee2ed 100644 --- a/internal/routes/admin_routes.go +++ b/internal/routes/admin_routes.go @@ -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) diff --git a/internal/services/team/get_team.go b/internal/services/team/get_team.go index b4d0b4e..ce43392 100644 --- a/internal/services/team/get_team.go +++ b/internal/services/team/get_team.go @@ -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 {