Skip to content

Commit

Permalink
Guarding with do_create_post function
Browse files Browse the repository at this point in the history
  • Loading branch information
jere0500 committed Jul 17, 2024
1 parent ac304d2 commit f1cfd44
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions lib/teiserver_web/controllers/admin/user_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -228,38 +228,32 @@ defmodule TeiserverWeb.Admin.UserController do
conn
|> put_flash(:danger, "Invalid user name")
|> redirect(to: ~p"/teiserver/admin/user")
else
if allow?(conn, "Server") do
do_create_post(conn, params)
else
conn
|> put_flash(:danger, "No access.")
|> redirect(to: ~p"/teiserver/admin/user")
end
end
end

if allow?(conn, "Server") do
password =
if is_nil(params["password"]) or String.trim(params["password"]) == "" do
"password"
else
params["password"]
end

email =
if is_nil(params["email"]) or String.trim(params["email"]) == "" do
UUID.uuid1()
else
params["email"]
end
@spec do_create_post(Plug.Conn.t(), map) :: Plug.Conn.t()
defp do_create_post(conn, params \\ %{}) do

Check warning on line 243 in lib/teiserver_web/controllers/admin/user_controller.ex

View workflow job for this annotation

GitHub Actions / Dialyzer

no_return

Function do_create_post/2 has no local return.

Check warning on line 243 in lib/teiserver_web/controllers/admin/user_controller.ex

View workflow job for this annotation

GitHub Actions / Dialyzer

default values for the optional arguments in do_create_post/2 are never used

Check warning on line 243 in lib/teiserver_web/controllers/admin/user_controller.ex

View workflow job for this annotation

GitHub Actions / Build and test

default values for the optional arguments in do_create_post/2 are never used

Check warning on line 243 in lib/teiserver_web/controllers/admin/user_controller.ex

View workflow job for this annotation

GitHub Actions / mix format

default values for the optional arguments in do_create_post/2 are never used
password = Map.get(params, "password", "password")
email = Map.get(params, "email", UUID.uuid1())

case Teiserver.CacheUser.register_user(params["name"], email, password) do
:success ->
conn
|> put_flash(:info, "User created successfully.")
|> redirect(to: ~p"/teiserver/admin/user")
case Teiserver.CacheUser.register_user(params["name"], email, password) do
:success ->
conn
|> put_flash(:info, "User created successfully.")
|> redirect(to: ~p"/teiserver/admin/user")

{:failure, str} ->
conn
|> put_flash(:error, "Problem creating user: " <> str)
|> redirect(to: ~p"/teiserver/admin/user")
end
else
conn
|> put_flash(:danger, "No access.")
|> redirect(to: ~p"/teiserver/admin/user")
{:failure, str} ->
conn
|> put_flash(:error, "Problem creating user: " <> str)
|> redirect(to: ~p"/teiserver/admin/user")
end
end

Expand Down

0 comments on commit f1cfd44

Please sign in to comment.