Module: ActsAsRoleRestricted
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/acts_as_role_restricted.rb
Overview
ActsAsRoleRestricted
This model implements the github.com/ryanb/cancan/wiki/Role-Based-Authorization multi role based authorization based on the roles_mask field
Mark your model with ‘acts_as_role_restricted’
and create the migration to add the following field:
roles_mask :integer
Defined Under Namespace
Modules: Base, ClassMethods
Instance Method Summary collapse
-
#is?(role) ⇒ Boolean
if user.is? :admin.
-
#is_any?(*queried_roles) ⇒ Boolean
if user.is_any?(:admin, :editor) returns true if user has any role given.
- #is_role_restricted? ⇒ Boolean
- #roles ⇒ Object
- #roles=(roles) ⇒ Object
-
#roles_match?(obj) ⇒ Boolean
Are both objects unrestricted, or are both roles identical?.
-
#roles_overlap?(obj) ⇒ Boolean
Are both objects unrestricted, or do any roles overlap?.
-
#roles_permit?(obj) ⇒ Boolean
Any I unrestricted, or do any roles overlap?.
Instance Method Details
#is?(role) ⇒ Boolean
if user.is? :admin
99 100 101 |
# File 'app/models/concerns/acts_as_role_restricted.rb', line 99 def is?(role) roles.include?(role.try(:to_sym)) end |
#is_any?(*queried_roles) ⇒ Boolean
if user.is_any?(:admin, :editor) returns true if user has any role given
105 106 107 |
# File 'app/models/concerns/acts_as_role_restricted.rb', line 105 def is_any?(*queried_roles) (queried_roles & roles).present? end |
#is_role_restricted? ⇒ Boolean
127 128 129 |
# File 'app/models/concerns/acts_as_role_restricted.rb', line 127 def is_role_restricted? roles.present? end |
#roles ⇒ Object
94 95 96 |
# File 'app/models/concerns/acts_as_role_restricted.rb', line 94 def roles EffectiveRoles.roles_for(roles_mask) end |
#roles=(roles) ⇒ Object
90 91 92 |
# File 'app/models/concerns/acts_as_role_restricted.rb', line 90 def roles=(roles) self.roles_mask = EffectiveRoles.roles_mask_for(roles) end |
#roles_match?(obj) ⇒ Boolean
Are both objects unrestricted, or are both roles identical?
116 117 118 119 120 |
# File 'app/models/concerns/acts_as_role_restricted.rb', line 116 def roles_match?(obj) obj_roles = EffectiveRoles.roles_for(obj) matching_roles = (roles & obj_roles) matching_roles.length == roles.length && matching_roles.length == obj_roles.length end |
#roles_overlap?(obj) ⇒ Boolean
Are both objects unrestricted, or do any roles overlap?
110 111 112 113 |
# File 'app/models/concerns/acts_as_role_restricted.rb', line 110 def roles_overlap?(obj) obj_roles = EffectiveRoles.roles_for(obj) (roles.blank? && obj_roles.blank?) || (roles & obj_roles).any? end |
#roles_permit?(obj) ⇒ Boolean
Any I unrestricted, or do any roles overlap?
123 124 125 |
# File 'app/models/concerns/acts_as_role_restricted.rb', line 123 def roles_permit?(obj) roles.blank? || roles_overlap?(obj) end |