Bind permissions to roles and scope operator access by branch.
Replace permission_user with permission_role, enforce RBAC on admin routes, and limit operators to their assigned branches. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
427d5b4e07
commit
1fd0abbcb2
57 files changed
+1281
-657
No files matched your search
@@ -1,18 +1,12 @@
|
||||
// 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"
|
||||
"errors"
|
||||
|
||||
"online-back/pkg/client/postgresql"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
@@ -23,15 +17,14 @@ 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
|
||||
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
|
||||
)`
|
||||
|
||||
var granted bool
|
||||
@@ -40,3 +33,57 @@ func (s *Service) HasPermission(ctx context.Context, userID int64, code string)
|
||||
}
|
||||
return granted, nil
|
||||
}
|
||||
|
||||
func (s *Service) RoleCode(ctx context.Context, userID int64) (string, error) {
|
||||
const query = `
|
||||
SELECT r.code
|
||||
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 string
|
||||
err := s.client.QueryRow(ctx, query, userID).Scan(&code)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", nil
|
||||
}
|
||||
return code, 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
|
||||
}
|
||||
Reference in new issue
Block a user