Git Product home page Git Product logo

praetorian's Introduction

Praetorian

Build Status Version License

Praetorian is a minimalist Crystal authorization system inspired by Pundit. It aims to be both lightweight and dependency-less.

Installation

dependencies:
  praetorian:
    github: ilanusse/praetorian

How to use

Praetorian, inspired by Pundit, works with policy classes. This shard is not designed to be extra compatible with any framework but rather with flexibility in mind. This is a simple example that allows updating a post if the user is an admin, or if the post is unpublished:

class Post
  def policy_class
    PostPolicy
  end
end


class PostPolicy
  include Praetorian::Policy

  property user, post

  def initialize(user, post)
    @user = user
    @post = post
  end

  def update?
    user.admin? || !post.published?
  end
end


# Somewhere in your code
def update
  @post = Post.find(params[:id])
  Praetorian.authorize(current_user, @post, :update?) # You can also use .authorise if you're a Brit
  # Rest of code flow
end

There are two things to notice here:

  • The Post is a class that should obey a certain Policy. We can either write a policy_class method to return the policy class name, or Praetorian will assume the policy classname to be #{variable_name}Policy.

  • The Policy class includes Praetorian::Policy. This adds default query methods to our policy as defaults that should be overwritten as necessary.

The default query methods defined in Praetorian::Policy are: index?, show?, create?, new?, update?, edit?, destroy?.

A Praetorian::NotAuthorizedException will be raised if the user is not authorized to perform said query on the record.

Ok. So far, pretty simple.

You can set up a simple base class to inherit from:

class ApplicationPolicy
  include Praetorian::Policy

  property user, object

  def initialize(user, object)
    @user = user
    @object = object
  end
end

Including the shard as a module

You can include the shard as a module in your controller base class to avoid the prefix:

class ApplicationController
  include Praetorian
end

class PostController < ApplicationController
  @post = Post.find(params[:id])
  authorize(current_user, @post, :update?) # yay no prefix
end

Using a specific policy class

You can pass an argument to override the policy class if necessary. For example:

def create
  @publication = find_publication # assume this method returns any model that behaves like a publication
  # @publication.class => Post
  Praetorian.authorize(current_user, @publication, :create?, PublicationPolicy)
  # Rest of code flow
end

License

Licensed under the MIT license, see the separate LICENSE.txt file.

praetorian's People

Contributors

ilanusse avatar mamantoha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

praetorian's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.