43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
// 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
|
|
}
|