Added ChangeUserName in db

This commit is contained in:
borean 2024-03-20 12:11:05 +01:00
parent bb93cef1d3
commit 1919b7cb99
3 changed files with 51 additions and 0 deletions

View file

@ -38,6 +38,7 @@ type GlobalState interface {
ListAllUsers(c *fiber.Ctx) error // To get a list of all users in the application database
ListAllUsersProject(c *fiber.Ctx) error // To get a list of all users for a specific project
ProjectRoleChange(c *fiber.Ctx) error // To change a users role in a project
// ProjectNameChange(c * fiber.Ctx) error // WIP
}
// "Constructor"

View file

@ -93,6 +93,40 @@ func (gs *GState) ProjectRoleChange(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
}
// ProjectRoleChange is a handler that changes a user's role within a project
func (gs *GState) ProjectNameChange(c *fiber.Ctx) error {
//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)
// 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())
}
// 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 name when not projectmanager:", err)
return c.Status(401).SendString("you can not change name when not projectManager")
}
// Change the user's role within the project in the database
if err := gs.Db.ChangeUserRole(data.Username, data.Projectname, data.Role); err != nil {
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