Added ChangeUserName handler, untested and WIP,
This commit is contained in:
parent
1919b7cb99
commit
3515a86bbb
5 changed files with 53 additions and 47 deletions
|
@ -38,7 +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
|
||||
ChangeUserName(c *fiber.Ctx) error // WIP
|
||||
}
|
||||
|
||||
// "Constructor"
|
||||
|
|
|
@ -93,40 +93,6 @@ 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
|
||||
|
|
|
@ -105,7 +105,7 @@ func (gs *GState) Login(c *fiber.Ctx) error {
|
|||
if err != nil {
|
||||
log.Info("Error checking admin status:", err)
|
||||
return c.Status(500).SendString(err.Error())
|
||||
}
|
||||
}
|
||||
// Create the Claims
|
||||
claims := jwt.MapClaims{
|
||||
"name": u.Username,
|
||||
|
@ -187,17 +187,17 @@ func (gs *GState) ListAllUsers(c *fiber.Ctx) error {
|
|||
return c.JSON(users)
|
||||
}
|
||||
|
||||
// @Summary PromoteToAdmin
|
||||
// @Description promote chosen user to admin
|
||||
// @Tags User
|
||||
// @Accept json
|
||||
// @Produce plain
|
||||
// @Param NewUser body types.NewUser true "user info"
|
||||
// @Success 200 {json} json "Successfully prometed user"
|
||||
// @Failure 400 {string} string "bad request"
|
||||
// @Failure 401 {string} string "Unauthorized"
|
||||
// @Failure 500 {string} string "Internal server error"
|
||||
// @Router /promoteToAdmin [post]
|
||||
// @Summary PromoteToAdmin
|
||||
// @Description promote chosen user to admin
|
||||
// @Tags User
|
||||
// @Accept json
|
||||
// @Produce plain
|
||||
// @Param NewUser body types.NewUser true "user info"
|
||||
// @Success 200 {json} json "Successfully prometed user"
|
||||
// @Failure 400 {string} string "bad request"
|
||||
// @Failure 401 {string} string "Unauthorized"
|
||||
// @Failure 500 {string} string "Internal server error"
|
||||
// @Router /promoteToAdmin [post]
|
||||
func (gs *GState) PromoteToAdmin(c *fiber.Ctx) error {
|
||||
// Extract the username from the request body
|
||||
var newUser types.NewUser
|
||||
|
@ -219,3 +219,37 @@ func (gs *GState) PromoteToAdmin(c *fiber.Ctx) error {
|
|||
// Return a success message
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
||||
// Changes a users name in the database
|
||||
func (gs *GState) ChangeUserName(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.NameChange)
|
||||
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, c.Params(data.Name)); err != nil {
|
||||
log.Warn("Error checking if projectmanager:", err)
|
||||
return c.Status(500).SendString(err.Error())
|
||||
} else if !ismanager {
|
||||
log.Warn("tried changing name when not projectmanager:", err)
|
||||
return c.Status(401).SendString("you can not change name when not projectManager")
|
||||
}
|
||||
|
||||
// Change the user's name within the project in the database
|
||||
if err := gs.Db.ChangeUserName(projectManagerUsername, data.Name); err != nil {
|
||||
return c.Status(500).SendString(err.Error())
|
||||
}
|
||||
|
||||
// Return a success message
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
|
|
@ -19,3 +19,8 @@ type RoleChange struct {
|
|||
Username string `json:"username"`
|
||||
Projectname string `json:"projectname"`
|
||||
}
|
||||
|
||||
type NameChange struct {
|
||||
ID int `json:"id" db:"id"`
|
||||
Name string `json:"name" db:"name"`
|
||||
}
|
||||
|
|
|
@ -93,6 +93,7 @@ func main() {
|
|||
server.Get("/api/getWeeklyReport", gs.GetWeeklyReport)
|
||||
server.Post("/api/signReport", gs.SignReport)
|
||||
server.Put("/api/addUserToProject", gs.AddUserToProjectHandler)
|
||||
server.Put("/api/changeUserName", gs.ChangeUserName)
|
||||
server.Post("/api/promoteToAdmin", gs.PromoteToAdmin)
|
||||
server.Get("/api/users/all", gs.ListAllUsers)
|
||||
server.Get("/api/getWeeklyReportsUser", gs.GetWeeklyReportsUserHandler)
|
||||
|
|
Loading…
Reference in a new issue