Skip to content

Commit

Permalink
create permissions schema and resources #27 / #31
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Jul 22, 2020
1 parent 0114fc8 commit 18be2a0
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 1 deletion.
96 changes: 96 additions & 0 deletions lib/auth/ctx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,100 @@ defmodule Auth.Ctx do
def change_role(%Role{} = role, attrs \\ %{}) do
Role.changeset(role, attrs)
end

alias Auth.Ctx.Permission

@doc """
Returns the list of permissions.
## Examples
iex> list_permissions()
[%Permission{}, ...]
"""
def list_permissions do
Repo.all(Permission)
end

@doc """
Gets a single permission.
Raises `Ecto.NoResultsError` if the Permission does not exist.
## Examples
iex> get_permission!(123)
%Permission{}
iex> get_permission!(456)
** (Ecto.NoResultsError)
"""
def get_permission!(id), do: Repo.get!(Permission, id)

@doc """
Creates a permission.
## Examples
iex> create_permission(%{field: value})
{:ok, %Permission{}}
iex> create_permission(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_permission(attrs \\ %{}) do
%Permission{}
|> Permission.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a permission.
## Examples
iex> update_permission(permission, %{field: new_value})
{:ok, %Permission{}}
iex> update_permission(permission, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_permission(%Permission{} = permission, attrs) do
permission
|> Permission.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a permission.
## Examples
iex> delete_permission(permission)
{:ok, %Permission{}}
iex> delete_permission(permission)
{:error, %Ecto.Changeset{}}
"""
def delete_permission(%Permission{} = permission) do
Repo.delete(permission)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking permission changes.
## Examples
iex> change_permission(permission)
%Ecto.Changeset{data: %Permission{}}
"""
def change_permission(%Permission{} = permission, attrs \\ %{}) do
Permission.changeset(permission, attrs)
end
end
19 changes: 19 additions & 0 deletions lib/auth/ctx/permission.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Auth.Ctx.Permission do
use Ecto.Schema
import Ecto.Changeset

schema "permissions" do
field :desc, :string
field :name, :string
field :person_id, :id

timestamps()
end

@doc false
def changeset(permission, attrs) do
permission
|> cast(attrs, [:name, :desc])
|> validate_required([:name, :desc])
end
end
62 changes: 62 additions & 0 deletions lib/auth_web/controllers/permission_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
defmodule AuthWeb.PermissionController do
use AuthWeb, :controller

alias Auth.Ctx
alias Auth.Ctx.Permission

def index(conn, _params) do
permissions = Ctx.list_permissions()
render(conn, "index.html", permissions: permissions)
end

def new(conn, _params) do
changeset = Ctx.change_permission(%Permission{})
render(conn, "new.html", changeset: changeset)
end

def create(conn, %{"permission" => permission_params}) do
case Ctx.create_permission(permission_params) do
{:ok, permission} ->
conn
|> put_flash(:info, "Permission created successfully.")
|> redirect(to: Routes.permission_path(conn, :show, permission))

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end

def show(conn, %{"id" => id}) do
permission = Ctx.get_permission!(id)
render(conn, "show.html", permission: permission)
end

def edit(conn, %{"id" => id}) do
permission = Ctx.get_permission!(id)
changeset = Ctx.change_permission(permission)
render(conn, "edit.html", permission: permission, changeset: changeset)
end

def update(conn, %{"id" => id, "permission" => permission_params}) do
permission = Ctx.get_permission!(id)

case Ctx.update_permission(permission, permission_params) do
{:ok, permission} ->
conn
|> put_flash(:info, "Permission updated successfully.")
|> redirect(to: Routes.permission_path(conn, :show, permission))

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", permission: permission, changeset: changeset)
end
end

def delete(conn, %{"id" => id}) do
permission = Ctx.get_permission!(id)
{:ok, _permission} = Ctx.delete_permission(permission)

conn
|> put_flash(:info, "Permission deleted successfully.")
|> redirect(to: Routes.permission_path(conn, :index))
end
end
2 changes: 1 addition & 1 deletion lib/auth_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule AuthWeb.Router do

get "/profile", AuthController, :admin
resources "/roles", RoleController

resources "/permissions", PermissionController
resources "/settings/apikeys", ApikeyController
end

Expand Down
5 changes: 5 additions & 0 deletions lib/auth_web/templates/permission/edit.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>Edit Permission</h1>

<%= render "form.html", Map.put(assigns, :action, Routes.permission_path(@conn, :update, @permission)) %>

<span><%= link "Back", to: Routes.permission_path(@conn, :index) %></span>
19 changes: 19 additions & 0 deletions lib/auth_web/templates/permission/form.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<%= form_for @changeset, @action, fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>

<%= label f, :name %>
<%= text_input f, :name %>
<%= error_tag f, :name %>

<%= label f, :desc %>
<%= text_input f, :desc %>
<%= error_tag f, :desc %>

<div>
<%= submit "Save" %>
</div>
<% end %>
28 changes: 28 additions & 0 deletions lib/auth_web/templates/permission/index.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1>Listing Permissions</h1>

<table>
<thead>
<tr>
<th>Name</th>
<th>Desc</th>

<th></th>
</tr>
</thead>
<tbody>
<%= for permission <- @permissions do %>
<tr>
<td><%= permission.name %></td>
<td><%= permission.desc %></td>

<td>
<span><%= link "Show", to: Routes.permission_path(@conn, :show, permission) %></span>
<span><%= link "Edit", to: Routes.permission_path(@conn, :edit, permission) %></span>
<span><%= link "Delete", to: Routes.permission_path(@conn, :delete, permission), method: :delete, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>

<span><%= link "New Permission", to: Routes.permission_path(@conn, :new) %></span>
5 changes: 5 additions & 0 deletions lib/auth_web/templates/permission/new.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New Permission</h1>

<%= render "form.html", Map.put(assigns, :action, Routes.permission_path(@conn, :create)) %>

<span><%= link "Back", to: Routes.permission_path(@conn, :index) %></span>
18 changes: 18 additions & 0 deletions lib/auth_web/templates/permission/show.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h1>Show Permission</h1>

<ul>

<li>
<strong>Name:</strong>
<%= @permission.name %>
</li>

<li>
<strong>Desc:</strong>
<%= @permission.desc %>
</li>

</ul>

<span><%= link "Edit", to: Routes.permission_path(@conn, :edit, @permission) %></span>
<span><%= link "Back", to: Routes.permission_path(@conn, :index) %></span>
3 changes: 3 additions & 0 deletions lib/auth_web/views/permission_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule AuthWeb.PermissionView do
use AuthWeb, :view
end
15 changes: 15 additions & 0 deletions priv/repo/migrations/20200722180019_create_permissions.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Auth.Repo.Migrations.CreatePermissions do
use Ecto.Migration

def change do
create table(:permissions) do
add :name, :string
add :desc, :string
add :person_id, references(:people, on_delete: :nothing)

timestamps()
end

create index(:permissions, [:person_id])
end
end
61 changes: 61 additions & 0 deletions test/auth/ctx_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,65 @@ defmodule Auth.CtxTest do
assert %Ecto.Changeset{} = Ctx.change_role(role)
end
end

describe "permissions" do
alias Auth.Ctx.Permission

@valid_attrs %{desc: "some desc", name: "some name"}
@update_attrs %{desc: "some updated desc", name: "some updated name"}
@invalid_attrs %{desc: nil, name: nil}

def permission_fixture(attrs \\ %{}) do
{:ok, permission} =
attrs
|> Enum.into(@valid_attrs)
|> Ctx.create_permission()

permission
end

test "list_permissions/0 returns all permissions" do
permission = permission_fixture()
assert Ctx.list_permissions() == [permission]
end

test "get_permission!/1 returns the permission with given id" do
permission = permission_fixture()
assert Ctx.get_permission!(permission.id) == permission
end

test "create_permission/1 with valid data creates a permission" do
assert {:ok, %Permission{} = permission} = Ctx.create_permission(@valid_attrs)
assert permission.desc == "some desc"
assert permission.name == "some name"
end

test "create_permission/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Ctx.create_permission(@invalid_attrs)
end

test "update_permission/2 with valid data updates the permission" do
permission = permission_fixture()
assert {:ok, %Permission{} = permission} = Ctx.update_permission(permission, @update_attrs)
assert permission.desc == "some updated desc"
assert permission.name == "some updated name"
end

test "update_permission/2 with invalid data returns error changeset" do
permission = permission_fixture()
assert {:error, %Ecto.Changeset{}} = Ctx.update_permission(permission, @invalid_attrs)
assert permission == Ctx.get_permission!(permission.id)
end

test "delete_permission/1 deletes the permission" do
permission = permission_fixture()
assert {:ok, %Permission{}} = Ctx.delete_permission(permission)
assert_raise Ecto.NoResultsError, fn -> Ctx.get_permission!(permission.id) end
end

test "change_permission/1 returns a permission changeset" do
permission = permission_fixture()
assert %Ecto.Changeset{} = Ctx.change_permission(permission)
end
end
end
Loading

0 comments on commit 18be2a0

Please sign in to comment.