Class | User |
In: |
app/models/user.rb
|
Parent: | ActiveRecord::Base |
File: | user.rb |
(C): | Hipposoft 2009 |
Purpose: | Describe the behaviour of User objects. See below for more details. |
03-Jan-2008 (ADH): Created.
DEFAULT_SORT_COLUMN | = | 'name' |
DEFAULT_SORT_DIRECTION | = | 'ASC' |
DEFAULT_SORT_ORDER | = | "#{ DEFAULT_SORT_COLUMN } #{ DEFAULT_SORT_DIRECTION }" |
Class method - rationalise a URL for use with Open ID by ensuring that the scheme and host are in lower case, the port nubmer is explicit and query or fragment strings are stripped out. Only call for HTTP or HTTPS URLs. If given ‘nil’, returns ‘nil’. If given something with no apparent scheme, assumes ‘HTTP’.
# File app/models/user.rb, line 197 197: def self.rationalise_id( uri ) 198: return nil if ( uri.nil? ) 199: 200: uri = uri.strip 201: original = URI.parse( uri ) 202: 203: # Did the user omit the 'http' prefix? If so, the URI parser will 204: # be a bit confused. Try adding in 'http' instead. 205: 206: orignal = URI.parse( "http://#{uri}" ) if ( original.scheme.nil? ) 207: 208: # We must by now have at least a scheme and host. If not, something 209: # very odd is going on - bail out. 210: 211: return uri if ( original.scheme.nil? or original.host.nil? ) 212: 213: # Looks good - assemble a clean equivalent. 214: 215: if ( original.scheme.downcase == 'https' ) 216: mod = URI::HTTPS 217: else 218: mod = URI::HTTP 219: end 220: 221: rational = mod.build( { 222: :scheme => original.scheme.downcase, 223: :host => original.host.downcase, 224: :port => original.port, 225: :path => original.path 226: } ) 227: 228: return rational.to_s() 229: 230: rescue 231: 232: # Catch URI parser exceptions by just bailing out 233: 234: return uri 235: end
Find all tasks which this user is permitted to see; only active tasks are returned. Returns an association-like object on which other methods may be called, e.g. a "find" call, a "count" (for efficient counting of items without needing a special additional count method), and so-on.
Call this rather than "user.tasks.active" if you want to retrieve valid task lists even for privileged users, where otherwise there may be no assigned task list (since privileged users can view anything anyway) and "user.tasks" would thus return nothing. Note that the actual assigned task list for privileged users, if any, will be IGNORED by this call.
# File app/models/user.rb, line 111 111: def active_permitted_tasks 112: ( self.restricted? ) ? self.tasks.active : Task.active 113: end
Is this user an administrator? This generally means full read/write system access. This does given the potential to completely break the system (e.g. delete a user‘s control panel but not the user), although steps are taken to try and protect against it. In the end, though, an administrator is assumed to be With Clue.
See also "privileged?" and "manager?". Administrators are considered to be both managers and privileged.
# File app/models/user.rb, line 156 156: def admin? 157: return ( self.user_type == 'Admin' ) 158: end
As ‘permitted_tasks’ above, but returns details for both active and inactive tasks.
# File app/models/user.rb, line 118 118: def all_permitted_tasks 119: ( self.restricted? ) ? self.tasks : Task.all 120: end
Assign default conditions for a brand new object. The interface is a little odd. If given a user in the first parameter, then it is assumed that this user is setting up a new user; the new user details are created as a blank template. If given nil then an identity URL and optional user type, it is assumed that a new user account is being auto-created for that identity URL and defaults are assigned accordingly. If omitted, the user type will be set to "Normal".
# File app/models/user.rb, line 175 175: def assign_defaults( user, identity_url = nil, user_type = nil ) 176: if ( user.nil? ) 177: user_type = user_type || 'Normal' 178: else 179: identity_url = '' 180: user_type = 'Normal' 181: end 182: 183: self.code = "UID%04d" % User.count 184: self.active = true 185: self.name = '' 186: self.email = '' 187: self.identity_url = identity_url 188: self.user_type = user_type 189: end
Is this user a manager? This generally means elevated privileges but still no full read/write system access for safety. See also "privileged?" - manager accounts are considered privileged.
Administrators acquire manager privileges in passing.
# File app/models/user.rb, line 143 143: def manager? 144: return ( self.user_type == 'Manager' or self.user_type == 'Admin' ) 145: end
Remove inactive tasks from a user‘s tasks list. The caller is responsible for saving the updated object.
# File app/models/user.rb, line 125 125: def remove_inactive_tasks 126: self.tasks = self.tasks.active 127: end
Is this user restricted? This generally means they can only see anything related to tasks belonging to this user, which only a manager or administrator can assign. See also "privileged?".
# File app/models/user.rb, line 133 133: def restricted? 134: return ( self.user_type == 'Normal' ) 135: end