Module: PrivatePerson::ActsAsPermitted::InstanceMethods

Defined in:
lib/private_person/extensions/acts_as_permitted.rb

Instance Method Summary collapse

Instance Method Details

#is_permitted?(permissor, permissible) ⇒ Boolean

Returns:

  • (Boolean)

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/private_person/extensions/acts_as_permitted.rb', line 13

def is_permitted?(permissor, permissible)
  if permissible.nil?
    raise 'Called is_permitted? on nil. Does not compute. Preparing to self destruct.'
  end
  unless PrivatePerson::Permission.by_permissible(permissible).blocked.empty?
    return false
  end
  wildcards = permissions_by(permissor).by_wildcard(permissible.class.name).legitimate
  if wildcards.present?
    return true
  end
  permissions = permissions_by(permissor).by_permissible(permissible).legitimate
  if permissions.present?
    return true
  end
  return false
end

#permissions_by(permissor) ⇒ Object


31
32
33
# File 'lib/private_person/extensions/acts_as_permitted.rb', line 31

def permissions_by(permissor)
  PrivatePerson::Permission.by_permissor(permissor).by_relationship_type(relationship_to(permissor))
end

#relationship_to(permissor) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/private_person/extensions/acts_as_permitted.rb', line 35

def relationship_to(permissor)
  # First make sure we're not a new user
  if self.new_record?
    return 'public'
  end
  # Next check for an efficient method
  for relationship_method in permissor.class.of
    is_method = ('is_' + relationship_method.to_s.singularize + '_of?').to_sym
    if respond_to?(is_method) and self.send(is_method, permissor)
      return relationship_method.to_s
    end
  end
  # Then check for a slow method
  for relationship_method in permissor.class.of
    relationship_members = permissor.send(relationship_method.to_sym)
    if relationship_members.present? and relationship_members.include? self
      return relationship_method.to_s
    end
  end
  return 'public'
end