feat: add the ability to approve users

This commit is contained in:
Ivan R. 2024-08-28 00:58:20 +05:00
parent 03256685c0
commit 7aa77b1604
Signed by: lumin
GPG key ID: E0937DC7CD6D3817
5 changed files with 64 additions and 0 deletions

View file

@ -290,6 +290,12 @@ defmodule Comfycamp.Accounts do
end end
end end
def update_approval_status(user, is_approved) do
user
|> User.approval_changeset(%{"is_approved" => is_approved})
|> Repo.update()
end
defp confirm_user_multi(user) do defp confirm_user_multi(user) do
Ecto.Multi.new() Ecto.Multi.new()
|> Ecto.Multi.update(:user, User.confirm_changeset(user)) |> Ecto.Multi.update(:user, User.confirm_changeset(user))

View file

@ -136,6 +136,12 @@ defmodule Comfycamp.Accounts.User do
change(user, confirmed_at: now) change(user, confirmed_at: now)
end end
def approval_changeset(user, attrs) do
user
|> cast(attrs, [:is_approved])
|> validate_required([:is_approved])
end
@doc """ @doc """
Verifies the password. Verifies the password.

View file

@ -21,4 +21,36 @@ defmodule ComfycampWeb.UserEditorController do
|> put_layout(html: :admin) |> put_layout(html: :admin)
|> render(:show, page_title: user.email, user: user, stylesheets: ["/assets/admin.css"]) |> render(:show, page_title: user.email, user: user, stylesheets: ["/assets/admin.css"])
end end
def approve(conn, %{"id" => id}) do
user = Accounts.get_user!(id)
case Accounts.update_approval_status(user, true) do
{:ok, user} ->
conn
|> put_flash(:info, "Пользователь одобрен")
|> redirect(to: ~p"/admin/users/#{user}")
{:error, _changeset} ->
conn
|> put_flash(:error, "Ошибка при одобрении")
|> redirect(to: ~p"/admin/users/#{user}")
end
end
def disapprove(conn, %{"id" => id}) do
user = Accounts.get_user!(id)
case Accounts.update_approval_status(user, false) do
{:ok, user} ->
conn
|> put_flash(:info, "Одобрение отменено")
|> redirect(to: ~p"/admin/users/#{user}")
{:error, _changeset} ->
conn
|> put_flash(:error, "Ошибка при отмене одобрения")
|> redirect(to: ~p"/admin/users/#{user}")
end
end
end end

View file

@ -39,6 +39,24 @@ defmodule ComfycampWeb.UserEditorHTML do
Описания нет. Описания нет.
<% end %> <% end %>
</p> </p>
<%= if @user.is_approved == false do %>
<.link
method="PUT"
href={~p"/admin/users/#{@user}/approve"}
data-confirm="Точно хотите одобрить пользователя?"
>
Одобрить
</.link>
<% else %>
<.link
method="PUT"
href={~p"/admin/users/#{@user}/disapprove"}
data-confirm="Точно хотите отменить одобрение?"
>
Отменить одобрение
</.link>
<% end %>
</div> </div>
""" """
end end

View file

@ -95,5 +95,7 @@ defmodule ComfycampWeb.Router do
get "/services", AdminPageController, :services get "/services", AdminPageController, :services
resources "/notes", NotesEditorController resources "/notes", NotesEditorController
resources "/users", UserEditorController, only: [:index, :show] resources "/users", UserEditorController, only: [:index, :show]
put "/users/:id/approve", UserEditorController, :approve
put "/users/:id/disapprove", UserEditorController, :disapprove
end end
end end