Skip to content

Commit

Permalink
create script and default_roles.json (version controllable data file)…
Browse files Browse the repository at this point in the history
… for #86
  • Loading branch information
nelsonic committed Jul 25, 2020
1 parent ef4261d commit 723ae83
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/auth/role.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions priv/repo/create_default_roles.exs

This file was deleted.

12 changes: 12 additions & 0 deletions priv/repo/default_roles.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
32 changes: 28 additions & 4 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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()
26 changes: 23 additions & 3 deletions role-based-access-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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







Expand Down

0 comments on commit 723ae83

Please sign in to comment.