All Projects → bmuller → Pundit Elixir

bmuller / Pundit Elixir

Licence: mit
Simple authorization helpers for Elixir stucts, like Ruby's Pundit

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to Pundit Elixir

Rbac.dev
A collection of good practices and tools for Kubernetes RBAC
Stars: ✭ 115 (+576.47%)
Mutual labels:  permissions, access-control
Ngx Permissions
Permission and roles based access control for your angular(angular 2,4,5,6,7,9+) applications(AOT, lazy modules compatible
Stars: ✭ 749 (+4305.88%)
Mutual labels:  permissions, access-control
Accesscontrol
Role and Attribute based Access Control for Node.js
Stars: ✭ 1,723 (+10035.29%)
Mutual labels:  permissions, access-control
Nest Access Control
Role and Attribute based Access Control for Nestjs 🔐
Stars: ✭ 562 (+3205.88%)
Mutual labels:  permissions, access-control
rbac-tool
Rapid7 | insightCloudSec | Kubernetes RBAC Power Toys - Visualize, Analyze, Generate & Query
Stars: ✭ 546 (+3111.76%)
Mutual labels:  permissions, access-control
Unix Permissions
Swiss Army knife for Unix permissions
Stars: ✭ 106 (+523.53%)
Mutual labels:  permissions, access-control
Think Authz
An authorization library that supports access control models like ACL, RBAC, ABAC in ThinkPHP 6.0 .
Stars: ✭ 155 (+811.76%)
Mutual labels:  permissions, access-control
Vakt
Attribute-based access control (ABAC) SDK for Python
Stars: ✭ 92 (+441.18%)
Mutual labels:  permissions, access-control
nova-permissions
Add Permissions based authorization for your Nova installation via User-based Roles and Permissions. Roles are defined in the database whereas Permissions are defined in the code base.
Stars: ✭ 115 (+576.47%)
Mutual labels:  permissions, access-control
ngx-access
Add access control to your components using hierarchical configuration with logical expressions.
Stars: ✭ 21 (+23.53%)
Mutual labels:  permissions, access-control
Authr
🔑 a flexible and expressive approach to access-control
Stars: ✭ 33 (+94.12%)
Mutual labels:  permissions, access-control
react-abac
Attribute Based Access Control for React
Stars: ✭ 54 (+217.65%)
Mutual labels:  permissions, access-control
Laravel Authz
An authorization library that supports access control models like ACL, RBAC, ABAC in Laravel.
Stars: ✭ 136 (+700%)
Mutual labels:  permissions, access-control
Drf Access Policy
Declarative access policies/permissions modeled after AWS' IAM policies.
Stars: ✭ 200 (+1076.47%)
Mutual labels:  permissions, access-control
Casbin4D
An authorization library that supports access control models like ACL, RBAC, ABAC in Delphi
Stars: ✭ 25 (+47.06%)
Mutual labels:  permissions, access-control
Access Granted
Multi-role and whitelist based authorization gem for Rails (and not only Rails!)
Stars: ✭ 733 (+4211.76%)
Mutual labels:  permissions, access-control
Sppermissions
Ask permissions with ready-use interface. You can check status permission and if it has been requested before. Support SwiftUI.
Stars: ✭ 4,701 (+27552.94%)
Mutual labels:  permissions
Graphql Engine
Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
Stars: ✭ 24,845 (+146047.06%)
Mutual labels:  access-control
Policyserver.local
Sample OSS version of PolicyServer
Stars: ✭ 444 (+2511.76%)
Mutual labels:  permissions
Sig Security
🔐CNCF Special Interest Group on Security -- secure access, policy control, privacy, auditing, explainability and more!
Stars: ✭ 662 (+3794.12%)
Mutual labels:  access-control

Pundit

Build Status Hex pm API Docs

Pundit provides a set of helpers which guide you in leveraging regular Elixir methods to build a simple authorization system. This library is based heavily on Jonas Nicklas' Ruby project of the same name.

Simple Elixir functions are defined for a given struct and allow you to encapsulate authentication logic. You can use this code within a module that is an Ecto.Schema, but that's not necessary (Ecto isn't required). The action names are taken from the list of actions defined by Phoenix controllers.

Installation

To install Pundit, just add an entry to your mix.exs:

def deps do
  [
    # ...
    {:pundit, "~> 1.0"}
  ]
end

(Check Hex to make sure you're using an up-to-date version number.)

Usage

Here's a basic example, starting with a simple struct for a Post. A module named Post.Policy should be created to encapsulate all of the access methods (Pundit will automatically look for the <struct module>.Policy module to determine the module name to look at for access methods).

To declare an initial set of access functions (show?, edit?, delete?, etc) which all return false (default safe!), just use Pundit.DefaultPolicy. You can then override the functions as needed with the logic necessary to determine whether a user should be able to perform the given action. In this example, we only determine whether a user can edit? a post, leaving all other functions (like delete?) to return the default of false.

defmodule Post do
  defstruct [:author, :title, :body, :comments]

  defmodule Policy do
    # This will initialize all the action functions, all of which return false
    # by default. Override them individually to return true when they should,
    # like edit? is overriden below.
    use Pundit.DefaultPolicy

    def edit?(post, user) do
      user.name == post.author
    end
  end
end

post = %Post{author: "Snake Plissken"}
author = %{name: "Snake Plissken"}
# next line is same as Pundit.can?(post, author, :edit?)
# Pundit will just delegate to Post.Policy.edit?(post, user)
if Pundit.edit?(post, author) do
  IO.puts("Can edit!")
end

if Pundit.delete?(post, author) do
  IO.puts("This line should never be called")
end

# raise exception if user should be able to do a thing
Pundit.authorize!(post, author, :edit?)

Scope

You can also provide query scope for a struct (say, if you're using Ecto.Schema) for a given user. For instance, say our Post was an Ecto schema. Our function for scoping all Posts to a specific User could be to find all Posts that were authored by a user. For instance:

defmodule Post do
  use Ecto.Schema
  import Ecto.Query, only: [from: 2]

  defmodule Policy do
    use Pundit.DefaultPolicy
          
    def scope(query, user) do
      from post in query,
        where: post.author_id == ^user.id
    end
  end
end

user = MyApp.Repo.get(User, 1)
posts = Pundit.scope(Post, user) |> Repo.all()

query = from p in Post, where: p.comment_count > 10
popular_posts = Pundit.scope(query, user) |> Repo.all()

See the docs for more examples.

Running Tests

To run tests:

$ mix test

Reporting Issues

Please report all issues on github.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].