Class: Rolypoly::RoleGatekeeper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(roles, actions, resource = nil) ⇒ RoleGatekeeper

Returns a new instance of RoleGatekeeper.



5
6
7
8
9
10
11
# File 'lib/rolypoly/role_gatekeeper.rb', line 5

def initialize(roles, actions, resource = nil)
  self.roles = Set.new Array(roles).map(&:to_s)
  self.actions = Set.new Array(actions).map(&:to_s)
  self.resource = resource
  self.all_actions = false
  self.public = false
end

Instance Attribute Details

#rolesObject

Returns the value of attribute roles.



4
5
6
# File 'lib/rolypoly/role_gatekeeper.rb', line 4

def roles
  @roles
end

Instance Method Details

#action?(check_actions) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
# File 'lib/rolypoly/role_gatekeeper.rb', line 103

def action?(check_actions)
  check_actions = Set.new Array(check_actions).map(&:to_s)
  all_actions? || !(check_actions & actions).empty?
end

#all_publicObject



67
68
69
70
# File 'lib/rolypoly/role_gatekeeper.rb', line 67

def all_public
  self.public = true
  self.all_actions = true
end

#allow(*roles) ⇒ Object

on(resource).allow(*roles).to_access(*actions)



19
20
21
22
# File 'lib/rolypoly/role_gatekeeper.rb', line 19

def allow(*roles)
  to(*roles)
  self
end

#allow?(current_roles, action, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rolypoly/role_gatekeeper.rb', line 58

def allow?(current_roles, action, options = {})
  action?(action) && role?(current_roles, options)
end

#allowed_roles(current_roles, action, options = {}) ⇒ Object



62
63
64
65
# File 'lib/rolypoly/role_gatekeeper.rb', line 62

def allowed_roles(current_roles, action, options = {})
  return [] if public? || !action?(action)
  match_roles(current_roles, options)
end

#initialize_copy(other) ⇒ Object



13
14
15
16
# File 'lib/rolypoly/role_gatekeeper.rb', line 13

def initialize_copy(other)
  @roles = @roles.dup
  @actions = @actions.dup
end

#on(resource) ⇒ Object

allow(*roles).on(resource).to_access(*actions)



31
32
33
34
# File 'lib/rolypoly/role_gatekeeper.rb', line 31

def on(resource)
  self.resource = resource
  self
end

#public?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/rolypoly/role_gatekeeper.rb', line 108

def public?
  !!public
end

#restrict(*actions) ⇒ Object

on(resource).restrict(*actions).to(*roles)



25
26
27
28
# File 'lib/rolypoly/role_gatekeeper.rb', line 25

def restrict(*actions)
  to_access(*actions)
  self
end

#role?(check_roles, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
# File 'lib/rolypoly/role_gatekeeper.rb', line 72

def role?(check_roles, options = {})
  return true if public?
  required_resource = find_required_resource(options)

  Array(check_roles).any? do |check_role|
    allowed_role?(check_role) && allowed_resource?(check_role, required_resource)
  end
end

#to(*roles) ⇒ Object

restrict(*actions).to *roles



37
38
39
# File 'lib/rolypoly/role_gatekeeper.rb', line 37

def to(*roles)
  self.roles = self.roles.merge roles.flatten.compact.map(&:to_s)
end

#to_access(*actions) ⇒ Object

allow(*roles).to_access *actions



48
49
50
# File 'lib/rolypoly/role_gatekeeper.rb', line 48

def to_access(*actions)
  self.actions = self.actions.merge actions.flatten.compact.map(&:to_s)
end

#to_allObject

allow role access to all actions allow(*roles).to_all



54
55
56
# File 'lib/rolypoly/role_gatekeeper.rb', line 54

def to_all
  self.all_actions = true
end

#to_noneObject

make actions public basically restrict(:index).to_none



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

def to_none
  self.public = true
end