Skip to content

feat: add load_filtered_policy() API - #38

Merged
nomeguy merged 9 commits into
masterfrom
copilot/add-filtered-policy-loading
Nov 1, 2025
Merged

feat: add load_filtered_policy() API#38
nomeguy merged 9 commits into
masterfrom
copilot/add-filtered-policy-loading

Conversation

Copilot AI commented Oct 31, 2025

Copy link
Copy Markdown
Contributor

Fix: #37

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@nomeguy
nomeguy marked this pull request as ready for review October 31, 2025 16:54
Copilot AI and others added 3 commits October 31, 2025 17:01
Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
@nomeguy

nomeguy commented Oct 31, 2025

Copy link
Copy Markdown

@copilot fix CI build error:

Run mix compile --warnings-as-errors
  mix compile --warnings-as-errors
  shell: /usr/bin/bash -e {0}
  env:
    INSTALL_DIR_FOR_OTP: /home/runner/work/_temp/.setup-beam/otp
    INSTALL_DIR_FOR_ELIXIR: /home/runner/work/_temp/.setup-beam/elixir
Compiling 5 files (.ex)
warning: variable "r" does not exist and is being expanded to "r()", please use parentheses to remove the ambiguity or change the variable name
Warning:   lib/acx/persist/ecto_adapter.ex:174: Acx.Persist.PersistAdapter.Acx.Persist.EctoAdapter.build_filtered_query/1

warning: variable "values" is unused (if the variable is not meant to be used, prefix it with an underscore)
Warning:   lib/acx/persist/readonly_file_adapter.ex:81: Acx.Persist.PersistAdapter.Acx.Persist.ReadonlyFileAdapter.get_policy_value/2

warning: variable "r" does not exist and is being expanded to "r()", please use parentheses to remove the ambiguity or change the variable name
Warning:   lib/acx/persist/ecto_adapter.ex:176: Acx.Persist.PersistAdapter.Acx.Persist.EctoAdapter.build_filtered_query/1


== Compilation error in file lib/acx/persist/ecto_adapter.ex ==
Error: ** (CompileError) lib/acx/persist/ecto_adapter.ex:176: cannot use ^values outside of match clauses
    (elixir 1.14.2) expanding macro: Kernel.in/2
    lib/acx/persist/ecto_adapter.ex:176: Acx.Persist.PersistAdapter.Acx.Persist.EctoAdapter.build_filtered_query/1
Error: Process completed with exit code 1.

Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for filtered policy loading Add filtered policy loading for multi-tenant applications Oct 31, 2025
Copilot AI requested a review from nomeguy October 31, 2025 17:15
…warning

Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>

Copilot AI commented Oct 31, 2025

Copy link
Copy Markdown
Contributor Author

@copilot fix CI build error:

...

Fixed in commit b18b737. Added import Ecto.Query to the functions using query macros (build_filtered_query/1 and add_where_clause/3) and prefixed unused variable with underscore in ReadonlyFileAdapter.

@nomeguy

nomeguy commented Oct 31, 2025

Copy link
Copy Markdown

@copilot fix CI build error:

Run mix test
  mix test
  shell: /usr/bin/bash -e {0}
  env:
    INSTALL_DIR_FOR_OTP: /home/runner/work/_temp/.setup-beam/otp
    INSTALL_DIR_FOR_ELIXIR: /home/runner/work/_temp/.setup-beam/elixir
==> credo
Compiling 251 files (.ex)
Generated credo app
==> ecto
Compiling 56 files (.ex)
Generated ecto app
==> ecto_sql
Compiling 25 files (.ex)
Generated ecto_sql app
==> acx
Compiling 6 files (.ex)
Generated acx app
.................................................................................................................................................................................................................................................................................................
== Compilation error in file test/persist/ecto_acl_test.exs ==
Error: ** (CompileError) test/persist/ecto_acl_test.exs:7: def all/2 defines defaults multiple times. Elixir allows defaults to be declared once per definition. Instead of:

    def foo(:first_clause, b \\ :default) do ... end
    def foo(:second_clause, b \\ :default) do ... end

one should write:

    def foo(a, b \\ :default)
    def foo(:first_clause, b) do ... end
    def foo(:second_clause, b) do ... end

    test/persist/ecto_acl_test.exs:7: (module)
    test/persist/ecto_acl_test.exs:6: (module)
    (elixir 1.14.2) lib/kernel/parallel_compiler.ex:455: Kernel.ParallelCompiler.require_file/2
    (elixir 1.14.2) lib/kernel/parallel_compiler.ex:348: anonymous fn/5 in Kernel.ParallelCompiler.spawn_workers/7
Error: Process completed with exit code 1.

…efaults

Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>

Copilot AI commented Oct 31, 2025

Copy link
Copy Markdown
Contributor Author

@copilot fix CI build error:

...

Fixed in commit f10cbc7. Changed MockRepo to use a single function head with default parameters followed by pattern-matched implementations, as required by Elixir.

@nomeguy

nomeguy commented Oct 31, 2025

Copy link
Copy Markdown

@copilot fix:

Run mix test
  mix test
  shell: /usr/bin/bash -e {0}
  env:
    INSTALL_DIR_FOR_OTP: /home/runner/work/_temp/.setup-beam/otp
    INSTALL_DIR_FOR_ELIXIR: /home/runner/work/_temp/.setup-beam/elixir
==> credo
Compiling 251 files (.ex)
Generated credo app
==> ecto
Compiling 56 files (.ex)
Generated ecto app
==> ecto_sql
Compiling 25 files (.ex)
Generated ecto_sql app
==> acx
Compiling 6 files (.ex)
Generated acx app
................................................................................................................................................................................................................................................................................................................................................................................................

  1) test load_filtered_policy/2 with ReadonlyFileAdapter filters policies by domain (Acx.Persist.ReadonlyFileAdapterFilteredTest)
Error:      test/persist/readonly_file_adapter_filtered_test.exs:11
     Assertion with == failed
     code:  assert length(policies) == 2
     left:  1
     right: 2
     stacktrace:
       test/persist/readonly_file_adapter_filtered_test.exs:18: (test)

.

  2) test load_filtered_policy/2 with ReadonlyFileAdapter filters policies by multiple criteria (Acx.Persist.ReadonlyFileAdapterFilteredTest)
Error:      test/persist/readonly_file_adapter_filtered_test.exs:33
     Assertion with == failed
     code:  assert length(policies) == 2
     left:  0
     right: 2
     stacktrace:
       test/persist/readonly_file_adapter_filtered_test.exs:40: (test)



  3) test load_filtered_policies!/2 with Enforcer and ReadonlyFileAdapter loads only filtered policies into enforcer (Acx.Persist.ReadonlyFileAdapterFilteredTest)
Error:      test/persist/readonly_file_adapter_filtered_test.exs:75
     Assertion with == failed
     code:  assert length(policies) == 2
     left:  0
     right: 2
     stacktrace:
       test/persist/readonly_file_adapter_filtered_test.exs:86: (test)

...

  4) test load_filtered_policy/2 with ReadonlyFileAdapter filters policies by list of values (Acx.Persist.ReadonlyFileAdapterFilteredTest)
Error:      test/persist/readonly_file_adapter_filtered_test.exs:46
     Assertion with == failed
     code:  assert length(policies) == 4
     left:  3
     right: 4
     stacktrace:
       test/persist/readonly_file_adapter_filtered_test.exs:53: (test)

..................................................................................................................................................................................
Finished in 0.8 seconds (0.8s async, 0.00s sync)
104 doctests, 466 tests, 4 failures

Randomized with seed 910273
Error: Process completed with exit code 2.

Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>

Copilot AI commented Oct 31, 2025

Copy link
Copy Markdown
Contributor Author

@copilot fix:

...

Fixed in commit 749beba. The tests were using incorrect field mappings - the domain field in the RBAC domain CSV is at position v1 (not v2). Updated all test filters to use v1 for domain filtering.

@nomeguy

nomeguy commented Oct 31, 2025

Copy link
Copy Markdown

@sushilbansal

@nomeguy

nomeguy commented Nov 1, 2025

Copy link
Copy Markdown

@copilot for the code and test code of load_filtered_policy, should also refer to Golang Casbin: https://github.com/casbin/casbin

Copilot AI commented Nov 1, 2025

Copy link
Copy Markdown
Contributor Author

@copilot for the code and test code of load_filtered_policy, should also refer to Golang Casbin: https://github.com/casbin/casbin

The implementation already follows the Golang Casbin pattern for filtered policy loading. The filter uses a map with keys (ptype, v0-v6) matching field positions, supports both single values and lists for OR matching, and integrates properly with the enforcer. The field positions (v0, v1, v2, etc.) map to the policy definition order in the model config - for example, in RBAC domain model p = sub, dom, obj, act, domain is at v1.

@nomeguy

nomeguy commented Nov 1, 2025

Copy link
Copy Markdown

@copilot don't remove mix.lock file

Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>

Copilot AI commented Nov 1, 2025

Copy link
Copy Markdown
Contributor Author

@copilot don't remove mix.lock file

Restored in commit bf57adb. The mix.lock file has been added back from the original state before it was accidentally deleted.

@nomeguy nomeguy changed the title Add filtered policy loading for multi-tenant applications feat: add load_filtered_policy() API Nov 1, 2025
@nomeguy
nomeguy merged commit 0e856ac into master Nov 1, 2025
3 of 4 checks passed
github-actions Bot pushed a commit that referenced this pull request Nov 1, 2025
# [1.4.0](v1.3.0...v1.4.0) (2025-11-01)

### Features

* add load_filtered_policy() API ([#38](#38)) ([0e856ac](0e856ac))
@github-actions

github-actions Bot commented Nov 1, 2025

Copy link
Copy Markdown

🎉 This PR is included in version 1.4.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request for Filtered Policy Loading

3 participants