diff --git a/config/runtime.exs b/config/runtime.exs index 5ebb59e9a..5d4732a75 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -30,6 +30,7 @@ config :philomena, tag_file_root: System.fetch_env!("TAG_FILE_ROOT"), site_domains: System.fetch_env!("SITE_DOMAINS"), tag_url_root: System.fetch_env!("TAG_URL_ROOT"), + event_secret: System.fetch_env!("EVENT_SECRET_PHRASE"), redis_host: System.get_env("REDIS_HOST", "localhost"), proxy_host: System.get_env("PROXY_HOST"), camo_host: System.get_env("CAMO_HOST"), diff --git a/docker-compose.yml b/docker-compose.yml index 5b102408a..1a10e87f8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,6 +20,7 @@ services: - PASSWORD_PEPPER=dn2e0EpZrvBLoxUM3gfQveBhjf0bG/6/bYhrOyq3L3hV9hdo/bimJ+irbDWsuXLP - TUMBLR_API_KEY=fuiKNFp9vQFvjLNvx4sUwti4Yb5yGutBN4Xh10LXZhhRKjWlV4 - OTP_SECRET_KEY=Wn7O/8DD+qxL0X4X7bvT90wOkVGcA90bIHww4twR03Ci//zq7PnMw8ypqyyT/b/C + - EVENT_SECRET_PHRASE=yfbWGiSxyMvsWbs7OdLCrXeACKXpT7DVmwitMBOfArSgEsJQ9QHHI3e0UkNIUaey - ADVERT_FILE_ROOT=adverts - AVATAR_FILE_ROOT=avatars - BADGE_FILE_ROOT=badges diff --git a/lib/philomena/artist_links.ex b/lib/philomena/artist_links.ex index 28f468a92..ecccfe0bf 100644 --- a/lib/philomena/artist_links.ex +++ b/lib/philomena/artist_links.ex @@ -93,7 +93,10 @@ defmodule Philomena.ArtistLinks do Multi.new() |> Multi.update(:artist_link, artist_link_changeset) - |> Multi.run(:add_award, BadgeAwarder.award_callback(artist_link, verifying_user)) + |> Multi.run( + :add_award, + BadgeAwarder.award_callback(artist_link.user, verifying_user, "Artist") + ) |> Repo.transaction() |> case do {:ok, %{artist_link: artist_link}} -> diff --git a/lib/philomena/artist_links/badge_awarder.ex b/lib/philomena/artist_links/badge_awarder.ex index 275a5aeed..9536455cd 100644 --- a/lib/philomena/artist_links/badge_awarder.ex +++ b/lib/philomena/artist_links/badge_awarder.ex @@ -5,21 +5,19 @@ defmodule Philomena.ArtistLinks.BadgeAwarder do alias Philomena.Badges - @badge_title "Artist" - @doc """ - Awards a badge to an artist with a verified link. + Awards a badge to a user. - If the badge with the title `"Artist"` does not exist, no award will be created. + If the badge with the given title does not exist, no award will be created. If the user already has an award with that badge title, no award will be created. Returns `{:ok, award}`, `{:ok, nil}`, or `{:error, changeset}`. The return value is suitable for use as the return value to an `Ecto.Multi.run/3` callback. """ - def award_badge(artist_link, verifying_user) do - with badge when not is_nil(badge) <- Badges.get_badge_by_title(@badge_title), - award when is_nil(award) <- Badges.get_badge_award_for(badge, artist_link.user) do - Badges.create_badge_award(verifying_user, artist_link.user, %{badge_id: badge.id}) + def award_badge(user, verifying_user, title) do + with badge when not is_nil(badge) <- Badges.get_badge_by_title(title), + award when is_nil(award) <- Badges.get_badge_award_for(badge, user) do + Badges.create_badge_award(verifying_user, user, %{badge_id: badge.id}) else _ -> {:ok, nil} @@ -29,9 +27,9 @@ defmodule Philomena.ArtistLinks.BadgeAwarder do @doc """ Get a callback for issuing a badge award from within an `m:Ecto.Multi`. """ - def award_callback(artist_link, verifying_user) do + def award_callback(user, verifying_user, title) do fn _repo, _changes -> - award_badge(artist_link, verifying_user) + award_badge(user, verifying_user, title) end end end diff --git a/lib/philomena_web/controllers/pch_controller.ex b/lib/philomena_web/controllers/pch_controller.ex new file mode 100644 index 000000000..90ad348c1 --- /dev/null +++ b/lib/philomena_web/controllers/pch_controller.ex @@ -0,0 +1,32 @@ +defmodule PhilomenaWeb.PchController do + use PhilomenaWeb, :controller + + alias Philomena.ArtistLinks.BadgeAwarder + + def index(conn, _params) do + render(conn, title: "PCH Secret") + end + + def create(conn, %{"event" => event_params}) do + user = conn.assigns.current_user + secret = event_secret() + + case event_params do + %{"passphrase" => ^secret} -> + {:ok, _badge} = BadgeAwarder.award_badge(user, user, "PonyCon Holland") + + conn + |> put_flash(:info, "Verification granted.") + |> redirect(to: ~p"/") + + _ -> + conn + |> put_flash(:error, "Incorrect passphrase.") + |> redirect(to: ~p"/") + end + end + + defp event_secret do + Application.fetch_env!(:philomena, :event_secret) + end +end diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 2ed82dc19..348f20a91 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -101,6 +101,7 @@ defmodule PhilomenaWeb.Router do :require_authenticated_user ] + resources "/pch", PchController, only: [:index, :new, :create] resources "/registrations", RegistrationController, only: [:edit, :update], singleton: true resources "/sessions", SessionController, only: [:delete], singleton: true diff --git a/lib/philomena_web/templates/pch/index.html.slime b/lib/philomena_web/templates/pch/index.html.slime new file mode 100644 index 000000000..fe7239abf --- /dev/null +++ b/lib/philomena_web/templates/pch/index.html.slime @@ -0,0 +1,8 @@ +h1 Enter passphrase for verification + += form_for :event, ~p"/pch", fn f -> + .field + = text_input f, :passphrase, class: "input", placeholder: "Passphrase", required: true + + .field + = submit "Submit", class: "button" diff --git a/lib/philomena_web/views/pch_view.ex b/lib/philomena_web/views/pch_view.ex new file mode 100644 index 000000000..743dbfaeb --- /dev/null +++ b/lib/philomena_web/views/pch_view.ex @@ -0,0 +1,3 @@ +defmodule PhilomenaWeb.PchView do + use PhilomenaWeb, :view +end