MayI

A plugable access rights API. Meant to make integrations easier. Verry useful as an integration point for blog,forum and CMS components. Also its much nicer to read than the basic stuff i usually do.

An example basic way to do rights handeling

if user_object.is_admin?
...
end

With mayi this changes to method with an explicit mening to it.

access.may_add_user! do
...
end

An example

Your rights implements with boolean questions.

class MyAccessHandler

  include MayI
  
  def initialize(user)
    @user = user
  end
  
  def may_view_secret_project(project)
    stuff.owner_id ==  @user.id
  end
  
  def may_create_new_record
    @user.type == "admin"
  end
end

This can then be used with the MayI::Access class.

access = MyAccessHandler.new(user)

# Simple boolean
if access.may_create_new_record?
  # You do stuff here
end

# With a block
access.may_create_new_record? do
  # You do stuff here
end

# Raise errors on false
access.may_view_secret_project!(project)
# or
access.error_message("A custom error message").may_view_secret_project!(project)

A Rails example

class ApplicationController < ActionController::Base

  helper_method :current_user
  def current_user 
    ...
  end
  
  helper_method :access
  def access
    @@access_cache ||= MyAccessHandler.new(current_user)
  end
  
end

Check if a user should be able to view some secret stuff.

class StuffController < ApplicationController

  def show
    stuff = Stuff.find(params[:id])
    
    access.may_view_secret_stuff?(stuff) do
    
    end
  end
  
end

Contributing to MayI

Copyright © 2012 Darwin. See LICENSE.txt for further details.