From 33029dae043d0a8f3768ac1619fed3e9237734fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Gilberto=20Balsini=20Moura?= Date: Sat, 12 Sep 2020 21:14:06 -0300 Subject: [PATCH] Add roles to user --- lib/real_estate/accounts/user.ex | 7 +++++++ mix.exs | 3 ++- mix.lock | 1 + .../20200913000905_add_role_to_users.exs | 21 +++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 priv/repo/migrations/20200913000905_add_role_to_users.exs diff --git a/lib/real_estate/accounts/user.ex b/lib/real_estate/accounts/user.ex index 427aef1c..d53a15a1 100644 --- a/lib/real_estate/accounts/user.ex +++ b/lib/real_estate/accounts/user.ex @@ -1,6 +1,12 @@ defmodule RealEstate.Accounts.User do use Ecto.Schema import Ecto.Changeset + import EctoEnum + + defenum(RolesEnum, :role, [ + :user, + :admin + ]) @derive {Inspect, except: [:password]} schema "users" do @@ -9,6 +15,7 @@ defmodule RealEstate.Accounts.User do field :hashed_password, :string field :confirmed_at, :naive_datetime + field :role, RolesEnum, default: :user timestamps() end diff --git a/mix.exs b/mix.exs index 1af1968c..77a77023 100644 --- a/mix.exs +++ b/mix.exs @@ -48,7 +48,8 @@ defmodule RealEstate.MixProject do {:gettext, "~> 0.11"}, {:jason, "~> 1.0"}, {:plug_cowboy, "~> 2.0"}, - {:phx_gen_auth, "~> 0.5", only: [:dev], runtime: false} + {:phx_gen_auth, "~> 0.5", only: [:dev], runtime: false}, + {:ecto_enum, "~> 1.4"} ] end diff --git a/mix.lock b/mix.lock index af456650..7e26b0b5 100644 --- a/mix.lock +++ b/mix.lock @@ -7,6 +7,7 @@ "db_connection": {:hex, :db_connection, "2.2.2", "3bbca41b199e1598245b716248964926303b5d4609ff065125ce98bcd368939e", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm", "642af240d8a8affb93b4ba5a6fcd2bbcbdc327e1a524b825d383711536f8070c"}, "decimal": {:hex, :decimal, "1.9.0", "83e8daf59631d632b171faabafb4a9f4242c514b0a06ba3df493951c08f64d07", [:mix], [], "hexpm", "b1f2343568eed6928f3e751cf2dffde95bfaa19dd95d09e8a9ea92ccfd6f7d85"}, "ecto": {:hex, :ecto, "3.4.6", "08f7afad3257d6eb8613309af31037e16c36808dfda5a3cd0cb4e9738db030e4", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6f13a9e2a62e75c2dcfc7207bfc65645ab387af8360db4c89fee8b5a4bf3f70b"}, + "ecto_enum": {:hex, :ecto_enum, "1.4.0", "d14b00e04b974afc69c251632d1e49594d899067ee2b376277efd8233027aec8", [:mix], [{:ecto, ">= 3.0.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "> 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:mariaex, ">= 0.0.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm", "8fb55c087181c2b15eee406519dc22578fa60dd82c088be376d0010172764ee4"}, "ecto_sql": {:hex, :ecto_sql, "3.4.5", "30161f81b167d561a9a2df4329c10ae05ff36eca7ccc84628f2c8b9fa1e43323", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.4.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.3.0 or ~> 0.4.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.0", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "31990c6a3579b36a3c0841d34a94c275e727de8b84f58509da5f1b2032c98ac2"}, "elixir_make": {:hex, :elixir_make, "0.6.1", "8faa29a5597faba999aeeb72bbb9c91694ef8068f0131192fb199f98d32994ef", [:mix], [], "hexpm", "35d33270680f8d839a4003c3e9f43afb595310a592405a00afc12de4c7f55a18"}, "file_system": {:hex, :file_system, "0.2.8", "f632bd287927a1eed2b718f22af727c5aeaccc9a98d8c2bd7bff709e851dc986", [:mix], [], "hexpm", "97a3b6f8d63ef53bd0113070102db2ce05352ecf0d25390eb8d747c2bde98bca"}, diff --git a/priv/repo/migrations/20200913000905_add_role_to_users.exs b/priv/repo/migrations/20200913000905_add_role_to_users.exs new file mode 100644 index 00000000..c400c658 --- /dev/null +++ b/priv/repo/migrations/20200913000905_add_role_to_users.exs @@ -0,0 +1,21 @@ +defmodule RealEstate.Repo.Migrations.AddRoleToUsers do + use Ecto.Migration + + alias RealEstate.Accounts.User.RolesEnum + + def up do + RolesEnum.create_type() + + alter table(:users) do + add :role, RolesEnum.type(), null: false + end + end + + def down do + alter table(:users) do + remove :role + end + + RolesEnum.drop_type() + end +end