Class: HubSsoLib::Roles
- Inherits:
-
Object
- Object
- HubSsoLib::Roles
- Defined in:
- lib/hub_sso_lib.rb
Overview
Class: Roles #
(C) Hipposoft 2006 #
#
Purpose: Shared methods for handling user account roles. #
#
Author: A.D.Hodgkinson #
#
History: 17-Oct-2006 (ADH): Adapted from Clubhouse. #
20-Oct-2006 (ADH): Integrated into HubSsoLib. #
Constant Summary collapse
- ROLES =
Association of symbolic role names to display names, in no particular order.
{ :admin => 'Administrator', :webmaster => 'Webmaster', :privileged => 'Advanced user', :normal => 'Normal user' }
- ADMIN =
:admin- NORMAL =
:normal
Class Method Summary collapse
-
.get_display_name(symbol) ⇒ Object
Return the display name of a given role symbol.
-
.get_display_names ⇒ Object
Return all display names in an array.
-
.get_role_symbols ⇒ Object
Return an array of known role symbols.
Instance Method Summary collapse
-
#add(role) ⇒ Object
Adds a role, supplied as a string or symbol, to the internal list.
-
#clear ⇒ Object
Delete all roles from the internal list.
-
#delete(role) ⇒ Object
Deletes a role, supplied as a string or symbol, from the internal list.
-
#include?(roles) ⇒ Boolean
(also: #includes?)
Does the internal list of roles include the supplied role or roles? The roles can be given as an array of individual role symbols or equivalent strings, or as a single symbol or single equivalent symbol, or as a string containing equivalents of role symbols in a comma-separated list (no white space or other spurious characters).
-
#initialize(admin = false) ⇒ Roles
constructor
Initialize a new Roles object.
-
#to_a ⇒ Object
Return a copy of the internal roles list as an array.
-
#to_authenticated_roles ⇒ Object
Do nothing - this is just useful for polymorphic code, where a function can take a String, Array, Symbol or Roles object and make the same method call to return a Roles object in return.
-
#to_human_s ⇒ Object
Return a copy of the internal roles list as a human readable string.
-
#to_s ⇒ Object
Return a copy of the internal roles list as a string.
-
#validate ⇒ Object
Validate the list of roles.
Constructor Details
#initialize(admin = false) ⇒ Roles
Initialize a new Roles object. Pass ‘true’ if this is for an admin user account, else ‘false’. Default is ‘false’. Note that further down in this file, the String, Symbol and Array classes are extended with to_authenticated_roles methods, which provide other ways of creating Roles objects.
186 187 188 189 190 191 192 |
# File 'lib/hub_sso_lib.rb', line 186 def initialize(admin = false) if (admin) @role_array = [ ADMIN ] else @role_array = [ NORMAL ] end end |
Class Method Details
.get_display_name(symbol) ⇒ Object
Return the display name of a given role symbol. Class method.
163 164 165 |
# File 'lib/hub_sso_lib.rb', line 163 def self.get_display_name(symbol) ROLES[symbol] end |
.get_display_names ⇒ Object
Return all display names in an array. Class method.
169 170 171 |
# File 'lib/hub_sso_lib.rb', line 169 def self.get_display_names ROLES.values end |
.get_role_symbols ⇒ Object
Return an array of known role symbols. They can be used with methods like get_display_name. Class method.
176 177 178 |
# File 'lib/hub_sso_lib.rb', line 176 def self.get_role_symbols ROLES.keys end |
Instance Method Details
#add(role) ⇒ Object
Adds a role, supplied as a string or symbol, to the internal list. A non-nil return indicates that the role was already present.
197 198 199 |
# File 'lib/hub_sso_lib.rb', line 197 def add(role) @role_array.push(role.to_s.intern).uniq! end |
#clear ⇒ Object
Delete all roles from the internal list.
210 211 212 |
# File 'lib/hub_sso_lib.rb', line 210 def clear @role_array.clear end |
#delete(role) ⇒ Object
Deletes a role, supplied as a string or symbol, from the internal list. A nil return indicates that the role was not in the list.
204 205 206 |
# File 'lib/hub_sso_lib.rb', line 204 def delete(role) @role_array.delete(role.to_s.intern) end |
#include?(roles) ⇒ Boolean Also known as: includes?
Does the internal list of roles include the supplied role or roles? The roles can be given as an array of individual role symbols or equivalent strings, or as a single symbol or single equivalent symbol, or as a string containing equivalents of role symbols in a comma-separated list (no white space or other spurious characters). Returns ‘true’ if the internal list of roles includes at least one of the supplied roles, else ‘false’.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/hub_sso_lib.rb', line 260 def include?(roles) return false if roles.nil? # Ensure we've an array of roles, one way or another # roles = roles.to_s if roles.class == Symbol roles = roles.split(',') if roles.class == String roles.each do |role| return true if @role_array.include?(role.to_s.intern) end return false end |
#to_a ⇒ Object
Return a copy of the internal roles list as an array.
222 223 224 |
# File 'lib/hub_sso_lib.rb', line 222 def to_a return @role_array.dup end |
#to_authenticated_roles ⇒ Object
Do nothing - this is just useful for polymorphic code, where a function can take a String, Array, Symbol or Roles object and make the same method call to return a Roles object in return.
248 249 250 |
# File 'lib/hub_sso_lib.rb', line 248 def to_authenticated_roles return self end |
#to_human_s ⇒ Object
Return a copy of the internal roles list as a human readable string.
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/hub_sso_lib.rb', line 228 def to_human_s human_names = [] @role_array.each do |role| human_names.push(HubSsoLib::Roles.get_display_name(role)) end if (human_names.length == 0) return '' elsif (human_names.length == 1) return human_names[0] else return human_names[0..-2].join(', ') + ' and ' + human_names.last end end |
#to_s ⇒ Object
Return a copy of the internal roles list as a string.
216 217 218 |
# File 'lib/hub_sso_lib.rb', line 216 def to_s return @role_array.join(',') end |
#validate ⇒ Object
Validate the list of roles. Validation means ensuring that all roles in this object are found in the internal ROLES hash. Returns true if the roles validate or false if unknown roles are found.
283 284 285 286 287 288 289 290 291 |
# File 'lib/hub_sso_lib.rb', line 283 def validate return false if @role_array.empty? @role_array.each do |role| return false unless ROLES[role] end return true end |