Closed
Conversation
|
|
nomeguy
marked this pull request as ready for review
November 9, 2025 18:03
… functions Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add support for loading policies from database on startup
Add LoadPolicy API for database adapters
Nov 9, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
EctoAdapter auto-saves policies to the database but provides no built-in way to load them back into memory on startup, forcing manual database queries and loops. This implements the missing LoadPolicy API following Casbin conventions from Go/Java adapters.
Changes
EnforcerServer.load_policies_from_adapter/1- Loads policies from configured persist adapter into memoryEnforcerServer.load_mapping_policies_from_adapter/1- Loads role mappings from configured persist adapter into memoryEnforcer.load_policies!/1andEnforcer.load_mapping_policies!/1(no-argument variants)Usage
Eliminates the previous workaround:
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
esm.ubuntu.com/usr/lib/apt/methods/https(dns block)repo.hex.pm/usr/lib/erlang/erts-13.2.2.5/bin/inet_gethost 4(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
This section details on the original issue you should resolve
<issue_title>EctoAdapter: No Built-in Way to Load Policies from Database on Startup</issue_title>
<issue_description>## Summary
The EctoAdapter automatically saves policies to the database but provides no clean way to load them back into the enforcer's memory on application startup. This creates an asymmetric API and forces developers to implement manual workarounds.
Current Behavior
What Works (Auto-Save)
What Doesn't Work (No Auto-Load)
Expected API
Current Workaround (Manual Implementation)
Developers must implement their own loading logic by querying the database directly and manually adding each policy:
Why This is Problematic
But: There's no function to feed these policies back into the enforcer's memory. Developers must manually loop through and add each one.
Proposed Solutions
Option 1: Add load_policies_from_adapter/1
EnforcerServer.load_policies_from_adapter("my_enforcer")
Option 2: Enhance set_persist_adapter/3 with options
EnforcerServer.set_persist_adapter("my_enforcer", adapter, auto_load: true)
Option 3: Make load_policies/2 adapter-aware
Accept atom :adapter as second parameter
EnforcerServer.load_policies("my_enforcer", :adapter)
Option 4: Add Enforcer.load_policies_from_adapter/2
enforcer = Enforcer.init(model_path)
enforcer = Enforcer.load_policies_from_adapter(enforcer, adapter)</issue_description>
Comments on the Issue (you are @copilot in this section)
@hsluoyz Casbin adapters use LoadPolicy API to load policy from DBWhy not use LoadPolicy? If LoadPolicy is not supported, need to support it
Refer to Golang's adapters:
<comment_new>@nomeguy
@sushilbansal already supported, see: Add LoadPolicy API for database adapters #41</comment_new>
<comment_new>@nomeguy
About how to design an adapter, refer to:
don't add docs</comment_new>
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.