Skip to content

Commit

Permalink
Add users/onboard API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
KilianBoute committed Jun 13, 2024
1 parent 7543f7b commit 5c69ffc
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 74 deletions.
27 changes: 25 additions & 2 deletions api/controllers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,34 @@ func DeleteUser(c *gin.Context) error {

// user godoc
// @Router /api/users/onboard [post]
// @in header
// @ID onboardUser
// @Tags users
// @Summary Onboard a user using a fingerprint and a video
// @Description Onboard a user using a fingerprint and a video
// @Success 200
// @Description Onboard a user using a fingerprint and a videossss
// @Accept json
// @Produce json
// @Param user body models.User true "User data"
// @Success 201 {object} models.User
func OnboardUser(c *gin.Context) error {
var user models.User
if err := c.ShouldBindJSON(&user); err != nil {
c.JSON(400, gin.H{
"error": "Invalid user data",
})
return err
}

if err := database.OnboardUser(user); err != nil {
c.JSON(500, gin.H{
"error": "Failed to onboard user",
})
return err
}

c.JSON(201, gin.H{
"message": "User onboarded successfully",
"user": user,
})
return nil
}
27 changes: 15 additions & 12 deletions api/data/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import (
"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"},
{Id: 1, FirstName: "user", LastName: "user", Email: "[email protected]", Password: "user", Role: "user", Language: "en", Status: "invited"},
{Id: 2, FirstName: "Kilian", LastName: "Smith", Email: "[email protected]", Password: "Kilian", Role: "admin", Language: "en", Status: "onboarded"},
{Id: 3, FirstName: "Cedric", LastName: "Johnson", Email: "[email protected]", Password: "Cedric", Role: "admin", Language: "en", Status: "pending"},
{Id: 4, FirstName: "Johann", LastName: "Brown", Email: "[email protected]", Password: "Johann", Role: "admin", Language: "en", Status: "invited"},
{Id: 5, FirstName: "Romain", LastName: "Davis", Email: "[email protected]", Password: "Romain", Role: "admin", Language: "en", Status: "onboarded"},
{Id: 6, FirstName: "Alex", LastName: "Wilson", Email: "[email protected]", Password: "Alex", Role: "admin", Language: "en", Status: "pending"},
{Id: 7, FirstName: "Mickael", LastName: "Taylor", Email: "[email protected]", Password: "Mickael", Role: "admin", Language: "en", Status: "invited"},
{Id: 8, FirstName: "Mickael", LastName: "Thomas", Email: "[email protected]", Password: "Mickael", Role: "admin", Language: "en", Status: "onboarded"},
{Id: 9, FirstName: "Mickael", LastName: "Robinson", Email: "[email protected]", Password: "Mickael", Role: "admin", Language: "en", Status: "pending"},
{Id: 10, FirstName: "Mickael", LastName: "Clark", Email: "[email protected]", Password: "Mickael", Role: "admin", Language: "en", Status: "invited"},
}
{Id: 0, FirstName: "admin", LastName: "admin", Email: "[email protected]", Password: "admin", Role: "admin", Language: "en", Status: "pending", VideoPath: ""},
{Id: 1, FirstName: "user", LastName: "user", Email: "[email protected]", Password: "user", Role: "user", Language: "en", Status: "invited", VideoPath: "/"},
{Id: 2, FirstName: "Kilian", LastName: "Smith", Email: "[email protected]", Password: "Kilian", Role: "admin", Language: "en", Status: "onboarded", VideoPath: "/path/to/kilian-video.mp4"},
{Id: 3, FirstName: "Cedric", LastName: "Johnson", Email: "[email protected]", Password: "Cedric", Role: "admin", Language: "en", Status: "pending", VideoPath: ""},
{Id: 4, FirstName: "Johann", LastName: "Brown", Email: "[email protected]", Password: "Johann", Role: "admin", Language: "en", Status: "invited", VideoPath: ""},
{Id: 5, FirstName: "Romain", LastName: "Davis", Email: "[email protected]", Password: "Romain", Role: "admin", Language: "en", Status: "onboarded", VideoPath: "/path/to/romain-video.mp4"},
{Id: 6, FirstName: "Alex", LastName: "Wilson", Email: "[email protected]", Password: "Alex", Role: "admin", Language: "en", Status: "pending", VideoPath: ""},
{Id: 7, FirstName: "Mickael", LastName: "Taylor", Email: "[email protected]", Password: "Mickael", Role: "admin", Language: "en", Status: "invited", VideoPath: ""},
{Id: 8, FirstName: "Mickael", LastName: "Thomas", Email: "[email protected]", Password: "Mickael", Role: "admin", Language: "en", Status: "onboarded", VideoPath: "/path/to/mickael1-video.mp4"},
{Id: 9, FirstName: "Mickael", LastName: "Robinson", Email: "[email protected]", Password: "Mickael", Role: "admin", Language: "en", Status: "pending", VideoPath: ""},
{Id: 10, FirstName: "Mickael", LastName: "Clark", Email: "[email protected]", Password: "Mickael", Role: "admin", Language: "en", Status: "invited", VideoPath: ""},

}

var Locations = []models.Location{
{Id: 1, Name: "Location 1", Address: "Address 1", Lat: 1.0, Lng: 1.0},
{Id: 2, Name: "Location 2", Address: "Address 2", Lat: 2.0, Lng: 2.0},
Expand Down
11 changes: 11 additions & 0 deletions api/database/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ func DeleteUserFromFile(id int) error {
return errors.New("user not found")
}

func OnboardUserToFile(user models.User) error {
if(user.Status == "onboarded") {
return errors.New("user already onboarded")
}
if(user.Status != "invited") {
return errors.New("user must be invited before onboarding")
}
data.Users[user.Id] = user
data.Users[user.Id].Status = "onboarded"
return nil;
}

func GetLocationsFromFile() []models.Location {
locations := data.Locations
Expand Down
4 changes: 4 additions & 0 deletions api/database/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func AddUser(user models.User) error {
func DeleteUser(id int) error {
return DeleteUserFromFile(id)
}
func OnboardUser(user models.User) error {
return OnboardUserToFile(user)
}
func GetLocations() []models.Location {
return GetLocationsFromFile()
}
Expand All @@ -31,3 +34,4 @@ func AddLocation(location models.Location) error {
func DeleteLocation(id int) error {
return DeleteLocationFromFile(id)
}

67 changes: 46 additions & 21 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@ const docTemplate = `{
"info": {
"description": "{{escape .Description}}",
"title": "{{.Title}}",
"termsOfService": "https://kerberos.io",
"contact": {
"name": "API Support",
"url": "https://www.kerberos.io",
"email": "[email protected]"
},
"license": {
"name": "Apache 2.0 - Commons Clause",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"contact": {},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
Expand Down Expand Up @@ -240,6 +231,41 @@ const docTemplate = `{
}
}
},
"/api/users/onboard": {
"post": {
"description": "Onboard a user using a fingerprint and a videossss",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"users"
],
"summary": "Onboard a user using a fingerprint and a video",
"operationId": "onboardUser",
"parameters": [
{
"description": "User data",
"name": "user",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/models.User"
}
}
],
"responses": {
"201": {
"description": "Created",
"schema": {
"$ref": "#/definitions/models.User"
}
}
}
}
},
"/api/users/{email}": {
"get": {
"security": [
Expand Down Expand Up @@ -407,27 +433,26 @@ const docTemplate = `{
},
"role": {
"type": "string"
},
"status": {
"type": "string"
},
"videopath": {
"type": "string"
}
}
}
},
"securityDefinitions": {
"Bearer": {
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
}
}`

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "1.0",
Version: "",
Host: "",
BasePath: "/",
BasePath: "",
Schemes: []string{},
Title: "Swagger Kerberos Agent API",
Description: "This is the API for using and configuring Kerberos Agent.",
Title: "",
Description: "",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
Expand Down
63 changes: 42 additions & 21 deletions api/docs/swagger.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
{
"swagger": "2.0",
"info": {
"description": "This is the API for using and configuring Kerberos Agent.",
"title": "Swagger Kerberos Agent API",
"termsOfService": "https://kerberos.io",
"contact": {
"name": "API Support",
"url": "https://www.kerberos.io",
"email": "[email protected]"
},
"license": {
"name": "Apache 2.0 - Commons Clause",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "1.0"
"contact": {}
},
"basePath": "/",
"paths": {
"/api/locations": {
"get": {
Expand Down Expand Up @@ -233,6 +220,41 @@
}
}
},
"/api/users/onboard": {
"post": {
"description": "Onboard a user using a fingerprint and a videossss",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"users"
],
"summary": "Onboard a user using a fingerprint and a video",
"operationId": "onboardUser",
"parameters": [
{
"description": "User data",
"name": "user",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/models.User"
}
}
],
"responses": {
"201": {
"description": "Created",
"schema": {
"$ref": "#/definitions/models.User"
}
}
}
}
},
"/api/users/{email}": {
"get": {
"security": [
Expand Down Expand Up @@ -400,15 +422,14 @@
},
"role": {
"type": "string"
},
"status": {
"type": "string"
},
"videopath": {
"type": "string"
}
}
}
},
"securityDefinitions": {
"Bearer": {
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
}
}
45 changes: 28 additions & 17 deletions api/docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
basePath: /
definitions:
models.Authentication:
properties:
Expand Down Expand Up @@ -49,19 +48,13 @@ definitions:
type: string
role:
type: string
status:
type: string
videopath:
type: string
type: object
info:
contact:
email: [email protected]
name: API Support
url: https://www.kerberos.io
description: This is the API for using and configuring Kerberos Agent.
license:
name: Apache 2.0 - Commons Clause
url: http://www.apache.org/licenses/LICENSE-2.0.html
termsOfService: https://kerberos.io
title: Swagger Kerberos Agent API
version: "1.0"
contact: {}
paths:
/api/locations:
get:
Expand Down Expand Up @@ -256,9 +249,27 @@ paths:
summary: Get user
tags:
- users
securityDefinitions:
Bearer:
in: header
name: Authorization
type: apiKey
/api/users/onboard:
post:
consumes:
- application/json
description: Onboard a user using a fingerprint and a videossss
operationId: onboardUser
parameters:
- description: User data
in: body
name: user
required: true
schema:
$ref: '#/definitions/models.User'
produces:
- application/json
responses:
"201":
description: Created
schema:
$ref: '#/definitions/models.User'
summary: Onboard a user using a fingerprint and a video
tags:
- users
swagger: "2.0"
2 changes: 1 addition & 1 deletion api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/crypto v0.23.0 // direct
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions api/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type User struct {
Role string `json:"role" bson:"role"`
Language string `json:"language" bson:"language"`
Status string `json:"status" bson:"status"`
VideoPath string `json:"videopath" bson:"videopath"`
}

type Authentication struct {
Expand Down
6 changes: 6 additions & 0 deletions api/routers/http/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func AddRoutes(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) *gin.RouterG
{
api.POST("/login", authMiddleware.LoginHandler)

api.POST("/users/onboard", func(c *gin.Context) {
controllers.OnboardUser(c)
})

// Secured endpoints..
api.Use(authMiddleware.MiddlewareFunc())
{
Expand All @@ -37,6 +41,8 @@ func AddRoutes(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware) *gin.RouterG
api.DELETE("/users/:id", func(c *gin.Context) {
controllers.DeleteUser(c)
})


// End users

// Locations
Expand Down

0 comments on commit 5c69ffc

Please sign in to comment.