/
Auth.pm
1365 lines (986 loc) · 40.3 KB
/
Auth.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package WebGUI::Auth;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2012 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict qw(subs vars);
use Scalar::Util qw( blessed );
use WebGUI::International;
use WebGUI::Asset::Template;
use WebGUI::User;
use WebGUI::Workflow::Instance;
use WebGUI::Shop::AddressBook;
use WebGUI::Inbox;
use WebGUI::Friends;
use WebGUI::Deprecate;
use URI;
# Profile field name for the number of times the showMessageOnLogin has been
# seen.
my $LOGIN_MESSAGE_SEEN = 'showMessageOnLoginSeen';
=head1 NAME
Package WebGUI::Auth
=head1 SYNOPSIS
package WebGUI::Auth::MyAuth;
use base 'WebGUI::Auth';
sub www_view {
# default entry point
}
=head1 DESCRIPTION
WebGUI::Auth allows you to authenticate and login users.
To write your own auth module, you should override C<www_view> to start the
user off with a login form or a create account form.
To access your new auth module, add it to the config file (authMethods) and go to
C<?op=auth;authType=MyAuth>. See L<WebGUI::Operation::Auth>.
=head1 SEE ALSO
WebGUI::Operation::Auth
WebGUI::User
=head1 METHODS
These methods are available from this class:
=cut
#-------------------------------------------------------------------
sub _isDuplicateUsername {
my $self = shift;
my $username = shift;
#Return false if the user is already logged in, but not changing their username.
return 0 if($self->isRegistered && $self->session->user->username eq $username);
my ($otherUser) = $self->session->db->quickArray("select count(*) from users where username=".$self->session->db->quote($username));
return 0 if !$otherUser;
my $i18n = WebGUI::International->new($self->session);
$self->error('<li>'.sprintf($i18n->get(77), $username,$username,$username,$self->session->datetime->epochToHuman(time(),"%y")).'</li>');
return 1;
}
#-------------------------------------------------------------------
=head2 _isValidUsername ( username )
Validates the username passed in.
=cut
sub _isValidUsername {
my $self = shift;
my $username = shift;
my $error = "";
return 1 if($self->isRegistered && $self->session->user->username eq $username);
my $i18n = WebGUI::International->new($self->session);
my $filteredUsername = WebGUI::HTML::filter($username, 'all');
if ($username ne $filteredUsername) {
$error .= '<li>' . $i18n->get('username no html') . '</li>';
}
if ($username =~ /^\s/ || $username =~ /\s$/) {
$error .= '<li>'.$i18n->get(724).'</li>';
}
if ($username eq "") {
$error .= '<li>'.$i18n->get(725).'</li>';
}
$self->error($error);
return $error eq "";
}
#-------------------------------------------------------------------
sub _logLogin {
my $self = shift;
$self->timeRecordSession;
$self->session->db->write("insert into userLoginLog values (?,?,?,?,?,?,?)",
[
$_[0],
$_[1],
time(),
$self->session->request->address,
$self->session->request->user_agent,
$self->session->getId,
time(),
]
);
}
#-------------------------------------------------------------------
=head2 authenticate ( )
Superclass method that performs standard login routines. This method returns true if login success, otherwise returns false.
=cut
sub authenticate {
my $self = shift;
my $username = shift;
my $i18n = WebGUI::International->new($self->session);
my $user = $self->session->db->quickHashRef("select userId,authMethod,status from users where username=".$self->session->db->quote($username));
my $uid = $user->{userId};
#If userId does not exist or is not active, fail login
if (!$uid) {
$self->authenticationError;
return 0;
} elsif($user->{status} ne 'Active') {
$self->error($i18n->get(820));
$self->_logLogin($uid, "failure");
return 0;
}
#Set User Id
$self->user(WebGUI::User->new($self->session,$uid));
return 1;
}
#-------------------------------------------------------------------
=head2 authenticationError
This subroutine is called by authenticate and its subclasses to make
sure these subroutines return the same errormessage on login failure.
Different errormessages would reveil if a username exists after
which only the password has to be guessed by brute force for example.
=cut
sub authenticationError {
my $self = shift;
my $i18n = WebGUI::International->new($self->session);
return ($self->error('<li>'.$i18n->get(68).'</li>'));
}
#-------------------------------------------------------------------
=head2 authMethod ( [authMethod] )
Gets or sets the authMethod in the Auth Object. Returns 'WebGUI' as the
default method if a user has been created without an authMethod.
=head3 authMethod
A string which sets the auth method for an instance of this class
=cut
sub authMethod {
my $self = shift;
if (!$_[0]) {
return $self->{authMethod} || 'WebGUI';
}
$self->{authMethod} = $_[0];
}
#-------------------------------------------------------------------
=head2 delete ( [param] )
Delete one or all parameters for this auth method. Deleting all parameters
effectively removes this auth method from the user.
=cut
sub delete {
my ( $self, $param ) = @_;
my ( $db ) = $self->session->quick(qw( db ));
if ( $param ) {
$db->write( "DELETE FROM authentication WHERE userId=? AND authMethod=? AND fieldName=?",
[ $self->userId, $self->authMethod, $param ]
);
}
else {
$db->write( "DELETE FROM authentication WHERE userId=? AND authMethod=?",
[ $self->userId, $self->authMethod ]
);
}
}
#-------------------------------------------------------------------
=head2 deleteParams ( )
NOTE: This method is deprecated and will be removed in a future version. Instead,
use delete() to delete this auth method from the user.
Removes the user's authentication parameters from the database for all
authentication methods. This is primarily useful when deleting the user's
account.
=cut
# DEPRECATED. Remove in 9.0
sub deleteParams {
my $self = shift;
$self->delete;
}
#-------------------------------------------------------------------
=head2 deleteSingleParam ( )
NOTE: This method is deprecated and will be removed in a future version. Instead,
use delete("param") to delete a single param from this auth method.
Removes a single authentication parameter from the database.
=cut
# DEPRECATED. Remove in 9.0
sub deleteSingleParam {
my $self = shift;
my ($userId, $authMethod, $fieldName) = @_;
$self->delete( $fieldName );
}
#-------------------------------------------------------------------
=head2 editUserForm ( )
Creates user form elements specific to this Auth Method.
=cut
sub editUserForm {
#Added for interface purposes only. Needs to be implemented in the subclass.
}
#-------------------------------------------------------------------
=head2 editUserFormSave ( )
Saves user elements unique to this authentication method
=cut
sub editUserFormSave {
# Added for interface purposes only. Needs to be implemented in the subclass
}
#-------------------------------------------------------------------
=head2 editSettingsForm ( )
You need to override this method in your auth module. It needs to return a the rows in a form for the stuff you want to be configured through webgui settings.
=cut
sub editSettingsForm {
}
deprecate editUserSettingsForm => 'editSettingsForm';
#-------------------------------------------------------------------
=head2 editSettingsFormSave ( )
You need to override this method in your auth module. It's the save for the editSettingsFormSave method.
=cut
sub editSettingsFormSave {
}
# Backwards compatiblity for method renaming
deprecate editSettingsFormSave => 'editSettingsFormSave';
#-------------------------------------------------------------------
=head2 error ( [errorMsg] )
Sets or returns the error currently stored in the object
=cut
sub error {
my $self = shift;
return $self->{error} if (!$_[0]);
$self->session->log->error( $_[0] );
$self->{error} = $_[0];
}
#----------------------------------------------------------------------------
=head2 get ( [param] )
Get one or all parameters for this auth instance. Returns either a hashref or a
single scalar.
=cut
sub get {
my ( $self, $param ) = @_;
my ( $db ) = $self->session->quick(qw( db ));
if ( $param ) {
return $db->quickScalar(
"SELECT fieldData FROM authentication WHERE userId=? AND authMethod=? AND fieldName=?",
[ $self->userId, $self->authMethod, $param ],
);
}
else {
return $db->buildHashRef(
"SELECT fieldName, fieldData FROM authentication WHERE userId=? AND authMethod=?",
[ $self->userId, $self->authMethod ],
);
}
}
#-------------------------------------------------------------------
=head2 getAccountTemplateId ( )
This method should be overridden by the subclass and should return the template ID for the display/edit account screen.
=cut
sub getAccountTemplateId {
return "PBtmpl0000000000000010";
}
#-------------------------------------------------------------------
=head2 getCreateAccountTemplateId ( )
This method should be overridden by the subclass and should return the template ID for the create account screen.
=cut
sub getCreateAccountTemplateId {
return "PBtmpl0000000000000011";
}
#-------------------------------------------------------------------
=head2 getDeactivateAccountTemplateId ( )
This method should be overridden by the subclass and should return the template ID for the deactivate account screen.
=cut
sub getDeactivateAccountTemplateId {
return "PBtmpl0000000000000057";
}
#-------------------------------------------------------------------
=head2 getDefaultLoginTemplateId ( )
This method should be overridden by the subclass and should return the default template ID for the login screen.
=cut
sub getDefaultLoginTemplateId {
return "PBtmpl0000000000000013";
}
#-------------------------------------------------------------------
=head2 getExtrasStyle ( )
This method returns the proper field to display for required fields.
=cut
sub getExtrasStyle {
my $self = shift;
my $value = shift;
my $requiredStyleOff = q{class="authfield_required_off"};
my $requiredStyle = q{class="authfield_required"};
my $errorStyle = q{class="authfield_error"}; #Required Field Not Filled In and Error Returend
return $errorStyle if($self->error && $value eq "");
return $requiredStyle unless($value);
return $requiredStyleOff;
}
#-------------------------------------------------------------------
=head2 getLoginTemplate ( )
Returns a WebGUI::Asset::Template object for the login template. If the configured
template cannot be used, then it returns a default template object.
=cut
sub getLoginTemplate {
my $self = shift;
my $session = $self->session;
my $templateId = $self->getLoginTemplateId;
my $template = WebGUI::Asset::Template->newById($session, $templateId);
if (!$template) {
$templateId = $self->getDefaultLoginTemplateId;
$template = WebGUI::Asset::Template->newById($session, $templateId);
}
return $template;
}
#-------------------------------------------------------------------
=head2 getLoginTemplateId ( )
This method should be overridden by the subclass and should return the template ID for the login screen.
=cut
sub getLoginTemplateId {
return "PBtmpl0000000000000013";
}
#-------------------------------------------------------------------
=head2 getParams ( )
NOTE: This method is deprecated and will be removed in a future version. Use get() instead.
Returns a hash reference with the user's authentication information. This method uses data stored in the instance of the object.
=cut
# DEPRECATED. Remove in 9.0
sub getParams {
my $self = shift;
my $userId = $_[0] || $self->userId;
my $authMethod = $_[1] || $self->authMethod;
return $self->session->db->buildHashRef("select fieldName, fieldData from authentication where userId=".$self->session->db->quote($userId)." and authMethod=".$self->session->db->quote($authMethod));
}
#-------------------------------------------------------------------
=head2 getSetting ( setting )
Returns a setting for this authMethod instance. If none is specified, returns the system authMethod setting
=head3 setting
Specify a setting to retrieve
=cut
sub getSetting {
my $self = shift;
my $setting = $_[0];
$setting = lc($self->authMethod).ucfirst($setting);
return $self->session->setting->get($setting);
}
#-------------------------------------------------------------------
=head2 isAdmin ()
NOTE: This method is deprecated. Use user->isAdmin instead.
Returns 1 if the user is user 3 (admin).
=cut
# DEPRECATED. Remove in 9.0
sub isAdmin {
my $self = shift;
return $self->user->isAdmin;
}
#-------------------------------------------------------------------
=head2 isCallable ( method )
NOTE: Deprecated. Unnecessary when setCallable is removed.
Returns whether or not a method is callable
=cut
# DEPRECATED. Remove in 9.0
sub isCallable {
my $self = shift;
return 1 if $_[0] ~~ $self->{callable};
return 1 if $self->can( 'www_' . $_[0] );
return 0;
}
#-------------------------------------------------------------------
=head2 isRegistered ()
NOTE: Deprecated. Use user->isRegistered instead.
Returns 1 if the user is not a visitor.
=cut
# DEPRECATED. Remove in 9.0
sub isRegistered {
my $self = shift;
return $self->user->isRegistered;
}
#-------------------------------------------------------------------
=head2 isVisitor ()
NOTE: Deprecated. Use user->isVisitor instead.
Returns 1 if the user is a visitor.
=cut
# DEPRECATED. Remove in 9.0
sub isVisitor {
my $self = shift;
return $self->userId eq '1';
}
#-------------------------------------------------------------------
=head2 new ( session, [ user|userId ] )
Constructor.
=head3 session
=head3 user|userId
A WebGUI::User object, or userId for the user requesting authentication.
This defaults to $self->session->user->userId
=cut
sub new {
my $class = shift;
my $self = bless {}, $class;
$self->{_session} = shift;
if ( blessed $_[0] && $_[0]->isa('WebGUI::User') ) {
$self->{user} = shift;
}
elsif ( my $userId = shift ) {
$self->{user} = WebGUI::User->new($self->{_session}, $userId);
}
else {
$self->{user} = $self->session->user;
}
$self->{error} = "";
$self->{profile} = ();
# Determine the authmethod from the classname
($self->{authMethod}) = $class =~ m/^WebGUI::Auth::(.+)/;
$self->setCallable([qw( init showMessageOnLogin )]);
return $self;
}
#-------------------------------------------------------------------
sub session {
my $self = shift;
return $self->{_session};
}
#-------------------------------------------------------------------
=head2 setCallable ( callableMethods )
NOTE: This method is deprecated and will be removed in a future version. Instead,
any method prefixed with www_ is available from the web interface.
adds elements to the callable routines list. This list determines whether or not a method in this instance is
allowed to be called externally
=head3 callableMethods
Array reference containing a list of methods for this authentication instance that can be called externally
=cut
# DEPRECATED. Remove in 9.0
sub setCallable {
my $self = shift;
my @callable = @{$self->{callable}};
@callable = (@callable,@{$_[0]});
$self->{callable} = \@callable;
}
#-------------------------------------------------------------------
=head2 saveParams ( userId, authMethod, data )
NOTE: This method is deprecated and will be removed in a future version. Instead,
use update() to update the parameters of this auth instance.
Saves the user's authentication parameters to the database.
=head3 userId
Specify a user id.
=head3 authMethod
Specify the authentication method to save these paramaters under.
=head3 data
A hash reference containing parameter names and values to be saved.
=cut
# DEPRECATED. Remove in 9.0
sub saveParams {
my $self = shift;
my ($uid, $authMethod, $data) = @_;
return $self->update( $data );
}
#----------------------------------------------------------------------------
=head2 timeRecordSession
Record the last page viewed and the time viewed for the user
=cut
sub timeRecordSession {
my $self = shift;
my ($nonTimeRecordedRows) = $self->session->db->quickArray("select count(*) from userLoginLog where lastPageViewed = timeStamp and sessionId = ? ", [$self->session->getId] );
if ($nonTimeRecordedRows eq "1") {
# We would normally expect to only find one entry
$self->session->db->write("update userLoginLog set lastPageViewed = (select lastPageView from userSession where sessionId = ?) where lastPageViewed = timeStamp and sessionId = ? ",
[ $self->session->getId,
$self->session->getId]);
} elsif ($nonTimeRecordedRows eq "0") {
# Do nothing
} else {
# If something strange happened and we ended up with > 1 matching rows, cut our losses and remove offending userLoginLog rows (otherwise we
# could end up with ridiculously long user recorded times)
$self->session->log->warn("More than 1 old userLoginLog rows found, removing offending rows");
$self->session->db->write("delete from userLoginLog where lastPageViewed = timeStamp and sessionId = ? ", [$self->session->getId] );
}
}
#----------------------------------------------------------------------------
=head2 update ( params )
Update the parameters for this auth instance. Params is a list of name => value pairs.
=cut
sub update {
my $self = shift;
my ( $db ) = $self->session->quick(qw( db ));
my %params;
# Allow both hashref and hash
if ( @_ == 1 ) {
%params = %{ $_[0] };
}
else {
%params = @_;
}
foreach my $param (keys %params) {
$db->write(
"delete from authentication where userId=? and authMethod=? and fieldName=?",
[ $self->userId, $self->authMethod, $param ],
);
$db->write(
"insert into authentication (userId,authMethod,fieldName,fieldData) values (?,?,?,?)",
[ $self->userId, $self->authMethod, $param, $params{ $param } ],
);
}
}
#-------------------------------------------------------------------
=head2 user ( [user] )
Sets or Returns the user object stored in the wobject
=cut
sub user {
my $self = shift;
return $self->{user} if (!$_[0]);
$self->{user} = $_[0];
}
#-------------------------------------------------------------------
=head2 userId ( )
Returns the userId currently stored in the object
=cut
sub userId {
my $self = shift;
my $u = $self->user;
return $u->userId;
}
#-------------------------------------------------------------------
=head2 username ( )
Returns the username currently stored in the object
=cut
sub username {
my $self = shift;
my $u = $self->user;
return $u->username;
}
#-------------------------------------------------------------------
=head2 validUsername ( username )
Validates the a username.
=cut
sub validUsername {
my $self = shift;
my $username = shift;
my $error = "";
if ($self->_isDuplicateUsername($username)) {
$error .= $self->error;
}
if (!$self->_isValidUsername($username)) {
$error .= $self->error;
}
$self->error($error);
return $error eq "";
}
#-------------------------------------------------------------------
=head2 www_createAccount ( method [,vars] )
Superclass method that performs general functionality for creating new accounts.
=head3 method
Auth method that the form for creating users should call
=head3 vars
Array ref of template vars from subclass
=cut
sub www_createAccount {
my $self = shift;
my $method = shift;
my $vars = shift;
my $i18n = WebGUI::International->new($self->session);
$vars->{title} = $i18n->get(54);
$vars->{'create.form.header'}
= WebGUI::Form::formHeader($self->session)
. WebGUI::Form::Hidden->new($self->session,{"name"=>"op","value"=>"auth"})->toHtml
. WebGUI::Form::Hidden->new($self->session,{"name"=>"method","value"=>$method})->toHtml
;
# User Defined Options
my $userInvitation = $self->session->setting->get('inboxInviteUserEnabled');
$vars->{'create.form.profile'} = [];
foreach my $field (@{WebGUI::ProfileField->getRegistrationFields($self->session)}) {
my $id = $field->getId;
my $label = $field->getLabel;
my $required = $field->isRequired;
my $properties = {};
if ($required) {
my $fieldValue = $self->session->form->process($field->getId,$field->get("fieldType"));
$properties->{extras} = $self->getExtrasStyle($fieldValue);
}
my $formField;
# Get the default email from the invitation
if ($field->get('fieldName') eq "email" && $userInvitation ) {
my $code = $self->session->form->get('code')
|| $self->session->form->get('uniqueUserInvitationCode');
my $defaultValue
= $self->session->db->quickScalar(
'SELECT email FROM userInvitations WHERE inviteId=?',
[$code]
);
$vars->{'create.form.header'} .= WebGUI::Form::hidden($self->session, {name=>"uniqueUserInvitationCode", value=>$code});
$formField = $field->formField($properties, undef, undef, undef, $defaultValue);
}
else {
$formField = $field->formField($properties);
}
# Old-style field loop.
push @{$vars->{'create.form.profile'}}, {
'profile.formElement' => $formField,
'profile.formElement.label' => $label,
'profile.required' => $required,
};
# Individual field template vars.
my $prefix = 'create.form.profile.'.$id.'.';
$vars->{ $prefix . 'formElement' } = $formField;
$vars->{ $prefix . 'formElement.label' } = $label;
$vars->{ $prefix . 'required' } = $required;
}
$vars->{'create.form.submit'} = WebGUI::Form::submit($self->session,{});
$vars->{'create.form.footer'} = WebGUI::Form::formFooter($self->session,);
$vars->{'login.url'} = $self->session->url->page('op=auth;method=init');
$vars->{'login.label'} = $i18n->get(58);
return WebGUI::Asset::Template->newById($self->session,$self->getCreateAccountTemplateId)->process($vars);
}
deprecate createAccount => 'www_createAccount';
#-------------------------------------------------------------------
=head2 www_createAccountSave ( username,properties [,password,profile] )
Superclass method that performs general functionality for saving new accounts. Based
on various settings and user actions, it may return output that should be displayed
to the user.
=head3 username
Username for the account being created
=head3 properties
Properties from the subclass that should be saved as authentication parameters
=head3 password
Password entered by the user. This is only used in for sending the user a notification by email of his/her username/password
=head3 profile
Hashref of profile values returned by the function WebGUI::User::validateProfileDataFromForm($fields);
=cut
sub www_createAccountSave {
my $self = shift;
my $username = $_[0];
my $properties = $_[1];
my $password = $_[2];
my $profile = $_[3] || {};
my $i18n = WebGUI::International->new($self->session);
my $u = WebGUI::User->new($self->session,"new");
$self->user($u);
my $userId = $u->userId;
$u->username($username);
$u->authMethod($self->authMethod);
if (!$profile->{'language'} && $self->session->scratch->getLanguageOverride) {
$profile->{'language'} = $self->session->scratch->getLanguageOverride;
}
$u->karma($self->session->setting->get("karmaPerLogin"),"Login","Just for logging in.") if ($self->session->setting->get("useKarma"));
$u->update($profile);
$self->update($properties);
my $address = {};
my $address_mappings = WebGUI::Shop::AddressBook->getProfileAddressMappings;
foreach my $fieldId (keys %$profile) {
#set the shop address fields
my $address_key = $address_mappings->{$fieldId};
$address->{$address_key} = $profile->{$fieldId} if ($address_key);
}
$self->session->user({user=>$u});
#Update or create and update the shop address
if ( keys %$address ) {
$address->{'isProfile' } = 1;
#Get home address only mappings to avoid creating addresses with just firstName, lastName, email
my %home_address_map = %{$address_mappings};
foreach my $exclude ( qw{ firstName lastName email } ) {
delete $home_address_map{$exclude};
}
#Add the profile address for the user if there are homeAddress fields
if( grep { $address->{$_} } values %home_address_map ) {
#Create the address book for the user
my $addressBook = WebGUI::Shop::AddressBook->newByUserId($self->session,$userId);
$address->{label} = "Profile Address";
my $new_address = $addressBook->addAddress($address);
#Set this as the default address if one doesn't already exist
$addressBook->update( { defaultAddressId => $new_address->getId } );
}
}
if ($self->getSetting("sendWelcomeMessage")){
my $var;
$var->{welcomeMessage} = $self->getSetting("welcomeMessage");
$var->{newUser_username} = $username;
$var->{newUser_password} = $password;
my $message = WebGUI::Asset::Template->newById($self->session,$self->getSetting('welcomeMessageTemplate'))->process($var);
WebGUI::Macro::process($self->session,\$message);
WebGUI::Inbox->new($self->session)->addMessage({
message => $message,
subject => $i18n->get(870),
userId => $self->userId,
status => 'completed',
});
}
$self->_logLogin($userId,"success");
if ($self->session->setting->get("runOnRegistration")) {
WebGUI::Workflow::Instance->create($self->session, {
workflowId=>$self->session->setting->get("runOnRegistration"),
methodName=>"new",
className=>"WebGUI::User",
parameters=>$self->session->user->userId,
priority=>1
})->start;
}
##Finalize the record in the user invitation table.
my $inviteId = $self->session->form->get('uniqueUserInvitationCode');
if ($inviteId) {
$self->session->db->setRow('userInvitations','inviteId',{
inviteId => $inviteId,
newUserId => $u->userId,
dateCreated => WebGUI::DateTime->new($self->session, time)->toMysqlDate,
});
#Get the invite record
my $inviteRecord = $self->session->db->getRow('userInvitations','inviteId',$inviteId);
#Get the user
my $inviteUser = WebGUI::User->new($self->session,$inviteRecord->{userId});
#Automatically add the friend that invited the user and vice versa if the friend has friends enabled
if($inviteUser->acceptsFriendsRequests($u)) {
my $friends = WebGUI::Friends->new($self->session,$u);
$friends->add([$inviteUser->userId]);
}
}
# If we have something to do after login, do it
if ( $self->session->setting->get( 'showMessageOnLogin' ) ) {