ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - jarednorman/canada: Easy permission definitions in Elixir apps!
Easy permission definitions in Elixir apps! Contribute to jarednorman/canada development by creating an account on GitHub.
Visit Site

GitHub - jarednorman/canada: Easy permission definitions in Elixir apps!

GitHub - jarednorman/canada: Easy permission definitions in Elixir apps!

Canada: Define you some permissions

Module Version Hex Docs Total Download License Last Updated

NOTE: If you're concerned by the fact that this repository has very little activity, don't be. The functionality this package provides is very simple, it has no dependencies, and the Elixir language hasn't changed in any way that would break it. It still works just as well as when I first wrote it. :smiley:

Canada provides a friendly interface for making easy use of Elixir's excellent pattern matching to create readable declarative permission rules.

If you're looking for something that fills more of what CanCan would provide you in a Rails application you should have a look at Canary which adds Ecto/Plug support.

Installation

Add it to your deps list in your mix.exs if you want the latest release?

defp deps do
  [
    {:canada, "~> 2.0"}
  ]
end

Or you want the latest greatest master?

defp deps do
  [
    {:canada, github: "jarednorman/canada"}
  ]
end

Becoming Canadian

Becoming Canadian is easy. Presumably you have some kind of resource like a user, and probably some kind of resource that belongs to users. Let's call that hypothetical resource a "post". Let's say they're structs.

defmodule User do
  defstruct id: nil, name: nil, admin: false
end

defmodule Post do
  defstruct user_id: nil, content: nil
end

To make use of Canada, you need to implement the Canada.Can protocol (defining whatever rules you need) for the "subject" resource (your User struct in this case).

defimpl Canada.Can, for: User do
  def can?(%User{id: user_id}, action, %Post{user_id: user_id})
    when action in [:update, :read, :destroy, :touch], do: true

  def can?(%User{admin: admin}, action, _)
    when action in [:update, :read, :destroy, :touch], do: admin

  def can?(%User{}, :create, Post), do: true
end

With this in place, you're good to start testing permissions wherever you need to, just remember to import the can? macro.

import Canada, only: [can?: 2]

if some_user |> can? read(some_post) do
  # render the post
else
  # sorry (raise a 403)
end

A note from the author

This is very much what happened when I said to myself, "I want the thing I had in Ruby, but in Elixir." I would be entirely unsurprised if myself or someone else comes up with a more "functional" solution. That said, permissions are necessarily a matter that governs conditional logic, so I currently see this as a reasonable solution.

Copyright and License

Copyright (c) 2014 Jared Norman

This software is licensed under the MIT license.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory