Added FindByURLAndServiceForRole func to URLAccessRepository
This commit is contained in:
@@ -20,9 +20,9 @@ func NewRoleRepository(db *pgxpool.Pool) *RoleRepository {
|
|||||||
func (r *RoleRepository) FindByID(id string) (*entity.Role, error) {
|
func (r *RoleRepository) FindByID(id string) (*entity.Role, error) {
|
||||||
var role entity.Role
|
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).
|
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 {
|
if err != nil {
|
||||||
return nil, errors.New("failed to fetch role from DB: " + err.Error())
|
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) {
|
func (r *RoleRepository) Delete(id int64) (bool, error) {
|
||||||
return true, nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,8 +3,10 @@ package repository
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
entity "git.ego.freeddns.org/egommerce/api-entities/identity/entity"
|
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"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
)
|
)
|
||||||
@@ -56,3 +58,24 @@ func (r *URLAccessRepository) FindByURLAndService(url, service string) (*entity.
|
|||||||
|
|
||||||
return &urlAccess, nil
|
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) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -48,9 +48,6 @@ type JWT struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *JWT) CreateAccessToken(id string) (string, error) {
|
func (s *JWT) CreateAccessToken(id string) (string, error) {
|
||||||
fmt.Println(time.Now().Add(s.accessTokenExpireTime).Unix())
|
|
||||||
fmt.Println(s.accessTokenExpireTime)
|
|
||||||
|
|
||||||
claims := &jwt.StandardClaims{
|
claims := &jwt.StandardClaims{
|
||||||
Subject: id,
|
Subject: id,
|
||||||
IssuedAt: time.Now().Unix(),
|
IssuedAt: time.Now().Unix(),
|
||||||
|
|||||||
Reference in New Issue
Block a user