Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/craig-day/simple_can
A simple library to facilitate authorization.
https://github.com/craig-day/simple_can
Last synced: 20 days ago
JSON representation
A simple library to facilitate authorization.
- Host: GitHub
- URL: https://github.com/craig-day/simple_can
- Owner: craig-day
- License: apache-2.0
- Created: 2017-12-10T07:58:15.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-12-10T09:26:59.000Z (about 7 years ago)
- Last Synced: 2024-10-19T04:10:57.357Z (3 months ago)
- Language: Elixir
- Size: 14.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SimpleCan
[![Build Status](https://travis-ci.org/craig-day/simple_can.svg?branch=master)](https://travis-ci.org/craig-day/simple_can)
A simple library to facilitate authorization in your app. This was inspired by [Canada](https://github.com/jarednorman/canada) but with a more straightforward API.
## Installation
The package can be installed by adding `simple_can` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:simple_can, "~> 1.0.0"}
]
end
```The docs can be found at [https://hexdocs.pm/simple_can](https://hexdocs.pm/simple_can).
## Usage
1. Define an implementation of the `SimpleCan.Can` protocol for a specific module
```elixir
defimpl SimpleCan.Can, for: MyApp.User do
def can?(%MyApp.User{role: role}, :create, %MyApp.OtherResource{}) do
case role do
:admin -> true
:user -> false
_ -> false
end
enddef can?(%MyApp.User{}, action, %MyApp.OtherResource{}) when action in [:read, :view] do
true
end
end
```1. Import `SimpleCan` and use it
_note:_ (you can optionally specify `only: [can?: 3]` but that is the only function defined anyway)
```elixir
import SimpleCandef create_object(object) do
if can?(current_user, :create, object) do
{:ok, create(object)}
else
{:error, :unauthorized}
end
end
```