-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f5de08
commit 26a60c0
Showing
18 changed files
with
421 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
defmodule Teiserver.AutohostLib do | ||
@spec icon :: String.t() | ||
def icon, do: "fa-solid fa-robot" | ||
|
||
@spec colours :: atom | ||
def colours, do: :success2 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
defmodule Teiserver.OAuth.ApplicationLib do | ||
|
||
@spec icon :: String.t() | ||
def icon, do: "fa-solid fa-passport" | ||
|
||
@spec colours :: atom | ||
def colours, do: :success2 | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule TeiserverWeb.Components.AutohostComponent do | ||
use Phoenix.Component | ||
alias TeiserverWeb.CoreComponents, as: CC | ||
|
||
attr :changeset, Ecto.Changeset, required: true | ||
attr :action, :string, required: true | ||
|
||
def autohost_form(assigns) do | ||
~H""" | ||
<CC.simple_form :let={f} for={@changeset} action={@action}> | ||
<CC.error :if={@changeset.action}> | ||
Oops, something went wrong! Please check the errors below. | ||
</CC.error> | ||
<CC.input field={f[:name]} type="text" label="Name" /> | ||
<:actions> | ||
<CC.button type="submit" class="btn-primary"><%= @button_label %></CC.button> | ||
</:actions> | ||
</CC.simple_form> | ||
""" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
lib/teiserver_web/controllers/admin/autohost_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
defmodule TeiserverWeb.Admin.AutohostController do | ||
@moduledoc """ | ||
management of autohosts and their credentials | ||
""" | ||
|
||
use TeiserverWeb, :controller | ||
|
||
alias Teiserver.{Autohost, AutohostQueries} | ||
|
||
plug Bodyguard.Plug.Authorize, | ||
# The policy should be Admin or something fairly high. But while we're | ||
# developping the new lobby, it's easier if this is allowed for any | ||
# contributors | ||
policy: Teiserver.Staff, | ||
action: {Phoenix.Controller, :action_name}, | ||
user: {Teiserver.Account.AuthLib, :current_user} | ||
|
||
plug :add_breadcrumb, name: 'Admin', url: '/teiserver/admin' | ||
plug :add_breadcrumb, name: 'Autohosts', url: '/teiserver/admin/autohost' | ||
|
||
@spec index(Plug.Conn.t(), map()) :: Plug.Conn.t() | ||
def index(conn, _params) do | ||
autohosts = AutohostQueries.list_autohosts() | ||
|
||
conn | ||
|> render("index.html", autohosts: autohosts) | ||
end | ||
|
||
@spec new(Plug.Conn.t(), map()) :: Plug.Conn.t() | ||
def new(conn, _params) do | ||
changeset = Autohost.change_autohost(%Autohost.Autohost{}) | ||
|
||
conn | ||
|> assign(:page_title, "") | ||
|> render("new.html", changeset: changeset) | ||
end | ||
|
||
@spec create(Plug.Conn.t(), map()) :: Plug.Conn.t() | ||
def create(conn, %{"autohost" => attrs}) do | ||
case Autohost.create_autohost(attrs) do | ||
{:ok, %Autohost.Autohost{} = autohost} -> | ||
conn | ||
|> put_flash(:info, "Autohost created") | ||
|> redirect(to: ~p"/teiserver/admin/autohost/#{autohost.id}") | ||
|
||
{:error, changeset} -> | ||
conn | ||
|> assign(:page_title, "BAR - new autohost") | ||
|> put_status(400) | ||
|> render("new.html", changeset: changeset) | ||
end | ||
end | ||
|
||
def create(conn, _), | ||
do: | ||
conn | ||
|> put_status(400) | ||
|> assign(:page_title, "BAR - new autohost") | ||
|> render("new.html", changeset: Autohost.Autohost.changeset(%Autohost.Autohost{}, %{})) | ||
|
||
@spec show(Plug.Conn.t(), map()) :: Plug.Conn.t() | ||
def show(conn, assigns) do | ||
case Autohost.get_by_id(Map.get(assigns, "id")) do | ||
%Autohost.Autohost{} = autohost -> | ||
conn | ||
|> assign(:page_title, "BAR - autohost #{autohost.name}") | ||
|> render("show.html", autohost: autohost) | ||
|
||
nil -> | ||
conn | ||
|> put_status(:not_found) | ||
|> render("not_found.html") | ||
end | ||
end | ||
|
||
@spec edit(Plug.Conn.t(), map()) :: Plug.Conn.t() | ||
def edit(conn, assigns) do | ||
case Autohost.get_by_id(Map.get(assigns, "id")) do | ||
%Autohost.Autohost{} = autohost -> | ||
changeset = Autohost.change_autohost(autohost) | ||
|
||
conn | ||
|> assign(:page_title, "BAR - edit autohost #{autohost.name}") | ||
|> render("edit.html", autohost: autohost, changeset: changeset) | ||
|
||
nil -> | ||
conn | ||
|> put_status(:not_found) | ||
|> render("not_found.html") | ||
end | ||
end | ||
|
||
@spec update(Plug.Conn.t(), map()) :: Plug.Conn.t() | ||
def update(conn, %{"autohost" => params} = assigns) do | ||
case Autohost.get_by_id(Map.get(assigns, "id")) do | ||
%Autohost.Autohost{} = autohost -> | ||
case Autohost.update_autohost(autohost, params) do | ||
{:ok, autohost} -> | ||
conn | ||
|> put_flash(:info, "Autohost updated") | ||
|> render(:show, autohost: autohost) | ||
|
||
{:error, changeset} -> | ||
conn | ||
|> put_status(400) | ||
|> render(:edit, autohost: autohost, changeset: changeset) | ||
end | ||
|
||
nil -> | ||
conn | ||
|> put_status(:not_found) | ||
|> render("not_found.html") | ||
end | ||
end | ||
|
||
def update(conn, _) do | ||
conn | ||
|> put_status(:not_found) | ||
|> render("not_found.html") | ||
end | ||
|
||
@spec delete(Plug.Conn.t(), map()) :: Plug.Conn.t() | ||
def delete(conn, assigns) do | ||
case Autohost.get_by_id(Map.get(assigns, "id")) do | ||
%Autohost.Autohost{} = autohost -> | ||
case Autohost.delete(autohost) do | ||
:ok -> | ||
conn | ||
|> put_flash(:info, "Deleted!") | ||
|> redirect(to: ~p"/teiserver/admin/autohost") | ||
|
||
{:error, err} -> | ||
conn | ||
|> put_flash(:danger, inspect(err)) | ||
|> redirect(to: ~p"/teiserver/admin/autohost/#{autohost.id}") | ||
end | ||
|
||
nil -> | ||
conn | ||
|> put_status(:not_found) | ||
|> render("not_found.html") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<div class="card page-card"> | ||
<div class="card-body"> | ||
<h1>Edit autohost <%= @autohost.name %></h1> | ||
|
||
<div class="row d-flex justify-content-center"> | ||
<div class="col-md-6"> | ||
<.autohost_form | ||
button_label="Update" | ||
changeset={@changeset} | ||
action={~p"/teiserver/admin/autohost/#{@autohost.id}"} | ||
method="PUT" | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
39 changes: 39 additions & 0 deletions
39
lib/teiserver_web/templates/admin/autohost/index.html.heex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<div class="container"> | ||
<h1>List of autohosts</h1> | ||
|
||
<p> | ||
<div class="btn btn-sm btn-outline-success"> | ||
<a href={~p"/teiserver/admin/autohost/new"}>Create autohost</a> | ||
</div> | ||
</p> | ||
|
||
<%= if length(@autohosts) > 0 do %> | ||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th>id</th> | ||
<th>name</th> | ||
<th>actions</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<%= for autohost <- @autohosts do %> | ||
<tr> | ||
<td><%= autohost.id %></td> | ||
<td><%= autohost.name %></td> | ||
<td> | ||
<a href={~p"/teiserver/admin/autohost/#{autohost.id}"}> | ||
<button type="button" class="btn btn-primary btn-sm">show</button> | ||
</a> | ||
<a href={~p"/teiserver/admin/autohost/#{autohost.id}/edit"}> | ||
<button type="button" class="btn btn-primary btn-sm">Edit</button> | ||
</a> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
<% else %> | ||
No autohost! | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<div class="card page-card"> | ||
<div class="card-body"> | ||
<h1>Create a new autohost</h1> | ||
|
||
<div class="row d-flex justify-content-center"> | ||
<div class="col-md-6"> | ||
<.autohost_form | ||
button_label="Create autohost" | ||
changeset={@changeset} | ||
action={~p"/teiserver/admin/autohost"} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
3 changes: 3 additions & 0 deletions
3
lib/teiserver_web/templates/admin/autohost/not_found.html.heex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<h1>No autohost found</h1> | ||
|
||
<a href={~p"/teiserver/admin/autohost/"}>Back to the list of autohosts</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<div class="container"> | ||
<h1>Autohost: <%= @autohost.name %></h1> | ||
<%!-- TODO: add a list of oauth credentials + action to delete them --%> | ||
|
||
<p> | ||
<a href={~p"/teiserver/admin/autohost/#{@autohost.id}/edit"}> | ||
<button type="button" class="btn btn-primary">Edit</button> | ||
</a> | ||
|
||
<%!-- TODO: add a modal confirmation for deleting an autohost --%> | ||
<CC.simple_form for={} action={~p"/teiserver/admin/autohost/#{@autohost.id}"} method="delete"> | ||
<:actions> | ||
<CC.button type="submit" class="btn-danger">Delete</CC.button> | ||
</:actions> | ||
</CC.simple_form> | ||
</p> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule TeiserverWeb.Admin.AutohostView do | ||
use TeiserverWeb, :view | ||
|
||
import TeiserverWeb.Components.AutohostComponent | ||
alias TeiserverWeb.CoreComponents, as: CC | ||
|
||
def view_colour(), do: Teiserver.AutohostLib.colours() | ||
def icon(), do: Teiserver.AutohostLib.icon() | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.