Merge remote-tracking branch 'origin/AlexTester' into gruppDM

This commit is contained in:
Davenludd 2024-03-28 12:19:45 +01:00
commit c002f0e530

View file

@ -219,7 +219,7 @@ func (gs *GState) IsProjectManagerHandler(c *fiber.Ctx) error {
username := claims["name"].(string) username := claims["name"].(string)
// Extract necessary parameters from the request query string // Extract necessary parameters from the request query string
projectName := c.Params("projectName") projectName := c.Query("projectName")
log.Info("Checking if user ", username, " is a project manager for project ", projectName) log.Info("Checking if user ", username, " is a project manager for project ", projectName)
@ -231,68 +231,68 @@ func (gs *GState) IsProjectManagerHandler(c *fiber.Ctx) error {
} }
// Return the result as JSON // Return the result as JSON
return c.JSON(fiber.Map{"isProjectManager": isManager}) return c.JSON(map[string]bool{"isProjectManager": isManager})
} }
func (gs *GState) GetProjectTimesHandler(c *fiber.Ctx) error { func (gs *GState) GetProjectTimesHandler(c *fiber.Ctx) error {
// Get the username from the token // Get the username from the token
user := c.Locals("user").(*jwt.Token) user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims) claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string) username := claims["name"].(string)
// Get project // Get project
projectName := c.Params("projectName") projectName := c.Params("projectName")
if projectName == "" { if projectName == "" {
log.Info("No project name provided") log.Info("No project name provided")
return c.Status(400).SendString("No project name provided") return c.Status(400).SendString("No project name provided")
} }
// Get all users in the project and roles // Get all users in the project and roles
userProjects, err := gs.Db.GetAllUsersProject(projectName) userProjects, err := gs.Db.GetAllUsersProject(projectName)
if err != nil { if err != nil {
log.Info("Error getting users in project:", err) log.Info("Error getting users in project:", err)
return c.Status(500).SendString(err.Error()) return c.Status(500).SendString(err.Error())
} }
// If the user is member // If the user is member
isMember := false isMember := false
for _, userProject := range userProjects { for _, userProject := range userProjects {
if userProject.Username == username { if userProject.Username == username {
isMember = true isMember = true
break break
} }
} }
// If the user is admin // If the user is admin
if !isMember { if !isMember {
isAdmin, err := gs.Db.IsSiteAdmin(username) isAdmin, err := gs.Db.IsSiteAdmin(username)
if err != nil { if err != nil {
log.Info("Error checking admin status:", err) log.Info("Error checking admin status:", err)
return c.Status(500).SendString(err.Error()) return c.Status(500).SendString(err.Error())
} }
if !isAdmin { if !isAdmin {
log.Info("User is neither a project member nor a site admin:", username) log.Info("User is neither a project member nor a site admin:", username)
return c.Status(403).SendString("User is neither a project member nor a site admin") return c.Status(403).SendString("User is neither a project member nor a site admin")
} }
} }
// Get project times // Get project times
projectTimes, err := gs.Db.GetProjectTimes(projectName) projectTimes, err := gs.Db.GetProjectTimes(projectName)
if err != nil { if err != nil {
log.Info("Error getting project times:", err) log.Info("Error getting project times:", err)
return c.Status(500).SendString(err.Error()) return c.Status(500).SendString(err.Error())
} }
// Return project times as JSON // Return project times as JSON
log.Info("Returning project times for project:", projectName) log.Info("Returning project times for project:", projectName)
return c.JSON(projectTimes) return c.JSON(projectTimes)
} }
func (gs *GState) RemoveProject(c *fiber.Ctx) error { func (gs *GState) RemoveProject(c *fiber.Ctx) error {
user := c.Locals("user").(*jwt.Token) user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims) claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string) username := claims["name"].(string)
// Check if the user is a site admin // Check if the user is a site admin
isAdmin, err := gs.Db.IsSiteAdmin(username) isAdmin, err := gs.Db.IsSiteAdmin(username)
if err != nil { if err != nil {
@ -312,4 +312,4 @@ func (gs *GState) RemoveProject(c *fiber.Ctx) error {
} }
return c.Status(200).SendString("Project deleted") return c.Status(200).SendString("Project deleted")
} }