fixing error tag ids problem in liveview, and activating stricter password requirements

This commit is contained in:
Adam Piontek 2021-03-06 18:36:45 -05:00
parent a151bfcee7
commit 73c97f194b
9 changed files with 56 additions and 1038 deletions

View file

@ -14,7 +14,7 @@ defmodule Shift73k.Accounts.User do
defenum(RolesEnum, :role, Keyword.keys(@roles))
@max_email 254
@min_password 6
@min_password 8
@max_password 80
@derive {Inspect, except: [:password]}
@ -29,7 +29,6 @@ defmodule Shift73k.Accounts.User do
field(:role, RolesEnum, default: :user)
has_many(:shift_templates, ShiftTemplate)
# has_one(:default_shift_template, ShiftTemplate, references: :default_shift_template_id)
belongs_to(:default_shift_template, ShiftTemplate)
timestamps()
@ -122,9 +121,11 @@ defmodule Shift73k.Accounts.User do
defp validate_password_not_required(changeset, opts) do
changeset
|> validate_length(:password, min: @min_password, max: @max_password)
# |> validate_format(:password, ~r/[a-z]/, message: "at least one lower case character")
# |> validate_format(:password, ~r/[A-Z]/, message: "at least one upper case character")
# |> validate_format(:password, ~r/[!?@#$%^&*_0-9]/, message: "at least one digit or punctuation character")
|> validate_format(:password, ~r/[a-z]/, message: "at least one lower case character")
|> validate_format(:password, ~r/[A-Z]/, message: "at least one upper case character")
|> validate_format(:password, ~r/[!?@#$%^&*_0-9]/,
message: "at least one digit or punctuation character"
)
|> maybe_hash_password(opts)
end

View file

@ -22,7 +22,7 @@
maxlength: User.max_email,
autofocus: true,
phx_debounce: "blur",
aria_describedby: error_id(f, :email)
aria_describedby: error_ids(f, :email)
%>
<%= error_tag f, :email %>
</div>
@ -39,7 +39,7 @@
class: input_class(f, :password, "form-control"),
maxlength: User.max_password,
phx_debounce: "250",
aria_describedby: error_id(f, :password)
aria_describedby: error_ids(f, :password)
%>
<%= error_tag f, :password %>
</div>

View file

@ -20,7 +20,7 @@
class: input_class(f, :password, "form-control"),
maxlength: User.max_password,
autofocus: true,
aria_describedby: error_id(f, :password)
aria_describedby: error_ids(f, :password)
%>
<%= error_tag f, :password %>
</div>
@ -36,7 +36,7 @@
value: input_value(f, :password_confirmation),
class: input_class(f, :password_confirmation, "form-control"),
maxlength: User.max_password,
aria_describedby: error_id(f, :password_confirmation)
aria_describedby: error_ids(f, :password_confirmation)
%>
<%= error_tag f, :password_confirmation %>
</div>

View file

@ -16,7 +16,7 @@
placeholder: "e.g., babka@73k.us",
maxlength: User.max_email,
phx_debounce: "500",
aria_describedby: error_id(f, :email)
aria_describedby: error_ids(f, :email)
%>
<%= error_tag f, :email %>
</div>
@ -32,7 +32,7 @@
<%= password_input f, :current_password,
value: input_value(f, :current_password),
class: "form-control",
aria_describedby: error_id(f, :current_password)
aria_describedby: error_ids(f, :current_password)
%>
<%= error_tag f, :current_password %>
</div>

View file

@ -15,7 +15,7 @@
class: input_class(f, :password, "form-control"),
maxlength: User.max_password,
phx_debounce: "500",
aria_describedby: error_id(f, :password)
aria_describedby: error_ids(f, :password)
%>
<%= error_tag f, :password %>
</div>
@ -31,7 +31,7 @@
value: input_value(f, :password_confirmation),
class: input_class(f, :password_confirmation, "form-control"),
maxlength: User.max_password,
aria_describedby: error_id(f, :password_confirmation)
aria_describedby: error_ids(f, :password_confirmation)
%>
<%= error_tag f, :password_confirmation %>
</div>
@ -46,7 +46,7 @@
<%= password_input f, :current_password,
value: input_value(f, :current_password),
class: "form-control",
aria_describedby: error_id(f, :current_password)
aria_describedby: error_ids(f, :current_password)
%>
<%= error_tag f, :current_password %>
</div>

View file

@ -19,7 +19,7 @@
maxlength: User.max_email,
autofocus: true,
phx_debounce: "250",
aria_describedby: error_id(f, :email)
aria_describedby: error_ids(f, :email)
%>
<%= error_tag f, :email %>
</div>
@ -50,7 +50,7 @@
value: input_value(f, :password),
class: input_class(f, :password, "form-control"),
maxlength: User.max_password,
aria_describedby: error_id(f, :password)
aria_describedby: error_ids(f, :password)
%>
<%= error_tag f, :password %>
</div>

View file

@ -8,26 +8,41 @@ defmodule Shift73kWeb.ErrorHelpers do
@doc """
Generates tag for inlined form input errors.
"""
def error_tag(form, field, opts \\ []) do
opts = error_opts(form, field, opts)
def error_tag(%Phoenix.HTML.Form{} = form, field, opts \\ []) do
opts = error_common_opts(form, field, "invalid-feedback", opts)
form.errors
|> Keyword.get_values(field)
|> Enum.map(fn error -> content_tag(:span, translate_error(error), opts) end)
|> Stream.with_index()
|> Enum.map(fn err_with_index -> error_tag_span(err_with_index, opts) end)
end
defp error_opts(form, field, opts) do
append = "invalid-feedback"
input_id = input_id(form, field)
defp error_tag_span({err, _} = err_with_index, opts) do
opts = error_tag_opts(err_with_index, opts)
content_tag(:span, translate_error(err), opts)
end
opts
|> Keyword.put_new(:id, error_id(input_id))
|> Keyword.put_new(:phx_feedback_for, input_id)
defp error_common_opts(form, field, append, opts) do
Keyword.put(opts, :phx_feedback_for, input_id(form, field))
|> Keyword.update(:class, append, fn c -> "#{append} #{c}" end)
end
def error_id(%Phoenix.HTML.Form{} = form, field), do: input_id(form, field) |> error_id()
def error_id(input_id) when is_binary(input_id), do: "#{input_id}_feedback"
defp error_tag_opts({_err, err_index}, opts) do
input_id = Keyword.get(opts, :phx_feedback_for, "")
Keyword.put(opts, :id, error_id(input_id, err_index))
end
defp error_id(input_id, err_index), do: "#{input_id}_feedback-#{err_index}"
def error_ids(%Phoenix.HTML.Form{} = form, field) do
input_id = input_id(form, field)
form.errors
|> Keyword.get_values(field)
|> Stream.with_index()
|> Stream.map(fn {_, index} -> error_id(input_id, index) end)
|> Enum.join(" ")
end
def input_class(form, field, classes \\ "") do
case form.source.action do

File diff suppressed because one or more lines are too long

View file

@ -20,24 +20,24 @@ alias Shift73k.Accounts.User
{:ok, _admin} =
Accounts.register_user(%{
email: "admin@company.com",
password: "123456789abc",
password_confirmation: "123456789abc",
password: "123456789abC",
password_confirmation: "123456789abC",
role: Accounts.registration_role()
})
{:ok, _user_1} =
Accounts.register_user(%{
email: "user1@company.com",
password: "123456789abc",
password_confirmation: "123456789abc",
password: "123456789abC",
password_confirmation: "123456789abC",
role: Accounts.registration_role()
})
{:ok, _user_2} =
Accounts.register_user(%{
email: "user2@company.com",
password: "123456789abc",
password_confirmation: "123456789abc",
password: "123456789abC",
password_confirmation: "123456789abC",
role: Accounts.registration_role()
})
@ -46,14 +46,15 @@ this_path = Path.dirname(__ENV__.file)
users_json = Path.join(this_path, "MOCK_DATA_users.json")
count_to_take = 15
# count_to_take = 15
mock_users = users_json |> File.read!() |> Jason.decode!() |> Enum.take_random(count_to_take)
mock_users = users_json |> File.read!() |> Jason.decode!()
# |> Enum.take_random(count_to_take)
mock_users = ~s([
{"email":"adam@73k.us","password":"adamadam","role":"admin","inserted_at":"2018-12-14T01:01:01Z","confirmed_at":true},
{"email":"karen@73k.us","password":"karenkaren","role":"manager","inserted_at":"2018-12-14T01:06:01Z","confirmed_at":true},
{"email":"kat@73k.us","password":"katkat","role":"manager","inserted_at":"2018-12-14T01:06:01Z","confirmed_at":true}
{"email":"adam@73k.us","password":"adamadamA1","role":"admin","inserted_at":"2018-12-14T01:01:01Z","confirmed_at":true},
{"email":"karen@73k.us","password":"karenkarenA1","role":"manager","inserted_at":"2018-12-14T01:06:01Z","confirmed_at":true},
{"email":"kat@73k.us","password":"katkatA1","role":"manager","inserted_at":"2018-12-14T01:06:01Z","confirmed_at":true}
]) |> Jason.decode!() |> Enum.concat(mock_users)
mock_users =