46 lines
1.4 KiB
Go
46 lines
1.4 KiB
Go
|
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())
|
||
|
}
|
||
|
|
||
|
log.Info("Changing role for user: ", 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(username, data.Projectname, data.Role); err != nil {
|
||
|
return c.Status(500).SendString(err.Error())
|
||
|
}
|
||
|
|
||
|
// Return a success message
|
||
|
return c.SendStatus(fiber.StatusOK)
|
||
|
}
|