class User

File

user.rb

(C)

Hipposoft 2008

Purpose

Describe the behaviour of User objects. See below for more details.


03-Jan-2008 (ADH): Created.

Constants

DEFAULT_SORT_COLUMN
DEFAULT_SORT_DIRECTION
DEFAULT_SORT_ORDER
USED_RANGE_COLUMN
USER_TYPE_ADMIN
USER_TYPE_MANAGER
USER_TYPE_NORMAL

Public Class Methods

new( params = nil, user = nil ) click to toggle source

Some default properties are dynamic, so assign these here rather than as defaults in a migration.

Parameters:

Optional hash used for instance initialisation in the traditional way
for an ActiveRecord subclass.

Optional User object. Default data from a user control panel may be used
for the new object in future, though presently this parameter is ignored.
# File app/models/user.rb, line 116
def initialize( params = nil, user = nil )
  super( params )

  if ( params.nil? )
    self.code         = "UID%04d" % User.count
    self.active       = true
    self.name         = ''
    self.email        = ''
    self.identity_url = ''
    self.user_type    = User::USER_TYPE_NORMAL
  end
end
rationalise_id( uri ) click to toggle source

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 202
def self.rationalise_id( uri )
  return nil if ( uri.nil? )

  uri      = uri.strip
  original = URI.parse( uri )

  # Did the user omit the 'http' prefix? If so, the URI parser will
  # be a bit confused. Try adding in 'http' instead.

  original = URI.parse( "http://#{uri}" ) if ( original.scheme.nil? )

  # We must by now have at least a scheme and host. If not, something
  # very odd is going on - bail out.

  return uri if ( original.scheme.nil? or original.host.nil? )

  original.path.chomp!( '/' )

  # Looks good - assemble a clean equivalent.

  if ( original.scheme.downcase == 'https' )
    mod = URI::HTTPS
  else
    mod = URI::HTTP
  end

  rational = mod.build( {
    :scheme => original.scheme.downcase,
    :host   => original.host.downcase,
    :port   => original.port,
    :path   => original.path
  } )

  return rational.to_s()

rescue

  # Catch URI parser exceptions by just bailing out

  return uri
end

Public Instance Methods

active_permitted_tasks() click to toggle source

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 141
def active_permitted_tasks
  ( self.restricted? ) ? self.tasks.active : Task.active
end
admin?() click to toggle source

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 186
def admin?
  return ( self.user_type == User::USER_TYPE_ADMIN )
end
all_permitted_tasks() click to toggle source

As ‘permitted_tasks’ above, but returns details for both active and inactive tasks.

# File app/models/user.rb, line 148
def all_permitted_tasks
  ( self.restricted? ) ? self.tasks.scoped : Task.scoped
end
manager?() click to toggle source

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 173
def manager?
  return ( self.user_type == User::USER_TYPE_MANAGER or self.user_type == User::USER_TYPE_ADMIN )
end
Also aliased as: privileged?
privileged?() click to toggle source

Is this user not restricted? Means the same thing as “manager?” in practice since administrators are also managers, but use of this alias can lead to more legible code.

Alias for: manager?
remove_inactive_tasks() click to toggle source

Remove inactive tasks from a user’s tasks list. The caller is responsible for saving the updated object.

# File app/models/user.rb, line 155
def remove_inactive_tasks
  self.tasks = self.tasks.active
end
restricted?() click to toggle source

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 163
def restricted?
  return ( self.user_type == User::USER_TYPE_NORMAL )
end
tasks_are_active() click to toggle source
# File app/models/user.rb, line 91
def tasks_are_active
  self.tasks.all.each do | task |
    errors.add( :base, "Cannot assign task '#{ task.title }' to this user - it is no longer active" ) unless task.active
  end
end