2024-03-17 16:55:40 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"ttime/internal/types"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-03-18 23:38:50 +01:00
|
|
|
"github.com/gofiber/fiber/v2/log"
|
2024-03-17 16:55:40 +01:00
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateProject is a simple handler that creates a new project
|
|
|
|
func (gs *GState) CreateProject(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("user").(*jwt.Token)
|
|
|
|
|
|
|
|
p := new(types.NewProject)
|
|
|
|
if err := c.BodyParser(p); err != nil {
|
|
|
|
return c.Status(400).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the username from the token and set it as the owner of the project
|
|
|
|
// This is ugly but
|
|
|
|
claims := user.Claims.(jwt.MapClaims)
|
|
|
|
owner := claims["name"].(string)
|
|
|
|
|
|
|
|
if err := gs.Db.AddProject(p.Name, p.Description, owner); err != nil {
|
|
|
|
return c.Status(500).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Status(200).SendString("Project added")
|
|
|
|
}
|
|
|
|
|
2024-03-20 11:22:33 +01:00
|
|
|
func (gs *GState) DeleteProject(c *fiber.Ctx) error {
|
|
|
|
|
|
|
|
projectID := c.Params("projectID")
|
|
|
|
username := c.Params("username")
|
|
|
|
|
|
|
|
if err := gs.Db.DeleteProject(projectID, username); err != nil {
|
|
|
|
return c.Status(500).SendString((err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Status(200).SendString("Project deleted")
|
|
|
|
}
|
|
|
|
|
2024-03-17 16:55:40 +01:00
|
|
|
// GetUserProjects returns all projects that the user is a member of
|
|
|
|
func (gs *GState) GetUserProjects(c *fiber.Ctx) error {
|
|
|
|
// First we get the username from the token
|
|
|
|
user := c.Locals("user").(*jwt.Token)
|
|
|
|
claims := user.Claims.(jwt.MapClaims)
|
|
|
|
username := claims["name"].(string)
|
|
|
|
|
|
|
|
// Then dip into the database to get the projects
|
|
|
|
projects, err := gs.Db.GetProjectsForUser(username)
|
|
|
|
if err != nil {
|
|
|
|
return c.Status(500).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a json serialized list of projects
|
|
|
|
return c.JSON(projects)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProjectRoleChange is a handler that changes a user's role within a project
|
|
|
|
func (gs *GState) ProjectRoleChange(c *fiber.Ctx) error {
|
2024-03-19 23:08:14 +01:00
|
|
|
|
|
|
|
//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)
|
2024-03-17 16:55:40 +01:00
|
|
|
// Extract the necessary parameters from the request
|
2024-03-19 23:08:14 +01:00
|
|
|
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 role when not projectmanager:", err)
|
|
|
|
return c.Status(401).SendString("you can not change role when not projectManager")
|
|
|
|
}
|
2024-03-17 16:55:40 +01:00
|
|
|
|
|
|
|
// Change the user's role within the project in the database
|
2024-03-19 23:08:14 +01:00
|
|
|
if err := gs.Db.ChangeUserRole(data.Username, data.Projectname, data.Role); err != nil {
|
2024-03-17 16:55:40 +01:00
|
|
|
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
|
|
|
|
projectID := c.Params("projectID")
|
2024-03-18 16:42:35 +01:00
|
|
|
if projectID == "" {
|
2024-03-19 00:00:18 +01:00
|
|
|
log.Info("No project ID provided")
|
2024-03-18 16:42:35 +01:00
|
|
|
return c.Status(400).SendString("No project ID provided")
|
|
|
|
}
|
2024-03-18 23:38:50 +01:00
|
|
|
log.Info("Getting project with ID: ", projectID)
|
2024-03-17 16:55:40 +01:00
|
|
|
|
|
|
|
// Parse the project ID into an integer
|
|
|
|
projectIDInt, err := strconv.Atoi(projectID)
|
|
|
|
if err != nil {
|
2024-03-19 00:00:18 +01:00
|
|
|
log.Info("Invalid project ID")
|
2024-03-17 16:55:40 +01:00
|
|
|
return c.Status(400).SendString("Invalid project ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the project from the database by its ID
|
|
|
|
project, err := gs.Db.GetProject(projectIDInt)
|
|
|
|
if err != nil {
|
2024-03-19 00:00:18 +01:00
|
|
|
log.Info("Error getting project:", err)
|
2024-03-17 16:55:40 +01:00
|
|
|
return c.Status(500).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the project as JSON
|
2024-03-18 23:38:50 +01:00
|
|
|
log.Info("Returning project: ", project.Name)
|
2024-03-17 16:55:40 +01:00
|
|
|
return c.JSON(project)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gs *GState) ListAllUsersProject(c *fiber.Ctx) error {
|
|
|
|
// Extract the project name from the request parameters or body
|
|
|
|
projectName := c.Params("projectName")
|
2024-03-19 00:00:18 +01:00
|
|
|
if projectName == "" {
|
|
|
|
log.Info("No project name provided")
|
|
|
|
return c.Status(400).SendString("No project name provided")
|
|
|
|
}
|
2024-03-17 16:55:40 +01:00
|
|
|
|
2024-03-20 00:35:37 +01:00
|
|
|
// Get the user token
|
|
|
|
userToken := c.Locals("user").(*jwt.Token)
|
|
|
|
claims := userToken.Claims.(jwt.MapClaims)
|
|
|
|
username := claims["name"].(string)
|
|
|
|
|
|
|
|
// Check if the user is a project manager for the specified project
|
|
|
|
isManager, err := gs.Db.IsProjectManager(username, projectName)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Error checking project manager status:", err)
|
|
|
|
return c.Status(500).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user is not a project manager, check if the user is a site admin
|
|
|
|
if !isManager {
|
|
|
|
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 neither a project manager nor a site admin:", username)
|
|
|
|
return c.Status(403).SendString("User is neither a project manager nor a site admin")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 16:55:40 +01:00
|
|
|
// Get all users associated with the project from the database
|
|
|
|
users, err := gs.Db.GetAllUsersProject(projectName)
|
|
|
|
if err != nil {
|
2024-03-19 00:00:18 +01:00
|
|
|
log.Info("Error getting users for project:", err)
|
2024-03-17 16:55:40 +01:00
|
|
|
return c.Status(500).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
2024-03-19 00:00:18 +01:00
|
|
|
log.Info("Returning users for project: ", projectName)
|
|
|
|
|
2024-03-17 16:55:40 +01:00
|
|
|
// Return the list of users as JSON
|
|
|
|
return c.JSON(users)
|
|
|
|
}
|
2024-03-18 14:47:15 +01:00
|
|
|
|
|
|
|
// 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 {
|
2024-03-18 23:38:50 +01:00
|
|
|
log.Info("Error parsing request body:", err)
|
2024-03-18 14:47:15 +01:00
|
|
|
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)
|
2024-03-18 23:38:50 +01:00
|
|
|
log.Info("Admin username from claims:", adminUsername)
|
2024-03-18 14:47:15 +01:00
|
|
|
|
|
|
|
isAdmin, err := gs.Db.IsSiteAdmin(adminUsername)
|
|
|
|
if err != nil {
|
2024-03-18 23:38:50 +01:00
|
|
|
log.Info("Error checking admin status:", err)
|
2024-03-18 14:47:15 +01:00
|
|
|
return c.Status(500).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isAdmin {
|
2024-03-18 23:38:50 +01:00
|
|
|
log.Info("User is not a site admin:", adminUsername)
|
2024-03-18 14:47:15 +01:00
|
|
|
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 {
|
2024-03-18 23:38:50 +01:00
|
|
|
log.Info("Error adding user to project:", err)
|
2024-03-18 14:47:15 +01:00
|
|
|
return c.Status(500).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return success message
|
2024-03-18 23:38:50 +01:00
|
|
|
log.Info("User added to project successfully:", requestData.Username)
|
2024-03-18 14:47:15 +01:00
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
2024-03-19 19:30:01 +01:00
|
|
|
|
|
|
|
// IsProjectManagerHandler is a handler that checks if a user is a project manager for a given project
|
|
|
|
func (gs *GState) IsProjectManagerHandler(c *fiber.Ctx) error {
|
2024-03-20 17:50:51 +01:00
|
|
|
// Get the username from the token
|
|
|
|
user := c.Locals("user").(*jwt.Token)
|
|
|
|
claims := user.Claims.(jwt.MapClaims)
|
|
|
|
username := claims["name"].(string)
|
|
|
|
|
2024-03-19 19:30:01 +01:00
|
|
|
// Extract necessary parameters from the request query string
|
|
|
|
projectName := c.Query("projectName")
|
|
|
|
|
|
|
|
// Check if the user is a project manager for the specified project
|
|
|
|
isManager, err := gs.Db.IsProjectManager(username, projectName)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Error checking project manager status:", err)
|
|
|
|
return c.Status(500).SendString(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the result as JSON
|
|
|
|
return c.JSON(map[string]bool{"isProjectManager": isManager})
|
|
|
|
}
|
2024-03-20 11:43:47 +01:00
|
|
|
|
|
|
|
func (gs *GState) CreateTask(c *fiber.Ctx) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|