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

@ -27,6 +27,7 @@ type Database interface {
AddWeeklyReport(projectName string, userName string, week int, developmentTime int, meetingTime int, adminTime int, ownWorkTime int, studyTime int, testingTime int) error AddWeeklyReport(projectName string, userName string, week int, developmentTime int, meetingTime int, adminTime int, ownWorkTime int, studyTime int, testingTime int) error
AddUserToProject(username string, projectname string, role string) error AddUserToProject(username string, projectname string, role string) error
ChangeUserRole(username string, projectname string, role string) error ChangeUserRole(username string, projectname string, role string) error
ChangeUserName(username string, newname string) error
GetAllUsersProject(projectname string) ([]UserProjectMember, error) GetAllUsersProject(projectname string) ([]UserProjectMember, error)
GetAllUsersApplication() ([]string, error) GetAllUsersApplication() ([]string, error)
GetProjectsForUser(username string) ([]types.Project, error) GetProjectsForUser(username string) ([]types.Project, error)
@ -67,6 +68,7 @@ const addWeeklyReport = `WITH UserLookup AS (SELECT id FROM users WHERE username
VALUES ((SELECT id FROM ProjectLookup), (SELECT id FROM UserLookup),?, ?, ?, ?, ?, ?, ?);` VALUES ((SELECT id FROM ProjectLookup), (SELECT id FROM UserLookup),?, ?, ?, ?, ?, ?, ?);`
const addUserToProject = "INSERT INTO user_roles (user_id, project_id, p_role) VALUES (?, ?, ?)" // WIP const addUserToProject = "INSERT INTO user_roles (user_id, project_id, p_role) VALUES (?, ?, ?)" // WIP
const changeUserRole = "UPDATE user_roles SET p_role = ? WHERE user_id = ? AND project_id = ?" const changeUserRole = "UPDATE user_roles SET p_role = ? WHERE user_id = ? AND project_id = ?"
const changeUserName = "UPDATE user SET username = ? WHERE user_id = ?" // WIP
const getProjectsForUser = `SELECT p.id, p.name, p.description FROM projects p const getProjectsForUser = `SELECT p.id, p.name, p.description FROM projects p
JOIN user_roles ur ON p.id = ur.project_id JOIN user_roles ur ON p.id = ur.project_id
@ -169,6 +171,20 @@ func (d *Db) ChangeUserRole(username string, projectname string, role string) er
return err3 return err3
} }
// ChangeUserRole changes the role of a user within a project.
func (d *Db) ChangeUserName(username string, newname string) error {
// Get the user ID
var userid int
userid, err := d.GetUserId(username)
if err != nil {
panic(err)
}
// Execute the SQL query to change the user's role
_, err2 := d.Exec(changeUserName, username, userid)
return err2
}
// GetUserRole retrieves the role of a user within a project. // GetUserRole retrieves the role of a user within a project.
func (d *Db) GetUserRole(username string, projectname string) (string, error) { func (d *Db) GetUserRole(username string, projectname string) (string, error) {
var role string var role string

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 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 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 ProjectRoleChange(c *fiber.Ctx) error // To change a users role in a project
// ProjectNameChange(c * fiber.Ctx) error // WIP
} }
// "Constructor" // "Constructor"

View file

@ -93,6 +93,40 @@ func (gs *GState) ProjectRoleChange(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK) 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 // GetProject retrieves a specific project by its ID
func (gs *GState) GetProject(c *fiber.Ctx) error { func (gs *GState) GetProject(c *fiber.Ctx) error {
// Extract the project ID from the request parameters or body // Extract the project ID from the request parameters or body