Skip to content

Commit f45b5d9

Browse files
authored
Merge pull request #33 from epochtalk/role-controller-acls
Role controller acls
2 parents d5ad8fe + 3a7528b commit f45b5d9

File tree

5 files changed

+99
-5
lines changed

5 files changed

+99
-5
lines changed

lib/epochtalk_server_web/controllers/role_controller.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ defmodule EpochtalkServerWeb.RoleController do
99
alias EpochtalkServerWeb.Helpers.Validate
1010
alias EpochtalkServer.Models.Role
1111
alias EpochtalkServer.Models.RolePermission
12+
alias EpochtalkServerWeb.Helpers.ACL
1213

1314
@doc """
1415
Used to update a specific `Role`
1516
"""
1617
def update(conn, attrs) do
17-
with id <- Validate.cast(attrs, "id", :integer, min: 1),
18+
with {:auth, _user} <- {:auth, Guardian.Plug.current_resource(conn)},
19+
:ok <- ACL.allow!(conn, "roles.update"),
20+
id <- Validate.cast(attrs, "id", :integer, min: 1),
1821
# TODO(boka): implement validators
1922
priority_restrictions <- Validate.sanitize_list(attrs, "priority_restrictions"),
2023
permissions <- attrs["permissions"],
21-
{:auth, _user} <- {:auth, Guardian.Plug.current_resource(conn)},
2224
{:ok, data} <-
2325
RolePermission.modify_by_role(%Role{
2426
id: id,

mix.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ defmodule EpochtalkServer.MixProject do
8080
"seed.roles": ["run priv/repo/seed_roles.exs"],
8181
"seed.rp": ["run priv/repo/seed_roles_permissions.exs"],
8282
"seed.user": ["run priv/repo/seed_user.exs"],
83+
"seed.test_users": ["run priv/repo/seed_test_users.exs"],
8384
test: [
8485
"ecto.drop",
8586
"ecto.create --quiet",
8687
"ecto.migrate --quiet",
8788
"seed.test_banned_address",
8889
"seed.all",
89-
"seed.user test [email protected] password",
90-
"seed.user admin [email protected] password admin",
90+
"seed.test_users",
9191
"test"
9292
]
9393
]

priv/repo/seed_test_users.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
alias EpochtalkServer.Models.User
2+
3+
test_user_username = "test"
4+
test_user_email = "[email protected]"
5+
test_user_password = "password"
6+
7+
test_admin_user_username = "admin"
8+
test_admin_user_email = "[email protected]"
9+
test_admin_user_password = "password"
10+
test_admin_user_admin = true
11+
12+
User.create(%{username: test_user_username, email: test_user_email, password: test_user_password})
13+
|> case do
14+
{:ok, _} -> IO.puts("Successfully seeded test user")
15+
{:error, error} ->
16+
IO.puts("Error seeding test user")
17+
IO.inspect(error)
18+
end
19+
20+
User.create(%{username: test_admin_user_username, email: test_admin_user_email, password: test_admin_user_password}, test_admin_user_admin)
21+
|> case do
22+
{:ok, _} -> IO.puts("Successfully seeded test admin user")
23+
{:error, error} ->
24+
IO.puts("Error seeding test admin user")
25+
IO.inspect(error)
26+
end

test/epochtalk_server_web/controllers/role_controller_test.exs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule EpochtalkServerWeb.RoleControllerTest do
22
use EpochtalkServerWeb.ConnCase, async: false
3+
alias EpochtalkServerWeb.CustomErrors.InvalidPermission
34

45
describe "all/2" do
56
@tag :authenticated
@@ -43,7 +44,51 @@ defmodule EpochtalkServerWeb.RoleControllerTest do
4344
end
4445

4546
describe "update/2" do
47+
test "errors with unauthorized when not logged", %{conn: conn} do
48+
modified_newbie_priority_restrictions = [1, 2, 3]
49+
50+
new_newbie_permissions_attrs = %{
51+
id: 7,
52+
permissions: %{
53+
adminAccess: %{
54+
management: %{
55+
bannedAddresses: true
56+
}
57+
}
58+
},
59+
priority_restrictions: modified_newbie_priority_restrictions
60+
}
61+
62+
update_conn = put(conn, Routes.role_path(conn, :update), new_newbie_permissions_attrs)
63+
64+
assert %{"error" => "Unauthorized", "message" => "No resource found", "status" => 401} ==
65+
json_response(update_conn, 401)
66+
end
67+
4668
@tag :authenticated
69+
test "errors with unauthorized when logged in but without correct ACL", %{conn: conn} do
70+
modified_newbie_priority_restrictions = [1, 2, 3]
71+
72+
new_newbie_permissions_attrs = %{
73+
id: 7,
74+
permissions: %{
75+
adminAccess: %{
76+
management: %{
77+
bannedAddresses: true
78+
}
79+
}
80+
},
81+
priority_restrictions: modified_newbie_priority_restrictions
82+
}
83+
84+
assert_raise InvalidPermission,
85+
~r/^Forbidden, invalid permissions to perform this action/,
86+
fn ->
87+
put(conn, Routes.role_path(conn, :update), new_newbie_permissions_attrs)
88+
end
89+
end
90+
91+
@tag authenticated: :admin
4792
test "modifies a role's priority_restrictions when authenticated", %{conn: conn} do
4893
initial_newbie_priority_restrictions = nil
4994

@@ -102,7 +147,7 @@ defmodule EpochtalkServerWeb.RoleControllerTest do
102147
assert nil == modified_newbie["priority_restrictions"]
103148
end
104149

105-
@tag :authenticated
150+
@tag authenticated: :admin
106151
test "modifies a role's permissions when authenticated", %{conn: conn} do
107152
initial_newbie_permissions = %{
108153
"ads" => %{

test/support/conn_case.ex

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ defmodule EpochtalkServerWeb.ConnCase do
2525
password: @test_password
2626
}
2727

28+
# admin username/email/password from user seed in `mix test` (see mix.exs)
29+
@test_admin_username "admin"
30+
@test_admin_email "[email protected]"
31+
@test_admin_password "password"
32+
@test_admin_user_attrs %{
33+
username: @test_admin_username,
34+
email: @test_admin_email,
35+
password: @test_admin_password
36+
}
37+
2838
use ExUnit.CaseTemplate
2939

3040
using do
@@ -53,6 +63,7 @@ defmodule EpochtalkServerWeb.ConnCase do
5363
end
5464

5565
{:ok, user} = User.by_username(@test_username)
66+
{:ok, admin_user} = User.by_username(@test_admin_username)
5667
conn = Phoenix.ConnTest.build_conn()
5768

5869
# log user in if necessary
@@ -65,6 +76,16 @@ defmodule EpochtalkServerWeb.ConnCase do
6576
{:ok,
6677
conn: authed_conn, authed_user: user, token: token, authed_user_attrs: @test_user_attrs}
6778

79+
:admin ->
80+
remember_me = false
81+
{:ok, admin_user, token, authed_conn} = Session.create(admin_user, remember_me, conn)
82+
83+
{:ok,
84+
conn: authed_conn,
85+
authed_user: admin_user,
86+
token: token,
87+
authed_user_attrs: @test_admin_user_attrs}
88+
6889
# :authenticated not set, return default conn
6990
_ ->
7091
{:ok, conn: conn, user: user, user_attrs: @test_user_attrs}

0 commit comments

Comments
 (0)