Skip to content

Commit

Permalink
Updated suff
Browse files Browse the repository at this point in the history
  • Loading branch information
AriaFantom committed Oct 2, 2024
1 parent ff3c4bd commit abad6f9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
14 changes: 14 additions & 0 deletions backend/middleware/PermissionCheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package middleware

import (
"InventoryManagement/handler"
"InventoryManagement/repository"
"github.com/gofiber/fiber/v3"
)

Expand All @@ -20,6 +21,19 @@ func CheckPermission(c fiber.Ctx, permission string) error {
})
}

// get the role of the user from the token

role, err := repository.FetchPermissionfromUser(permission)

if err != nil {
return c.Status(500).JSON(fiber.Map{
"error": err.Error(),
})
}
return c.Status(200).JSON(fiber.Map{
"permission": role,
})

return c.Next()

}
13 changes: 12 additions & 1 deletion backend/migration/20240915115947_create_database.sql
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
-- +goose Up
CREATE TABLE roles
(
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
permissions TEXT[] NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE users
(
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
role INTEGER REFERENCES ROLES(id),
isVerified BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);



-- +goose Down
DROP TABLE users;
DROP TABLE roles;



11 changes: 0 additions & 11 deletions backend/migration/20240926044842_new_schema.sql

This file was deleted.

38 changes: 38 additions & 0 deletions backend/repository/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,41 @@ func UpdateRoles(role *models.Role) error {

return nil
}

func FetchPermissionfromUser(user string) ([]string, error) {
conn, err := database.Connect()
if err != nil {
return nil, err
}

var permissions []string
err = conn.QueryRow(context.Background(), "SELECT FROM users u JOIN roles r O", roleName).Scan(&permissions)
if err != nil {
return nil, err
}

return permissions, nil
}

/*
func FetchPermissionfromUser(username string) ([]string, error) {
conn, err := database.Connect()
if err != nil {
return nil, err
}
var permissions []string
query := `
SELECT r.permissions
FROM users u
JOIN roles r ON u.role_id = r.id
WHERE u.username = $1
`
err = conn.QueryRow(context.Background(), query, username).Scan(&permissions)
if err != nil {
return nil, err
}
return permissions, nil
}
*/
2 changes: 2 additions & 0 deletions backend/routes/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func HandleRoutes(app *fiber.App) {
app.Get("/roles", handler.FetchRoles)
app.Put("/roles", handler.UpdateRoles)

app.Get("/checkperm", handler.CheckPermission)

// Inventory addition routes
app.Post("/categories", handler.CreateCategory, middleware.IsAuthorized)
app.Post("/suppliers", handler.CreateSupplier, middleware.IsAuthorized)
Expand Down

0 comments on commit abad6f9

Please sign in to comment.