Skip to content

Commit

Permalink
Merge branch 'feature-onboard-user' of github.com:uug-ai/facial-acces…
Browse files Browse the repository at this point in the history
…s-control into feature-onboard-user
  • Loading branch information
KilianBoute committed Jun 12, 2024
2 parents 802424e + b7004f7 commit bc74a39
Show file tree
Hide file tree
Showing 48 changed files with 1,067 additions and 2,338 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ ml/data/face_inference.mp4
ml/data/test_images
node_modules
ui/out
__debug_bin*
__debug_bin*
api/__debug_bin*
2 changes: 2 additions & 0 deletions api/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
EMAIL = "[email protected]"
PASSWORD = "password"
Empty file removed api/__debug_bin141116329
Empty file.
Empty file removed api/__debug_bin383952432
Empty file.
Binary file removed api/__debug_bin783398833
Binary file not shown.
Empty file removed api/__debug_bin825136468
Empty file.
27 changes: 20 additions & 7 deletions api/controllers/locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (

// Locations godoc
// @Router /api/locations [get]
// @Security Bearer
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @ID getLocations
// @Tags locations
// @Summary Get all locations
Expand All @@ -20,11 +24,15 @@ func GetLocations(c *gin.Context) []models.Location {
c.JSON(200, gin.H{
"data": locations,
})
return nil;
return nil
}

// Location godoc
// @Router /api/locations/{id} [get]
// @Security Bearer
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @ID getLocation
// @Tags locations
// @Summary Get location by ID
Expand All @@ -50,9 +58,12 @@ func GetLocation(c *gin.Context) models.Location {
return location
}


// location godoc
// @Router /api/locations [post]
// @Security Bearer
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @ID addLocation
// @Tags locations
// @Summary Create location
Expand All @@ -74,18 +85,22 @@ func AddLocation(c *gin.Context) error {
c.JSON(500, gin.H{
"error": "Failed to add location",
})
return err
return err
}

c.JSON(201, gin.H{
"message": "Location added successfully",
"message": "Location added successfully",
"location": location,
})
return nil
return nil
}

// location godoc
// @Router /api/locations/{id} [delete]
// @Security Bearer
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @ID deleteLocation
// @Tags locations
// @Summary Delete location
Expand Down Expand Up @@ -115,5 +130,3 @@ func DeleteLocation(c *gin.Context) error {
})
return nil
}


43 changes: 41 additions & 2 deletions api/controllers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (

// user godoc
// @Router /api/users [get]
// @Security Bearer
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @ID getUsers
// @Tags users
// @Summary Get all users
Expand All @@ -25,13 +29,17 @@ func GetUsers(c *gin.Context) []models.User {

// user godoc
// @Router /api/users/{id} [get]
// @Security Bearer
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @ID getUser
// @Tags users
// @Summary Get user
// @Description Get user
// @Param id path int true "User ID"
// @Success 200 {object} models.User
func GetUser(c *gin.Context) models.User {
func GetUserById(c *gin.Context) models.User {
id := c.Param("id")

userID, err := strconv.Atoi(id)
Expand All @@ -42,7 +50,30 @@ func GetUser(c *gin.Context) models.User {
return models.User{}
}

user := database.GetUser(userID)
user := database.GetUserById(userID)

c.JSON(200, gin.H{
"data": user,
})
return user
}

// user godoc
// @Router /api/users/{email} [get]
// @Security Bearer
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @ID getUserByEmail
// @Tags users
// @Summary Get user by email
// @Description Get user by email
// @Param email path string true "User email"
// @Success 200 {object} models.User
func GetUserByEmail(c *gin.Context) models.User {
email := c.Param("email")

user := database.GetUserByEmail(email)

c.JSON(200, gin.H{
"data": user,
Expand All @@ -52,6 +83,10 @@ func GetUser(c *gin.Context) models.User {

// user godoc
// @Router /api/users [post]
// @Security Bearer
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @ID addUser
// @Tags users
// @Summary Add user
Expand Down Expand Up @@ -85,6 +120,10 @@ func AddUser(c *gin.Context) error {

// user godoc
// @Router /api/users/{id} [delete]
// @Security Bearer
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
// @ID deleteUser
// @Tags users
// @Summary Delete user
Expand Down
18 changes: 16 additions & 2 deletions api/data/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package data

import "github.com/uug-ai/facial-access-control/api/models"
import (
"log"

"github.com/uug-ai/facial-access-control/api/models"
"github.com/uug-ai/facial-access-control/api/utils"
)

var Users = []models.User{
{Id: 0, FirstName: "admin", LastName: "admin", Email: "[email protected]", Password: "admin", Role: "admin", Language: "en", Status: "pending"},
Expand All @@ -22,5 +27,14 @@ var Locations = []models.Location{
{Id: 4, Name: "Location 4", Address: "Address 4", Lat: 4.0, Lng: 4.0},
}


// Initialize function to hash passwords
func Initialize() {
for i, user := range Users {
hashedPassword, err := utils.Hash(user.Password)
if err != nil {
log.Fatalf("Error hashing password for user %s: %v", user.Email, err)
}
Users[i].Password = hashedPassword
}
}

56 changes: 37 additions & 19 deletions api/database/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,38 @@ import (

"github.com/uug-ai/facial-access-control/api/data"
"github.com/uug-ai/facial-access-control/api/models"
"github.com/uug-ai/facial-access-control/api/utils"
)


func GetUsersFromFile() []models.User {
users := data.Users
return users
// Directly return users from data without re-hashing passwords
return data.Users
}

func GetUserFromFile(id int) models.User {
users := data.Users
for _, user := range users {
if user.Id == id {
return user
}
}
return models.User{}
func GetUserByIdFromFile(id int) models.User {
users := GetUsersFromFile()
for _, user := range users {
if user.Id == id {
return user
}
}
return models.User{}
}

func GetUserByEmailFromFile(email string) models.User {
users := GetUsersFromFile()
for _, user := range users {
if user.Email == email {
return user
}
}
return models.User{}
}


func AddUserToFile(user models.User) error {
users := data.Users
users := GetUsersFromFile()

// Find the maximum ID in the current user list
maxID := 0
Expand All @@ -38,19 +49,26 @@ func AddUserToFile(user models.User) error {
// Assign the new user an ID that is one greater than the current maximum
user.Id = maxID + 1

// Hash the user's password before saving
hashedPassword, err := utils.Hash(user.Password)
if err != nil {
return err
}
user.Password = hashedPassword

data.Users = append(data.Users, user)
return nil
}

func DeleteUserFromFile(id int) error {
users := data.Users
for i, user := range users {
if user.Id == id {
data.Users = append(users[:i], users[i+1:]...)
return nil
}
}
return errors.New("user not found")
users := GetUsersFromFile()
for i, user := range users {
if user.Id == id {
data.Users = append(users[:i], users[i+1:]...)
return nil
}
}
return errors.New("user not found")
}


Expand Down
7 changes: 5 additions & 2 deletions api/database/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
func GetUsers() []models.User {
return GetUsersFromFile()
}
func GetUser(id int) models.User {
return GetUserFromFile(id)
func GetUserById(id int) models.User {
return GetUserByIdFromFile(id)
}
func GetUserByEmail(email string) models.User {
return GetUserByEmailFromFile(email)
}
func AddUser(user models.User) error {
return AddUserToFile(user)
Expand Down
Loading

0 comments on commit bc74a39

Please sign in to comment.