From fe9d9ee4d9809bc87bbf094a1d51ae7ac2875125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F?= Date: Thu, 8 Oct 2020 10:52:32 +0800 Subject: [PATCH 1/3] fix: In the plugs/authorize.ex , The action of 'show' is missing --- lib/wait_list_web/plugs/authorize.ex | 39 +++++++++++++++++++++------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/wait_list_web/plugs/authorize.ex b/lib/wait_list_web/plugs/authorize.ex index b1c9ad1..554ac50 100644 --- a/lib/wait_list_web/plugs/authorize.ex +++ b/lib/wait_list_web/plugs/authorize.ex @@ -3,31 +3,51 @@ defmodule WaitListWeb.Authorize do import Phoenix.Controller import WaitList.Authorization alias WaitListWeb.Router.Helpers, as: Routes - + def init(opts), do: opts - + def call(conn, opts) do role = conn.assigns.current_user.role resource = Keyword.get(opts, :resource) action = action_name(conn) - + check(action, role, resource) |> maybe_continue(conn) end - + defp maybe_continue(true, conn), do: conn - + defp maybe_continue(false, conn) do conn |> put_flash(:error, "You're not authorized to do that!") |> redirect(to: Routes.page_path(conn, :index)) |> halt() end - - defp check(:index, role, resource) do + + # Old Code + # defp check(:index, role, resource) do + # can(role) |> read?(resource) + # end + + """ + Hi Mr.Moore + There is have a bug!!! + When i use `mix phx.gen.html ` generate html/controller/view and schema. + + In the `Old Code` ,if i click the `Save` button, that will have two flashes and redirect to the index page : + - "Article created successfully" + - "You're not authorized to do that!" + + Because the action of `show` is missing + """ + + # New Code + defp check(action, role, resource) when action in [:show, :index] do can(role) |> read?(resource) end - + + # + defp check(action, role, resource) when action in [:new, :create] do can(role) |> create?(resource) end @@ -41,5 +61,4 @@ defmodule WaitListWeb.Authorize do end defp check(_action, _role, _resource), do: false - -end \ No newline at end of file +end From 64729fd8cb5a679924f5c5cf371f456ffbbab425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F?= Date: Thu, 8 Oct 2020 10:55:11 +0800 Subject: [PATCH 2/3] fix: In the plugs/authorize.ex , The action of 'show' is missing --- lib/wait_list_web/plugs/authorize.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/wait_list_web/plugs/authorize.ex b/lib/wait_list_web/plugs/authorize.ex index 554ac50..ab5a526 100644 --- a/lib/wait_list_web/plugs/authorize.ex +++ b/lib/wait_list_web/plugs/authorize.ex @@ -38,7 +38,7 @@ defmodule WaitListWeb.Authorize do - "Article created successfully" - "You're not authorized to do that!" - Because the action of `show` is missing + Because the action of `show` is missing. """ # New Code From 44e3987a65dfc1c3171063ce3aabc85df8d53aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F?= Date: Thu, 8 Oct 2020 11:12:56 +0800 Subject: [PATCH 3/3] fix: In the plugs/authorize.ex , The action of 'show' is missing --- lib/wait_list_web/plugs/authorize.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/wait_list_web/plugs/authorize.ex b/lib/wait_list_web/plugs/authorize.ex index ab5a526..21e4ea9 100644 --- a/lib/wait_list_web/plugs/authorize.ex +++ b/lib/wait_list_web/plugs/authorize.ex @@ -31,7 +31,7 @@ defmodule WaitListWeb.Authorize do """ Hi Mr.Moore - There is have a bug!!! + I found a bug When i use `mix phx.gen.html ` generate html/controller/view and schema. In the `Old Code` ,if i click the `Save` button, that will have two flashes and redirect to the index page :