package projects import ( db "ttime/internal/database" "ttime/internal/types" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/log" "github.com/golang-jwt/jwt/v5" ) // ProjectRoleChange is a handler that changes a user's role within a project func ProjectRoleChange(c *fiber.Ctx) error { //check token and get username of current user user := c.Locals("user").(*jwt.Token) claims := user.Claims.(jwt.MapClaims) username := claims["name"].(string) // 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()) } // Check if user is trying to change its own role if username == data.UserName { log.Info("Can't change your own role") return c.Status(403).SendString("Can't change your own role") } log.Info("Changing role for user: ", data.UserName, " in project: ", data.Projectname, " to: ", data.Role) // Dubble diping and checcking if current user is if ismanager, err := db.GetDb(c).IsProjectManager(username, data.Projectname); err != nil { log.Warn("Error checking if projectmanager:", err) return c.Status(500).SendString(err.Error()) } else if !ismanager { log.Warn("User is not projectmanager") return c.Status(401).SendString("User is not projectmanager") } // Change the user's role within the project in the database if err := db.GetDb(c).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) }