-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove JaResource/Canary from RoleSkillController
- Loading branch information
1 parent
61485ab
commit ad25293
Showing
5 changed files
with
63 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
defmodule CodeCorps.Policy.RoleSkill do | ||
alias CodeCorps.User | ||
|
||
@spec create?(User.t) :: boolean | ||
def create?(%User{admin: true}), do: true | ||
def create?(%User{admin: false}), do: false | ||
|
||
@spec delete?(User.t) :: boolean | ||
def delete?(%User{admin: true}), do: true | ||
def delete?(%User{admin: false}), do: false | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,44 @@ | ||
defmodule CodeCorpsWeb.RoleSkillController do | ||
use CodeCorpsWeb, :controller | ||
use JaResource | ||
|
||
import CodeCorps.Helpers.Query, only: [id_filter: 2] | ||
alias CodeCorps.{RoleSkill, User, Helpers.Query} | ||
|
||
alias CodeCorps.RoleSkill | ||
action_fallback CodeCorpsWeb.FallbackController | ||
plug CodeCorpsWeb.Plug.DataToAttributes | ||
plug CodeCorpsWeb.Plug.IdsToIntegers | ||
|
||
plug :load_and_authorize_resource, model: RoleSkill, only: [:create, :delete] | ||
plug JaResource | ||
@spec index(Conn.t, map) :: Conn.t | ||
def index(%Conn{} = conn, %{} = params) do | ||
with role_skills <- RoleSkill |> Query.id_filter(params) |> Repo.all do | ||
conn |> render("index.json-api", data: role_skills) | ||
end | ||
end | ||
|
||
@spec show(Conn.t, map) :: Conn.t | ||
def show(%Conn{} = conn, %{"id" => id}) do | ||
with %RoleSkill{} = role_skill <- RoleSkill |> Repo.get(id) do | ||
conn |> render("show.json-api", data: role_skill) | ||
end | ||
end | ||
|
||
@spec model :: module | ||
def model, do: CodeCorps.RoleSkill | ||
@spec create(Conn.t, map) :: Conn.t | ||
def create(%Conn{} = conn, %{} = params) do | ||
with %User{} = current_user <- conn |> Guardian.Plug.current_resource, | ||
{:ok, :authorized} <- current_user |> Policy.authorize(:create, %RoleSkill{}, params), | ||
{:ok, %RoleSkill{} = role_skill} <- %RoleSkill{} |> RoleSkill.create_changeset(params) |> Repo.insert | ||
do | ||
conn |> put_status(:created) |> render("show.json-api", data: role_skill) | ||
end | ||
end | ||
|
||
def filter(_conn, query, "id", id_list) do | ||
query |> id_filter(id_list) | ||
@spec delete(Conn.t, map) :: Conn.t | ||
def delete(%Conn{} = conn, %{"id" => id} = _params) do | ||
with %RoleSkill{} = role_skill <- RoleSkill |> Repo.get(id), | ||
%User{} = current_user <- conn |> Guardian.Plug.current_resource, | ||
{:ok, :authorized} <- current_user |> Policy.authorize(:delete, role_skill), | ||
{:ok, %RoleSkill{} = _role_skill} <- role_skill |> Repo.delete | ||
do | ||
conn |> Conn.assign(:role_skill, role_skill) |> send_resp(:no_content, "") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters