Project

conduct

0.0
No commit activity in last 3 years
No release in over 3 years
Simple authorization
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

Middle - simple authorization

image

Build Status

Installation

Gemfile

gem "middle"

Command line interface

$ gem install middle

Note This is work in progress. README will be updated/improved when this is completed.

Examples:

Define rules in Ability class

class Ability

  include Middle

  define_action create: [:new, :create]
  define_action read:   [:index, :show]
  define_action update: [:edit, :update]
  define_action delete: [:destroy]

  define_action manage: [:create, :read, :update, :delete]

  can :manage, :all do
    current_user.present? && current_user.admin?
  end

  can :manage, Post do |post|
    post.member?(current_user)
  end

  can :comment, Post, :create_comment?

  def create_comment?(post)
    can? :create, post.comments.new
  end

end

Rails

Check for permission if current_user is authorized to view user's all posts.

class PostsController < ApplicationController

  def index
    if can?(:read, @user.posts)
      @posts = @user.posts
    else
      @posts = @user.posts.public
    end
  end

  ...

end

Check for permission in views

<% if can?(:create, @post) %>
  <%= link_to "New Post", new_post_path %>
<% end %>

Contributing

If you wish to contribute to this project, please:

  • Fork this project on GitHub
  • If it involves code, please also write tests for it
  • Use named branch
  • Send a pull request

License

Middle is licensed under the MIT license.