Module: ActsAsRoleRestricted::ClassMethods

Defined in:
app/models/concerns/acts_as_role_restricted.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_role_restricted?Boolean



56
# File 'app/models/concerns/acts_as_role_restricted.rb', line 56

def acts_as_role_restricted?; true; end

#for_role(*roles) ⇒ Object

Returns all records which have been assigned any of the given roles, as well as any record with no role assigned



66
67
68
69
70
71
# File 'app/models/concerns/acts_as_role_restricted.rb', line 66

def for_role(*roles)
  sql = with_role_sql(roles) || ''
  sql += ' OR ' if sql.present?
  sql += "(#{self.table_name}.roles_mask = 0) OR (#{self.table_name}.roles_mask IS NULL)"
  where(sql)
end

#with_role(*roles) ⇒ Object

Returns all records which have been assigned any of the the given roles



61
62
63
# File 'app/models/concerns/acts_as_role_restricted.rb', line 61

def with_role(*roles)
  where(with_role_sql(roles))
end

#with_role_sql(*roles) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/concerns/acts_as_role_restricted.rb', line 73

def with_role_sql(*roles)
  roles = roles.flatten.compact
  roles = roles.first.roles if roles.length == 1 && roles.first.respond_to?(:roles)
  roles = roles.map { |role| role.to_sym }

  if(invalid = (roles - EffectiveRoles.roles)).present?
    raise("unknown role :#{invalid.to_sentence}")
  end

  roles.map { |role| "(#{self.table_name}.roles_mask & %d > 0)" % 2**EffectiveRoles.roles.index(role) }.join(' OR ')
end

#without_role(*roles) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/concerns/acts_as_role_restricted.rb', line 85

def without_role(*roles)
  roles = roles.flatten.compact
  roles = roles.first.roles if roles.length == 1 && roles.first.respond_to?(:roles)
  roles = roles.map { |role| role.to_sym }

  if(invalid = (roles - EffectiveRoles.roles)).present?
    raise("unknown role :#{invalid.to_sentence}")
  end

  where(
    roles.map { |role| "NOT(#{self.table_name}.roles_mask & %d > 0)" % 2**EffectiveRoles.roles.index(role) }.join(' AND ')
  ).or(where(roles_mask: nil))
end