From 3e72b556c531892f18b9871b2a5df950c435deef Mon Sep 17 00:00:00 2001 From: dogukanoksuz Date: Thu, 17 Oct 2024 17:51:56 +0300 Subject: [PATCH 1/3] refactor: extension handling in multiple handlers --- app/handlers/extension.go | 4 ---- app/handlers/external.go | 4 ---- app/handlers/file.go | 4 ---- app/handlers/job.go | 4 ---- app/models/extension.go | 24 +++++++++--------------- internal/process_queue/create_report.go | 6 ------ pkg/cron_jobs/cron_jobs.go | 6 ------ 7 files changed, 9 insertions(+), 43 deletions(-) diff --git a/app/handlers/extension.go b/app/handlers/extension.go index 4600059d..d3aa6e6a 100644 --- a/app/handlers/extension.go +++ b/app/handlers/extension.go @@ -27,10 +27,6 @@ func ExtensionRunner(c *fiber.Ctx) error { return err } - if extension.Status == "0" { - return logger.FiberError(fiber.StatusServiceUnavailable, "extension is unavailable") - } - credentials := &models.Credentials{} if extension.RequireKey == "true" { credentials, err = liman.GetCredentials( diff --git a/app/handlers/external.go b/app/handlers/external.go index f8d80b93..0168aca6 100644 --- a/app/handlers/external.go +++ b/app/handlers/external.go @@ -32,10 +32,6 @@ func ExternalAPI(c *fiber.Ctx) error { return logger.FiberError(fiber.StatusBadRequest, "extension not found") } - if extension.Status == "0" { - return logger.FiberError(fiber.StatusServiceUnavailable, "extension is unavailable") - } - credentials := &models.Credentials{} if extension.RequireKey == "true" { credentials, err = liman.GetCredentials( diff --git a/app/handlers/file.go b/app/handlers/file.go index e35f9bf1..565e6fed 100644 --- a/app/handlers/file.go +++ b/app/handlers/file.go @@ -112,10 +112,6 @@ func DownloadFile(c *fiber.Ctx) error { return err } - if extension.Status == "0" { - return logger.FiberError(fiber.StatusServiceUnavailable, "extension is unavailable") - } - credentials := &models.Credentials{} if extension.RequireKey == "true" { credentials, err = liman.GetCredentials( diff --git a/app/handlers/job.go b/app/handlers/job.go index 845f3b30..9ac34baf 100644 --- a/app/handlers/job.go +++ b/app/handlers/job.go @@ -23,10 +23,6 @@ func BackgroundJob(c *fiber.Ctx) error { return err } - if extension.Status == "0" { - return logger.FiberError(fiber.StatusServiceUnavailable, "extension is unavailable") - } - credentials := &models.Credentials{} if extension.RequireKey == "true" { credentials, err = liman.GetCredentials( diff --git a/app/models/extension.go b/app/models/extension.go index 2f788586..538a0530 100644 --- a/app/models/extension.go +++ b/app/models/extension.go @@ -2,21 +2,15 @@ package models // Extension Structure of the extension obj type Extension struct { - ID string `json:"id"` - Name string `json:"name"` - Version string `json:"version"` - Icon string `json:"-"` - Service string `json:"service"` - CreatedAt string `json:"-"` - UpdatedAt string `json:"-"` - Order int `json:"-"` - SslPorts string `json:"sslPorts" pg:"sslPorts"` - Issuer string `json:"issuer"` - Language string `json:"-"` - Support string `json:"support"` - Displays string `json:"displays"` - Status string `json:"status"` - RequireKey string `json:"require_key"` + ID string `json:"id"` + Name string `json:"name"` + Version string `json:"version"` + VersionCode string `json:"version_code"` + Icon string `json:"-"` + CreatedAt string `json:"-"` + UpdatedAt string `json:"-"` + SslPorts string `json:"sslPorts" pg:"sslPorts"` + RequireKey string `json:"require_key"` } func (Extension) TableName() string { diff --git a/internal/process_queue/create_report.go b/internal/process_queue/create_report.go index 23c78ed6..d93725c8 100644 --- a/internal/process_queue/create_report.go +++ b/internal/process_queue/create_report.go @@ -33,12 +33,6 @@ func (c CreateReport) Process() error { c.Queue.UpdateError(err.Error()) return err } - // Check extension status - if extension.Status == "0" { - // Update job as failed - c.Queue.UpdateError("extension is unavailable") - return err - } // Get credentials credentials := &models.Credentials{} diff --git a/pkg/cron_jobs/cron_jobs.go b/pkg/cron_jobs/cron_jobs.go index da02db12..9a5fb170 100644 --- a/pkg/cron_jobs/cron_jobs.go +++ b/pkg/cron_jobs/cron_jobs.go @@ -31,12 +31,6 @@ func RegisterAndRun(cj models.CronJob) error { cj.UpdateAsFailed(err.Error()) return } - // Check extension status - if extension.Status == "0" { - // Update job as failed - cj.UpdateAsFailed("extension is unavailable") - return - } // Get credentials credentials := &models.Credentials{} From db39479b7aedbd6e0bc415a6c639aafd0e0034bb Mon Sep 17 00:00:00 2001 From: dogukanoksuz Date: Fri, 18 Oct 2024 16:08:38 +0300 Subject: [PATCH 2/3] refactor: Update role_system.go to handle extension permissions and variables as interface maps --- internal/liman/role_system.go | 26 +++++++++++++++++++++----- pkg/helpers/maps.go | 11 +++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/internal/liman/role_system.go b/internal/liman/role_system.go index bf397eff..a783b0e1 100644 --- a/internal/liman/role_system.go +++ b/internal/liman/role_system.go @@ -1,6 +1,7 @@ package liman import ( + "encoding/json" "strings" "github.com/gofiber/fiber/v2" @@ -11,14 +12,14 @@ import ( ) // GetPermissions Gets user and extensions permissons and variables -func GetPermissions(user *models.User, extFilter string) ([]string, map[string]string, error) { +func GetPermissions(user *models.User, extFilter string) ([]string, map[string]interface{}, error) { roles, err := getRoleMaps(user) if err != nil { return nil, nil, err } permissions := []string{} - variables := make(map[string]string) + variables := make(map[string]interface{}) for _, role := range roles { permission, variable, err := getPermissionsFromMorph(role, strings.ToLower(extFilter)) if err != nil { @@ -27,7 +28,7 @@ func GetPermissions(user *models.User, extFilter string) ([]string, map[string]s permissions = append(permissions, permission...) - variables = helpers.MergeStringMaps(variables, variable) + variables = helpers.MergeInterfaceMaps(variables, variable) } if user.AuthType == "keycloak" { @@ -63,7 +64,7 @@ func GetObjectPermissions(user *models.User) ([]string, error) { } // getPermissionsFromMorph Searches db for morph relationships and returns permissions -func getPermissionsFromMorph(morphID string, extFilter string) ([]string, map[string]string, error) { +func getPermissionsFromMorph(morphID string, extFilter string) ([]string, map[string]interface{}, error) { permission := []*models.Permission{} err := database.Connection().Find(&permission, "morph_id = ?", morphID).Error @@ -72,7 +73,7 @@ func getPermissionsFromMorph(morphID string, extFilter string) ([]string, map[st } funcPerms := []string{} - varPerms := make(map[string]string) + varPerms := make(map[string]interface{}) for _, item := range permission { if item.Type == "function" { @@ -86,6 +87,21 @@ func getPermissionsFromMorph(morphID string, extFilter string) ([]string, map[st } if item.Type == "variable" { + if item.Extra == "multiselect" || item.Extra == "array" { + var value []interface{} + json.Unmarshal([]byte(item.Value), &value) + varPerms[item.Key] = value + continue + } + + // Check if item is a json object + if item.Extra == "json" { + var value interface{} + json.Unmarshal([]byte(item.Value), &value) + varPerms[item.Key] = value + continue + } + varPerms[item.Key] = item.Value } } diff --git a/pkg/helpers/maps.go b/pkg/helpers/maps.go index 8d5f7920..7452e8f3 100644 --- a/pkg/helpers/maps.go +++ b/pkg/helpers/maps.go @@ -10,3 +10,14 @@ func MergeStringMaps(ms ...map[string]string) map[string]string { } return res } + +// This function x interface maps together +func MergeInterfaceMaps(ms ...map[string]interface{}) map[string]interface{} { + res := map[string]interface{}{} + for _, m := range ms { + for k, v := range m { + res[k] = v + } + } + return res +} From 9eb96d6b2ed21429650817d444648d614f2d5675 Mon Sep 17 00:00:00 2001 From: dogukanoksuz Date: Wed, 6 Nov 2024 10:43:00 +0300 Subject: [PATCH 3/3] fix: Variable same key multiple item support --- internal/liman/role_system.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/liman/role_system.go b/internal/liman/role_system.go index a783b0e1..774dcba2 100644 --- a/internal/liman/role_system.go +++ b/internal/liman/role_system.go @@ -102,7 +102,17 @@ func getPermissionsFromMorph(morphID string, extFilter string) ([]string, map[st continue } - varPerms[item.Key] = item.Value + if existing, ok := varPerms[item.Key]; ok { + if existingSlice, ok := existing.([]string); ok { + existingSlice = append(existingSlice, item.Value) + varPerms[item.Key] = existingSlice + } else { + // If it's not a slice, convert it to a slice + varPerms[item.Key] = []string{existing.(string), item.Value} + } + } else { + varPerms[item.Key] = item.Value + } } }