-added auth for rolechange, endpoint and test
This commit is contained in:
parent
ce4cf788ae
commit
cb44954477
4 changed files with 59 additions and 5 deletions
|
@ -49,13 +49,31 @@ func (gs *GState) GetUserProjects(c *fiber.Ctx) error {
|
|||
|
||||
// ProjectRoleChange is a handler that changes a user's role within a project
|
||||
func (gs *GState) ProjectRoleChange(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
|
||||
username := c.Params("username")
|
||||
projectName := c.Params("projectName")
|
||||
role := c.Params("role")
|
||||
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")
|
||||
}
|
||||
|
||||
// Change the user's role within the project in the database
|
||||
if err := gs.Db.ChangeUserRole(username, projectName, role); err != nil {
|
||||
if err := gs.Db.ChangeUserRole(data.Username, data.Projectname, data.Role); err != nil {
|
||||
return c.Status(500).SendString(err.Error())
|
||||
}
|
||||
|
||||
|
|
|
@ -13,3 +13,9 @@ type NewProject struct {
|
|||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type RoleChange struct {
|
||||
Role string `json:"role" tstype:"'project_manager' | 'user'"`
|
||||
Username string `json:"username"`
|
||||
Projectname string `json:"projectname"`
|
||||
}
|
||||
|
|
|
@ -96,7 +96,7 @@ func main() {
|
|||
server.Get("/api/users/all", gs.ListAllUsers)
|
||||
server.Get("/api/getWeeklyReportsUser", gs.GetWeeklyReportsUserHandler)
|
||||
server.Get("api/checkIfProjectManager", gs.IsProjectManagerHandler)
|
||||
|
||||
server.Post("/api/ProjectRoleChange", gs.ProjectRoleChange)
|
||||
// Announce the port we are listening on and start the server
|
||||
err = server.Listen(fmt.Sprintf(":%d", conf.Port))
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue