Class User
In: lib/user.rb
Parent: ActiveRecord::Base

Liferay users.

Methods

External Aliases

organizations -> orgs
groups -> communities
wikipages -> articles

Public Class methods

Actions for Permissions.

[Source]

    # File lib/user.rb, line 23
23:   def self.actions
24:     %w{ DELETE IMPERSONATE PERMISSIONS UPDATE VIEW }
25:   end

This method is meant to be overridden in custom configurations.

[Source]

     # File lib/user.rb, line 441
441:   def self.cleanup_mobile_number(number)
442:     return number
443:   end

com.liferay.portal.model.User

[Source]

    # File lib/user.rb, line 13
13:   def self.liferay_class
14:     'com.liferay.portal.model.User'
15:   end

Creates a new user.

It is a complicated process, as Liferay inserts new data into many tables. This process is engineered by creating a new role with Liferay‘s (v. 5.1.1) tools and inspecting the database dump diffs.

Mandatory parameters:

 - companyid
 - firstname
 - lastname
 - emailaddress

[Source]

     # File lib/user.rb, line 38
 38:   def initialize(params)
 39:     raise 'No companyid given' unless (params[:companyid] or params[:company])
 40:     raise 'No firstname' unless params[:firstname]
 41:     raise 'No lastname' unless params[:lastname]
 42:     raise 'No emailaddress' unless params[:emailaddress]
 43:     # Do not create Contact before creating self!
 44:     firstname = params.delete(:firstname)
 45:     lastname = params.delete(:lastname)
 46:     # Do not create contact before creating self!
 47:     gsm = params.delete(:gsm)
 48:     male = params.delete(:male)
 49: 
 50:     super(params)
 51: 
 52:     # COPY user_ (uuid_, userid, companyid, createdate, modifieddate, defaultuser, contactid, password_, passwordencrypted, passwordreset, passwordmodifieddate, gracelogincount, screenname, emailaddress, openid, portraitid, languageid, timezoneid, greeting, comments, logindate, loginip, lastlogindate, lastloginip, lastfailedlogindate, failedloginattempts, lockout, lockoutdate, agreedtotermsofuse, active_) FROM stdin;
 53:     # +c130e60d-d85a-40b8-b778-e0c0c868e508     10164       10109 2009-01-15 00:51:58.558 2009-01-15 00:51:58.558 f       10165 jGrep/LZ9ghV6N1IJ5iWcH3d4ps=    t  f        \N     0   teppo     teppo@localhost.localdomain         0      en_US        GMT    Welcome Teppo Testaaja!            \N                \N            \N        0      f    \N f       t
 54: 
 55:     # insert data into user_
 56:     unless self.uuid_
 57:       require 'rubygems'
 58:       require 'uuidtools'
 59:       self.uuid_ = UUID.random_create.to_s
 60:     end
 61:     self.createdate          = Time.now
 62:     self.modifieddate        = Time.now
 63:     self.defaultuser         = false
 64:     unless self.password_
 65:       self.password_         = firstname
 66:       self.passwordencrypted = false
 67:     end
 68:     self.passwordreset=false if self.passwordreset.nil?
 69:     self.gracelogincount     = 0
 70:     self.screenname          ||= [firstname, lastname].join('-').downcase
 71:     self.emailaddress        ||= ''
 72:     self.portraitid          ||= 0
 73:     self.languageid          ||= ''
 74:     self.timezoneid          ||= 'Europe/Istanbul'
 75:     self.greeting            ||= 'Welcome'
 76:     self.comments            ||= ''
 77:     self.loginip             ||= ''
 78:     self.lastloginip         ||= ''
 79:     self.failedloginattempts = 0
 80:     self.lockout             = false
 81:     self.agreedtotermsofuse  = true
 82:     self.active_ = true if self.active_.nil?
 83:     self.openid              ||= ''
 84: 
 85:     self.save
 86:     self.reload
 87: 
 88:     # create new contact
 89: 
 90:     # COPY contact_ (contactid, companyid, userid, username, createdate, modifieddate, accountid, parentcontactid, firstname, middlename, lastname, prefixid, suffixid, male, birthday, smssn, aimsn, facebooksn, icqsn, jabbersn, msnsn, myspacesn, skypesn, twittersn, ymsn, employeestatusid, employeenumber, jobtitle, jobclass, hoursofoperation) FROM stdin;
 91:     # +10165    10109      10129        Test Test      2009-01-15 00:51:58.558      2009-01-15 00:51:58.558      10110        0      Teppo                Testaaja      0    0  t        1970-01-01 00:00:00                                            
 92: 
 93:     if male.is_a?(String)
 94:       male = (male=='true')
 95:     end
 96:     self.contact = Contact.create({
 97:       :userid => self.id,
 98:       :username => '', # name of the user who created this user
 99:       :firstname => firstname,
100:       :lastname => lastname,
101:       :male => male
102:     })
103: 
104:     # assign "User" and "Power User" roles
105: 
106:     # COPY users_roles (userid, roleid) FROM stdin;
107:     # +10164    10116
108:     # +10164    10117
109: 
110:     self.roles << Role.find(:first, :conditions => "companyid=#{self.companyid} AND name='User'")
111:     self.roles << Role.find(:first, :conditions => "companyid=#{self.companyid} AND name='Power User'")
112: 
113:     # Personal group
114: 
115:     # COPY group_ (groupid, companyid, creatoruserid, classnameid, classpk, parentgroupid, livegroupid, name, description, type_, typesettings, friendlyurl, active_) FROM stdin;
116:     # +10166    10109      10164        10034  10164    0  0                        0            /teppo    t
117: 
118:     g = Group.create(
119:       :companyid     => self.companyid,
120:       :creatoruserid => self.id,
121:       :classnameid   => Classname.find_by_value(self.liferay_class).id,
122:       :classpk       => self.id,
123:       :friendlyurl   => '/'+self.screenname
124:     )
125: 
126:     # Group's public + private layoutsets (should be created only for Power Users?)
127:     # are created in the Group model.
128: 
129:     # COPY layoutset (layoutsetid, groupid, companyid, privatelayout, logo, logoid, themeid, colorschemeid, wapthemeid, wapcolorschemeid, css, pagecount, virtualhost) FROM stdin;
130:     # +10167    10166      10109        t      f    0          01      mobile       01            0 
131:     # +10168    10166      10109        f      f    0          01      mobile       01            0 
132: 
133: 
134:     # scope=4 means that the resource is "owned" by User
135:     rc = self.resource_code(4)
136:     raise 'Required ResourceCode not found' unless rc
137: 
138:     # insert data into counter_ so that Hibernate won't die
139:     Counter.increment(:resource, 100)
140: 
141:     # COPY resource_ (resourceid, codeid, primkey) FROM stdin;
142:     # +44       5     10164
143: 
144:     resource = Resource.create(
145:       :codeid  => rc.id,
146:       :primkey => self.id
147:     )
148: 
149:     permissions = []
150:     # insert data into counter_ so that Hibernate won't die
151: 
152:     # COPY permission_ (permissionid, companyid, actionid, resourceid) FROM stdin;
153:     # +114      10109        DELETE 44
154:     # +115      10109        IMPERSONATE    44
155:     # +116      10109        PERMISSIONS    44
156:     # +117      10109        UPDATE 44
157:     # +118      10109        VIEW   44
158: 
159:     # COPY users_permissions (userid, permissionid) FROM stdin;
160:     # +10129    114
161:     # +10129    115
162:     # +10129    116
163:     # +10129    117
164:     # +10129    118
165: 
166:     Counter.increment(:permission, 100)
167:     self.class.actions.each do |action|
168:       self.user_permissions << Permission.create(
169:         :companyid  => self.companyid,
170:         :actionid   => action,
171:         :resourceid => resource.id
172:       )
173:     end
174: 
175:     # announcementsdelivery
176: 
177:     # COPY announcementsdelivery (deliveryid, companyid, userid, type_, email, sms, website) FROM stdin;
178:     # +10169    10109      10164        general        f      f    t
179:     # +10170    10109      10164        news   f f       t
180:     # +10171    10109      10164        test   f f       t
181: 
182:     %w{ general news test }.each do |type|
183:       Announcement::Delivery.create(
184:         :userid    => self.id,
185:         :companyid => self.companyid,
186:         :type_     => type,
187:         :email     => false,
188:         :sms       => false,
189:         :website   => true
190:       )
191:     end
192: 
193:     self.save
194: 
195:     # insert data into phone
196:     self.gsm=gsm if gsm
197:     return self
198:   end

Public Instance methods

Activates inactive users. Makes it easy to extend functionality with mixins (to send email, for example).

[Source]

     # File lib/user.rb, line 502
502:   def activate
503:     self.active_ = true
504:     self.save
505:     self.class.instance_method_already_implemented?(:activate) ?
506:       super : true
507:   end

Creates a Address, unless one exists.

[Source]

     # File lib/user.rb, line 396
396:   def address
397:     return @address if @address
398:     @address = Address.find_by_userid self.id
399:     if @address.nil?
400:       @address = Address.new(
401:         :user => self,
402:         :createdate => Time.now,
403:         :company => self.company)
404:     end
405:     return @address
406:   end

Adds User to Organization members. TODO: move to Organization

Updates users_organizations, adds the ‘Organization Member’ Role, and updates 3-way associations to usergrouprole.

[Source]

     # File lib/user.rb, line 302
302:   def assign_organization_membership(org)
303:     # organization membership
304:     org = self.company.organizations.select{|o| o.parent==nil}.first
305:     self.organizations << org
306: 
307:     org_role = Role.find(:first, :conditions => "companyid=#{self.companyid} AND name='Organization Member'")
308:     group = org.group
309: 
310:     # can ActiveRecord handle 3-way associations?
311:     # - perhaps with hacking, http://bjhess.com/blog/2007/09/19/my-foray-into-has_many_polymorphs/
312:     ActiveRecord::Base.connection.execute(
313:       "INSERT INTO usergrouprole (userid, groupid, roleid) VALUES (%i, %i, %i);" % [
314:         self.id, org.group.id, org_role.id])
315: 
316:     true
317:   end

[Source]

     # File lib/user.rb, line 445
445:   def birthday
446:     self.contact.birthday
447:   end

FIXME: should create a new Contact

[Source]

     # File lib/user.rb, line 449
449:   def birthday=(val)
450:     self.contact.birthday = val
451:     self.contact.save
452:   end

[Source]

     # File lib/user.rb, line 472
472:   def city
473:     self.address.city
474:   end

FIXME: should create a new Address

[Source]

     # File lib/user.rb, line 476
476:   def city=(val)
477:     self.address.city = val
478:     self.address.save
479:   end

Deactivates active users

[Source]

     # File lib/user.rb, line 510
510:   def deactivate
511:     self.active_ = false
512:     self.save
513:     self.class.instance_method_already_implemented?(:deactivate) ?
514:       super : true
515:   end

[Source]

     # File lib/user.rb, line 327
327:   def firstname
328:     self.contact ?
329:       self.contact.firstname : nil
330:   end

Creates a new Contact unless it exists

[Source]

     # File lib/user.rb, line 332
332:   def firstname=(v)
333:     if self.contact
334:       c = self.contact
335:       c.firstname = v
336:       c.save
337:     else
338:       c = Contact.create(
339:         :userid => self.userid,
340:         :firstname => v )
341:       self.contact = c
342:       self.save
343:     end
344:     true
345:   end
fullname()

Alias for name

[Source]

     # File lib/user.rb, line 282
282:   def group_permissions
283:     self.groups.map{|g| g.permissions }.flatten
284:   end

Perhaps it would make more sense to select phones with classnameid of "mobile"

[Source]

     # File lib/user.rb, line 417
417:   def gsm
418:     self.phones.any? ? self.phones.first.number : nil
419:   end

Creates a new Phone unless it exists

[Source]

     # File lib/user.rb, line 421
421:   def gsm=(number)
422:     _number = self.class.cleanup_mobile_number(number)
423:     return false if _number.empty? # no not save empty numbers
424: 
425:     if self.phones.any?
426:       phone = self.phones.first
427:       phone.number=_number
428:       phone.save
429:     else
430:       Phone.create(
431:         :userid => self.id,
432:         :number_ => _number,
433:         :primary_ => true
434:       )
435:       self.phones.reload
436:     end
437:     true
438:   end

[Source]

     # File lib/user.rb, line 492
492:   def is_active?
493:     self.active_
494:   end

[Source]

     # File lib/user.rb, line 496
496:   def is_default?
497:     self.defaultuser
498:   end
is_guest?()

Alias for is_default?

[Source]

     # File lib/user.rb, line 408
408:   def lang
409:     self.languageid
410:   end

[Source]

     # File lib/user.rb, line 411
411:   def lang=(value)
412:     self.languageid=value
413:     true
414:   end

[Source]

     # File lib/user.rb, line 346
346:   def lastname
347:     self.contact ?
348:       self.contact.lastname : nil
349:   end

Creates a new Contact unless it exists

[Source]

     # File lib/user.rb, line 351
351:   def lastname=(v)
352:     if self.contact
353:       c = self.contact
354:       c.lastname = v
355:       c.save
356:     else
357:       c = Contact.create(
358:         :userid => self.userid,
359:         :lastname => v )
360:       self.contact = c
361:       self.save
362:     end
363:     true
364:   end

FIXME: move all references from instance method to class method

[Source]

    # File lib/user.rb, line 18
18:   def liferay_class
19:     self.class.liferay_class
20:   end

Fullname is fetched from Contact

[Source]

     # File lib/user.rb, line 321
321:   def name
322:     self.contact ?
323:       self.contact.fullname : nil
324:   end

URL path to this User‘s public or private page

[Source]

     # File lib/user.rb, line 518
518:   def path(pl=:public)
519:     self.hive.nil? ? nil : self.hive.path(pl)
520:   end

All permissions that the user has. Permissions are summed up from user, group and role permissions.

[Source]

     # File lib/user.rb, line 292
292:   def permissions
293:     user_permissions + group_permissions + role_permissions
294:   end

URL to user‘s portrait (needs to be prefixed with Liferay server URL). Unless the user has a portrait, a default image should be displayed.

[Source]

     # File lib/user.rb, line 524
524:   def portrait_path
525:     path = '/image/user_%s_portrait' % (self.contact.male ? 'male' : 'female')
526:     path << "?img_id=#{self.portraitid}" if self.portraitid != 0
527:     return path
528:   end

ResourceCode associated to this instance (and scope)

[Source]

     # File lib/user.rb, line 482
482:   def resource_code(scope=4)
483:     ResourceCode.find(:first,
484:       :conditions => "companyid=#{self.companyid} AND name='#{self.liferay_class}' AND scope=#{scope}")
485:   end

Resources associated to this instance

[Source]

     # File lib/user.rb, line 488
488:   def resources
489:     Resource.find(:all, :conditions => "primkey='#{self.id}'")
490:   end

[Source]

     # File lib/user.rb, line 286
286:   def role_permissions
287:     self.roles.map{|g| g.permissions }.flatten
288:   end

[Source]

     # File lib/user.rb, line 200
200:   def save
201:     super
202: 
203:     raise self.errors[:uuid_] if self.errors[:uuid_]
204:     raise self.errors[:emailaddress] if self.errors[:emailaddress]
205:     raise self.errors[:screenname] if self.errors[:screenname]
206:   end

Sex. 0=female, 1=male

[Source]

     # File lib/user.rb, line 367
367:   def sex
368:     self.contact.male==true ? 1 : 0
369:   end

Creates a new Contact unless it exists

1=female, 2=male

[Source]

     # File lib/user.rb, line 374
374:   def sex=(v)
375:     male = (v==2 ? true : false)
376:     if self.contact
377:       c = self.contact
378:       c.male=male
379:       c.save
380:     else
381:       c = Contact.create(
382:         :userid => self.userid,
383:         :male => male )
384:       self.contact = c
385:       self.save
386:     end
387:     true
388:   end

[Source]

     # File lib/user.rb, line 454
454:   def streetaddress
455:     self.address.street1
456:   end

FIXME: should create a new Address

[Source]

     # File lib/user.rb, line 458
458:   def streetaddress=(val)
459:     self.address.street1 = val
460:     self.address.save
461:   end

[Source]

     # File lib/user.rb, line 463
463:   def zipcode
464:     self.address.zip
465:   end

FIXME: should create a new Address

[Source]

     # File lib/user.rb, line 467
467:   def zipcode=(val)
468:     self.address.zip = val
469:     self.address.save
470:   end

[Validate]