Merge imbs -> dev

This commit is contained in:
Imbus 2024-03-18 23:40:56 +01:00
commit 108850c20c
5 changed files with 35 additions and 29 deletions

View file

@ -5,6 +5,7 @@ import (
"ttime/internal/types"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
@ -69,7 +70,7 @@ func (gs *GState) GetProject(c *fiber.Ctx) error {
if projectID == "" {
return c.Status(400).SendString("No project ID provided")
}
println("Getting project with ID: ", projectID)
log.Info("Getting project with ID: ", projectID)
// Parse the project ID into an integer
projectIDInt, err := strconv.Atoi(projectID)
@ -84,7 +85,7 @@ func (gs *GState) GetProject(c *fiber.Ctx) error {
}
// Return the project as JSON
println("Returning project: ", project.Name)
log.Info("Returning project: ", project.Name)
return c.JSON(project)
}
@ -111,7 +112,7 @@ func (gs *GState) AddUserToProjectHandler(c *fiber.Ctx) error {
Role string `json:"role"`
}
if err := c.BodyParser(&requestData); err != nil {
println("Error parsing request body:", err)
log.Info("Error parsing request body:", err)
return c.Status(400).SendString("Bad request")
}
@ -119,27 +120,27 @@ func (gs *GState) AddUserToProjectHandler(c *fiber.Ctx) error {
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
adminUsername := claims["name"].(string)
println("Admin username from claims:", adminUsername)
log.Info("Admin username from claims:", adminUsername)
isAdmin, err := gs.Db.IsSiteAdmin(adminUsername)
if err != nil {
println("Error checking admin status:", err)
log.Info("Error checking admin status:", err)
return c.Status(500).SendString(err.Error())
}
if !isAdmin {
println("User is not a site admin:", adminUsername)
log.Info("User is not a site admin:", adminUsername)
return c.Status(403).SendString("User is not a site admin")
}
// Add the user to the project with the specified role
err = gs.Db.AddUserToProject(requestData.Username, requestData.ProjectName, requestData.Role)
if err != nil {
println("Error adding user to project:", err)
log.Info("Error adding user to project:", err)
return c.Status(500).SendString(err.Error())
}
// Return success message
println("User added to project successfully:", requestData.Username)
log.Info("User added to project successfully:", requestData.Username)
return c.SendStatus(fiber.StatusOK)
}

View file

@ -5,6 +5,7 @@ import (
"ttime/internal/types"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/golang-jwt/jwt/v5"
)
@ -37,16 +38,16 @@ func (gs *GState) SubmitWeeklyReport(c *fiber.Ctx) error {
// Handler for retrieving weekly report
func (gs *GState) GetWeeklyReport(c *fiber.Ctx) error {
// Extract the necessary parameters from the request
println("GetWeeklyReport")
log.Info("GetWeeklyReport")
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
username := claims["name"].(string)
// Extract project name and week from query parameters
projectName := c.Query("projectName")
println(projectName)
log.Info(projectName)
week := c.Query("week")
println(week)
log.Info(week)
// Convert week to integer
weekInt, err := strconv.Atoi(week)
@ -69,7 +70,7 @@ type ReportId struct {
}
func (gs *GState) SignReport(c *fiber.Ctx) error {
println("Signing report...")
log.Info("Signing report...")
// Extract the necessary parameters from the token
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
@ -81,9 +82,9 @@ func (gs *GState) SignReport(c *fiber.Ctx) error {
if err := c.BodyParser(rid); err != nil {
return err
}
println("Signing report for: ", rid.ReportId)
log.Info("Signing report for: ", rid.ReportId)
// reportIDInt, err := strconv.Atoi(rid.ReportId)
// println("Signing report for: ", rid.ReportId)
// log.Info("Signing report for: ", rid.ReportId)
// if err != nil {
// return c.Status(400).SendString("Invalid report ID")
// }
@ -93,7 +94,7 @@ func (gs *GState) SignReport(c *fiber.Ctx) error {
if err != nil {
return c.Status(500).SendString("Failed to get project manager ID")
}
println("blabla", projectManagerID)
log.Info("blabla", projectManagerID)
// Call the database function to sign the weekly report
err = gs.Db.SignWeeklyReport(rid.ReportId, projectManagerID)

View file

@ -1,10 +1,11 @@
package handlers
import (
"fmt"
"time"
"ttime/internal/types"
"github.com/gofiber/fiber/v2/log"
"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v5"
)
@ -23,16 +24,17 @@ import (
func (gs *GState) Register(c *fiber.Ctx) error {
u := new(types.NewUser)
if err := c.BodyParser(u); err != nil {
println("Error parsing body")
log.Warn("Error parsing body")
return c.Status(400).SendString(err.Error())
}
println("Adding user:", u.Username)
log.Info("Adding user:", u.Username)
log.Info("Adding user:", u.Username)
if err := gs.Db.AddUser(u.Username, u.Password); err != nil {
return c.Status(500).SendString(err.Error())
}
println("User added:", u.Username)
log.Info("User added:", u.Username)
return c.Status(200).SendString("User added")
}
@ -61,13 +63,13 @@ func (gs *GState) Login(c *fiber.Ctx) error {
// The body type is identical to a NewUser
u := new(types.NewUser)
if err := c.BodyParser(u); err != nil {
println("Error parsing body")
log.Warn("Error parsing body")
return c.Status(400).SendString(err.Error())
}
println("Username:", u.Username)
log.Info("Username:", u.Username)
if !gs.Db.CheckUser(u.Username, u.Password) {
println("User not found")
log.Info("User not found")
return c.SendStatus(fiber.StatusUnauthorized)
}
@ -80,16 +82,16 @@ func (gs *GState) Login(c *fiber.Ctx) error {
// Create token
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
println("Token created for user:", u.Username)
log.Info("Token created for user:", u.Username)
// Generate encoded token and send it as response.
t, err := token.SignedString([]byte("secret"))
if err != nil {
println("Error signing token")
log.Warn("Error signing token")
return c.SendStatus(fiber.StatusInternalServerError)
}
println("Successfully signed token for user:", u.Username)
log.Info("Successfully signed token for user:", u.Username)
return c.JSON(fiber.Map{"token": t})
}
@ -132,15 +134,15 @@ func (gs *GState) PromoteToAdmin(c *fiber.Ctx) error {
}
username := newUser.Username
println("Promoting user to admin:", username) // Debug print
log.Info("Promoting user to admin:", username) // Debug print
// Promote the user to a site admin in the database
if err := gs.Db.PromoteToAdmin(username); err != nil {
fmt.Println("Error promoting user to admin:", err) // Debug print
log.Info("Error promoting user to admin:", err) // Debug print
return c.Status(500).SendString(err.Error())
}
println("User promoted to admin successfully:", username) // Debug print
log.Info("User promoted to admin successfully:", username) // Debug print
// Return a success message
return c.SendStatus(fiber.StatusOK)

View file

@ -10,6 +10,7 @@ import (
"github.com/BurntSushi/toml"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/swagger"
jwtware "github.com/gofiber/contrib/jwt"
@ -59,6 +60,8 @@ func main() {
// Create the server
server := fiber.New()
server.Use(logger.New())
// Mounts the swagger documentation, this is available at /swagger/index.html
server.Get("/swagger/*", swagger.HandlerDefault)

View file

@ -28,7 +28,6 @@ addUserToProjectPath = base_url + "/api/addUserToProject"
promoteToAdminPath = base_url + "/api/promoteToAdmin"
getUserProjectsPath = base_url + "/api/getUserProjects"
# Posts the username and password to the register endpoint
def register(username: string, password: string):
print("Registering with username: ", username, " and password: ", password)