Class: Users::User

Inherits:
Object show all
Defined in:
lib/users/user.rb

Overview

Entity central to the Users subsystem representing an end user which may be assigned roles containing privleges to query / operate on one or more entities

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ User

User initializer

Parameters:

  • args (Hash) (defaults to: {})

    hash of options to initialize user with

Options Hash (args):

  • :id,'id' (String)

    id to assign to the user

  • :email,'email' (String)

    email to assign to the user

  • :password,'password' (String)

    password to assign to the user

  • :registration_code,'registration_code' (String)

    registration_code to assign to the user

  • :recaptcha_challenge,'recaptcha_challenge' (String)

    recaptcha_challenge to assign to the user

  • :recaptcha_response,'recaptcha_response' (String)

    recaptcha_response to assign to the user


100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/users/user.rb', line 100

def initialize(args = {})
  attr_from_args args,
                 :id => nil, :email => nil, :password => nil,
                 :registration_code => -1, # nil registration code has special value
                 :recaptcha_challenge => nil,
                 :recaptcha_response  => nil,
                 :npc => false, :attributes => nil,
                 :secure_password => false, :permenant => false,
                 :roles => roles # [] ???

  @attributes.each { |attr|
    attr.user = self
  } if @attributes
end

Instance Attribute Details

#attributesObject

List of attributes currently owned by the user


82
83
84
# File 'lib/users/user.rb', line 82

def attributes
  @attributes
end

#created_atObject

Time user account was created


66
67
68
# File 'lib/users/user.rb', line 66

def created_at
  @created_at
end

#emailObject

String

string email of the user


22
23
24
# File 'lib/users/user.rb', line 22

def email
  @email
end

#idObject

String

unique string identifier of the user


19
20
21
# File 'lib/users/user.rb', line 19

def id
  @id
end

#last_login_atObject

Time user last logged in


72
73
74
# File 'lib/users/user.rb', line 72

def 
  @last_login_at
end

#last_modified_atObject

Time user account was last modified


69
70
71
# File 'lib/users/user.rb', line 69

def last_modified_at
  @last_modified_at
end

#npcObject

Boolean

indicating if this user is a npc

TODO prohibit npcs from logging in?


79
80
81
# File 'lib/users/user.rb', line 79

def npc
  @npc
end

#passwordObject

String

user password (encrypted if secure_password is enabled)


28
29
30
# File 'lib/users/user.rb', line 28

def password
  @password
end

#permenantObject

Boolean

indicating if this user is permenantly logged in


75
76
77
# File 'lib/users/user.rb', line 75

def permenant
  @permenant
end

#recaptcha_challengeObject

Recaptcha challenge from new account request


60
61
62
# File 'lib/users/user.rb', line 60

def recaptcha_challenge
  @recaptcha_challenge
end

#recaptcha_responseObject

Recaptcha response from new account request


63
64
65
# File 'lib/users/user.rb', line 63

def recaptcha_response
  @recaptcha_response
end

#registration_codeObject

Registration code, set on new user registration then deleted on confirmation. If set the user has registered but hasn't confirmed their email yet


57
58
59
# File 'lib/users/user.rb', line 57

def registration_code
  @registration_code
end

#rolesObject

Array<Users::Role>

array of roles the user has


25
26
27
# File 'lib/users/user.rb', line 25

def roles
  @roles
end

#secure_passwordObject

Boolean indicating if we should take additional steps to secure pass


39
40
41
# File 'lib/users/user.rb', line 39

def secure_password
  @secure_password
end

Class Method Details

.json_create(o) ⇒ Object

Create new user from json representation


292
293
294
295
# File 'lib/users/user.rb', line 292

def self.json_create(o)
  user = new(o['data'])
  return user
end

.random_registration_codeObject

Generate random string registration code


298
299
300
# File 'lib/users/user.rb', line 298

def self.random_registration_code
  Users.random_string(8)
end

Instance Method Details

#add_role(role) ⇒ Object

Adds the roles specified by its arguments to the user

Parameters:


183
184
185
186
187
# File 'lib/users/user.rb', line 183

def add_role(role)
  @roles ||= []
  @roles << role unless role.nil? ||
                        has_role?(role.id)
end

#attribute(attribute_id) ⇒ Object

Return attribute w/ the specified id, else null


169
170
171
172
# File 'lib/users/user.rb', line 169

def attribute(attribute_id)
  @attributes ||= []
  @attributes.find { |a| a.type.id == attribute_id.intern }
end

#clear_rolesObject

Clear the roles the user has


175
176
177
178
# File 'lib/users/user.rb', line 175

def clear_roles
  @roles ||= []
  @roles.clear
end

#has_attribute?(attribute_id, level = nil) ⇒ Boolean

Return boolean indicating if the user has the specified attribute at an optional minimum level

Returns:

  • (Boolean)

162
163
164
165
166
# File 'lib/users/user.rb', line 162

def has_attribute?(attribute_id, level = nil)
  @attributes ||= []
  !@attributes.find { |a| a.type.id == attribute_id.intern &&
                         (level.nil? || a.level >= level ) }.nil?
end

#has_privilege?(privilege_id) ⇒ true, false

Returns boolean indicating if the user has the specified privilege

Parameters:

  • privilege_id (String)

    id of privilege to lookup in local privileges array

Returns:

  • (true, false)

    indicating if user has / does not have privilege


272
273
274
# File 'lib/users/user.rb', line 272

def has_privilege?(privilege_id)
  has_privilege_on?(privilege_id, nil)
end

#has_privilege_on?(privilege_id, entity_id) ⇒ true, false

Returns boolean indicating if the user has the specified privilege on the specified entity

Parameters:

  • privilege_id (String)

    id of privilege to lookup in local privileges array

  • entity_id (String)

    id of entity to lookup in local privileges array

Returns:

  • (true, false)

    indicating if user has / does not have privilege


262
263
264
265
266
# File 'lib/users/user.rb', line 262

def has_privilege_on?(privilege_id, entity_id)
  @roles ||= []
  @roles.each { |r| return true if r.has_privilege_on?(privilege_id, entity_id) }
  return false
end

#has_role?(role_id) ⇒ Boolean

Return bool indicating if the user has the specified role

Parameters:

  • role_id (String)

    id of the role to look for

Returns:

  • (Boolean)

    bool indicating if the user has the role


193
194
195
196
# File 'lib/users/user.rb', line 193

def has_role?(role_id)
  @roles ||= []
  @roles.any? { |r| r.id == role_id }
end

#levelObject

Return overall user level calculated from attributes


88
89
90
# File 'lib/users/user.rb', line 88

def level
  # TODO
end

#privilegesArray<Users::Privilege>

Return a list of privileges which the roles assigned to the user provides

Returns:


252
253
254
255
# File 'lib/users/user.rb', line 252

def privileges
  @roles ||= []
  @roles.collect { |r| r.privileges }.flatten.uniq
end

#remove_role(role_id) ⇒ Object

Remove the specified role from the user


199
200
201
202
# File 'lib/users/user.rb', line 199

def remove_role(role_id)
  return unless has_role?(role_id)
  @roles.reject! { |r| r.id == role_id }
end

#to_json(*a) ⇒ Object

Convert user to json representation and return it


282
283
284
285
286
287
288
289
# File 'lib/users/user.rb', line 282

def to_json(*a)
  {
    'json_class' => self.class.name,
    'data'       => {:id => id, :email => email, :roles => roles,
                     :permenant => permenant, :npc => npc, :attributes => attributes,
                    }.merge(@secure_password ? {} : {:password => password, :registration_code => registration_code})
  }.to_json(*a)
end

#to_sObject

Convert user to human readable string and return it


277
278
279
# File 'lib/users/user.rb', line 277

def to_s
  "user-#{@id}"
end

#update(new_user) ⇒ Object

Update this users's properties from other user.

Parameters:

  • new_user (Users::User)

    user from which to copy values from


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/users/user.rb', line 118

def update(new_user)
  @last_modified_at = Time.now

  # update select attributes
  #@email             = new_user.email
  @registration_code =
    new_user.registration_code unless new_user.registration_code == -1
  @roles             = new_user.roles unless new_user.roles.nil?
  @attributes        = new_user.attributes unless new_user.attributes.nil?

  if new_user.password
    @password = new_user.password

    # XXX hack, ensure password is salted after updating if necessary
    self.secure_password=@secure_password
  end
end

#update_attribute!(attribute_id, change) ⇒ Object

Updates user attribute with specified change

Parameters:

  • attribute_id (String)

    id of attribute to update

  • change (Integer, Float)

    positive/negative amount to change attribute progression by


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/users/user.rb', line 144

def update_attribute!(attribute_id, change)
  @attributes ||= []
  attribute = @attributes.find { |a| a.type.id.to_s == attribute_id.to_s }

  if attribute.nil?
    # TODO also need to assign permissions to view attribute to user
    attribute = AttributeClass.create_attribute(:type_id => attribute_id.intern)
    attribute.user = self
    raise ArgumentError, "invalid attribute #{attribute_id}" if attribute.type.nil?
    @attributes << attribute
  end

  attribute.update!(change)
  attribute
end

#valid?Boolean

Return boolean indicating if the user is valid.

Note special users such as the admin aren't constrained to these retrictions

Currently tests:

  • email is valid

  • id is valid

  • password is valid

Returns:

  • (Boolean)

    bool indicating if the user is valid or not


215
216
217
218
219
220
# File 'lib/users/user.rb', line 215

def valid?
  valid_email?          &&
  id.is_a?(String)      && !id.empty? &&
  password.is_a?(String) && !password.empty?
  # TODO validate roles
end

#valid_email?true, false

Returns boolean indicating if email is valid

Returns:

  • (true, false)

    if email matches valid regex


225
226
227
# File 'lib/users/user.rb', line 225

def valid_email?
  !(self.email =~ (/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i)).nil?
end

#valid_login?(user_id, password) ⇒ true, false

Returns boolean indicating if login credentials are valid for the current user

Parameters:

  • user_id (String)

    id of user to compare against local @id attribute

  • password (String)

    password to encrypt and compare against the local @password parameter

Returns:

  • (true, false)

    indicating if login are credentials are valid for user


234
235
236
237
# File 'lib/users/user.rb', line 234

def valid_login?(user_id, password)
  self.id == user_id && self.registration_code.nil? &&
  (@secure_password ? PasswordHelper.check(password, self.password) : password == self.password)
end