Skip to content

Commit

Permalink
mix ecto.gen.migration create_people_roles for rbac #27 / #31
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Jul 23, 2020
1 parent 2b7a66d commit b6e4a5f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ defmodule Auth.Repo.Migrations.CreateRolePermissions do

def change do
create table(:role_permissions) do
add :role_id, references(:roles)
add :permission_id, references(:permissions)
add :role_id, references(:roles, on_delete: :nothing)
add :permission_id, references(:permissions, on_delete: :nothing)

timestamps()
end

create unique_index(:role_permissionss, [:role_id, :permission_id])
create unique_index(:role_permissions, [:role_id, :permission_id])
end
end
14 changes: 14 additions & 0 deletions priv/repo/migrations/20200723154847_create_people_roles.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Auth.Repo.Migrations.CreatePeopleRoles do
use Ecto.Migration

def change do
create table(:people_roles) do
add :person_id, references(:people, on_delete: :nothing)
add :role_id, references(:roles, on_delete: :nothing)

timestamps()
end

create unique_index(:people_roles, [:person_id, :role_id])
end
end
46 changes: 35 additions & 11 deletions role-based-access-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ Access Control List (ACL) based permissions systems
and helps everyone building and maintaining the app
to focus on security.

## _Who_?

This document is relevant to anyone
that is interested in developing and _maintaining_
secure multi-person applications
should learn about RBAC.


## What?

Expand Down Expand Up @@ -76,13 +83,6 @@ that still allows the person to login and view their _own_ content,
but they have no other privileges.


## Who?

Anyone who is interested in developing and _maintaining_
secure multi-person applications
should learn about RBAC.


## _How_?

_Before_ creating any roles,
Expand Down Expand Up @@ -134,18 +134,18 @@ defmodule Auth.Repo.Migrations.CreateRolePermissions do

def change do
create table(:role_permissions) do
add :role_id, references(:roles)
add :permission_id, references(:permissions)
add :role_id, references(:roles, on_delete: :nothing)
add :permission_id, references(:permissions, on_delete: :nothing)

timestamps()
end

create unique_index(:role_permissionss, [:role_id, :permission_id])
create unique_index(:role_permissions, [:role_id, :permission_id])
end
end
```


### Create People<->Roles Associations

Now create the **`many-to-many`** relationship
between **`people`** and **`roles`**:
Expand All @@ -154,6 +154,30 @@ between **`people`** and **`roles`**:
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`]()


Replace the contents of the file with the following code:

```elixir
defmodule Auth.Repo.Migrations.CreatePeopleRoles do
use Ecto.Migration

def change do
create table(:people_roles) do
add :person_id, references(:people, on_delete: :nothing)
add :role_id, references(:roles, on_delete: :nothing)

timestamps()
end

create unique_index(:people_roles, [:person_id, :role_id])
end
end
```




## Recommended Reading
Expand Down

0 comments on commit b6e4a5f

Please sign in to comment.