Skip to content

Commit

Permalink
Merge pull request #3 from limanmys/feature/new-vault-system
Browse files Browse the repository at this point in the history
Feature/new vault system
  • Loading branch information
dogukanoksuz authored Nov 6, 2024
2 parents a29d9eb + 9eb96d6 commit a5220d9
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 49 deletions.
4 changes: 0 additions & 4 deletions app/handlers/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 0 additions & 4 deletions app/handlers/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 0 additions & 4 deletions app/handlers/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 0 additions & 4 deletions app/handlers/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
24 changes: 9 additions & 15 deletions app/models/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 32 additions & 6 deletions internal/liman/role_system.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package liman

import (
"encoding/json"
"strings"

"github.com/gofiber/fiber/v2"
Expand All @@ -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 {
Expand All @@ -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" {
Expand Down Expand Up @@ -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
Expand All @@ -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" {
Expand All @@ -86,7 +87,32 @@ func getPermissionsFromMorph(morphID string, extFilter string) ([]string, map[st
}

if item.Type == "variable" {
varPerms[item.Key] = item.Value
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
}

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
}
}
}

Expand Down
6 changes: 0 additions & 6 deletions internal/process_queue/create_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
6 changes: 0 additions & 6 deletions pkg/cron_jobs/cron_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
11 changes: 11 additions & 0 deletions pkg/helpers/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit a5220d9

Please sign in to comment.