feat: admin panel

I just added is_admin field to user schema, /admin scope,
admin page controller and view.

No extra functions were implemented.
This commit is contained in:
Ivan R. 2024-07-28 21:52:12 +05:00
parent 2d81bf20ce
commit 45c91eb3bf
Signed by: lumin
GPG key ID: E0937DC7CD6D3817
10 changed files with 110 additions and 0 deletions

10
assets/css/admin.css Normal file
View file

@ -0,0 +1,10 @@
.admin-panel {
display: flex;
gap: 16px;
margin-top: 16px;
}
.admin-panel ul {
padding: 0;
margin: 0;
}

View file

@ -1,5 +1,6 @@
@import "./core_components.css";
@import "./flash.css";
@import "./admin.css";
:root {
--bg: #13151a;

View file

@ -7,6 +7,7 @@ defmodule Comfycamp.Accounts.User do
field :password, :string, virtual: true, redact: true
field :hashed_password, :string, redact: true
field :confirmed_at, :naive_datetime
field :is_admin, :boolean, default: false
timestamps(type: :utc_datetime)
end

View file

@ -0,0 +1,30 @@
<.flash_group flash={@flash} />
<main>
<div class="limiter">
<h1>Comfycamp - админка</h1>
<.link href={~p"/"}>
Главная страница
</.link>
<div class="admin-panel">
<ul>
<li>
<.link href={~p"/admin/posts"}>
Посты
</.link>
</li>
<li>
<.link href={~p"/admin/users"}>
Пользователи
</.link>
</li>
<li>
<.link href={~p"/admin/services"}>
Сервисы
</.link>
</li>
</ul>
<%= @inner_content %>
</div>
</div>
</main>

View file

@ -23,6 +23,9 @@ defmodule ComfycampWeb.NavBar do
<div class="space" />
<%= if @current_user do %>
<.link :if={@current_user.is_admin} href={~p"/admin"}>
Админка
</.link>
<.link href={~p"/users/settings"}>
Настройки
</.link>

View file

@ -0,0 +1,27 @@
defmodule ComfycampWeb.AdminPageController do
use ComfycampWeb, :controller
def home(conn, _params) do
conn
|> put_layout(html: :admin)
|> render(:home, page_title: "Админка")
end
def posts(conn, _params) do
conn
|> put_layout(html: :admin)
|> render(:home, page_title: "Админка")
end
def users(conn, _params) do
conn
|> put_layout(html: :admin)
|> render(:home, page_title: "Админка")
end
def services(conn, _params) do
conn
|> put_layout(html: :admin)
|> render(:home, page_title: "Админка")
end
end

View file

@ -0,0 +1,9 @@
defmodule ComfycampWeb.AdminPageHTML do
use ComfycampWeb, :html
def home(assigns) do
~H"""
Добро пожаловать, админ.
"""
end
end

View file

@ -84,4 +84,13 @@ defmodule ComfycampWeb.Router do
live "/users/confirm", UserConfirmationInstructionsLive, :new
end
end
scope "/admin", ComfycampWeb do
pipe_through [:browser, :require_authenticated_user, :ensure_admin]
get "/", AdminPageController, :home
get "/posts", AdminPageController, :posts
get "/users", AdminPageController, :users
get "/services", AdminPageController, :services
end
end

View file

@ -213,6 +213,17 @@ defmodule ComfycampWeb.UserAuth do
end
end
def ensure_admin(conn, _opts) do
if conn.assigns[:current_user].is_admin do
conn
else
conn
|> put_flash(:error, "Вы должны быть администратором для просмотра.")
|> redirect(to: ~p"/")
|> halt()
end
end
defp put_token_in_session(conn, token) do
conn
|> put_session(:user_token, token)

View file

@ -0,0 +1,9 @@
defmodule Comfycamp.Repo.Migrations.AddIsAdminField do
use Ecto.Migration
def change do
alter table(:users) do
add :is_admin, :boolean, null: false, default: false
end
end
end