でプラグを使用しますUserController
。0.4.x
条件付きでプラグインする機能はありませんが、次のような方法で目的を達成できます。
defmodule MyApp.UserController do
use Phoenix.Controller
plug :authenticate, :admin
plug :action
def index(conn, _) do
render conn, "index"
end
def create(conn, params) do
# do the creating
end
...
defp authenticate(conn, :admin) do
do_auth(conn, action_name(conn))
end
defp do_auth(conn, action) when action in [:create, :update, :destroy] do
if AdminAuth.authenticated?(conn) do
conn
else
halt conn
end
end
defp do_auth(conn, _action), do: conn
end
間もなく導入さ0.5
れる変更により、条件付きプラグインがより簡単になります。つまり、次のようになります。
defmodule MyApp.UserController do
use Phoenix.Controller
plug :authenticate, :admin when action in [:create, :update, :destroy]
def index(conn, _) do
render conn, "index"
end
def create(conn, params) do
# do the creating
end
...
defp authenticate(conn, :admin) do
if AdminAuth.authenticated?(conn) do
conn
else
halt conn
end
end
end
パブリック アクセスと制限付きアクセスのコントローラーを分けておくことをお勧めしますAdmin.UserController
。そのため、制限付き機能について参照したようなものを追加します。