Skip to content

Commit 2e8601d

Browse files
committed
add auth
1 parent 8918472 commit 2e8601d

File tree

1 file changed

+76
-53
lines changed

1 file changed

+76
-53
lines changed

lib/algora_web/live/org/bounties_live.ex

Lines changed: 76 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -364,63 +364,86 @@ defmodule AlgoraWeb.Org.BountiesLive do
364364
end
365365

366366
def handle_event("delete-bounty", %{"id" => bounty_id}, socket) do
367-
bounty =
368-
Bounty
369-
|> Repo.get(bounty_id)
370-
|> Repo.preload([:owner, [ticket: [repository: :user]]])
371-
372-
with {:ok, installation} <-
373-
Workspace.fetch_installation_by(
374-
provider: "github",
375-
connected_user_id: bounty.ticket.repository.user.id
376-
),
377-
{:ok, token} <- Github.get_installation_token(installation.provider_id),
378-
{:ok, cr} <-
379-
Workspace.fetch_command_response(bounty.ticket_id, :bounty),
380-
dbg(cr),
381-
{:ok, _} <-
382-
Github.delete_issue_comment(
383-
token,
384-
bounty.ticket.repository.user.provider_login,
385-
bounty.ticket.repository.name,
386-
cr.provider_response_id
387-
),
388-
:ok <-
389-
Workspace.remove_existing_amount_labels(
390-
token,
391-
bounty.ticket.repository.user.provider_login,
392-
bounty.ticket.repository.name,
393-
bounty.ticket.number
394-
),
395-
{:ok, _} <-
396-
Github.remove_label_from_issue(
397-
token,
398-
bounty.ticket.repository.user.provider_login,
399-
bounty.ticket.repository.name,
400-
bounty.ticket.number,
401-
"💎 Bounty"),
402-
403-
{:ok, _} <- Workspace.delete_command_response(cr.id),
404-
{:ok, _bounty} <- Bounties.delete_bounty(bounty) do
405-
{:noreply,
406-
socket
407-
|> put_flash(:info, "Bounty deleted successfully")
408-
|> assign_bounties()}
409-
else
410-
{:error, _changeset} ->
411-
{:noreply, put_flash(socket, :error, "Failed to delete bounty")}
367+
cond do
368+
socket.assigns.current_user_role in [:admin, :mod] ->
369+
bounty =
370+
Bounty
371+
|> Repo.get(bounty_id)
372+
|> Repo.preload([:owner, [ticket: [repository: :user]]])
373+
374+
with {:ok, installation} <-
375+
Workspace.fetch_installation_by(
376+
provider: "github",
377+
connected_user_id: bounty.ticket.repository.user.id
378+
),
379+
{:ok, token} <- Github.get_installation_token(installation.provider_id),
380+
{:ok, cr} <-
381+
Workspace.fetch_command_response(bounty.ticket_id, :bounty),
382+
{:ok, _} <-
383+
Github.delete_issue_comment(
384+
token,
385+
bounty.ticket.repository.user.provider_login,
386+
bounty.ticket.repository.name,
387+
cr.provider_response_id
388+
),
389+
:ok <-
390+
Workspace.remove_existing_amount_labels(
391+
token,
392+
bounty.ticket.repository.user.provider_login,
393+
bounty.ticket.repository.name,
394+
bounty.ticket.number
395+
),
396+
{:ok, _} <-
397+
Github.remove_label_from_issue(
398+
token,
399+
bounty.ticket.repository.user.provider_login,
400+
bounty.ticket.repository.name,
401+
bounty.ticket.number,
402+
"💎 Bounty"),
403+
404+
{:ok, _} <- Workspace.delete_command_response(cr.id),
405+
{:ok, _bounty} <- Bounties.delete_bounty(bounty) do
406+
{:noreply,
407+
socket
408+
|> put_flash(:info, "Bounty deleted successfully")
409+
|> assign_bounties()}
410+
else
411+
{:error, _changeset} ->
412+
{:noreply, put_flash(socket, :error, "Failed to delete bounty")}
413+
end
414+
415+
is_nil(socket.assigns.current_user) ->
416+
{:noreply,
417+
redirect(socket,
418+
to: ~p"/auth/login?#{%{return_to: ~p"/#{socket.assigns.current_org.handle}/bounties"}}"
419+
)}
420+
421+
true ->
422+
{:noreply, put_flash(socket, :error, "You are not authorized to delete bounties")}
412423
end
413424
end
414425

415426
def handle_event("edit-bounty-amount", %{"id" => bounty_id}, socket) do
416-
[bounty] = Bounties.list_bounties(id: bounty_id)
417-
changeset = edit_amount_changeset(%{amount: bounty.amount})
418-
419-
{:noreply,
420-
socket
421-
|> assign(:editing_bounty, bounty)
422-
|> assign(:edit_form, to_form(changeset))
423-
|> assign(:show_edit_modal, true)}
427+
cond do
428+
socket.assigns.current_user_role in [:admin, :mod] ->
429+
[bounty] = Bounties.list_bounties(id: bounty_id)
430+
changeset = edit_amount_changeset(%{amount: bounty.amount})
431+
432+
{:noreply,
433+
socket
434+
|> assign(:editing_bounty, bounty)
435+
|> assign(:edit_form, to_form(changeset))
436+
|> assign(:show_edit_modal, true)}
437+
438+
is_nil(socket.assigns.current_user) ->
439+
{:noreply,
440+
redirect(socket,
441+
to: ~p"/auth/login?#{%{return_to: ~p"/#{socket.assigns.current_org.handle}/bounties"}}"
442+
)}
443+
444+
true ->
445+
{:noreply, put_flash(socket, :error, "You are not authorized to edit bounty amounts")}
446+
end
424447
end
425448

426449
def handle_event("validate-amount", params, socket) do

0 commit comments

Comments
 (0)