Files
online-backend/internal/identity/rbac/service.go
GuwandykA 28509eb4ca
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled
Add backups, currencies and dashboard APIs, and scope access by guard_name.
Web users may only CRUD their own records, operators stay in assigned branches, and admin/manager/superadmin keep full access.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-31 18:14:47 +05:00

115 lines
3.0 KiB
Go

package rbac
import (
"context"
"errors"
"online-back/pkg/client/postgresql"
"github.com/jackc/pgx/v4"
)
type Service struct {
client postgresql.Client
}
func NewService(client postgresql.Client) *Service {
return &Service{client: client}
}
// Authorizer is the lookup Guard, RequirePermission and GuardUser need.
type Authorizer interface {
RoleLookup(ctx context.Context, userID int64) (code, guardName string, err error)
BranchIDs(ctx context.Context, userID int64) ([]int64, error)
HasPermission(ctx context.Context, userID int64, code string) (bool, error)
UserVisibleInBranches(ctx context.Context, userID int64, branchIDs []int64) (bool, error)
}
// HasPermission checks the role's baseline grant, then lets a per-user
// permission_user override win: an explicit "deny" always wins (even over
// another allow), an explicit "allow" grants a permission the role does
// not, and no override falls back to the role.
func (s *Service) HasPermission(ctx context.Context, userID int64, code string) (bool, error) {
const query = `
SELECT
CASE
WHEN EXISTS (
SELECT 1 FROM permission_user pu
JOIN permissions p ON p.id = pu.permission_id
WHERE pu.user_id = $1 AND p.code = $2 AND pu.effect = 'deny'
) THEN false
WHEN EXISTS (
SELECT 1 FROM permission_user pu
JOIN permissions p ON p.id = pu.permission_id
WHERE pu.user_id = $1 AND p.code = $2 AND pu.effect = 'allow'
) THEN true
ELSE EXISTS (
SELECT 1
FROM role_user ru
JOIN permission_role pr ON pr.role_id = ru.role_id
JOIN permissions p ON p.id = pr.permission_id
WHERE ru.user_id = $1 AND p.code = $2
)
END`
var granted bool
if err := s.client.QueryRow(ctx, query, userID, code).Scan(&granted); err != nil {
return false, err
}
return granted, nil
}
func (s *Service) RoleLookup(ctx context.Context, userID int64) (string, string, error) {
const query = `
SELECT r.code, r.guard_name
FROM role_user ru
JOIN roles r ON r.id = ru.role_id
WHERE ru.user_id = $1
ORDER BY ru.id
LIMIT 1`
var code, guardName string
err := s.client.QueryRow(ctx, query, userID).Scan(&code, &guardName)
if errors.Is(err, pgx.ErrNoRows) {
return "", "", nil
}
return code, guardName, err
}
func (s *Service) BranchIDs(ctx context.Context, userID int64) ([]int64, error) {
const query = `
SELECT bu.branch_id
FROM branch_user bu
WHERE bu.user_id = $1
ORDER BY bu.id`
rows, err := s.client.Query(ctx, query, userID)
if err != nil {
return nil, err
}
defer rows.Close()
var ids []int64
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
}
return ids, rows.Err()
}
func (s *Service) UserVisibleInBranches(ctx context.Context, userID int64, branchIDs []int64) (bool, error) {
if len(branchIDs) == 0 {
return false, nil
}
query := `SELECT ` + UserInBranchSQL("u.id", 2) + ` FROM users u WHERE u.id = $1`
var ok bool
err := s.client.QueryRow(ctx, query, userID, branchIDs).Scan(&ok)
if errors.Is(err, pgx.ErrNoRows) {
return false, nil
}
return ok, err
}