Class: Permission

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/permission.rb

Overview

A permission defines access to a single part of an application, restricted by both controller and action specifications.

Permissions can be application-, controller-, or action-specific. Those using the application controller are global. Those without any action specified are controller-specific. Those with both controller and action specified are action-specific.

Examples

Permission.bootstrap(
  {:id => 1, :controller => 'application'},               # Access to the whole application
  {:id => 2, :controller => 'users'},                     # Access to the Users controller (any action)
  {:id => 3, :controller => 'users', :action => 'index'}  # Access to the Users controller and index action
)

Class Method Summary (collapse)

Class Method Details

+ (Object) recognize_path(options = '')

Parses the controller path and action from the given options. Options may be in any one of the following formats:

  • string - A relative or absolute path in the application

  • hash - A hash include the controller/action attributes



43
44
45
46
47
48
49
# File 'app/models/permission.rb', line 43

def recognize_path(options = '')
  # Grab the actual url options if the path is specified
  options = ActionController::Routing::Routes.recognize_path(URI.parse(options).path) if options.is_a?(String)
  
  # Only return the controller/action of the url options
  return options[:controller], options[:action] ? options[:action].to_s : 'index'
end

+ (Boolean) restricts?(options = '')

Is there a permission that exists which restricts the given url? If there is no permission that restricts the path, then anyone should be allowed access to it

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'app/models/permission.rb', line 31

def restricts?(options = '')
  controller, action = recognize_path(options)
  
  # See if a permission exists for either the controller or controller/action
  # combination.  If it doesn't, then the path isn't restricted
  exists?(:path => ["#{controller}/", "#{controller}/#{action}"])
end