Skip to content

Commit

Permalink
Allow test games
Browse files Browse the repository at this point in the history
  • Loading branch information
vtm9 committed Feb 27, 2025
1 parent 6d96a75 commit af72eac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,32 @@ defmodule CodebattleWeb.Plugs.AssignCurrentUser do
end
end

@allowed_paths [
~r{^\/games\/\d+\/?$},
~r{^\/games\/training\/?$}
]
defp handle_guest(conn) do
if FunWithFlags.enabled?(:restrict_guests_access) do
url = Application.get_env(:codebattle, :guest_user_force_redirect_url)
restrict_guests_access? = FunWithFlags.enabled?(:restrict_guests_access)
allow_test_game? = FunWithFlags.enabled?(:allow_test_game)
url = Application.get_env(:codebattle, :guest_user_force_redirect_url)

cond do
restrict_guests_access? && allow_test_game? && Enum.any?(@allowed_paths, &Regex.match?(&1, conn.request_path)) ->
assign(conn, :current_user, User.build_guest())

# redirect to login page if there is now custom guest_auth_url
if url in [nil, ""] do
restrict_guests_access? && url in [nil, ""] ->
conn
|> redirect(to: Routes.session_path(conn, :new))
|> halt()
else

restrict_guests_access? ->
conn
|> redirect(external: url)
|> halt()
end
else
assign(conn, :current_user, User.build_guest())

:default ->
assign(conn, :current_user, User.build_guest())
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ defmodule CodebattleWeb.Plugs.ForceRedirect do
~r{^\/api\/v1\/playbook\/\d+\/?$}
]

# @allowed_banned_paths [
# ~r{^\/$},
# ~r{^\/e\/\S+\/?$},
# ~r{^\/maintenance\/?$},
# ~r{^\/api\/v1\/events\/.+$},
# ~r{^\/auth\/token\/?$}
# ]
@allowed_banned_paths [
# ~r{^\/$},
# ~r{^\/e\/\S+\/?$},
~r{^\/maintenance\/?$},
# ~r{^\/api\/v1\/events\/.+$},
# ~r{^\/auth\/token\/?$}
]

@spec init(Keyword.t()) :: Keyword.t()
def init(opts), do: opts
Expand All @@ -43,7 +43,7 @@ defmodule CodebattleWeb.Plugs.ForceRedirect do
User.admin?(conn.assigns.current_user) ->
conn

conn.assigns.current_user.subscription_type == :banned ->
conn.assigns.current_user.subscription_type == :banned && !Enum.any?(@allowed_banned_paths, &Regex.match?(&1, conn.request_path)) ->
conn
|> redirect(to: "/maintenance")
|> halt()
Expand Down

0 comments on commit af72eac

Please sign in to comment.