Added FindByURLAndServiceForRole func to URLAccessRepository

This commit is contained in:
PB
2025-10-22 13:41:04 +02:00
parent 0321f7b767
commit 07dbe290f1
3 changed files with 34 additions and 5 deletions

View File

@@ -20,9 +20,9 @@ func NewRoleRepository(db *pgxpool.Pool) *RoleRepository {
func (r *RoleRepository) FindByID(id string) (*entity.Role, error) {
var role entity.Role
sql := `SELECT id, roles, url FROM identity.roles WHERE id=$1 LIMIT 1`
sql := `SELECT id, name, display_name FROM identity.roles WHERE id=$1 LIMIT 1`
err := r.db.QueryRow(context.Background(), sql, id).
Scan(&role.ID, &role.Roles, &role.URL)
Scan(&role.ID, &role.Name, &role.DisplayName)
if err != nil {
return nil, errors.New("failed to fetch role from DB: " + err.Error())
}
@@ -53,3 +53,12 @@ func (r *RoleRepository) Update(role *entity.Role) (*entity.Role, error) {
func (r *RoleRepository) Delete(id int64) (bool, error) {
return true, nil
}
func (r *RoleRepository) GetUserRole(user *entity.User) *entity.Role {
role := new(entity.Role)
sql := `SELECT r.id, r.name, r.display_name FROM identity.roles r JOIN identity.users_roles ur ON r.id = ur.role_id WHERE ur.user_id=$1 LIMIT 1`
r.db.QueryRow(context.Background(), sql, user.ID).Scan(&role.ID, &role.Name, &role.DisplayName)
return role
}

View File

@@ -3,8 +3,10 @@ package repository
import (
"context"
"errors"
"fmt"
entity "git.ego.freeddns.org/egommerce/api-entities/identity/entity"
"git.ego.freeddns.org/egommerce/go-api-pkg/database"
"github.com/jackc/pgx/v5/pgxpool"
)
@@ -56,3 +58,24 @@ func (r *URLAccessRepository) FindByURLAndService(url, service string) (*entity.
return &urlAccess, nil
}
func (r *URLAccessRepository) FindByURLAndServiceForRole(url, service, role string) (*entity.URLAccess, error) {
var entity entity.URLAccess
sql := fmt.Sprintf("SELECT id, roles, url, service FROM identity.url_access WHERE url=$1 AND service=$2 AND roles::jsonb @> '[\"%s\"]'::jsonb LIMIT 1", role)
err := r.db.QueryRow(context.Background(), sql, url, service).
Scan(&entity.ID, &entity.Roles, &entity.URL, &entity.Service)
if err != nil {
if err = database.NoRowsInQuerySet(err); err != nil {
return nil, errors.New("no url found for: " + url + " and role: " + role)
}
return nil, errors.New("failed to fetch url_access from DB: " + err.Error())
}
return &entity, nil
}
func (r *URLAccessRepository) FindForUser(user *entity.User) {
}

View File

@@ -48,9 +48,6 @@ type JWT struct {
}
func (s *JWT) CreateAccessToken(id string) (string, error) {
fmt.Println(time.Now().Add(s.accessTokenExpireTime).Unix())
fmt.Println(s.accessTokenExpireTime)
claims := &jwt.StandardClaims{
Subject: id,
IssuedAt: time.Now().Unix(),