Added function to SaveAll URL Access records into cache - tmp solution before worker gonna be working
This commit is contained in:
@@ -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