Skip to content

Commit

Permalink
Merge pull request #259 from digitalnatives/nonose/fix_email_trailing…
Browse files Browse the repository at this point in the history
…_space

Properly check e-mail format.
  • Loading branch information
rhnonose committed Sep 29, 2017
2 parents cc84e13 + 11d205d commit 40b4800
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 9 deletions.
3 changes: 3 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ config :course_planner, CoursePlanner.Notifications.NotifierScheduler,
{"30 17 * * *", {CoursePlanner.Notifications, :build_all_notifications, []}}
]

config :email_checker,
validations: [EmailChecker.Check.Format]

import_config "#{Mix.env}.exs"
22 changes: 20 additions & 2 deletions lib/course_planner/accounts/user.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule CoursePlanner.Accounts.User do
@moduledoc false
use Ecto.Schema
import Ecto.Changeset
use Coherence.Schema

alias CoursePlanner.Types.{UserRole, ParticipationType}
alias CoursePlanner.Notifications.Notification
alias Ecto.Changeset

@target_params [
:name, :family_name, :nickname,
Expand Down Expand Up @@ -38,7 +38,7 @@ defmodule CoursePlanner.Accounts.User do
|> cast(params, @target_params ++ coherence_fields())
|> update_change(:email, &String.downcase/1)
|> validate_required([:email, :role])
|> validate_format(:email, ~r/@/)
|> validate_email_format(:email)
|> unique_constraint(:email)
|> validate_length(:comments, max: 255)
|> validate_coherence(params)
Expand All @@ -60,4 +60,22 @@ defmodule CoursePlanner.Accounts.User do
def changeset(model, params, :update) do
changeset(model, params)
end

defp validate_email_format(changeset, field) do
if Changeset.get_field(changeset, field) do
do_validate_email_format(changeset, field)
else
changeset
end
end

defp do_validate_email_format(changeset, field) do
changeset
|> Changeset.get_field(field)
|> EmailChecker.valid?()
|> case do
true -> changeset
false -> Changeset.add_error(changeset, field, "has invalid format", [validation: :format])
end
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule CoursePlanner.Mixfile do
[mod: {CoursePlanner, []},
applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext,
:phoenix_ecto, :postgrex, :coherence, :swoosh, :quantum, :canada,
:canary, :elixir_make]]
:canary, :elixir_make, :email_checker]]
end

# Specifies which paths to compile per environment.
Expand Down Expand Up @@ -59,6 +59,7 @@ defmodule CoursePlanner.Mixfile do
{:excoveralls, "~> 0.6", only: :test},
{:quantum, ">= 2.0.0-beta.1"},
{:csv, "~> 2.0.0"},
{:email_checker, "~> 0.1.1"},
]
end

Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dogma": {:hex, :dogma, "0.1.15", "5bceba9054b2b97a4adcb2ab4948ca9245e5258b883946e82d32f785340fd411", [:mix], [{:poison, ">= 2.0.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
"ecto": {:hex, :ecto, "2.1.6", "29b45f393c2ecd99f83e418ea9b0a2af6078ecb30f401481abac8a473c490f84", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
"elixir_make": {:hex, :elixir_make, "0.4.0", "992f38fabe705bb45821a728f20914c554b276838433349d4f2341f7a687cddf", [:mix], [], "hexpm"},
"email_checker": {:hex, :email_checker, "0.1.1", "48a590555d1d36a4500047811e13d940e8d8cd298ee50d3f7822936bbec61f7b", [:mix], [{:socket, "~> 0.3.1", [hex: :socket, repo: "hexpm", optional: false]}], "hexpm"},
"ex_machina": {:hex, :ex_machina, "2.0.0", "ec284c6f57233729cea9319e083f66e613e82549f78eccdb2059aeba5d0df9f3", [:mix], [{:ecto, "~> 2.1", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm"},
"excoveralls": {:hex, :excoveralls, "0.7.2", "f69ede8c122ccd3b60afc775348a53fc8c39fe4278aee2f538f0d81cc5e7ff3a", [:mix], [{:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm"},
Expand All @@ -42,6 +43,7 @@
"postgrex": {:hex, :postgrex, "0.13.3", "c277cfb2a9c5034d445a722494c13359e361d344ef6f25d604c2353185682bfc", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm"},
"quantum": {:hex, :quantum, "2.0.0", "e09848e5ad702810942a585385ee59b35ff7780e0d05e992139d4920108f53da", [:mix], [{:calendar, "~> 0.17", [hex: :calendar, repo: "hexpm", optional: true]}, {:crontab, "~> 1.1", [hex: :crontab, repo: "hexpm", optional: false]}, {:timex, "~> 3.1.13", [hex: :timex, repo: "hexpm", optional: true]}], "hexpm"},
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm"},
"socket": {:hex, :socket, "0.3.12", "4a6543815136503fee67eff0932da1742fad83f84c49130c854114153cc549a6", [], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm"},
"swoosh": {:hex, :swoosh, "0.7.0", "bd29a06d95ee70e1ae44d5db76ba5920ae7a60fd418fe559b6b1a24c179521af", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, repo: "hexpm", optional: true]}, {:gen_smtp, "~> 0.11", [hex: :gen_smtp, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: false]}, {:plug, "~> 1.1", [hex: :plug, repo: "hexpm", optional: true]}, {:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
"timex": {:hex, :timex, "3.1.24", "d198ae9783ac807721cca0c5535384ebdf99da4976be8cefb9665a9262a1e9e3", [:mix], [{:combine, "~> 0.7", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm"},
Expand Down
8 changes: 4 additions & 4 deletions test/lib/course_planner/accounts/permissions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ defmodule CoursePlanner.PermissionsTest do
alias CoursePlannerWeb.TaskController

@coordinator %User{
email: "valid@email",
email: "coordiantor@courseplanner.com",
role: "Coordinator"
}
@volunteer %User{
id: 2,
email: "valid@email2",
email: "volunteer@courseplanner.com",
role: "Volunteer"
}
@student %User{
email: "student@example.com",
email: "student@courseplanner.com",
role: "Student"
}
@teacher %User{
email: "teacher@example.com",
email: "teacher@courseplanner.com",
role: "Teacher"
}

Expand Down
2 changes: 1 addition & 1 deletion test/lib/course_planner/accounts/students_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule CoursePlanner.StudentsTest do
alias CoursePlanner.{Courses.Course, Courses.OfferedCourse, Repo, Accounts.Students}
alias CoursePlanner.Terms.Term

@valid_attrs %{"name" => "some content", "email" => "valid@email"}
@valid_attrs %{"name" => "some content", "email" => "valid@courseplanner.com"}

defp create_term(name, start_date, end_date, course) do
Repo.insert!(
Expand Down
2 changes: 1 addition & 1 deletion test/lib/course_planner/accounts/teachers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule CoursePlanner.TeachersTest do
alias CoursePlanner.{Courses.Course, Courses.OfferedCourse, Repo, Accounts.Teachers}
alias CoursePlanner.Terms.Term

@valid_attrs %{"name" => "some content", "email" => "valid@email"}
@valid_attrs %{"name" => "some content", "email" => "valid@courseplanner.com"}

defp create_term(name, start_date, end_date, course) do
Repo.insert!(
Expand Down

0 comments on commit 40b4800

Please sign in to comment.