Class: AuthorityCheck

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

Overview

AuthorityCheck

The different user authorities are defined in a separate class so as to reduce clutter in the User model itself.

class User < ActiveRecord::Base
  wristband :has_authorities => true
end

This will refer to the class UserAuthorityCheck for all authority tests, but the name of this module can be defined as required:

class User < ActiveRecord::Base
  has_authorities => :permissions
end

That would reference the class UserPermissions instead for all tests.

A sample authority checking class is defined as:

class UserAuthorityCheck < AuthorityCheck
  def wear_shoes?
    unless (@user.name.match(/^a/i))
      fail!("Only people with names that start with 'A' can wear shoes.")
    end
  end
end

Note the syntax: All authority checks are defined as ending with a trailing question mark character.

A check is considered to have passed if

  • a call to allow! has been made, or

  • no calls to fail! have been made.

Once defined, the user authorities are checked via a call to a User instance:

user.has_authority_to?(:wear_shoes)

While the has_authority_to? method returns only true or false, a call to has_objections_to? will return nil on success or any error messages if there is a failure.

Passing parameters to the authority methods

Any call to these tests may include options in the form of a Hash:

user.has_authority_to?(:send_message, :text => "Foo bar")

These options can be acted upon within the authority check:

def send_message?
  if (options[:text].match(/foo/i))
    fail!("Messages may not contain forbidden words.")
  end
end

Before chains

In addition to defining straight tests, a chain can be defined to run before any of the tests themselves. This allows certain calls to be over-ruled. For example:

before_check :allow_if_admin!

def allow_if_admin!
  if (@user.is_admin?)
    allow!
  end
end

In this case, the allow_if_admin! method will be called before any checks are performed. If the allow! method is executed, all subsequent tests are halted and the check is considered to have passed.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, test_method, options = { }) ⇒ AuthorityCheck

Returns a new instance of AuthorityCheck.



80
81
82
83
84
85
86
87
# File 'lib/wristband/authority_check.rb', line 80

def initialize(user, test_method, options = { })
  self.user = user
  self.options = options
  
  @test_method = "#{test_method}?".to_sym

  @result = nil
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#userObject

Returns the value of attribute user.



77
78
79
# File 'lib/wristband/authority_check.rb', line 77

def user
  @user
end

Class Method Details

.before_check(method, options = { }) ⇒ Object



115
116
117
# File 'lib/wristband/authority_check.rb', line 115

def before_check(method, options = { })
  self.check_chain += remap_chain_methods([ method ])
end

.check_alias_as(original, *aliases) ⇒ Object



119
120
121
122
123
# File 'lib/wristband/authority_check.rb', line 119

def check_alias_as(original, *aliases)
  aliases.flatten.each do |alias_name|
    alias_method alias_name, original
  end
end

.check_chainObject



107
108
109
# File 'lib/wristband/authority_check.rb', line 107

def check_chain
  @check_chain ||= [ ]
end

.check_chain=(value) ⇒ Object



111
112
113
# File 'lib/wristband/authority_check.rb', line 111

def check_chain=(value)
  @check_chain = value
end

Instance Method Details

#allowed_to?Boolean

Checkes if the user is allowed to do something. Returns true or false

Returns:

  • (Boolean)


91
92
93
94
95
96
97
# File 'lib/wristband/authority_check.rb', line 91

def allowed_to?
  execute_tests!

  # Either explicitly allowed (true) or not given any reasons as to why
  # not (nil, empty)
  (@result === true or (@result === nil and @reasons.blank?)) ? true : false
end

#denied_for_reasonsObject



99
100
101
102
103
# File 'lib/wristband/authority_check.rb', line 99

def denied_for_reasons
  @reasons = [ ]
  
  allowed_to? ? nil : @reasons
end