Handler for AddUserToProject and promoteToAdmin, successfully tested
This commit is contained in:
parent
76fefd2b24
commit
4979378779
4 changed files with 70 additions and 0 deletions
|
@ -17,6 +17,8 @@ type GlobalState interface {
|
|||
SubmitWeeklyReport(c *fiber.Ctx) error
|
||||
GetWeeklyReport(c *fiber.Ctx) error
|
||||
SignReport(c *fiber.Ctx) error
|
||||
AddUserToProjectHandler(c *fiber.Ctx) error
|
||||
PromoteToAdmin(c *fiber.Ctx) error
|
||||
// GetProject(c *fiber.Ctx) error // To get a specific project
|
||||
// UpdateProject(c *fiber.Ctx) error // To update a project
|
||||
// DeleteProject(c *fiber.Ctx) error // To delete a project
|
||||
|
|
|
@ -96,3 +96,45 @@ func (gs *GState) ListAllUsersProject(c *fiber.Ctx) error {
|
|||
// Return the list of users as JSON
|
||||
return c.JSON(users)
|
||||
}
|
||||
|
||||
// AddUserToProjectHandler is a handler that adds a user to a project with a specified role
|
||||
func (gs *GState) AddUserToProjectHandler(c *fiber.Ctx) error {
|
||||
// Extract necessary parameters from the request
|
||||
var requestData struct {
|
||||
Username string `json:"username"`
|
||||
ProjectName string `json:"projectName"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
if err := c.BodyParser(&requestData); err != nil {
|
||||
println("Error parsing request body:", err)
|
||||
return c.Status(400).SendString("Bad request")
|
||||
}
|
||||
|
||||
// Check if the user adding another user to the project is a site admin
|
||||
user := c.Locals("user").(*jwt.Token)
|
||||
claims := user.Claims.(jwt.MapClaims)
|
||||
adminUsername := claims["name"].(string)
|
||||
println("Admin username from claims:", adminUsername)
|
||||
|
||||
isAdmin, err := gs.Db.IsSiteAdmin(adminUsername)
|
||||
if err != nil {
|
||||
println("Error checking admin status:", err)
|
||||
return c.Status(500).SendString(err.Error())
|
||||
}
|
||||
|
||||
if !isAdmin {
|
||||
println("User is not a site admin:", adminUsername)
|
||||
return c.Status(403).SendString("User is not a site admin")
|
||||
}
|
||||
|
||||
// Add the user to the project with the specified role
|
||||
err = gs.Db.AddUserToProject(requestData.Username, requestData.ProjectName, requestData.Role)
|
||||
if err != nil {
|
||||
println("Error adding user to project:", err)
|
||||
return c.Status(500).SendString(err.Error())
|
||||
}
|
||||
|
||||
// Return success message
|
||||
println("User added to project successfully:", requestData.Username)
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"ttime/internal/types"
|
||||
|
||||
|
@ -122,3 +123,25 @@ func (gs *GState) ListAllUsers(c *fiber.Ctx) error {
|
|||
// Return the list of users as JSON
|
||||
return c.JSON(users)
|
||||
}
|
||||
|
||||
func (gs *GState) PromoteToAdmin(c *fiber.Ctx) error {
|
||||
// Extract the username from the request body
|
||||
var newUser types.NewUser
|
||||
if err := c.BodyParser(&newUser); err != nil {
|
||||
return c.Status(400).SendString("Bad request")
|
||||
}
|
||||
username := newUser.Username
|
||||
|
||||
println("Promoting user to admin:", username) // Debug print
|
||||
|
||||
// Promote the user to a site admin in the database
|
||||
if err := gs.Db.PromoteToAdmin(username); err != nil {
|
||||
fmt.Println("Error promoting user to admin:", err) // Debug print
|
||||
return c.Status(500).SendString(err.Error())
|
||||
}
|
||||
|
||||
println("User promoted to admin successfully:", username) // Debug print
|
||||
|
||||
// Return a success message
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
|
|
@ -79,6 +79,9 @@ func main() {
|
|||
server.Delete("/api/userdelete/:username", gs.UserDelete) // Perhaps just use POST to avoid headaches
|
||||
server.Post("/api/project", gs.CreateProject)
|
||||
server.Get("/api/getWeeklyReport", gs.GetWeeklyReport)
|
||||
server.Post("/api/signReport", gs.SignReport)
|
||||
server.Put("/api/addUserToProject", gs.AddUserToProjectHandler)
|
||||
server.Post("/api/promoteToAdmin", gs.PromoteToAdmin)
|
||||
|
||||
// Announce the port we are listening on and start the server
|
||||
err = server.Listen(fmt.Sprintf(":%d", conf.Port))
|
||||
|
|
Loading…
Reference in a new issue