Module: Authorization::ObjectRolesTable::UserExtensions::InstanceMethods

Defined in:
lib/rails-authorization-plugin/lib/publishare/object_roles_table.rb

Instance Method Summary collapse

Instance Method Details

#authorizables_for(authorizable_class) ⇒ Object


80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rails-authorization-plugin/lib/publishare/object_roles_table.rb', line 80

def authorizables_for(authorizable_class)
  unless authorizable_class.is_a? Class
    raise CannotGetAuthorizables, "Invalid argument: '#{authorizable_class}'. You must provide a class here."
  end

  begin
    authorizable_class.find(
      roles.where(authorizable_type: authorizable_class.base_class.to_s).map(&:authorizable_id).uniq
    )
  rescue ActiveRecord::RecordNotFound
    []
  end
end

#has_no_role(role_name, authorizable_obj = nil) ⇒ Object


46
47
48
49
# File 'lib/rails-authorization-plugin/lib/publishare/object_roles_table.rb', line 46

def has_no_role(role_name, authorizable_obj = nil)
  role = get_role(role_name, authorizable_obj)
  delete_role(role)
end

#has_no_rolesObject


76
77
78
# File 'lib/rails-authorization-plugin/lib/publishare/object_roles_table.rb', line 76

def has_no_roles
  roles.each { |role| delete_role(role) }
end

#has_no_roles_for(authorizable_obj = nil) ⇒ Object


72
73
74
# File 'lib/rails-authorization-plugin/lib/publishare/object_roles_table.rb', line 72

def has_no_roles_for(authorizable_obj = nil)
  roles_for(authorizable_obj).each { |role| delete_role(role) }
end

#has_role(role_name, authorizable_obj = nil) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rails-authorization-plugin/lib/publishare/object_roles_table.rb', line 32

def has_role(role_name, authorizable_obj = nil)
  role = get_role(role_name, authorizable_obj)
  if role.nil?
    role = if authorizable_obj.is_a? Class
             Role.create(name: role_name, authorizable_type: authorizable_obj.to_s)
           elsif authorizable_obj
             Role.create(name: role_name, authorizable: authorizable_obj)
           else
             Role.create(name: role_name)
           end
  end
  roles << role if role and not roles.exists?(role.id)
end

#has_role?(role_name, authorizable_obj = nil) ⇒ Boolean

If roles aren't explicitly defined in user class then check roles table


23
24
25
26
27
28
29
30
# File 'lib/rails-authorization-plugin/lib/publishare/object_roles_table.rb', line 23

def has_role?(role_name, authorizable_obj = nil)
  if authorizable_obj.nil?
    roles.find_by(name: role_name) || roles.member?(get_role(role_name, authorizable_obj)) ? true : false # If we ask a general role question, return true if any role is defined.
  else
    role = get_role(role_name, authorizable_obj)
    role ? roles.exists?(role.id) : false
  end
end

#has_roles_for?(authorizable_obj) ⇒ Boolean Also known as: has_role_for?


51
52
53
54
55
56
57
58
59
# File 'lib/rails-authorization-plugin/lib/publishare/object_roles_table.rb', line 51

def has_roles_for?(authorizable_obj)
  if authorizable_obj.is_a? Class
    !roles.detect { |role| role.authorizable_type == authorizable_obj.to_s }.nil?
  elsif authorizable_obj
    !roles.detect { |role| role.authorizable_type == authorizable_obj.class.base_class.to_s && role.authorizable == authorizable_obj }.nil?
  else
    !roles.detect { |role| role.authorizable.nil? }.nil?
  end
end

#roles_for(authorizable_obj) ⇒ Object


62
63
64
65
66
67
68
69
70
# File 'lib/rails-authorization-plugin/lib/publishare/object_roles_table.rb', line 62

def roles_for(authorizable_obj)
  if authorizable_obj.is_a? Class
    roles.select { |role| role.authorizable_type == authorizable_obj.to_s }
  elsif authorizable_obj
    roles.select { |role| role.authorizable_type == authorizable_obj.class.base_class.to_s && role.authorizable.id == authorizable_obj.id }
  else
    roles.select { |role| role.authorizable.nil? }
  end
end