Skip to content
Open
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
24 changes: 24 additions & 0 deletions lib/ja_resource/authorize.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule JaResource.Authorize do
use Behaviour

@moduledoc """
Provides the `handle_authorize/0` callback used to authorize the resource.

This behaviour is used by all JaResource actions.
"""

@doc """
Called before all the actions with the model. Useful for authorizing.
"""
@callback handle_authorize(Plug.Conn.t, JaResource.record) :: any

defmacro __using__(_) do
quote do
@behaviour JaResource.Authorize

def handle_authorize(model, _conn), do: model

defoverridable [handle_authorize: 2]
end
end
end
5 changes: 5 additions & 0 deletions lib/ja_resource/create.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ defmodule JaResource.Create do
def call(controller, conn) do
merged = JaResource.Attributes.from_params(conn.params)
attributes = controller.permitted_attributes(conn, merged, :create)

controller.handle_authorize(controller.model(), conn)

conn
|> controller.handle_create(attributes)
|> JaResource.Create.insert(controller)
Expand All @@ -84,7 +87,9 @@ defmodule JaResource.Create do

@doc false
def respond(%Plug.Conn{} = conn, _old_conn), do: conn
def respond({:error, _name, errors, _changes}, conn), do: invalid(conn, errors)
def respond({:error, errors}, conn), do: invalid(conn, errors)
def respond({:ok, %{} = map}, conn), do: created(conn, Map.fetch(map, controller.atom()))
def respond({:ok, model}, conn), do: created(conn, model)
def respond(model, conn), do: created(conn, model)

Expand Down
4 changes: 4 additions & 0 deletions lib/ja_resource/delete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ defmodule JaResource.Delete do
def call(controller, conn) do
model = controller.record(conn, conn.params["id"])

controller.handle_authorize(model, conn)

conn
|> controller.handle_delete(model)
|> JaResource.Delete.respond(conn)
Expand All @@ -67,7 +69,9 @@ defmodule JaResource.Delete do
@doc false
def respond(nil, conn), do: not_found(conn)
def respond(%Plug.Conn{} = conn, _old_conn), do: conn
def respond({:ok, %{} = map}, conn), do: created(conn, Map.fetch(map, controller.atom()))
def respond({:ok, _model}, conn), do: deleted(conn)
def respond({:error, _name, errors, _changes}, conn), do: invalid(conn, errors)
def respond({:errors, errors}, conn), do: invalid(conn, errors)
def respond(_model, conn), do: deleted(conn)

Expand Down
2 changes: 2 additions & 0 deletions lib/ja_resource/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ defmodule JaResource.Index do
Execute the index action on a given module implementing Index behaviour and conn.
"""
def call(controller, conn) do
controller.handle_authorize(controller.model(), conn)

conn
|> controller.handle_index(conn.params)
|> JaResource.Index.filter(conn, controller)
Expand Down
10 changes: 10 additions & 0 deletions lib/ja_resource/model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@ defmodule JaResource.Model do
defmacro __using__(_) do
quote do
@behaviour JaResource.Model
use JaResource.Authorize

@inferred_model JaResource.Model.model_from_controller(__MODULE__)
def model(), do: @inferred_model

def atom() do
model()
|> Atom.to_string
|> String.split(".")
|> List.last
|> String.downcase
|> String.to_atom
end

defoverridable [model: 0]
end
end
Expand Down
8 changes: 5 additions & 3 deletions lib/ja_resource/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ defmodule JaResource.Show do
Execute the show action on a given module implementing Show behaviour and conn.
"""
def call(controller, conn) do
conn
|> controller.handle_show(conn.params["id"])
|> JaResource.Show.respond(conn, controller)
model = controller.handle_show(conn, conn.params["id"])

controller.handle_authorize(model, conn)

JaResource.Show.respond(model, conn, controller)
end

@doc false
Expand Down
4 changes: 4 additions & 0 deletions lib/ja_resource/update.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ defmodule JaResource.Update do
merged = JaResource.Attributes.from_params(conn.params)
attributes = controller.permitted_attributes(conn, merged, :update)

controller.handle_authorize(model, conn)

conn
|> controller.handle_update(model, attributes)
|> JaResource.Update.update(controller)
Expand All @@ -92,7 +94,9 @@ defmodule JaResource.Update do
@doc false
def respond(%Plug.Conn{} = conn, _oldconn), do: conn
def respond(nil, conn), do: send_resp(conn, :not_found, "")
def respond({:error, _name, errors, _changes}, conn), do: invalid(conn, errors)
def respond({:error, errors}, conn), do: invalid(conn, errors)
def respond({:ok, %{} = map}, conn), do: created(conn, Map.fetch(map, controller.atom()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to match regular models structs from changesets as well.

def respond({:ok, model}, conn), do: updated(conn, model)
def respond(model, conn), do: updated(conn, model)

Expand Down