Co-authored-by: al8763be <al8763be@users.noreply.github.com>

This commit is contained in:
Alexander Ek 2024-03-27 21:18:44 +01:00
parent e1b410c850
commit d4cc556366
8 changed files with 155 additions and 4 deletions

View file

@ -29,6 +29,7 @@ type GlobalState interface {
ChangeUserName(c *fiber.Ctx) error // WIP
GetAllUsersProject(c *fiber.Ctx) error // WIP
UpdateWeeklyReport(c *fiber.Ctx) error
RemoveProject(c *fiber.Ctx) error
}
// "Constructor"

View file

@ -286,4 +286,30 @@ func (gs *GState) GetProjectTimesHandler(c *fiber.Ctx) error {
// Return project times as JSON
log.Info("Returning project times for project:", projectName)
return c.JSON(projectTimes)
}
func (gs *GState) RemoveProject(c *fiber.Ctx) error {
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
// Check if the user is a site admin
isAdmin, err := gs.Db.IsSiteAdmin(username)
if err != nil {
log.Info("Error checking admin status:", err)
return c.Status(500).SendString(err.Error())
}
if !isAdmin {
log.Info("User is not a site admin:", username)
return c.Status(403).SendString("User is not a site admin")
}
projectName := c.Params("projectName")
if err := gs.Db.RemoveProject(projectName); err != nil {
return c.Status(500).SendString((err.Error()))
}
return c.Status(200).SendString("Project deleted")
}