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 intenal 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.
132 133 134 135 136 137 138 |
# File 'lib/hub_sso_lib.rb', line 132 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.
109 110 111 |
# File 'lib/hub_sso_lib.rb', line 109 def self.get_display_name(symbol) ROLES[symbol] end |
.get_display_names ⇒ Object
Return all display names in an array. Class method.
115 116 117 |
# File 'lib/hub_sso_lib.rb', line 115 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.
122 123 124 |
# File 'lib/hub_sso_lib.rb', line 122 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.
143 144 145 |
# File 'lib/hub_sso_lib.rb', line 143 def add(role) @role_array.push(role.to_s.intern).uniq! end |
#clear ⇒ Object
Delete all roles from the internal list.
156 157 158 |
# File 'lib/hub_sso_lib.rb', line 156 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.
150 151 152 |
# File 'lib/hub_sso_lib.rb', line 150 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’.
206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/hub_sso_lib.rb', line 206 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.
168 169 170 |
# File 'lib/hub_sso_lib.rb', line 168 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.
194 195 196 |
# File 'lib/hub_sso_lib.rb', line 194 def to_authenticated_roles return self end |
#to_human_s ⇒ Object
Return a copy of the intenal roles list as a human readable string.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/hub_sso_lib.rb', line 174 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.
162 163 164 |
# File 'lib/hub_sso_lib.rb', line 162 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.
228 229 230 231 232 233 234 235 236 |
# File 'lib/hub_sso_lib.rb', line 228 def validate return false if @role_array.empty? @role_array.each do |role| return false unless ROLES[role] end return true end |