53 lines
1.3 KiB
Elixir
53 lines
1.3 KiB
Elixir
defmodule MetaWeb.UserKeysController do
|
|
use MetaWeb, :controller
|
|
|
|
alias Meta.Accounts
|
|
|
|
def index(conn, %{"username" => username}) do
|
|
case Accounts.get_user_by_username(username) do
|
|
nil ->
|
|
conn
|
|
|> put_status(:not_found)
|
|
|> text("")
|
|
|
|
user ->
|
|
keys = Accounts.get_keys_for(user)
|
|
|> Enum.map(fn k -> k.value end)
|
|
|> Enum.join("\n")
|
|
|
|
body = "# Keys for #{user.username}\n" <> keys
|
|
conn |> text(body)
|
|
end
|
|
end
|
|
|
|
def new(conn, %{"key" => key} = params) do
|
|
%{"value" => value, "comment" => comment} = key
|
|
user = conn.assigns.current_user
|
|
|
|
case Accounts.add_key(user, value, comment) do
|
|
{:ok, _} ->
|
|
conn
|
|
|> put_flash(:info, "Key added successfully.")
|
|
|> redirect(to: Routes.user_settings_path(conn, :edit))
|
|
|
|
{:error, _} ->
|
|
conn
|
|
|> put_flash(:error, "Submitted key is invalid.")
|
|
|> redirect(to: Routes.user_settings_path(conn, :edit))
|
|
end
|
|
end
|
|
|
|
def delete(conn, %{"key_id" => id}) do
|
|
user = conn.assigns.current_user
|
|
key = Accounts.get_key!(id)
|
|
|
|
if key.user_id == user.id do
|
|
Accounts.remove_key!(key)
|
|
|
|
conn
|
|
|> put_flash(:info, "Key removed.")
|
|
|> redirect(to: Routes.user_settings_path(conn, :edit))
|
|
end
|
|
|
|
end
|
|
end
|