Merge branch 'main' of ssh://git.ma-al.com:8822/goc_daniel/b2b into no-vat-customers

This commit is contained in:
2026-04-14 13:40:37 +02:00
16 changed files with 140 additions and 49 deletions

View File

@@ -10,14 +10,14 @@ import (
"git.ma-al.com/goc_daniel/b2b/app/model" "git.ma-al.com/goc_daniel/b2b/app/model"
"git.ma-al.com/goc_daniel/b2b/app/service/authService" "git.ma-al.com/goc_daniel/b2b/app/service/authService"
constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data" constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data"
"git.ma-al.com/goc_daniel/b2b/app/utils/localeExtractor"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
) )
// AuthMiddleware creates authentication middleware // AuthMiddleware creates authentication middleware
func AuthMiddleware() fiber.Handler { func Authenticate() fiber.Handler {
authService := authService.NewAuthService() authService := authService.NewAuthService()
return func(c fiber.Ctx) error { return func(c fiber.Ctx) error {
// Get token from Authorization header // Get token from Authorization header
authHeader := c.Get("Authorization") authHeader := c.Get("Authorization")
@@ -25,17 +25,13 @@ func AuthMiddleware() fiber.Handler {
// Try to get from cookie // Try to get from cookie
authHeader = c.Cookies("access_token") authHeader = c.Cookies("access_token")
if authHeader == "" { if authHeader == "" {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Next()
"error": "authorization token required",
})
} }
} else { } else {
// Extract token from "Bearer <token>" // Extract token from "Bearer <token>"
parts := strings.Split(authHeader, " ") parts := strings.Split(authHeader, " ")
if len(parts) != 2 || parts[0] != "Bearer" { if len(parts) != 2 || parts[0] != "Bearer" {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Next()
"error": "invalid authorization header format",
})
} }
authHeader = parts[1] authHeader = parts[1]
} }
@@ -43,24 +39,18 @@ func AuthMiddleware() fiber.Handler {
// Validate token // Validate token
claims, err := authService.ValidateToken(authHeader) claims, err := authService.ValidateToken(authHeader)
if err != nil { if err != nil {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Next()
"error": "invalid or expired token",
})
} }
// Get user from database // Get user from database
user, err := authService.GetUserByID(claims.UserID) user, err := authService.GetUserByID(claims.UserID)
if err != nil { if err != nil {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Next()
"error": "user not found",
})
} }
// Check if user is active // Check if user is active
if !user.IsActive { if !user.IsActive {
return c.Status(fiber.StatusForbidden).JSON(fiber.Map{ return c.Next()
"error": "user account is inactive",
})
} }
// Create locale. LangID is overwritten by auth Token // Create locale. LangID is overwritten by auth Token
@@ -79,9 +69,7 @@ func AuthMiddleware() fiber.Handler {
// We now populate the target user // We now populate the target user
if model.CustomerRole(user.Role.Name) != model.RoleAdmin { if model.CustomerRole(user.Role.Name) != model.RoleAdmin {
return c.Status(fiber.StatusForbidden).JSON(fiber.Map{ return c.Next()
"error": "admin access required",
})
} }
targetUserID, err := strconv.Atoi(targetUserIDAttribute) targetUserID, err := strconv.Atoi(targetUserIDAttribute)
@@ -114,6 +102,18 @@ func AuthMiddleware() fiber.Handler {
} }
} }
func Authorize() fiber.Handler {
return func(c fiber.Ctx) error {
_, ok := localeExtractor.GetUserID(c)
if !ok {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"error": "not authenticated",
})
}
return c.Next()
}
}
// Webdav // Webdav
func Webdav() fiber.Handler { func Webdav() fiber.Handler {
authService := authService.NewAuthService() authService := authService.NewAuthService()

View File

@@ -49,7 +49,7 @@ func AuthHandlerRoutes(r fiber.Router) fiber.Router {
r.Get("/google", handler.GoogleLogin) r.Get("/google", handler.GoogleLogin)
r.Get("/google/callback", handler.GoogleCallback) r.Get("/google/callback", handler.GoogleCallback)
authProtected := r.Group("", middleware.AuthMiddleware()) authProtected := r.Group("", middleware.Authorize())
authProtected.Get("/me", handler.Me) authProtected.Get("/me", handler.Me)
authProtected.Post("/update-choice", handler.UpdateJWTToken) authProtected.Post("/update-choice", handler.UpdateJWTToken)

View File

@@ -2,6 +2,7 @@ package public
import ( import (
"git.ma-al.com/goc_daniel/b2b/app/service/menuService" "git.ma-al.com/goc_daniel/b2b/app/service/menuService"
constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data"
"git.ma-al.com/goc_daniel/b2b/app/utils/i18n" "git.ma-al.com/goc_daniel/b2b/app/utils/i18n"
"git.ma-al.com/goc_daniel/b2b/app/utils/localeExtractor" "git.ma-al.com/goc_daniel/b2b/app/utils/localeExtractor"
"git.ma-al.com/goc_daniel/b2b/app/utils/nullable" "git.ma-al.com/goc_daniel/b2b/app/utils/nullable"
@@ -31,12 +32,21 @@ func RoutingHandlerRoutes(r fiber.Router) fiber.Router {
} }
func (h *RoutingHandler) GetRouting(c fiber.Ctx) error { func (h *RoutingHandler) GetRouting(c fiber.Ctx) error {
lang_id, ok := localeExtractor.GetLangID(c) langId, ok := localeExtractor.GetLangID(c)
if !ok { if !ok {
return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrBadAttribute)). return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrInvalidBody)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrBadAttribute))) JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrInvalidBody)))
} }
menu, err := h.menuService.GetRoutes(lang_id)
var roleId uint
customer, ok := localeExtractor.GetCustomer(c)
if !ok {
roleId = constdata.UNLOGGED_USER_ROLE_ID
} else {
roleId = customer.RoleID
}
menu, err := h.menuService.GetRoutes(langId, roleId)
if err != nil { if err != nil {
return c.Status(responseErrors.GetErrorStatus(err)). return c.Status(responseErrors.GetErrorStatus(err)).
JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err)))

View File

@@ -86,9 +86,10 @@ func (s *Server) Setup() error {
// API routes // API routes
s.api = s.app.Group("/api/v1") s.api = s.app.Group("/api/v1")
s.api.Use(middleware.Authenticate())
s.public = s.api.Group("/public") s.public = s.api.Group("/public")
s.restricted = s.api.Group("/restricted") s.restricted = s.api.Group("/restricted")
s.restricted.Use(middleware.AuthMiddleware()) s.restricted.Use(middleware.Authorize())
s.webdav = s.api.Group("/webdav") s.webdav = s.api.Group("/webdav")
s.webdav.Use(middleware.Webdav()) s.webdav.Use(middleware.Webdav())

View File

@@ -7,7 +7,6 @@ type Route struct {
Component string `gorm:"type:varchar(255);not null;comment:path to component file" json:"component"` Component string `gorm:"type:varchar(255);not null;comment:path to component file" json:"component"`
Meta *string `gorm:"type:longtext;default:'{}'" json:"meta,omitempty"` Meta *string `gorm:"type:longtext;default:'{}'" json:"meta,omitempty"`
Active *bool `gorm:"type:tinyint;default:1" json:"active,omitempty"` Active *bool `gorm:"type:tinyint;default:1" json:"active,omitempty"`
SortOrder *int `gorm:"type:int;default:0" json:"sort_order,omitempty"`
} }
func (Route) TableName() string { func (Route) TableName() string {

View File

@@ -18,7 +18,7 @@ type UIProductsRepo interface {
// GetJSON(p_id_product, p_id_shop, p_id_lang, p_id_customer, b2b_id_country, p_quantity int) (*json.RawMessage, error) // GetJSON(p_id_product, p_id_shop, p_id_lang, p_id_customer, b2b_id_country, p_quantity int) (*json.RawMessage, error)
Find(id_lang uint, userID uint, p find.Paging, filt *filters.FiltersList) (*find.Found[model.ProductInList], error) Find(id_lang uint, userID uint, p find.Paging, filt *filters.FiltersList) (*find.Found[model.ProductInList], error)
GetProductVariants(langID uint, productID uint, shopID uint, customerID uint, countryID uint, quantity uint) ([]view.ProductAttribute, error) GetProductVariants(langID uint, productID uint, shopID uint, customerID uint, countryID uint, quantity uint) ([]view.ProductAttribute, error)
GetBase(p_id_product, p_id_shop, p_id_lang uint) (view.Product, error) GetBase(p_id_product, p_id_shop, p_id_lang, p_id_customer uint) (view.Product, error)
GetPrice(p_id_product uint, productAttributeID *uint, p_id_shop uint, p_id_customer uint, p_id_country uint, p_quantity uint) (view.Price, error) GetPrice(p_id_product uint, productAttributeID *uint, p_id_shop uint, p_id_customer uint, p_id_country uint, p_quantity uint) (view.Price, error)
GetVariants(p_id_product, p_id_shop, p_id_lang, p_id_customer, p_id_country, p_quantity uint) ([]view.ProductAttribute, error) GetVariants(p_id_product, p_id_shop, p_id_lang, p_id_customer, p_id_country, p_quantity uint) ([]view.ProductAttribute, error)
AddToFavorites(userID uint, productID uint) error AddToFavorites(userID uint, productID uint) error
@@ -33,11 +33,11 @@ func New() UIProductsRepo {
return &ProductsRepo{} return &ProductsRepo{}
} }
func (repo *ProductsRepo) GetBase(p_id_product, p_id_shop, p_id_lang uint) (view.Product, error) { func (repo *ProductsRepo) GetBase(p_id_product, p_id_shop, p_id_lang, p_id_customer uint) (view.Product, error) {
var result view.Product var result view.Product
err := db.DB.Raw(`CALL get_product_base(?,?,?)`, err := db.DB.Raw(`CALL get_product_base(?,?,?,?)`,
p_id_product, p_id_shop, p_id_lang). p_id_product, p_id_shop, p_id_lang, p_id_customer).
Scan(&result).Error Scan(&result).Error
return result, err return result, err

View File

@@ -7,7 +7,7 @@ import (
) )
type UIRoutesRepo interface { type UIRoutesRepo interface {
GetRoutes(langId uint) ([]model.Route, error) GetRoutes(langId uint, roleId uint) ([]model.Route, error)
GetTopMenu(id uint, roleId uint) ([]model.B2BTopMenu, error) GetTopMenu(id uint, roleId uint) ([]model.B2BTopMenu, error)
} }
@@ -17,13 +17,18 @@ func New() UIRoutesRepo {
return &RoutesRepo{} return &RoutesRepo{}
} }
func (p *RoutesRepo) GetRoutes(langId uint) ([]model.Route, error) { func (p *RoutesRepo) GetRoutes(langId uint, roleId uint) ([]model.Route, error) {
routes := []model.Route{} routes := []model.Route{}
err := db.DB.Find(&routes, model.Route{Active: nullable.GetNil(true)}).Error
if err != nil { err := db.
return nil, err Get().
} Model(model.Route{}).
return routes, nil Joins("JOIN b2b_route_roles rr ON rr.route_id = b2b_routes.id").
Where(model.Route{Active: nullable.GetNil(true)}).
Where("rr.role_id = ?", roleId).
Find(&routes).Error
return routes, err
} }
func (p *RoutesRepo) GetTopMenu(langId uint, roleId uint) ([]model.B2BTopMenu, error) { func (p *RoutesRepo) GetTopMenu(langId uint, roleId uint) ([]model.B2BTopMenu, error) {

View File

@@ -102,8 +102,8 @@ func (s *MenuService) createTree(index int, all_categories *([]model.ScannedCate
return node, true return node, true
} }
func (s *MenuService) GetRoutes(id_lang uint) ([]model.Route, error) { func (s *MenuService) GetRoutes(id_lang, roleId uint) ([]model.Route, error) {
return s.routesRepo.GetRoutes(id_lang) return s.routesRepo.GetRoutes(id_lang, roleId)
} }
func (s *MenuService) scannedToNormalCategory(scanned model.ScannedCategory) model.Category { func (s *MenuService) scannedToNormalCategory(scanned model.ScannedCategory) model.Category {

View File

@@ -27,7 +27,7 @@ func (s *ProductService) Get(
p_id_product, p_id_lang, p_id_customer, b2b_id_country, p_quantity uint, p_id_product, p_id_lang, p_id_customer, b2b_id_country, p_quantity uint,
) (*json.RawMessage, error) { ) (*json.RawMessage, error) {
product, err := s.productsRepo.GetBase(p_id_product, constdata.SHOP_ID, p_id_lang) product, err := s.productsRepo.GetBase(p_id_product, constdata.SHOP_ID, p_id_lang, p_id_customer)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -32,3 +32,5 @@ const WEBDAV_TRIMMED_ROOT = "localhost:3000/api/v1/webdav/storage"
const NON_ALNUM_REGEX = `[^a-z0-9]+` const NON_ALNUM_REGEX = `[^a-z0-9]+`
const MULTI_DASH_REGEX = `-+` const MULTI_DASH_REGEX = `-+`
const SLUG_REGEX = `^[a-z0-9]+(?:-[a-z0-9]+)*$` const SLUG_REGEX = `^[a-z0-9]+(?:-[a-z0-9]+)*$`
const UNLOGGED_USER_ROLE_ID = 4

View File

@@ -0,0 +1,15 @@
info:
name: Routes
type: http
seq: 1
http:
method: GET
url: ""
auth: inherit
settings:
encodeUrl: true
timeout: 0
followRedirects: true
maxRedirects: 5

View File

@@ -0,0 +1,7 @@
info:
name: routes
type: folder
seq: 10
request:
auth: inherit

View File

@@ -42,6 +42,11 @@ INSERT IGNORE INTO `b2b_top_menu` (`menu_id`, `label`, `parent_id`, `params`, `a
(3, JSON_COMPACT('{"name":"admin-products","trans":{"pl":{"label":"admin-products"},"en":{"label":"admin-products"},"de":{"label":"admin-products"}}}'),1,JSON_COMPACT('{}'),1,1), (3, JSON_COMPACT('{"name":"admin-products","trans":{"pl":{"label":"admin-products"},"en":{"label":"admin-products"},"de":{"label":"admin-products"}}}'),1,JSON_COMPACT('{}'),1,1),
(9, JSON_COMPACT('{"name":"carts","trans":{"pl":{"label":"Koszyki"},"en":{"label":"Carts"},"de":{"label":"Warenkörbe"}}}'),3,JSON_COMPACT('{"route": {"name": "home", "params":{"locale": ""}}}'),1,1); (9, JSON_COMPACT('{"name":"carts","trans":{"pl":{"label":"Koszyki"},"en":{"label":"Carts"},"de":{"label":"Warenkörbe"}}}'),3,JSON_COMPACT('{"route": {"name": "home", "params":{"locale": ""}}}'),1,1);
CREATE TABLE `b2b_route_roles` (
`route_id` INT NOT NULL,
`role_id` BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (`id`, `role_id`)
);
-- +goose Down -- +goose Down

View File

@@ -339,6 +339,24 @@ ON b2b_specific_price_customer (b2b_id_customer);
CREATE INDEX idx_bsp_country_rel CREATE INDEX idx_bsp_country_rel
ON b2b_specific_price_country (b2b_id_country); ON b2b_specific_price_country (b2b_id_country);
CREATE TABLE b2b_route_roles (
route_id INT NOT NULL,
role_id BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (route_id, role_id),
INDEX idx_role_id (role_id),
INDEX idx_route_id (route_id),
CONSTRAINT FK_b2b_route_roles_route_id
FOREIGN KEY (route_id)
REFERENCES b2b_routes (id)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT FK_b2b_route_roles_role_id
FOREIGN KEY (role_id)
REFERENCES b2b_roles (id)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB;
DELIMITER // DELIMITER //
CREATE FUNCTION IF NOT EXISTS slugify_eu(input TEXT) CREATE FUNCTION IF NOT EXISTS slugify_eu(input TEXT)
@@ -439,6 +457,7 @@ DROP TABLE IF EXISTS b2b_customer_carts;
DROP TABLE IF EXISTS b2b_specific_price_country; DROP TABLE IF EXISTS b2b_specific_price_country;
DROP TABLE IF EXISTS b2b_specific_price_customer; DROP TABLE IF EXISTS b2b_specific_price_customer;
DROP TABLE IF EXISTS b2b_specific_price_product_attribute; DROP TABLE IF EXISTS b2b_specific_price_product_attribute;
DROP TABLE IF EXISTS b2b_route_roles;
DROP TABLE IF EXISTS b2b_specific_price_category; DROP TABLE IF EXISTS b2b_specific_price_category;
DROP TABLE IF EXISTS b2b_specific_price_product; DROP TABLE IF EXISTS b2b_specific_price_product;
DROP TABLE IF EXISTS b2b_specific_price; DROP TABLE IF EXISTS b2b_specific_price;

View File

@@ -10,6 +10,7 @@ VALUES
INSERT INTO `b2b_roles` (`name`, `id`) VALUES ('user','1'); INSERT INTO `b2b_roles` (`name`, `id`) VALUES ('user','1');
INSERT INTO `b2b_roles` (`name`, `id`) VALUES ('admin','2'); INSERT INTO `b2b_roles` (`name`, `id`) VALUES ('admin','2');
INSERT INTO `b2b_roles` (`name`, `id`) VALUES ('super_admin','3'); INSERT INTO `b2b_roles` (`name`, `id`) VALUES ('super_admin','3');
INSERT INTO `b2b_roles` (`name`, `id`) VALUES ('unlogged','4');
-- insert sample admin user admin@ma-al.com/Maal12345678 -- insert sample admin user admin@ma-al.com/Maal12345678
@@ -58,4 +59,32 @@ INSERT INTO `b2b_role_permissions` (`role_id`, `permission_id`) VALUES ('3', '6'
INSERT INTO `b2b_role_permissions` (`role_id`, `permission_id`) VALUES ('3', '7'); INSERT INTO `b2b_role_permissions` (`role_id`, `permission_id`) VALUES ('3', '7');
INSERT INTO `b2b_role_permissions` (`role_id`, `permission_id`) VALUES ('3', '8'); INSERT INTO `b2b_role_permissions` (`role_id`, `permission_id`) VALUES ('3', '8');
INSERT INTO `b2b_role_permissions` (`role_id`, `permission_id`) VALUES ('3', '9'); INSERT INTO `b2b_role_permissions` (`role_id`, `permission_id`) VALUES ('3', '9');
INSERT INTO `b2b_route_roles` (`route_id`, `role_id`) VALUES
(1, '1'),
(1, '2'),
(1, '3'),
(2, '1'),
(2, '2'),
(2, '3'),
(3, '1'),
(3, '2'),
(3, '3'),
(3, '4'),
(4, '1'),
(4, '2'),
(4, '3'),
(4, '4'),
(5, '1'),
(5, '2'),
(5, '3'),
(5, '4'),
(6, '1'),
(6, '2'),
(6, '3'),
(6, '4'),
(7, '1'),
(7, '2'),
(7, '3'),
(7, '4');
-- +goose Down -- +goose Down

View File

@@ -319,7 +319,8 @@ DROP PROCEDURE IF EXISTS get_product_base //
CREATE PROCEDURE get_product_base( CREATE PROCEDURE get_product_base(
IN p_id_product INT, IN p_id_product INT,
IN p_id_shop INT, IN p_id_shop INT,
IN p_id_lang INT IN p_id_lang INT,
IN p_id_customer INT
) )
BEGIN BEGIN
SELECT SELECT
@@ -376,14 +377,12 @@ BEGIN
-- Relations -- Relations
m.name AS manufacturer, m.name AS manufacturer,
cl.name AS category cl.name AS category,
-- This doesn't fit to base product, I'll add proper is_favorite to product later EXISTS(
SELECT 1 FROM b2b_favorites f
-- EXISTS( WHERE f.user_id = p_id_customer AND f.product_id = p_id_product
-- SELECT 1 FROM b2b_favorites f ) AS is_favorite
-- WHERE f.user_id = p_id_customer AND f.product_id = p_id_product
-- ) AS is_favorite