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) HasModuleAccess(ctx context.Context, userID int64, module string) (bool, error) UserVisibleInBranches(ctx context.Context, userID int64, branchIDs []int64) (bool, error) } // HasPermission checks whether the user's role grants the exact permission code. func (s *Service) HasPermission(ctx context.Context, userID int64, code string) (bool, error) { const query = ` SELECT 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 AND ru.deleted_at IS NULL AND pr.deleted_at IS NULL )` var granted bool if err := s.client.QueryRow(ctx, query, userID, code).Scan(&granted); err != nil { return false, err } return granted, nil } // HasModuleAccess is HasPermission matched against a permission's module // instead of its exact code, so a new permission code created under an // existing module (e.g. "loans") is recognized without a code change here. func (s *Service) HasModuleAccess(ctx context.Context, userID int64, module string) (bool, error) { const query = ` SELECT 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.module = $2 AND ru.deleted_at IS NULL AND pr.deleted_at IS NULL )` var granted bool if err := s.client.QueryRow(ctx, query, userID, module).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 AND ru.deleted_at IS NULL AND r.deleted_at IS NULL 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 JOIN branches b ON b.id = bu.branch_id WHERE bu.user_id = $1 AND bu.deleted_at IS NULL AND b.deleted_at IS NULL 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 AND u.deleted_at IS NULL` 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 }