changed arhitecture

This commit is contained in:
2026-07-15 18:08:19 +05:00
parent 7e3a5cebcc
commit 585ba63bc2
95 changed files with 4985 additions and 1360 deletions

View File

@@ -0,0 +1,38 @@
package rbac
import (
"net/http"
"strconv"
"online-back/internal/appresult"
"github.com/gin-gonic/gin"
)
// RequirePermission returns a gin middleware that 403s unless the
// authenticated user (set by appresult.JwtTokenCheck, which must run first)
// has been directly granted the given permission code. Not currently
// attached to any route.
func RequirePermission(service *Service, code string) gin.HandlerFunc {
return func(c *gin.Context) {
rawID, exists := c.Get("id")
if !exists {
c.AbortWithStatusJSON(http.StatusForbidden, appresult.ErrUnauthorized)
return
}
userID, err := strconv.ParseInt(rawID.(string), 10, 64)
if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, appresult.ErrUnauthorized)
return
}
granted, err := service.HasPermission(c.Request.Context(), userID, code)
if err != nil || !granted {
c.AbortWithStatusJSON(http.StatusForbidden, appresult.ErrUnauthorized)
return
}
c.Next()
}
}

View File

@@ -0,0 +1,42 @@
// Package rbac provides permission-check capability on top of the existing
// permissions/permission_user tables. It is not wired into any route yet —
// today every route is still gated only by appresult.JwtTokenCheck()'s
// admin-claim check. RequirePermission is available for routes to opt into
// once permission data is populated and specific routes are chosen.
//
// Note: the schema has no role_permission pivot table, so role-based
// permission resolution isn't possible yet — HasPermission only resolves
// permissions granted directly via permission_user.
package rbac
import (
"context"
"online-back/pkg/client/postgresql"
)
type Service struct {
client postgresql.Client
}
func NewService(client postgresql.Client) *Service {
return &Service{client: client}
}
// HasPermission reports whether userID has been directly granted the
// permission identified by code (permissions.code) via permission_user.
func (s *Service) HasPermission(ctx context.Context, userID int64, code string) (bool, error) {
const query = `
SELECT 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
)`
var granted bool
if err := s.client.QueryRow(ctx, query, userID, code).Scan(&granted); err != nil {
return false, err
}
return granted, nil
}

View File

@@ -0,0 +1,56 @@
package rbac
import (
"context"
"testing"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
)
type fakeClient struct {
granted bool
}
func (f *fakeClient) Exec(ctx context.Context, sql string, args ...interface{}) (pgconn.CommandTag, error) {
return nil, nil
}
func (f *fakeClient) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error) {
return nil, nil
}
func (f *fakeClient) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row {
return fakeRow{granted: f.granted}
}
func (f *fakeClient) Begin(ctx context.Context) (pgx.Tx, error) {
return nil, nil
}
type fakeRow struct {
granted bool
}
func (r fakeRow) Scan(dest ...interface{}) error {
*dest[0].(*bool) = r.granted
return nil
}
func TestHasPermission(t *testing.T) {
granted, err := NewService(&fakeClient{granted: true}).HasPermission(context.Background(), 1, "province.write")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !granted {
t.Fatal("expected permission to be granted")
}
granted, err = NewService(&fakeClient{granted: false}).HasPermission(context.Background(), 1, "province.write")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if granted {
t.Fatal("expected permission to not be granted")
}
}