43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
|
package users
|
||
|
|
||
|
import (
|
||
|
db "ttime/internal/database"
|
||
|
|
||
|
"github.com/gofiber/fiber/v2"
|
||
|
"github.com/gofiber/fiber/v2/log"
|
||
|
"github.com/golang-jwt/jwt/v5"
|
||
|
)
|
||
|
|
||
|
// ChangeUserPassword is a handler that changes the password of a user
|
||
|
func ChangeUserPassword(c *fiber.Ctx) error {
|
||
|
|
||
|
//Check token and get username of current user
|
||
|
user := c.Locals("user").(*jwt.Token)
|
||
|
claims := user.Claims.(jwt.MapClaims)
|
||
|
admin := claims["name"].(string)
|
||
|
|
||
|
// Extract the necessary parameters from the request
|
||
|
username := c.Params("username")
|
||
|
newPassword := c.Query("newPassword")
|
||
|
|
||
|
// Check if user is site admin
|
||
|
issiteadmin, err := db.GetDb(c).IsSiteAdmin(admin)
|
||
|
if err != nil {
|
||
|
log.Warn("Error checking if siteadmin:", err)
|
||
|
return c.Status(500).SendString(err.Error())
|
||
|
} else if !issiteadmin {
|
||
|
log.Warn("User is not siteadmin")
|
||
|
return c.Status(401).SendString("User is not siteadmin")
|
||
|
}
|
||
|
|
||
|
// Perform the password change
|
||
|
err = db.GetDb(c).ChangeUserPassword(username, newPassword)
|
||
|
if err != nil {
|
||
|
log.Warn("Error changing password:", err)
|
||
|
return c.Status(500).SendString(err.Error())
|
||
|
}
|
||
|
|
||
|
// Return a success message
|
||
|
return c.Status(200).SendString("Password changed successfully")
|
||
|
}
|