Compare commits

..

No commits in common. "4c297ab2efc799a56f26b81ea7cc191751666038" and "4683dd459ac565cfacab98a835a8d17bd27fe84e" have entirely different histories.

5 changed files with 34 additions and 35 deletions

View file

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

View file

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

View file

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

View file

@ -10,7 +10,6 @@ import (
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/swagger" "github.com/gofiber/swagger"
jwtware "github.com/gofiber/contrib/jwt" jwtware "github.com/gofiber/contrib/jwt"
@ -54,8 +53,6 @@ func main() {
// Create the server // Create the server
server := fiber.New() server := fiber.New()
server.Use(logger.New())
server.Get("/swagger/*", swagger.HandlerDefault) server.Get("/swagger/*", swagger.HandlerDefault)
// Mount our static files (Beware of the security implications of this!) // Mount our static files (Beware of the security implications of this!)

View file

@ -22,10 +22,13 @@ loginPath = base_url + "/api/login"
addProjectPath = base_url + "/api/project" addProjectPath = base_url + "/api/project"
submitReportPath = base_url + "/api/submitReport" submitReportPath = base_url + "/api/submitReport"
getWeeklyReportPath = base_url + "/api/getWeeklyReport" getWeeklyReportPath = base_url + "/api/getWeeklyReport"
<<<<<<< HEAD
getProjectPath = base_url + "/api/project" getProjectPath = base_url + "/api/project"
=======
signReportPath = base_url + "/api/signReport" signReportPath = base_url + "/api/signReport"
addUserToProjectPath = base_url + "/api/addUserToProject" addUserToProjectPath = base_url + "/api/addUserToProject"
promoteToAdminPath = base_url + "/api/promoteToAdmin" promoteToAdminPath = base_url + "/api/promoteToAdmin"
>>>>>>> 9ad89d60636ac6091d71b0bf307982becc9b89fe
# Posts the username and password to the register endpoint # Posts the username and password to the register endpoint
@ -229,6 +232,9 @@ if __name__ == "__main__":
test_add_project() test_add_project()
test_submit_report() test_submit_report()
test_get_weekly_report() test_get_weekly_report()
<<<<<<< HEAD
test_get_project() test_get_project()
=======
test_sign_report() test_sign_report()
test_add_user_to_project() test_add_user_to_project()
>>>>>>> 9ad89d60636ac6091d71b0bf307982becc9b89fe