From 3515a86bbb7dd6062efc9e94dae3c6252a99270b Mon Sep 17 00:00:00 2001 From: borean Date: Wed, 20 Mar 2024 12:50:04 +0100 Subject: [PATCH] Added ChangeUserName handler, untested and WIP, --- backend/internal/handlers/global_state.go | 2 +- .../handlers/handlers_project_related.go | 34 ----------- .../handlers/handlers_user_related.go | 58 +++++++++++++++---- backend/internal/types/project.go | 5 ++ backend/main.go | 1 + 5 files changed, 53 insertions(+), 47 deletions(-) diff --git a/backend/internal/handlers/global_state.go b/backend/internal/handlers/global_state.go index 1934b21..4a4b73f 100644 --- a/backend/internal/handlers/global_state.go +++ b/backend/internal/handlers/global_state.go @@ -38,7 +38,7 @@ type GlobalState interface { ListAllUsers(c *fiber.Ctx) error // To get a list of all users in the application database ListAllUsersProject(c *fiber.Ctx) error // To get a list of all users for a specific project ProjectRoleChange(c *fiber.Ctx) error // To change a users role in a project - // ProjectNameChange(c * fiber.Ctx) error // WIP + ChangeUserName(c *fiber.Ctx) error // WIP } // "Constructor" diff --git a/backend/internal/handlers/handlers_project_related.go b/backend/internal/handlers/handlers_project_related.go index 055328b..7b95c26 100644 --- a/backend/internal/handlers/handlers_project_related.go +++ b/backend/internal/handlers/handlers_project_related.go @@ -93,40 +93,6 @@ func (gs *GState) ProjectRoleChange(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusOK) } -// ProjectRoleChange is a handler that changes a user's role within a project -func (gs *GState) ProjectNameChange(c *fiber.Ctx) error { - - //check token and get username of current user - user := c.Locals("user").(*jwt.Token) - claims := user.Claims.(jwt.MapClaims) - projectManagerUsername := claims["name"].(string) - log.Info(projectManagerUsername) - // Extract the necessary parameters from the request - data := new(types.RoleChange) - if err := c.BodyParser(data); err != nil { - log.Info("error parsing username, project or role") - return c.Status(400).SendString(err.Error()) - } - - // dubble diping and checcking if current user is - - if ismanager, err := gs.Db.IsProjectManager(projectManagerUsername, data.Projectname); err != nil { - log.Warn("Error checking if projectmanager:", err) - return c.Status(500).SendString(err.Error()) - } else if !ismanager { - log.Warn("tried chaning name when not projectmanager:", err) - return c.Status(401).SendString("you can not change name when not projectManager") - } - - // Change the user's role within the project in the database - if err := gs.Db.ChangeUserRole(data.Username, data.Projectname, data.Role); err != nil { - return c.Status(500).SendString(err.Error()) - } - - // Return a success message - return c.SendStatus(fiber.StatusOK) -} - // GetProject retrieves a specific project by its ID func (gs *GState) GetProject(c *fiber.Ctx) error { // Extract the project ID from the request parameters or body diff --git a/backend/internal/handlers/handlers_user_related.go b/backend/internal/handlers/handlers_user_related.go index 75b8953..862416c 100644 --- a/backend/internal/handlers/handlers_user_related.go +++ b/backend/internal/handlers/handlers_user_related.go @@ -105,7 +105,7 @@ func (gs *GState) Login(c *fiber.Ctx) error { if err != nil { log.Info("Error checking admin status:", err) return c.Status(500).SendString(err.Error()) -} + } // Create the Claims claims := jwt.MapClaims{ "name": u.Username, @@ -187,17 +187,17 @@ func (gs *GState) ListAllUsers(c *fiber.Ctx) error { return c.JSON(users) } -// @Summary PromoteToAdmin -// @Description promote chosen user to admin -// @Tags User -// @Accept json -// @Produce plain -// @Param NewUser body types.NewUser true "user info" -// @Success 200 {json} json "Successfully prometed user" -// @Failure 400 {string} string "bad request" -// @Failure 401 {string} string "Unauthorized" -// @Failure 500 {string} string "Internal server error" -// @Router /promoteToAdmin [post] +// @Summary PromoteToAdmin +// @Description promote chosen user to admin +// @Tags User +// @Accept json +// @Produce plain +// @Param NewUser body types.NewUser true "user info" +// @Success 200 {json} json "Successfully prometed user" +// @Failure 400 {string} string "bad request" +// @Failure 401 {string} string "Unauthorized" +// @Failure 500 {string} string "Internal server error" +// @Router /promoteToAdmin [post] func (gs *GState) PromoteToAdmin(c *fiber.Ctx) error { // Extract the username from the request body var newUser types.NewUser @@ -219,3 +219,37 @@ func (gs *GState) PromoteToAdmin(c *fiber.Ctx) error { // Return a success message return c.SendStatus(fiber.StatusOK) } + +// Changes a users name in the database +func (gs *GState) ChangeUserName(c *fiber.Ctx) error { + + //check token and get username of current user + user := c.Locals("user").(*jwt.Token) + claims := user.Claims.(jwt.MapClaims) + projectManagerUsername := claims["name"].(string) + log.Info(projectManagerUsername) + // Extract the necessary parameters from the request + data := new(types.NameChange) + if err := c.BodyParser(data); err != nil { + log.Info("error parsing username, project or role") + return c.Status(400).SendString(err.Error()) + } + + // dubble diping and checcking if current user is + + if ismanager, err := gs.Db.IsProjectManager(projectManagerUsername, c.Params(data.Name)); err != nil { + log.Warn("Error checking if projectmanager:", err) + return c.Status(500).SendString(err.Error()) + } else if !ismanager { + log.Warn("tried changing name when not projectmanager:", err) + return c.Status(401).SendString("you can not change name when not projectManager") + } + + // Change the user's name within the project in the database + if err := gs.Db.ChangeUserName(projectManagerUsername, data.Name); err != nil { + return c.Status(500).SendString(err.Error()) + } + + // Return a success message + return c.SendStatus(fiber.StatusOK) +} diff --git a/backend/internal/types/project.go b/backend/internal/types/project.go index c336bcb..6a7c91a 100644 --- a/backend/internal/types/project.go +++ b/backend/internal/types/project.go @@ -19,3 +19,8 @@ type RoleChange struct { Username string `json:"username"` Projectname string `json:"projectname"` } + +type NameChange struct { + ID int `json:"id" db:"id"` + Name string `json:"name" db:"name"` +} diff --git a/backend/main.go b/backend/main.go index d0ccdb1..8a70186 100644 --- a/backend/main.go +++ b/backend/main.go @@ -93,6 +93,7 @@ func main() { server.Get("/api/getWeeklyReport", gs.GetWeeklyReport) server.Post("/api/signReport", gs.SignReport) server.Put("/api/addUserToProject", gs.AddUserToProjectHandler) + server.Put("/api/changeUserName", gs.ChangeUserName) server.Post("/api/promoteToAdmin", gs.PromoteToAdmin) server.Get("/api/users/all", gs.ListAllUsers) server.Get("/api/getWeeklyReportsUser", gs.GetWeeklyReportsUserHandler)