From c984872066950fa82580b195a4268e8768baf478 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Mon, 30 Sep 2019 21:12:38 -0400 Subject: [PATCH] become canadian --- lib/philomena/users/ability.ex | 20 ++++++++++++++++++++ mix.exs | 3 ++- mix.lock | 1 + test/philomena/poll_options_test.exs | 9 +++++++-- test/philomena/topics_test.exs | 9 +++++++-- 5 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 lib/philomena/users/ability.ex diff --git a/lib/philomena/users/ability.ex b/lib/philomena/users/ability.ex new file mode 100644 index 00000000..d7c0009e --- /dev/null +++ b/lib/philomena/users/ability.ex @@ -0,0 +1,20 @@ +defimpl Canada.Can, for: [Atom, Philomena.Users.User] do + alias Philomena.Users.User + alias Philomena.Images.Image + + # Admins can do anything + def can?(%User{role: "admin"}, _action, _model), do: true + + # Users can... + + # View non-deleted images + def can?(_user, action, Image) + when action in [:new, :create, :index], + do: true + + def can?(_user, :read, %Image{hidden_from_users: true}), do: false + def can?(_user, :read, %Image{hidden_from_users: false}), do: true + + # Otherwise... + def can?(_user, _action, _model), do: false +end diff --git a/mix.exs b/mix.exs index d47c1910..097fc9e3 100644 --- a/mix.exs +++ b/mix.exs @@ -50,7 +50,8 @@ defmodule Philomena.MixProject do {:pot, "~> 0.10.1"}, {:secure_compare, "~> 0.1.0"}, {:elastix, "~> 0.7.1"}, - {:nimble_parsec, "~> 0.5.1"} + {:nimble_parsec, "~> 0.5.1"}, + {:canary, "~> 1.1.1"} ] end diff --git a/mix.lock b/mix.lock index af277f52..0c5c697f 100644 --- a/mix.lock +++ b/mix.lock @@ -1,5 +1,6 @@ %{ "bcrypt_elixir": {:hex, :bcrypt_elixir, "2.0.3", "64e0792d5b5064391927bf3b8e436994cafd18ca2d2b76dea5c76e0adcf66b7c", [:make, :mix], [{:comeonin, "~> 5.1", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.4", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm"}, + "canada": {:hex, :canada, "2.0.0", "ce5e058f576a0625959fc5427fcde15311fb28a5ebc13775eafd13468ad16553", [:mix], [], "hexpm"}, "certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"}, "comeonin": {:hex, :comeonin, "5.1.2", "fbbbbbfcf0f0e9900c0336d16c8d462edf838ba1759577e29cc5fbd7c28a4540", [:mix], [], "hexpm"}, "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"}, diff --git a/test/philomena/poll_options_test.exs b/test/philomena/poll_options_test.exs index 441a7291..e65f24a5 100644 --- a/test/philomena/poll_options_test.exs +++ b/test/philomena/poll_options_test.exs @@ -39,12 +39,17 @@ defmodule Philomena.PollOptionsTest do test "update_poll_option/2 with valid data updates the poll_option" do poll_option = poll_option_fixture() - assert {:ok, %PollOption{} = poll_option} = PollOptions.update_poll_option(poll_option, @update_attrs) + + assert {:ok, %PollOption{} = poll_option} = + PollOptions.update_poll_option(poll_option, @update_attrs) end test "update_poll_option/2 with invalid data returns error changeset" do poll_option = poll_option_fixture() - assert {:error, %Ecto.Changeset{}} = PollOptions.update_poll_option(poll_option, @invalid_attrs) + + assert {:error, %Ecto.Changeset{}} = + PollOptions.update_poll_option(poll_option, @invalid_attrs) + assert poll_option == PollOptions.get_poll_option!(poll_option.id) end diff --git a/test/philomena/topics_test.exs b/test/philomena/topics_test.exs index 08e3ca97..02aa1c13 100644 --- a/test/philomena/topics_test.exs +++ b/test/philomena/topics_test.exs @@ -96,12 +96,17 @@ defmodule Philomena.TopicsTest do test "update_subscription/2 with valid data updates the subscription" do subscription = subscription_fixture() - assert {:ok, %Subscription{} = subscription} = Topics.update_subscription(subscription, @update_attrs) + + assert {:ok, %Subscription{} = subscription} = + Topics.update_subscription(subscription, @update_attrs) end test "update_subscription/2 with invalid data returns error changeset" do subscription = subscription_fixture() - assert {:error, %Ecto.Changeset{}} = Topics.update_subscription(subscription, @invalid_attrs) + + assert {:error, %Ecto.Changeset{}} = + Topics.update_subscription(subscription, @invalid_attrs) + assert subscription == Topics.get_subscription!(subscription.id) end