diff --git a/lib/auth/role.ex b/lib/auth/role.ex index f2661a2c..8508c657 100644 --- a/lib/auth/role.ex +++ b/lib/auth/role.ex @@ -17,7 +17,7 @@ defmodule Auth.Role do @doc false def changeset(role, attrs) do role - |> cast(attrs, [:name, :desc]) + |> cast(attrs, [:name, :desc, :person_id]) |> validate_required([:name, :desc]) end diff --git a/priv/repo/create_default_roles.exs b/priv/repo/create_default_roles.exs deleted file mode 100644 index 3c4e11cd..00000000 --- a/priv/repo/create_default_roles.exs +++ /dev/null @@ -1,4 +0,0 @@ -# scripts for creating default roles and permissions -defmodule Setup.CreateDefaultRoles do - -end \ No newline at end of file diff --git a/priv/repo/default_roles.json b/priv/repo/default_roles.json new file mode 100644 index 00000000..1216f98e --- /dev/null +++ b/priv/repo/default_roles.json @@ -0,0 +1,12 @@ +[ + { + "name": "superadmin", + "desc": "With great power comes great responsibility", + "person_id": "1" + }, + { + "name": "admin", + "desc": "Can perform all system administration tasks", + "person_id": "1" + } +] \ No newline at end of file diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 55898e91..c71744b0 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -52,9 +52,9 @@ defmodule Auth.Seeds do # write the key:value pair to project .env file def write_env(key, value) do - IO.inspect(File.cwd!, label: "CWD") + # IO.inspect(File.cwd!, label: "cwd") path = File.cwd! <> "/.env" - IO.inspect(path, label: "path") + IO.inspect(path, label: ".env file path") {:ok, data} = File.read(path) # IO.inspect(data) @@ -77,12 +77,36 @@ defmodule Auth.Seeds do |> String.replace("'", "") |> String.split("=") - IO.inspect(List.last(parts), label: List.first(parts)) + # IO.inspect(List.last(parts), label: List.first(parts)) System.put_env(List.first(parts), List.last(parts)) end) end end - Auth.Seeds.create_admin() |> Auth.Seeds.create_apikey_for_admin() + + +# scripts for creating default roles and permissions +defmodule SetupRoles do + alias Auth.Role + + def get_json(filepath) do + # IO.inspect(filepath, label: "filepath") + path = File.cwd! <> filepath + # IO.inspect(path, label: "path") + {:ok, data} = File.read(path) + json = Jason.decode!(data) + # IO.inspect(json) + json + end + + def create_default_roles() do + json = get_json("/priv/repo/default_roles.json") + Enum.each(json, fn role -> + Role.create_role(role) + end) + end +end + +SetupRoles.create_default_roles() \ No newline at end of file diff --git a/role-based-access-control.md b/role-based-access-control.md index 0e846215..e883efcf 100644 --- a/role-based-access-control.md +++ b/role-based-access-control.md @@ -93,6 +93,7 @@ If you don't already have these schemas/tables, see: https://github.com/dwyl/app-mvp-phoenix#create-schemas + ### Create `Roles` and `Permissions` Schemas Let's create the Database Schemas (Tables) @@ -125,7 +126,7 @@ mix ecto.gen.migration create_role_permissions ``` Open the file that was just created, e.g: -[`priv/repo/migrations/20200723143204_create_role_permissions.exs`]() +[`priv/repo/migrations/20200723143204_create_role_permissions.exs`](https://github.com/dwyl/auth/blob/ef4261d09a702c4003cd84f30dabe630b47922d2/priv/repo/migrations/20200723143204_create_role_permissions.exs) And replace the contents with: ```elixir @@ -156,7 +157,7 @@ mix ecto.gen.migration create_people_roles ``` Open the migration file that was just created, e.g: -[`/Users/n/code/auth/priv/repo/migrations/20200723154847_create_people_roles.exs`]() +[`/Users/n/code/auth/priv/repo/migrations/20200723154847_create_people_roles.exs`](https://github.com/dwyl/auth/blob/ef4261d09a702c4003cd84f30dabe630b47922d2/priv/repo/migrations/20200723154847_create_people_roles.exs) Replace the contents of the file with the following code: @@ -179,7 +180,26 @@ defmodule Auth.Repo.Migrations.CreatePeopleRoles do end ``` -This +This is all we need in terms of database tables for now. +Run: +``` +mix ecto.migrate +``` +To create the tables. + +The Entity Relationship Diagram (ERD) should now look like this: + +[![auth-erd-with-roles-permissions](https://user-images.githubusercontent.com/194400/88439166-5c2e0e00-ce02-11ea-93ce-11c3a721cb18.png "Schema Diagram - Click to Enlarge")](https://user-images.githubusercontent.com/194400/88439166-5c2e0e00-ce02-11ea-93ce-11c3a721cb18.png) + +Next we need to create a script +that inserts the default roles and permissions +during the setup of the Auth App. + +### Setup Default Roles & Permissions + + + +