Class: Permit::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/permit.rb

Overview

Contains the configuration rules that Permit will apply during its processing.

role_class, authorization_class, and person_class are the model classes defined as representing their respective names by defining the corresponding permit_* method. authorizable_classes is an array of all classes that are authorizable to roles by having defined permit_authorizable.

Class Attribute Summary (collapse)

Class Method Summary (collapse)

Class Attribute Details

+ (Object) action_aliases (readonly)

Actions that when given to PermitRules#allow, and PermitRules#deny will be expanded into the actions given in the value array.

Defaults to:

{
  :create => [:new, :create], 
  :update => [:edit, :update], 
  :destroy => [:delete, :destroy],
  :read => [:index, :show], 
  :write => [:new, :create, :edit, :update]
}


65
66
67
# File 'lib/permit.rb', line 65

def action_aliases
  @action_aliases
end

+ (Object) authorizable_classes (readonly)

Classes that are marked as authorizable resources using permit_authorizable.



78
79
80
# File 'lib/permit.rb', line 78

def authorizable_classes
  @authorizable_classes
end

+ (Object) authorization_class (readonly)

The class that currently represents authorizations in the system, as set by set_core_models.



69
70
71
# File 'lib/permit.rb', line 69

def authorization_class
  @authorization_class
end

+ (Object) person_class (readonly)

The class that currently represents authorization subjects in the system, as set by set_core_models.



72
73
74
# File 'lib/permit.rb', line 72

def person_class
  @person_class
end

+ (Object) role_class (readonly)

The class that curretly represents roles in the system, as set by set_core_models.



75
76
77
# File 'lib/permit.rb', line 75

def role_class
  @role_class
end

Class Method Details

+ (Symbol?) controller_subject_method

The method to use to retrieve the current authorization subject when rules are being evaluated. If nil, then the method will be inferred from the subject set in the call to set_core_models.

Returns:

  • (Symbol, nil)


99
# File 'lib/permit.rb', line 99

def controller_subject_method; @controller_subject_method; end

+ (Object) controller_subject_method=(method)

Sets the name of the method to use to retrieve the current subject while checking authorizations. Set to nil, to infer the value from the subject set in set_core_models, or :current_person if named authorizations are not being used.

Parameters:

  • method (nil, Symbol)

    a symbol representing the method to use.



107
# File 'lib/permit.rb', line 107

def controller_subject_method=(method); @controller_subject_method = method; end

+ (Object) default_access

Indicates the response that PermitRules will take if no authorizations match. If set to :allow then a subject will be given access unless denied. By default this is set to :deny

Returns:

  • the current default access.



87
# File 'lib/permit.rb', line 87

def default_access; @default_access; end

+ (Object) default_access=(access)

Sets the response that PermitRules will use when no rules match.

Parameters:

  • access (:allow, :deny)

    the default response to use.



92
# File 'lib/permit.rb', line 92

def default_access=(access); @default_access = access; end

+ (Object) reset_core_models

Forces Permit to reload its core classes based off of those given in the initial call to Permit::Config.set_core_models. This is primarily needed so that Permit will work in Rails development mode because of class caching/reloading. These variables hang onto the original models as they were defined and end up in a weird state. Production does not experience this problem.



137
138
139
140
141
142
# File 'lib/permit.rb', line 137

def reset_core_models
  authz = Object.const_get authorization_class.name
  person = Object.const_get person_class.name
  role = Object.const_get role_class.name
  Permit::Config.set_core_models(authz, person, role)
end

+ (Object) set_core_models(authorization, person, role)

Sets the core authorization, person, and role models to be used for named authorizations, and configures them with their respective permit_* methods.

Parameters:

  • authorization (Class)

    an ActiveRecord model representing authorizations.

  • person (Class)

    an ActiveRecord model representing people.

  • role (Class)

    an ActiveRecord model representing roles.



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/permit.rb', line 119

def set_core_models(authorization, person, role)
  #raise PermitConfigurationError, "Core models cannot be redefined." if @@models_defined

  @authorization_class = authorization
  @person_class = person
  @role_class = role

  @authorization_class.send :permit_authorization
  @person_class.send :permit_person
  @role_class.send :permit_role
end