Add approved clients to account view

This commit is contained in:
Shadowfacts 2019-04-01 11:29:03 -04:00
parent a759be3969
commit eb313cf695
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 51 additions and 4 deletions

View File

@ -1,14 +1,22 @@
defmodule FrenzyWeb.AccountController do defmodule FrenzyWeb.AccountController do
use FrenzyWeb, :controller use FrenzyWeb, :controller
alias Frenzy.{Repo, User} alias Frenzy.{Repo, User, FervorClient}
alias FrenzyWeb.Router.Helpers, as: Routes alias FrenzyWeb.Router.Helpers, as: Routes
alias FrenzyWeb.Endpoint alias FrenzyWeb.Endpoint
def show(conn, _params) do def show(conn, _params) do
user = conn.assigns[:user] user = conn.assigns[:user] |> Repo.preload(:approved_clients)
clients =
user.approved_clients
|> Enum.map(fn approved_client ->
fervor_client = Repo.get_by(FervorClient, client_id: approved_client.client_id)
{approved_client, fervor_client}
end)
render(conn, "show.html", %{ render(conn, "show.html", %{
user: user user: user,
clients: clients
}) })
end end
@ -82,4 +90,16 @@ defmodule FrenzyWeb.AccountController do
def do_change_fever_password(conn, _params) do def do_change_fever_password(conn, _params) do
redirect(conn, to: Routes.account_path(Endpoint, :change_fever_password)) redirect(conn, to: Routes.account_path(Endpoint, :change_fever_password))
end end
def remove_client(conn, %{"client_id" => client_id}) do
user = conn.assigns[:user] |> Repo.preload(:approved_clients)
approved_client = Enum.find(user.approved_clients, fn c -> c.client_id == client_id end)
if not is_nil(approved_client) do
{:ok, _} = Repo.delete(approved_client)
end
redirect(conn, to: Routes.account_path(Endpoint, :show))
end
end end

View File

@ -40,6 +40,7 @@ defmodule FrenzyWeb.Router do
post "/account/change_password", AccountController, :do_change_password post "/account/change_password", AccountController, :do_change_password
get "/account/change_fever_password", AccountController, :change_fever_password get "/account/change_fever_password", AccountController, :change_fever_password
post "/account/change_fever_password", AccountController, :do_change_fever_password post "/account/change_fever_password", AccountController, :do_change_fever_password
post "/account/remove_client", AccountController, :remove_client
get "/", GroupController, :index get "/", GroupController, :index
resources "/groups", GroupController, except: [:edit, :update] resources "/groups", GroupController, except: [:edit, :update]

View File

@ -4,3 +4,29 @@
<a href="<%= Routes.account_path(@conn, :change_password) %>" class="button">Change Password</a> <a href="<%= Routes.account_path(@conn, :change_password) %>" class="button">Change Password</a>
<a href="<%= Routes.account_path(@conn, :change_fever_password) %>" class="button">Change Fever Password</a> <a href="<%= Routes.account_path(@conn, :change_fever_password) %>" class="button">Change Fever Password</a>
<h2>Approved Clients</h2>
<table>
<tr>
<th>Client</th>
<th>Revoke Access</th>
</tr>
<%= for {approved, fervor} <- @clients do %>
<tr>
<td>
<%= if fervor.website do %>
<a href="<%= fervor.website %>"><%= fervor.client_name %></a>
<% else %>
<%= fervor.client_name %>
<% end %>
</td>
<td>
<%= form_tag Routes.account_path(@conn, :remove_client), method: :post do %>
<input type="hidden" name="client_id" value="<%= approved.client_id %>">
<%= submit "Revoke" %>
<% end %>
</td>
</tr>
<% end %>
</table>