diff --git a/assets/css/admin.css b/assets/css/admin.css new file mode 100644 index 0000000..db85c68 --- /dev/null +++ b/assets/css/admin.css @@ -0,0 +1,10 @@ +.admin-panel { + display: flex; + gap: 16px; + margin-top: 16px; +} + +.admin-panel ul { + padding: 0; + margin: 0; +} diff --git a/assets/css/app.css b/assets/css/app.css index 39db93a..41f56fb 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -1,5 +1,6 @@ @import "./core_components.css"; @import "./flash.css"; +@import "./admin.css"; :root { --bg: #13151a; diff --git a/lib/comfycamp/accounts/user.ex b/lib/comfycamp/accounts/user.ex index 78bc616..66a3727 100644 --- a/lib/comfycamp/accounts/user.ex +++ b/lib/comfycamp/accounts/user.ex @@ -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 diff --git a/lib/comfycamp_web/components/layouts/admin.html.heex b/lib/comfycamp_web/components/layouts/admin.html.heex new file mode 100644 index 0000000..f0a86ba --- /dev/null +++ b/lib/comfycamp_web/components/layouts/admin.html.heex @@ -0,0 +1,30 @@ +<.flash_group flash={@flash} /> + +
+
+

Comfycamp - админка

+ <.link href={~p"/"}> + Главная страница + +
+
    +
  • + <.link href={~p"/admin/posts"}> + Посты + +
  • +
  • + <.link href={~p"/admin/users"}> + Пользователи + +
  • +
  • + <.link href={~p"/admin/services"}> + Сервисы + +
  • +
+ <%= @inner_content %> +
+
+
diff --git a/lib/comfycamp_web/components/navbar.ex b/lib/comfycamp_web/components/navbar.ex index a7c8ba5..14424c2 100644 --- a/lib/comfycamp_web/components/navbar.ex +++ b/lib/comfycamp_web/components/navbar.ex @@ -23,6 +23,9 @@ defmodule ComfycampWeb.NavBar do
<%= if @current_user do %> + <.link :if={@current_user.is_admin} href={~p"/admin"}> + Админка + <.link href={~p"/users/settings"}> Настройки diff --git a/lib/comfycamp_web/controllers/admin_page_controller.ex b/lib/comfycamp_web/controllers/admin_page_controller.ex new file mode 100644 index 0000000..d575ce1 --- /dev/null +++ b/lib/comfycamp_web/controllers/admin_page_controller.ex @@ -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 diff --git a/lib/comfycamp_web/controllers/admin_page_html.ex b/lib/comfycamp_web/controllers/admin_page_html.ex new file mode 100644 index 0000000..7592511 --- /dev/null +++ b/lib/comfycamp_web/controllers/admin_page_html.ex @@ -0,0 +1,9 @@ +defmodule ComfycampWeb.AdminPageHTML do + use ComfycampWeb, :html + + def home(assigns) do + ~H""" + Добро пожаловать, админ. + """ + end +end diff --git a/lib/comfycamp_web/router.ex b/lib/comfycamp_web/router.ex index e1306f4..3942437 100644 --- a/lib/comfycamp_web/router.ex +++ b/lib/comfycamp_web/router.ex @@ -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 diff --git a/lib/comfycamp_web/user_auth.ex b/lib/comfycamp_web/user_auth.ex index 9785923..f0f7ce5 100644 --- a/lib/comfycamp_web/user_auth.ex +++ b/lib/comfycamp_web/user_auth.ex @@ -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) diff --git a/priv/repo/migrations/20240728162610_add_is_admin_field.exs b/priv/repo/migrations/20240728162610_add_is_admin_field.exs new file mode 100644 index 0000000..57a4ba5 --- /dev/null +++ b/priv/repo/migrations/20240728162610_add_is_admin_field.exs @@ -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