Module: StoneWall

Defined in:
lib/stonewall/parser.rb,
lib/stonewall/helpers.rb,
lib/stonewall/stonewall.rb,
lib/stonewall/user_extensions.rb,
lib/stonewall/access_controller.rb

Defined Under Namespace

Modules: Helpers, UserExtensions Classes: AccessController, Parser

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/stonewall/stonewall.rb', line 2

def self.included(base)
  base.instance_eval do
    def stonewall()
      require File.expand_path(File.dirname(__FILE__)) +
                "/access_controller.rb"
      require File.expand_path(File.dirname(__FILE__)) +
                "/parser.rb"
      require File.expand_path(File.dirname(__FILE__)) +
                "/helpers.rb"
      require File.expand_path(File.dirname(__FILE__)) +
                "/user_extensions.rb"
      cattr_accessor :stonewall
      self.stonewall = StoneWall::AccessController.new(self)
      parser = StoneWall::Parser.new(self)
      yield parser
      
      # if we are being used with acts_as_state_machine (at least, our patched
      # version), then we also want the on_transition guards to function as
      # action guards in stonewall.
      if self.respond_to?(:aasm_events)
        parser.guard_aasm_events
      end
    end

    # --------------
    # This is 1/3rd of the magic in this gem.  After ActiceRecord defines
    # the classes attribute methods, we come along and alias all of the
    # guarded methods defined in the dsl in the class.
    def self.define_attribute_methods_with_stonewall
      define_attribute_methods_without_stonewall
      StoneWall::Helpers.fix_aliases_for(self) # if a stonewall enhanced class?
    end
    
    unless respond_to?(:define_attribute_methods_without_stonewall)
      class << self
        alias_method_chain :define_attribute_methods, :stonewall
      end
    end
  end

  # mimicking the send method, we want to ask permission first with send?
  def send?(method, user = User.current)
    self.class.stonewall.allowed?(self, user, method)
  end
  alias_method :allowed?, :send?
  
  def send_method_group?(group_name, user = User.current)
    self.class.stonewall.method_groups[group_name].each do |method|
      return false unless self.send?(method, user)
    end
    return true
  end
  alias_method :allowed_method_group?, :send_method_group?
  
end

Instance Method Details

#send?(method, user = User.current) ⇒ Boolean

mimicking the send method, we want to ask permission first with send?



43
44
45
# File 'lib/stonewall/stonewall.rb', line 43

def send?(method, user = User.current)
  self.class.stonewall.allowed?(self, user, method)
end

#send_method_group?(group_name, user = User.current) ⇒ Boolean



48
49
50
51
52
53
# File 'lib/stonewall/stonewall.rb', line 48

def send_method_group?(group_name, user = User.current)
  self.class.stonewall.method_groups[group_name].each do |method|
    return false unless self.send?(method, user)
  end
  return true
end