Sha256: 648b746e8faa813340ad6c27ccd1f1cd343429306786239a30219235ce777f8e

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

# Authorization::AuthorizationHelper
require File.dirname(__FILE__) + '/authorization.rb'

module Authorization
  module AuthorizationHelper
  
    # If the current user meets the given privilege, permitted_to? returns true
    # and yields to the optional block.  The attribute checks that are defined
    # in the authorization rules are only evaluated if an object is given
    # for context.
    # 
    # Examples:
    #     <% permitted_to? :create, :users do %>
    #     <%= link_to 'New', new_user_path %>
    #     <% end %>
    #     ...
    #     <% if permitted_to? :create, :users %>
    #     <%= link_to 'New', new_user_path %>
    #     <% else %>
    #     You are not allowed to create new users!
    #     <% end %>
    #     ...
    #     <% for user in @users %>
    #     <%= link_to 'Edit', edit_user_path(user) if permitted_to? :update, user %>
    #     <% end %>
    # 
    def permitted_to? (privilege, object_or_sym = nil, &block)
      controller.permitted_to?(privilege, object_or_sym, &block)
    end
  
    # While permitted_to? is used for authorization in views, in some cases
    # content should only be shown to some users without being concerned
    # with authorization.  E.g. to only show the most relevant menu options 
    # to a certain group of users.  That is what has_role? should be used for.
    # 
    # Examples:
    #     <% has_role?(:sales) do %>
    #     <%= link_to 'All contacts', contacts_path %>
    #     <% end %>
    #     ...
    #     <% if has_role?(:sales) %>
    #     <%= link_to 'Customer contacts', contacts_path %>
    #     <% else %>
    #     ...
    #     <% end %>
    # 
    def has_role? (*roles, &block)
      controller.has_role?(*roles, &block)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stffn-declarative_authorization-0.2.3 lib/helper.rb
stffn-declarative_authorization-0.2.4 lib/declarative_authorization/helper.rb