Skip to content

Commit

Permalink
mix format
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Sep 14, 2020
1 parent abeb6e1 commit ffebff5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
38 changes: 22 additions & 16 deletions lib/rbac.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ defmodule RBAC do
def get_approles(auth_url, client_id) do
url = "#{auth_url}/approles/#{client_id}"
HTTPoison.start()

HTTPoison.get(url)
|> parse_body_response()
end
Expand All @@ -52,21 +53,27 @@ defmodule RBAC do
`parse_body_response/1` parses the response
so your app can use the resulting JSON (list of roles).
"""
@spec parse_body_response({atom, String.t}) :: String.t
@spec parse_body_response({atom, String.t()}) :: String.t()
def parse_body_response({:error, err}), do: {:error, err}

def parse_body_response({:ok, response}) do
body = Map.get(response, :body)
# IO.inspect(body)
# make keys of map atoms for easier access in templates
if body == nil do
{:error, :no_body}
else # make keys of map atoms for easier access in templates
else
{:ok, str_key_map} = Jason.decode(body)
atom_key_map = Enum.map(str_key_map, fn role ->
for {key, val} <- role, into: %{},
do: {String.to_atom(key), val}
end)

atom_key_map =
Enum.map(str_key_map, fn role ->
for {key, val} <- role, into: %{}, do: {String.to_atom(key), val}
end)

{:ok, atom_key_map}
end # https://stackoverflow.com/questions/31990134
end

# https://stackoverflow.com/questions/31990134
end

@doc """
Expand All @@ -83,7 +90,7 @@ defmodule RBAC do
# insert full list:
:ets.insert(:roles_cache, {"roles", roles})
# insert individual roles for fast lookup:
Enum.each(roles, fn role ->
Enum.each(roles, fn role ->
:ets.insert(:roles_cache, {role.name, role})
:ets.insert(:roles_cache, {role.id, role})
end)
Expand All @@ -94,8 +101,9 @@ defmodule RBAC do
"""
def get_role_from_cache(term) do
case :ets.lookup(:roles_cache, term) do
# not found
# not found:
[] -> %{id: 0}
# extract role:
[{_term, role}] -> role
end
end
Expand All @@ -104,15 +112,13 @@ defmodule RBAC do
`has_role/2 confirms if the person has the given role
"""
def has_role(conn, role_name) do
# IO.inspect(conn, label: "conn")
# IO.inspect(role_name, label: "role_name")
role = get_role_from_cache(role_name)
# IO.inspect(role)
person_roles =
String.split(conn.assigns.person.roles, ",", trim: true)
|> Enum.map(&String.to_integer/1)

# IO.inspect(person_roles, label: "person_roles")
person_roles =
conn.assigns.person.roles
|> String.split(",", trim: true)
|> Enum.map(&String.to_integer/1)

Enum.member?(person_roles, role.id)
end
end
26 changes: 16 additions & 10 deletions test/rbac_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,17 @@ defmodule RBACTest do
end

test "transform_role_list_to_string/1" do
roles = [%{
__meta__: "#Ecto.Schema.Metadata<:loaded",
desc: "Subscribes for updates e.g. newsletter",
id: 6,
inserted_at: ~N[2020-08-21 16:40:22],
name: "subscriber",
person_id: 1,
updated_at: ~N[2020-08-21 16:40:22]
}]
roles = [
%{
__meta__: "#Ecto.Schema.Metadata<:loaded",
desc: "Subscribes for updates e.g. newsletter",
id: 6,
inserted_at: ~N[2020-08-21 16:40:22],
name: "subscriber",
person_id: 1,
updated_at: ~N[2020-08-21 16:40:22]
}
]

assert RBAC.transform_role_list_to_string(roles) == "6"
end
Expand All @@ -98,7 +100,7 @@ defmodule RBACTest do
client_id = AuthPlug.Token.client_id()
RBAC.init_roles(auth_url, client_id)

# confirm full roles inserted
#  confirm full roles inserted
{_, list} = :ets.lookup(:roles_cache, "roles") |> List.first()
assert length(list) == 9

Expand Down Expand Up @@ -127,25 +129,29 @@ defmodule RBACTest do

test "RBAC.has_role/1 returns boolean true/false" do
init()

fake_conn = %{
assigns: %{
person: %{
roles: "1"
}
}
}

assert RBAC.has_role(fake_conn, "superadmin")
end

test "RBAC.has_role/1 returns false when doesn't have role" do
init()

fake_conn = %{
assigns: %{
person: %{
roles: "1,2,3"
}
}
}

assert not RBAC.has_role(fake_conn, "non_existent_role")
end
end
2 changes: 1 addition & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ExUnit.start()
ExUnit.start()

0 comments on commit ffebff5

Please sign in to comment.