Added function to SaveAll URL Access records into cache - tmp solution before worker gonna be working
This commit is contained in:
@@ -32,14 +32,34 @@ func (r *URLAccessRepository) FindByID(id string) (*entity.URLAccess, error) {
|
||||
return &urlAccess, nil
|
||||
}
|
||||
|
||||
func (r *URLAccessRepository) FindAll() ([]entity.URLAccess, error) {
|
||||
sql := "SELECT id, roles, url, service FROM identity.url_access ORDER BY service"
|
||||
rows, err := r.db.Query(context.Background(), sql)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var results []entity.URLAccess
|
||||
for rows.Next() {
|
||||
var url entity.URLAccess
|
||||
if err := rows.Scan(&url.ID, &url.Roles, &url.URL, &url.Service); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, url)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (r *URLAccessRepository) Create(role *entity.URLAccess) (string, error) {
|
||||
var id string
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (r *URLAccessRepository) Update(role *entity.URLAccess) (*entity.URLAccess, error) {
|
||||
return &entity.URLAccess{}, nil
|
||||
func (r *URLAccessRepository) Update(role *entity.URLAccess) (entity.URLAccess, error) {
|
||||
return entity.URLAccess{}, nil
|
||||
}
|
||||
|
||||
func (r *URLAccessRepository) Delete(id int64) (bool, error) {
|
||||
|
||||
@@ -5,7 +5,7 @@ go 1.24.0
|
||||
toolchain go1.24.1
|
||||
|
||||
require (
|
||||
git.ego.freeddns.org/egommerce/api-entities v0.3.19
|
||||
git.ego.freeddns.org/egommerce/api-entities v0.3.20
|
||||
git.ego.freeddns.org/egommerce/go-api-pkg v0.4.9
|
||||
github.com/go-pg/migrations/v8 v8.1.0
|
||||
github.com/go-pg/pg/v10 v10.15.0
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
git.ego.freeddns.org/egommerce/api-entities v0.3.19 h1:hdw4IRnGiHxW9aU6fhasiKZAuwKfrCEvSuT1PFStqjc=
|
||||
git.ego.freeddns.org/egommerce/api-entities v0.3.19/go.mod h1:IqynARw+06GOm4eZGZuepmbi7bUxWBnOB4jd5cI7jf8=
|
||||
git.ego.freeddns.org/egommerce/api-entities v0.3.20 h1:CSxwbfsd2zYzwT985f3GaP6sTDXSwf3tq6GOQQ/MdHA=
|
||||
git.ego.freeddns.org/egommerce/api-entities v0.3.20/go.mod h1:IqynARw+06GOm4eZGZuepmbi7bUxWBnOB4jd5cI7jf8=
|
||||
git.ego.freeddns.org/egommerce/go-api-pkg v0.4.9 h1:Y9MisGDhl/ti4gsegl9MC7KoY2aHuyA0LvIESPoiPkE=
|
||||
git.ego.freeddns.org/egommerce/go-api-pkg v0.4.9/go.mod h1:Q4onxocNdFhzD9QnQK3ubd68chbJPexjDraEHoIEN3Y=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
|
||||
@@ -13,7 +13,7 @@ func (s *Server) AccessHandlerFn(c *fiber.Ctx) error {
|
||||
roleRepo := domain.NewRoleRepository(s.GetDatabase())
|
||||
urlRepo := domain.NewURLAccessRepository(s.GetDatabase())
|
||||
authSrv := service.NewAuthService(userRepo, s.GetCache())
|
||||
guardSrv := service.NewGuardService(authSrv, userRepo, roleRepo, urlRepo)
|
||||
guardSrv := service.NewGuardService(authSrv, s.GetCache(), userRepo, roleRepo, urlRepo)
|
||||
|
||||
url, srvName := c.Query("q"), c.Query("srv")
|
||||
header := new(dto.AuthorizationHeaderDTO)
|
||||
|
||||
@@ -19,9 +19,11 @@ func (s *Server) LoginHandlerFn(c *fiber.Ctx) error {
|
||||
roleRepo := domain.NewRoleRepository(s.GetDatabase())
|
||||
urlRepo := domain.NewURLAccessRepository(s.GetDatabase())
|
||||
authSrv := service.NewAuthService(userRepo, s.GetCache())
|
||||
guardSrv := service.NewGuardService(authSrv, userRepo, roleRepo, urlRepo)
|
||||
guardSrv := service.NewGuardService(authSrv, s.GetCache(), userRepo, roleRepo, urlRepo)
|
||||
|
||||
token, err := ui.NewLoginActionUI(authSrv, guardSrv).Execute(data)
|
||||
guardSrv.CacheAllPermissions() // FIXME: Move it to the worker and fire-up as a CRONJOB
|
||||
|
||||
token, err := ui.NewLoginActionUI(authSrv).Execute(data)
|
||||
if err != nil { // TODO: handle other response status codes -- add struct to decorate error with code and message
|
||||
return s.Error(c, fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
dto "git.ego.freeddns.org/egommerce/api-entities/identity/dto"
|
||||
entity "git.ego.freeddns.org/egommerce/api-entities/identity/entity"
|
||||
|
||||
domain "git.ego.freeddns.org/egommerce/identity-service/domain/repository"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
|
||||
@@ -1,22 +1,30 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
dto "git.ego.freeddns.org/egommerce/api-entities/identity/dto"
|
||||
entity "git.ego.freeddns.org/egommerce/api-entities/identity/entity"
|
||||
|
||||
domain "git.ego.freeddns.org/egommerce/identity-service/domain/repository"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
type GuardService struct {
|
||||
authSrv *AuthService
|
||||
cache *redis.Client
|
||||
userRepo *domain.UserRepository
|
||||
roleRepo *domain.RoleRepository
|
||||
urlRepo *domain.URLAccessRepository
|
||||
}
|
||||
|
||||
func NewGuardService(authSrv *AuthService, userRepo *domain.UserRepository, roleRepo *domain.RoleRepository, urlRepo *domain.URLAccessRepository) *GuardService {
|
||||
func NewGuardService(authSrv *AuthService, cache *redis.Client, userRepo *domain.UserRepository, roleRepo *domain.RoleRepository, urlRepo *domain.URLAccessRepository) *GuardService {
|
||||
return &GuardService{
|
||||
authSrv: authSrv,
|
||||
cache: cache,
|
||||
userRepo: userRepo,
|
||||
roleRepo: roleRepo,
|
||||
urlRepo: urlRepo,
|
||||
@@ -40,3 +48,36 @@ func (g *GuardService) CheckUserPermissions(authHeader *dto.AuthorizationHeaderD
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Move below functions to a worker and fire-up it in the CRONJOB
|
||||
// func (g *GuardService) fetchURLAccessFromCache() {}
|
||||
|
||||
func (g *GuardService) CacheAllPermissions() error {
|
||||
urls, err := g.urlRepo.FindAll()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var urlsArr = make(map[string][]entity.URLAccess)
|
||||
for _, url := range urls {
|
||||
urlsArr[url.Service] = append(urlsArr[url.Service], url)
|
||||
}
|
||||
|
||||
for service, url := range urlsArr {
|
||||
json, err := json.Marshal(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
jsonUrl := string(json)
|
||||
|
||||
if err := g.cache.HSet(context.Background(), "urls_access", service, jsonUrl).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := g.cache.Expire(context.Background(), "urls_access", time.Duration(time.Hour)).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
cnf "git.ego.freeddns.org/egommerce/go-api-pkg/config"
|
||||
|
||||
"github.com/golang-jwt/jwt"
|
||||
)
|
||||
|
||||
|
||||
@@ -6,14 +6,12 @@ import (
|
||||
)
|
||||
|
||||
type LoginActionUI struct {
|
||||
authSrv *service.AuthService
|
||||
guardSrv *service.GuardService
|
||||
authSrv *service.AuthService
|
||||
}
|
||||
|
||||
func NewLoginActionUI(authSrv *service.AuthService, guardSrv *service.GuardService) *LoginActionUI {
|
||||
func NewLoginActionUI(authSrv *service.AuthService) *LoginActionUI {
|
||||
return &LoginActionUI{
|
||||
authSrv: authSrv,
|
||||
guardSrv: guardSrv,
|
||||
authSrv: authSrv,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user