Skip to content

Add LoadPolicy API for database adapters - #45

Closed
nomeguy with Copilot wants to merge 3 commits into
masterfrom
copilot/add-auto-load-policies-another-one
Closed

Add LoadPolicy API for database adapters#45
nomeguy with Copilot wants to merge 3 commits into
masterfrom
copilot/add-auto-load-policies-another-one

Conversation

Copilot AI commented Nov 9, 2025

Copy link
Copy Markdown
Contributor

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 memory
  • EnforcerServer.load_mapping_policies_from_adapter/1 - Loads role mappings from configured persist adapter into memory
  • Both functions delegate to existing Enforcer.load_policies!/1 and Enforcer.load_mapping_policies!/1 (no-argument variants)

Usage

# Configure adapter
adapter = EctoAdapter.new(Repo)
EnforcerServer.set_persist_adapter("my_enforcer", adapter)

# Load from database on startup
EnforcerServer.load_policies_from_adapter("my_enforcer")
EnforcerServer.load_mapping_policies_from_adapter("my_enforcer")

# Policies now in memory
EnforcerServer.allow?("my_enforcer", ["admin", "data", "write"])

Eliminates the previous workaround:

# Before: manual DB queries and loops
rules = Repo.all(CasbinRule)
Enum.each(rules, fn rule ->
  # Parse and add each rule manually...
end)

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
    • Triggering command: /usr/lib/apt/methods/https (dns block)
  • repo.hex.pm
    • Triggering command: /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)

# Configure adapter
adapter = EctoAdapter.new(Repo)
EnforcerServer.set_persist_adapter("my_enforcer", adapter)

# Add policy - automatically saved to DB
EnforcerServer.add_policy("my_enforcer", {:p, ["admin", "data", "write", "org:abc"]})
# ✅ Policy is now in both memory AND database

What Doesn't Work (No Auto-Load)

# Application restart...

# Re-configure adapter
adapter = EctoAdapter.new(Repo)
EnforcerServer.set_persist_adapter("my_enforcer", adapter)

# ❌ Policies from database are NOT loaded into memory
EnforcerServer.allow?("my_enforcer", ["admin", "data", "write", "org:abc"])
# => false (policies exist in DB but not in memory)

Expected API

There should be a clean, built-in way to load policies from the database:
# Option 1: Automatic on adapter setup
adapter = EctoAdapter.new(Repo)
EnforcerServer.set_persist_adapter("my_enforcer", adapter, load: true)

# Option 2: Explicit load function
EnforcerServer.load_policies_from_adapter("my_enforcer")

# Option 3: Enhanced load_policies that works with adapters
EnforcerServer.load_policies("my_enforcer", :from_adapter)

Current Workaround (Manual Implementation)

Developers must implement their own loading logic by querying the database directly and manually adding each policy:

defp load_policies_from_db do
  # Manually query the database
  rules = Repo.all(Acx.Persist.EctoAdapter.CasbinRule)

  # Manually add each rule to the enforcer's memory
  Enum.each(rules, fn rule ->
    case rule.ptype do
      "p" ->
        attrs = build_attrs([rule.v0, rule.v1, rule.v2, rule.v3, rule.v4, rule.v5, rule.v6])
        EnforcerServer.add_policy(@enforcer_name, {:p, attrs})


      "g" ->
        attrs = build_attrs([rule.v0, rule.v1, rule.v2])
        case length(attrs) do
          3 ->
            [child, parent, domain] = attrs
            EnforcerServer.add_mapping_policy(@enforcer_name, {:g, child, parent, domain})
          2 ->
            [child, parent] = attrs
            EnforcerServer.add_mapping_policy(@enforcer_name, {:g, child, parent})
        end
    end
  end)
end

defp build_attrs(values) do
  Enum.reject(values, &is_nil/1)
end

Why This is Problematic

  1. The set_persist_adapter/2 function suggests persistence is fully configured, but it only handles saves, not loads.
  2. New users expect load_policies/2 to work with adapters, but it only accepts file paths:
# This signature is misleading:
EnforcerServer.load_policies(name, file)
# "file" parameter suggests it ONLY loads from files
  1. Comparison with PersistAdapter.load_policies/1
The library DOES have a function that retrieves policies from the adapter:
{:ok, {policies, grouping_policies}} = Acx.Persist.PersistAdapter.load_policies(adapter)

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 DB

Why not use LoadPolicy? If LoadPolicy is not supported, need to support it

Refer to Golang's adapters:

don't add docs</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@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 November 9, 2025 18:03
Copilot AI and others added 2 commits November 9, 2025 18:12
… 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
Copilot AI requested a review from nomeguy November 9, 2025 18:20
@hsluoyz hsluoyz closed this Jan 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EctoAdapter: No Built-in Way to Load Policies from Database on Startup

4 participants