Module: RoleAuthorization::User::InstanceMethods
- Defined in:
- lib/role_authorization/user.rb
Overview
ClassMethods
Instance Method Summary collapse
- #<<(value) ⇒ Object
- #admin? ⇒ Boolean
-
#enroll(role_name, scope = nil) ⇒ Object
adds a role to the user.
- #global_roles ⇒ Object
-
#global_roles=(role_names) ⇒ Object
mass enroll, global only.
- #has_role?(role, scopes = nil) ⇒ Boolean
- #roles(scopes = nil) ⇒ Object
- #scope_ids_from(*roles) ⇒ Object
- #scope_with(scope = nil) ⇒ Object
- #withdraw(role_name, scope = nil) ⇒ Object
- #withdraw_from_scope(scope) ⇒ Object
Instance Method Details
#<<(value) ⇒ Object
81 82 83 |
# File 'lib/role_authorization/user.rb', line 81 def <<(value) enroll(value) end |
#admin? ⇒ Boolean
172 173 174 |
# File 'lib/role_authorization/user.rb', line 172 def admin? has_role?(:all, :global) end |
#enroll(role_name, scope = nil) ⇒ Object
adds a role to the user
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/role_authorization/user.rb', line 105 def enroll(role_name, scope = nil) return true if has_role?(role_name.to_sym, scope) scope_key, scope_id = scope_with(scope) self.serialized_roles ||= Hash.new if scope_key.nil? self.serialized_roles[:global] ||= Array.new self.serialized_roles[:global] << role_name.to_sym else if scope_id.nil? self.serialized_roles[scope_key] ||= Array.new self.serialized_roles[scope_key] << role_name.to_sym else self.serialized_roles[scope_key] ||= Hash.new self.serialized_roles[scope_key][scope_id] ||= Array.new self.serialized_roles[scope_key][scope_id] << role_name.to_sym end end if save(:validate => false) RoleAuthorization::Roles.manager.klass.find_by_name(role_name).add_user(self.id, scope) true else false end end |
#global_roles ⇒ Object
85 86 87 |
# File 'lib/role_authorization/user.rb', line 85 def global_roles roles(:global) end |
#global_roles=(role_names) ⇒ Object
mass enroll, global only
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/role_authorization/user.rb', line 90 def global_roles=(role_names) # first get rid of all global roles removed_roles = [] self.serialized_roles ||= Hash.new (self.serialized_roles[:global] || []).map do |role_name| RoleAuthorization::Roles.manager.klass.find_by_name(role_name).remove_user(self.id) end self.serialized_roles[:global] = Array.new role_names.map {|role_name| enroll(role_name.to_s)} end |
#has_role?(role, scopes = nil) ⇒ Boolean
77 78 79 |
# File 'lib/role_authorization/user.rb', line 77 def has_role?(role, scopes = nil) roles(scopes).include?(role) end |
#roles(scopes = nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/role_authorization/user.rb', line 51 def roles(scopes = nil) scopes = [scopes] unless scopes.is_a? Array scopes.map do |scope| scope, scope_id = scope_with(scope) (serialized_roles || {}).inject([]) do |array, (key, value)| if key == :global && scope.nil? array << value else if scope.nil? || (key == scope.to_sym && scope_id.nil?) if value.is_a?(Hash) array << value.values else array << value unless value.nil? end else array << value[scope_id] unless scope_id.nil? end end array end end.flatten.uniq end |
#scope_ids_from(*roles) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/role_authorization/user.rb', line 39 def scope_ids_from(*roles) (serialized_roles || {}).inject([]) do |array, (key, value)| next if key == :global next unless value.is_a?(Hash) value.each_pair do |key, value| array << key.to_i unless (value & roles).empty? end array end end |
#scope_with(scope = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/role_authorization/user.rb', line 27 def scope_with(scope = nil) return [nil, nil] if scope.nil? if scope.is_a?(Symbol) || scope.is_a?(String) [scope, nil] elsif scope.is_a?(Class) [scope.to_s.downcase.to_sym, nil] else [scope.class.to_s.downcase.to_sym, scope.id] end end |
#withdraw(role_name, scope = nil) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/role_authorization/user.rb', line 133 def withdraw(role_name, scope = nil) return true unless has_role?(role_name.to_sym, scope) scope_key, scope_id = scope_with(scope) serialized_roles ||= Hash.new if scope_key.nil? self.serialized_roles[:global] ||= Array.new self.serialized_roles[:global].delete(role_name.to_sym) else if scope_id.nil? self.serialized_roles[scope_key] ||= Array.new self.serialized_roles[scope_key].delete(role_name.to_sym) else self.serialized_roles[scope_key] ||= Hash.new self.serialized_roles[scope_key][scope_id] ||= Array.new self.serialized_roles[scope_key][scope_id].delete(role_name.to_sym) end end if save(:validate => false) RoleAuthorization::Roles.manager.klass.find_by_name(role_name).remove_user(self.id, scope) true else false end end |
#withdraw_from_scope(scope) ⇒ Object
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/role_authorization/user.rb', line 161 def withdraw_from_scope(scope) scope_key, scope_id = scope_with(scope) return true if scope_key.nil? || self.serialized_roles[scope_key].nil? if scope_id.nil? self.serialized_roles.delete(scope_key) else self.serialized_roles[scope_key].delete(scope_id) end end |