This commit is contained in:
2026-02-11 17:46:22 +03:00
commit eacfacb13b
266 changed files with 51337 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
package authz
import "context"
type scopeKey string
const (
CtxDeptCodesKey scopeKey = "authz.dept_codes"
CtxPiyasaCodesKey scopeKey = "authz.piyasa_codes"
)
func WithDeptCodes(ctx context.Context, codes []string) context.Context {
return context.WithValue(ctx, CtxDeptCodesKey, codes)
}
func WithPiyasaCodes(ctx context.Context, codes []string) context.Context {
return context.WithValue(ctx, CtxPiyasaCodesKey, codes)
}
func GetDeptCodesFromCtx(ctx context.Context) []string {
if v := ctx.Value(CtxDeptCodesKey); v != nil {
if codes, ok := v.([]string); ok {
return codes
}
}
return nil
}
func GetPiyasaCodesFromCtx(ctx context.Context) []string {
if v := ctx.Value(CtxPiyasaCodesKey); v != nil {
if codes, ok := v.([]string); ok {
return codes
}
}
return nil
}

View File

@@ -0,0 +1,32 @@
package authz
import (
"context"
"fmt"
"strings"
)
func BuildMSSQLPiyasaFilter(
ctx context.Context,
column string,
) string {
codes := GetPiyasaCodesFromCtx(ctx)
if len(codes) == 0 {
return "1=1"
}
var quoted []string
for _, c := range codes {
quoted = append(quoted, "'"+c+"'")
}
return fmt.Sprintf(
"%s IN (%s)",
column,
strings.Join(quoted, ","),
)
}

View File

@@ -0,0 +1,24 @@
package authz
import (
"fmt"
"strings"
)
func BuildINClause(column string, codes []string) string {
if len(codes) == 0 {
return "1=0"
}
var quoted []string
for _, c := range codes {
c = strings.TrimSpace(strings.ToUpper(c))
if c == "" {
continue
}
quoted = append(quoted, "'"+c+"'")
}
if len(quoted) == 0 {
return "1=0"
}
return fmt.Sprintf("%s IN (%s)", column, strings.Join(quoted, ","))
}

View File

@@ -0,0 +1,74 @@
package authz
import (
"database/sql"
"fmt"
"sync"
)
// =====================================================
// 🧠 PIYASA CACHE (USER → CODES)
// =====================================================
var (
piyasaCache = make(map[int][]string)
piyasaMu sync.RWMutex
)
// =====================================================
// 📌 GET USER PIYASA CODES (CACHED)
// =====================================================
func GetUserPiyasaCodes(pg *sql.DB, userID int) ([]string, error) {
// -----------------------------
// CACHE READ
// -----------------------------
piyasaMu.RLock()
if it, ok := piyasaCache[userID]; ok {
piyasaMu.RUnlock()
return it, nil
}
piyasaMu.RUnlock()
// -----------------------------
// DB QUERY
// -----------------------------
rows, err := pg.Query(`
SELECT piyasa_code
FROM dfusr_piyasa
WHERE dfusr_id = $1
AND is_allowed = true
`, userID)
if err != nil {
return nil, fmt.Errorf("pg piyasa query error: %w", err)
}
defer rows.Close()
var out []string
for rows.Next() {
var code string
if err := rows.Scan(&code); err == nil {
out = append(out, code)
}
}
// -----------------------------
// CACHE WRITE
// -----------------------------
piyasaMu.Lock()
piyasaCache[userID] = out
piyasaMu.Unlock()
return out, nil
}
// =====================================================
// 🧹 CLEAR USER PIYASA CACHE
// =====================================================
func ClearPiyasaCache(userID int) {
piyasaMu.Lock()
defer piyasaMu.Unlock()
delete(piyasaCache, userID)
}