41 lines
1.2 KiB
Elixir
41 lines
1.2 KiB
Elixir
defmodule ToweropsWeb.InvitationController do
|
|
use ToweropsWeb, :controller
|
|
|
|
alias Towerops.Organizations
|
|
|
|
def show(conn, %{"token" => token}) do
|
|
case Organizations.get_invitation_by_token(token) do
|
|
nil ->
|
|
conn
|
|
|> put_flash(:error, t("This invitation is invalid or has expired."))
|
|
|> redirect(to: ~p"/")
|
|
|
|
invitation ->
|
|
accept_or_redirect(conn, invitation, token)
|
|
end
|
|
end
|
|
|
|
defp accept_or_redirect(conn, invitation, token) do
|
|
case conn.assigns[:current_scope] do
|
|
%{user: user} when not is_nil(user) ->
|
|
accept_invitation(conn, invitation, user)
|
|
|
|
_ ->
|
|
redirect(conn, to: ~p"/users/register?invitation_token=#{token}")
|
|
end
|
|
end
|
|
|
|
defp accept_invitation(conn, invitation, user) do
|
|
case Organizations.accept_invitation(invitation, user.id) do
|
|
{:ok, _membership} ->
|
|
conn
|
|
|> put_flash(:info, t("You've joined %{org}!", org: invitation.organization.name))
|
|
|> redirect(to: ~p"/orgs/#{invitation.organization.slug}/settings?tab=members")
|
|
|
|
{:error, _changeset} ->
|
|
conn
|
|
|> put_flash(:error, t("Could not accept invitation. You may already be a member."))
|
|
|> redirect(to: ~p"/dashboard")
|
|
end
|
|
end
|
|
end
|