Skip to content

Refactor Sales reports authorization into a reusable CodeIgniter filter #4655

Open

Description

@coderabbitai

Summary

The Sales controller repeats the reports_sales grant check in several per-record endpoints. The repeated controller logic uses different denial response types for browser routes and JSON routes.

Problem

app/Controllers/Sales.php contains duplicated authorization checks for Sales report actions, including receipt, invoice, edit, save, row, and email endpoints. This duplicates grant-check logic and makes response behavior harder to keep consistent as routes change.

Proposed resolution

Create a reusable CodeIgniter 4 before authorization filter that accepts a required grant as an argument.

Apply the filter to the applicable Sales routes that require reports_sales.

Keep authorization checks in controller methods when the decision depends on request or resource state. In particular, keep the sales_change_price check in Sales::postEditItem() because it depends on whether the submitted price differs from the current cart price.

Configure predictable denial responses by route type:

  • Return a redirect to no_access/sales/reports_sales for browser page routes.
  • Return an HTTP 403 JSON response for JSON/AJAX routes.

Affected areas

  • app/Filters/
  • app/Config/Filters.php
  • app/Config/Routes.php
  • app/Controllers/Sales.php
  • Relevant controller or route tests

Acceptance criteria

  • A reusable parameterized authorization before filter checks the current employee grant.
  • Applicable Sales routes use the filter for the reports_sales grant.
  • Unauthorized browser routes redirect to the existing access-denied page.
  • Unauthorized JSON/AJAX routes return HTTP 403 with the expected JSON error shape.
  • Sales::postEditItem() retains its state-dependent sales_change_price validation.
  • Tests cover authorized and unauthorized behavior for both browser and JSON route types.
  • The refactor does not change access rules for other Sales routes.

References

Activity

  1. jekkos commented on Aug 23, 2026

    @jekkos
    Member

    Sounds great that was what I was hinting at. Usually autjorization is a xross cutting concern and ideally its more config than code.

  2. objecttothis commented on Oct 22, 2026

    @objecttothis
    Member

    Claude Issue Checker Agent (cc @objecttothis)

    Investigated against current master.

    Status: Still present (refactor not done).

    app/Filters/ only contains Throttle.php and IsLoggedIn.php — no reusable grant/authorization filter exists. app/Controllers/Sales.php still repeats the inline check

    if (!$this->employee->has_grant('reports_sales', $personId)) { ... }

    individually in getManage(), getRow(), getSearch(), getSendPdf(), getSendReceipt(), postUnsuspend(), getEdit(), getReceipt(), and getInvoice(), with some returning a redirect() and others a 403 JSON response as described, but with no shared filter consolidating the logic. This refactor has not landed on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions