Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Casbin-Ex

[![GitHub Actions](https://github.com/casbin/casbin-ex/actions/workflows/ci.yml/badge.svg)](https://github.com/casbin/casbin-ex/actions/workflows/ci.yml)
[![Hex.pm](https://img.shields.io/hexpm/v/acx.svg)](https://hex.pm/packages/acx)
[![Hex.pm](https://img.shields.io/hexpm/v/casbin.svg)](https://hex.pm/packages/casbin)
[![Release](https://img.shields.io/github/release/casbin/casbin-ex.svg)](https://github.com/casbin/casbin-ex/releases/latest)
[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN)

Expand All @@ -27,12 +27,12 @@ https://casbin.org/docs/overview

## Installation

The package can be installed by adding `acx` to your list of dependencies in `mix.exs`:
The package can be installed by adding `casbin` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:acx, "~> 1.6"}
{:casbin, "~> 1.6"}
]
end
```
Expand All @@ -42,7 +42,7 @@ Or install from GitHub:
```elixir
def deps do
[
{:acx, git: "https://github.com/casbin/casbin-ex"}
{:casbin, git: "https://github.com/casbin/casbin-ex"}
]
end
```
Expand Down Expand Up @@ -126,7 +126,7 @@ The final step is to combine the model, the policy rules and Casbin-Ex to
construct our access control system.

```elixir
alias Acx.{EnforcerSupervisor, EnforcerServer}
alias Casbin.{EnforcerSupervisor, EnforcerServer}

# Give our system a name so that we can reference it by its name
# rather than the process ID (a.k.a `pid`).
Expand Down Expand Up @@ -220,7 +220,7 @@ g, admin, author
Finally:

```elixir
alias Acx.{EnforcerSupervisor, EnforcerServer}
alias Casbin.{EnforcerSupervisor, EnforcerServer}

ename = "blog_ac"
EnforcerSupervisor.start_enforcer(ename, blog_ac.conf)
Expand Down Expand Up @@ -281,7 +281,7 @@ p, peter, /peter_data, (GET)|(POST)
Code:

```elixir
alias Acx.{EnforcerSupervisor, EnforcerServer}
alias Casbin.{EnforcerSupervisor, EnforcerServer}

ename = "restful_ac"
EnforcerSupervisor.start_enforcer(ename, restful_ac.conf)
Expand Down
12 changes: 6 additions & 6 deletions guides/sandbox_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule MyApp.RolesTest do
Ecto.Adapters.SQL.Sandbox.mode(MyApp.Repo, {:shared, self()})

# Allow the EnforcerServer process to access the connection
case Registry.lookup(Acx.EnforcerRegistry, "my_enforcer") do
case Registry.lookup(Casbin.EnforcerRegistry, "my_enforcer") do
[{enforcer_pid, _}] ->
Ecto.Adapters.SQL.Sandbox.allow(MyApp.Repo, self(), enforcer_pid)
[] ->
Expand All @@ -52,7 +52,7 @@ defmodule MyApp.RolesTest do
assert {:ok, :created} =
MyApp.Repo.transaction(fn ->
Enum.each(permissions, fn %{resource: resource, action: action} ->
case Acx.EnforcerServer.add_policy("my_enforcer", {:p, ["analyst", resource, action]}) do
case Casbin.EnforcerServer.add_policy("my_enforcer", {:p, ["analyst", resource, action]}) do
:ok -> :ok
{:error, reason} -> MyApp.Repo.rollback(reason)
end
Expand Down Expand Up @@ -105,7 +105,7 @@ defmodule MyApp.Authorization.RolesTest do
Ecto.Adapters.SQL.Sandbox.mode(Repo, {:shared, self()})

# Allow EnforcerServer to access the connection
case Registry.lookup(Acx.EnforcerRegistry, "my_enforcer") do
case Registry.lookup(Casbin.EnforcerRegistry, "my_enforcer") do
[{pid, _}] -> Ecto.Adapters.SQL.Sandbox.allow(Repo, self(), pid)
[] -> :ok
end
Expand All @@ -126,7 +126,7 @@ defmodule MyApp.Authorization.RolesTest do

# Add Casbin policies
Enum.each(permissions, fn perm ->
case Acx.EnforcerServer.add_policy(
case Casbin.EnforcerServer.add_policy(
"my_enforcer",
{:p, ["admin", perm.resource, perm.action]}
) do
Expand All @@ -144,7 +144,7 @@ defmodule MyApp.Authorization.RolesTest do
test "rolls back Casbin operations on error" do
result = Repo.transaction(fn ->
# Add a policy
:ok = Acx.EnforcerServer.add_policy(
:ok = Casbin.EnforcerServer.add_policy(
"my_enforcer",
{:p, ["temp_role", "resource", "action"]}
)
Expand All @@ -156,7 +156,7 @@ defmodule MyApp.Authorization.RolesTest do
assert {:error, :simulated_error} = result

# Verify the policy was not persisted
policies = Acx.EnforcerServer.list_policies("my_enforcer", %{sub: "temp_role"})
policies = Casbin.EnforcerServer.list_policies("my_enforcer", %{sub: "temp_role"})
assert policies == []
end
end
Expand Down
18 changes: 0 additions & 18 deletions lib/acx.ex

This file was deleted.

18 changes: 18 additions & 0 deletions lib/casbin.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Casbin do
@moduledoc """
Casbin is an Elixir implementation of the Casbin authorization library.
"""
use Application

def start(_type, _args) do
children = [
{Registry, keys: :unique, name: Casbin.EnforcerRegistry},
Casbin.EnforcerSupervisor
]

:ets.new(:enforcers_table, [:public, :named_table])

opts = [strategy: :one_for_one, name: Casbin.Supervisor]
Supervisor.start_link(children, opts)
end
end
40 changes: 20 additions & 20 deletions lib/acx/enforcer.ex → lib/casbin/enforcer.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Acx.Enforcer do
defmodule Casbin.Enforcer do
@moduledoc """
TODO
"""
Expand All @@ -10,10 +10,10 @@ defmodule Acx.Enforcer do
env: %{},
persist_adapter: nil

alias Acx.Internal.RoleGroup
alias Acx.Model
alias Acx.Persist.PersistAdapter
alias Acx.Persist.ReadonlyFileAdapter
alias Casbin.Internal.RoleGroup
alias Casbin.Model
alias Casbin.Persist.PersistAdapter
alias Casbin.Persist.ReadonlyFileAdapter

@type mapping() ::
{atom(), String.t(), String.t()}
Expand Down Expand Up @@ -189,7 +189,7 @@ defmodule Acx.Enforcer do
...> e = Enforcer.remove_filtered_policy(e, :p, 0, ["admin"])
...> Enforcer.list_policies(e)
[
%Acx.Model.Policy{
%Casbin.Model.Policy{
key: :p,
attrs: [sub: "reader", obj: "blog_post", act: "read", eft: "allow"]
}
Expand All @@ -205,7 +205,7 @@ defmodule Acx.Enforcer do
...> e = Enforcer.remove_filtered_policy(e, :p, 1, ["blog_post"])
...> Enforcer.list_policies(e)
[
%Acx.Model.Policy{
%Casbin.Model.Policy{
key: :p,
attrs: [sub: "reader", obj: "comment", act: "read", eft: "allow"]
}
Expand Down Expand Up @@ -285,35 +285,35 @@ defmodule Acx.Enforcer do
...> %Enforcer{policies: policies} = e
...> policies
[
%Acx.Model.Policy{
%Casbin.Model.Policy{
attrs: [sub: "peter", obj: "blog_post", act: "read", eft: "allow"],
key: :p
},
%Acx.Model.Policy{
%Casbin.Model.Policy{
attrs: [sub: "peter", obj: "blog_post", act: "modify", eft: "allow"],
key: :p
},
%Acx.Model.Policy{
%Casbin.Model.Policy{
attrs: [sub: "peter", obj: "blog_post", act: "create", eft: "allow"],
key: :p
},
%Acx.Model.Policy{
%Casbin.Model.Policy{
attrs: [sub: "bob", obj: "blog_post", act: "read", eft: "allow"],
key: :p
},
%Acx.Model.Policy{
%Casbin.Model.Policy{
attrs: [sub: "alice", obj: "blog_post", act: "read", eft: "allow"],
key: :p
},
%Acx.Model.Policy{
%Casbin.Model.Policy{
attrs: [sub: "alice", obj: "blog_post", act: "modify", eft: "allow"],
key: :p
},
%Acx.Model.Policy{
%Casbin.Model.Policy{
attrs: [sub: "alice", obj: "blog_post", act: "delete", eft: "allow"],
key: :p
},
%Acx.Model.Policy{
%Casbin.Model.Policy{
attrs: [sub: "alice", obj: "blog_post", act: "create", eft: "allow"],
key: :p
}
Expand Down Expand Up @@ -409,15 +409,15 @@ defmodule Acx.Enforcer do
...> e = e |> Enforcer.load_policies!(pfile)
...> e |> Enforcer.list_policies(%{sub: "peter"})
[
%Acx.Model.Policy{
%Casbin.Model.Policy{
attrs: [sub: "peter", obj: "blog_post", act: "read", eft: "allow"],
key: :p
},
%Acx.Model.Policy{
%Casbin.Model.Policy{
attrs: [sub: "peter", obj: "blog_post", act: "modify", eft: "allow"],
key: :p
},
%Acx.Model.Policy{
%Casbin.Model.Policy{
attrs: [sub: "peter", obj: "blog_post", act: "create", eft: "allow"],
key: :p
}
Expand Down Expand Up @@ -856,7 +856,7 @@ defmodule Acx.Enforcer do
end)
end

@spec list_mapping_policies(Acx.Enforcer.t(), maybe_improper_list) :: [mapping()]
@spec list_mapping_policies(Casbin.Enforcer.t(), maybe_improper_list) :: [mapping()]
def list_mapping_policies(
%__MODULE__{mapping_policies: mapping_policies},
criteria
Expand All @@ -869,7 +869,7 @@ defmodule Acx.Enforcer do
end)
end

@spec list_mapping_policies(Acx.Enforcer.t()) :: [mapping()]
@spec list_mapping_policies(Casbin.Enforcer.t()) :: [mapping()]
def list_mapping_policies(%__MODULE__{mapping_policies: mapping_policies}), do: mapping_policies

@doc """
Expand Down
8 changes: 4 additions & 4 deletions lib/acx/enforcer_server.ex → lib/casbin/enforcer_server.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Acx.EnforcerServer do
defmodule Casbin.EnforcerServer do
@moduledoc """
An enforcer process that holds an `Enforcer` struct as its state.
"""
Expand All @@ -7,7 +7,7 @@ defmodule Acx.EnforcerServer do

require Logger

alias Acx.Enforcer
alias Casbin.Enforcer

#
# Client Public Interface
Expand Down Expand Up @@ -348,12 +348,12 @@ defmodule Acx.EnforcerServer do
# Returns a tuple used to register and lookup an enforcer process
# by name
defp via_tuple(ename) do
{:via, Registry, {Acx.EnforcerRegistry, ename}}
{:via, Registry, {Casbin.EnforcerRegistry, ename}}
end

# Returns the name of `self`.
defp self_name do
Registry.keys(Acx.EnforcerRegistry, self()) |> List.first()
Registry.keys(Casbin.EnforcerRegistry, self()) |> List.first()
end

# Creates a new enforcer or lookups existing one in the ets table.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Acx.EnforcerSupervisor do
defmodule Casbin.EnforcerSupervisor do
@moduledoc """
A supervisor that starts `Enforcer` processes dynamically.
"""
Expand All @@ -18,8 +18,8 @@ defmodule Acx.EnforcerSupervisor do
"""
def start_enforcer(ename, cfile) do
child_spec = %{
id: Acx.EnforcerServer,
start: {Acx.EnforcerServer, :start_link, [ename, cfile]},
id: Casbin.EnforcerServer,
start: {Casbin.EnforcerServer, :start_link, [ename, cfile]},
restart: :permanent
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Acx.Internal.Digraph do
defmodule Casbin.Internal.Digraph do
@moduledoc """
This module defines a simple directed graph structure to model the role
inheritance in the (H)RBAC ( Hierarchical Role Based Access Control)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Acx.Internal.Helpers do
defmodule Casbin.Internal.Helpers do
@moduledoc """
Helper functions used internally by Acx.
Helper functions used internally by Casbin.
"""
@doc """
Returns a tuple `{succeeds, remainder, count}` where `succeeds` is
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Acx.Internal.Operator do
defmodule Casbin.Internal.Operator do
@moduledoc """
This module defines a set of operators and helper functions used when
parsing operators in a matcher expression.
Expand Down
4 changes: 2 additions & 2 deletions lib/acx/internal/parser.ex → lib/casbin/internal/parser.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Acx.Internal.Parser do
defmodule Casbin.Internal.Parser do
@moduledoc """
`Parser` is responsible for converting a string representing a
boolean expression:
Expand All @@ -19,7 +19,7 @@ defmodule Acx.Internal.Parser do
and logical operators: `&&(and), ||(or), !(not)`.
"""

alias Acx.Internal.{Helpers, Operator}
alias Casbin.Internal.{Helpers, Operator}

@operators [
~c".",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Acx.Internal.PartialRange do
defmodule Casbin.Internal.PartialRange do
@moduledoc """
PartialRange represents one of the following mathematical objects:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Acx.Internal.RoleGroup do
defmodule Casbin.Internal.RoleGroup do
@moduledoc """
This module defines a structure to manage the roles and their inheritances
in the (H)RBAC model.
Expand All @@ -10,7 +10,7 @@ defmodule Acx.Internal.RoleGroup do

defstruct name: nil, role_graph: nil

alias Acx.Internal.Digraph
alias Casbin.Internal.Digraph

@type role_type() :: term()

Expand Down
4 changes: 2 additions & 2 deletions lib/acx/model.ex → lib/casbin/model.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Acx.Model do
defmodule Casbin.Model do
@moduledoc """
This module defines the structure to represent a PERM (Policy, Effect,
Request, Matchers) meta model. See [1] for more information.
Expand All @@ -14,7 +14,7 @@ defmodule Acx.Model do
effect: nil,
role_mappings: []

alias Acx.Model.{
alias Casbin.Model.{
Config,
Matcher,
Policy,
Expand Down
2 changes: 1 addition & 1 deletion lib/acx/model/config.ex → lib/casbin/model/config.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Acx.Model.Config do
defmodule Casbin.Model.Config do
@moduledoc """
A configuration file is a text files divided into named sections
(optional). Brackets enclose each section name.
Expand Down
Loading
Loading