Module: Permitter::Permission

Included in:
Permission
Defined in:
lib/permitter/permission.rb

Instance Method Summary collapse

Instance Method Details

#allow_action(controllers, actions, &block) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/permitter/permission.rb', line 31

def allow_action(controllers, actions, &block)
  @allowed_actions ||= {}
  Array(controllers).flatten.each do |controller|
    Array(actions).flatten.each do |action|
      @allowed_actions[[controller.to_s, action.to_s]] = block || true
    end
  end
end

#allow_allObject



51
52
53
# File 'lib/permitter/permission.rb', line 51

def allow_all
  @allow_all = true
end

#allow_all?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/permitter/permission.rb', line 55

def allow_all?
  @allow_all ||= false
end

#allow_param(resources, attributes) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/permitter/permission.rb', line 40

def allow_param(resources, attributes)
  @allowed_params ||= {}
  resource_array = Array(resources).flatten
  attribute_array = Array(attributes).flatten

  resource_array.each do |resource|
    @allowed_params[resource] ||= []
    @allowed_params[resource] += attribute_array
  end
end

#allowed_action(controller, action) ⇒ Object



25
26
27
28
29
# File 'lib/permitter/permission.rb', line 25

def allowed_action(controller, action)
  if @allowed_actions
    @allowed_actions[[controller.to_s, action.to_s]]
  end
end

#allowed_action?(controller, action, resource = nil) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
12
13
# File 'lib/permitter/permission.rb', line 4

def allowed_action?(controller, action, resource = nil)
  if allow_all?
    true
  elsif @allowed_actions
    allowed = @allowed_actions[[controller.to_s, action.to_s]]
    (allowed && (allowed == true || resource && allowed.call(resource))) == true
  else
    false
  end
end

#allowed_param?(resource, attribute) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
# File 'lib/permitter/permission.rb', line 15

def allowed_param?(resource, attribute)
  if allow_all?
    true
  elsif @allowed_params && @allowed_params[resource]
    @allowed_params[resource].include? attribute
  else
    false
  end
end

#permit_params!(params) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/permitter/permission.rb', line 59

def permit_params!(params)
  if @allow_all
    params.permit!
  elsif @allowed_params
    @allowed_params.each do |resource, attributes|
      if params[resource].respond_to? :permit
        params[resource] = params[resource].permit(*attributes)
      end
    end
  end
end