/
Register.pm
executable file
·1754 lines (1488 loc) · 53.6 KB
/
Register.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
# See bottom of file for license and copyright information
=begin TML
---+ package Foswiki::UI::Register
User registration handling.
=cut
package Foswiki::UI::Register;
use strict;
use warnings;
use Assert;
use Error qw( :try );
use Foswiki ();
use Foswiki::OopsException ();
use Foswiki::Sandbox ();
use Foswiki::UI ();
# Keys from the user data that should *not* be included in
# the user topic.
my %SKIPKEYS = (
'Photo' => 1,
'WikiName' => 1,
'LoginName' => 1,
'Password' => 1,
'Confirm' => 1,
'Email' => 1,
'AddToGroups' => 1
);
my @requiredFields = qw(WikiName FirstName LastName Email);
=begin TML
---++ StaticMethod register_cgi( $session )
=register= command handler.
This method is designed to be
invoked via the =UI::run= method.
=cut
sub register_cgi {
my $session = shift;
# absolute URL context for email generation
$session->enterContext('absolute_urls');
my $needApproval = 0;
# Register -> Verify -> Approve -> Finish
# NB. bulkRegister invoked from ManageCgiScript.
my $query = $session->{request};
my $action = $query->param('action') || '';
if ( $action ne 'verify'
&& $query
&& $query->method()
&& uc( $query->method() ) ne 'POST' )
{
throw Foswiki::OopsException(
'attention',
web => $session->{webName},
topic => $session->{topicName},
def => 'post_method_only',
params => ['register']
);
}
if ( $action eq 'register' ) {
if ( !$session->inContext('registration_supported') ) {
throw Foswiki::OopsException(
'attention',
web => $session->{webName},
topic => $session->{topicName},
def => 'registration_not_supported'
);
}
if ( !$Foswiki::cfg{Register}{EnableNewUserRegistration} ) {
throw Foswiki::OopsException(
'attention',
web => $session->{webName},
topic => $session->{topicName},
def => 'registration_disabled'
);
}
Foswiki::UI::checkValidationKey($session);
registerAndNext($session);
}
elsif ( $action eq 'verify' ) {
verifyEmailAddress($session);
if ($needApproval) {
throw Error::Simple('Approval code has not been written!');
}
_complete($session);
}
elsif ( $action eq 'resetPassword' ) {
if ( !$session->inContext("passwords_modifyable") ) {
throw Foswiki::OopsException(
'attention',
web => $session->{webName},
topic => $session->{topicName},
def => 'passwords_disabled'
);
}
# resetpasswd calls checkValidationKey - don't check it here
require Foswiki::UI::Passwords;
Foswiki::UI::Passwords::resetpasswd($session);
}
elsif ( $action eq 'approve' ) {
_complete($session);
}
else {
throw Foswiki::OopsException(
'attention',
web => $session->{webName},
topic => $session->{topicName},
def => 'unrecognized_action'
);
}
$session->leaveContext('absolute_urls');
# Output of register:
# UnsavedUser, accessible by username.$verificationCode
# Output of reset password:
# unaffected user, accessible by username.$verificationCode
# Output of verify:
# UnsavedUser, accessible by username.$approvalCode (only sent
# to administrator)
# Output of approve:
# RegisteredUser, all related UnsavedUsers deleted
}
my $b1 = "\t* ";
my $b2 = "\t$b1";
=begin TML
---++ StaticMethod bulkRegister($session)
Called by ManageCgiScript::bulkRegister (requires authentication) with
topic = the page with the entries on it.
=cut
sub bulkRegister {
my $session = shift;
my $user = $session->{user};
my $topic = $session->{topicName};
my $web = $session->{webName};
my $userweb = $Foswiki::cfg{UsersWebName};
my $query = $session->{request};
# absolute URL context for email generation
$session->enterContext('absolute_urls');
my $settings = {};
# This gets set from the value in the BulkRegistrations topic
$settings->{doOverwriteTopics} = $query->param('OverwriteHomeTopics') || 0;
unless ( $session->{users}->isAdmin($user) ) {
throw Foswiki::OopsException(
'accessdenied',
status => 403,
def => 'only_group',
web => $web,
topic => $topic,
params => [ $Foswiki::cfg{SuperAdminGroup} ]
);
}
# Validate
Foswiki::UI::checkValidationKey($session);
#-- Read the topic containing the table of people to be registered
my $meta = Foswiki::Meta->load( $session, $web, $topic );
my @fields;
my @data;
my $gotHdr = 0;
foreach my $line ( split( /\r?\n/, $meta->text ) ) {
# unchecked implicit untaint OK - this function is for admins only
if ( $line =~ /^\s*\|\s*(.*?)\s*\|\s*$/ ) {
if ($gotHdr) {
my $i = 0;
my %row = map { $fields[ $i++ ] => $_ } split( /\s*\|\s*/, $1 );
push( @data, \%row );
}
else {
foreach my $field ( split( /\s*\|\s*/, $1 ) ) {
$field =~ s/^[\s*]*(.*?)[\s*]*$/$1/;
push( @fields, $field );
}
$gotHdr = 1;
}
}
}
my $log = "---+ Report for Bulk Register\n";
# TODO: should check that the header row actually contains the
# required fields.
# TODO: and consider using MAKETEXT to enable translated tables.
#-- Process each row, generate a log as we go
for ( my $n = 0 ; $n < scalar(@data) ; $n++ ) {
my $row = $data[$n];
$row->{webName} = $userweb;
unless ( $row->{WikiName} ) {
$log .= "---++ Failed to register user on row $n: no !WikiName\n";
next;
}
$row->{LoginName} = $row->{WikiName} unless $row->{LoginName};
$log .= _registerSingleBulkUser( $session, \@fields, $row, $settings );
}
$log .= "----\n";
my $logWeb;
my $logTopic = Foswiki::Sandbox::untaint( $query->param('LogTopic'),
\&Foswiki::Sandbox::validateTopicName )
|| $topic . 'Result';
( $logWeb, $logTopic ) = $session->normalizeWebTopicName( '', $logTopic );
#-- Save the LogFile as designated, link back to the source topic
$meta = Foswiki::Meta->new( $session, $logWeb, $logTopic, $log );
unless ( $meta->haveAccess('CHANGE') ) {
throw Foswiki::AccessControlException( 'CHANGE', $session->{user},
$logWeb, $logTopic, $Foswiki::Meta::reason );
}
$meta->put( 'TOPICPARENT', { name => $web . '.' . $topic } );
$meta->save();
$session->leaveContext('absolute_urls');
my $nurl = $session->getScriptUrl( 1, 'view', $web, $logTopic );
$session->redirect($nurl);
}
# Register a single user during a bulk registration process
sub _registerSingleBulkUser {
my ( $session, $fieldNames, $row, $settings ) = @_;
ASSERT($row) if DEBUG;
my $doOverwriteTopics = defined $settings->{doOverwriteTopics}
|| throw Error::Simple('No doOverwriteTopics');
my $log = "---++ Registering $row->{WikiName}\n";
#-- call to the registrationHandler (to amend fields) should
# really happen in here.
#-- Ensure every required field exists
# NB. LoginName is OPTIONAL
if ( _missingElements( $fieldNames, \@requiredFields ) ) {
$log .=
$b1
. join( ' ', map { $_ . ' : ' . $row->{$_} } @$fieldNames )
. ' does not contain the full set of required fields '
. join( ' ', @requiredFields ) . "\n";
return ( undef, $log );
}
try {
#SMELL: Field Validations
# foreach my $field ( %$row ) {
# - validate with Users::validateRegistrationField( $field, $row->{$field} );
# - catch any errors to log
# Note, do this HERE and not in _validateRegistration.
# CGI registration validates earlier in _getDataFromQuery()
_validateRegistration( $session, $row, 0 );
}
catch Foswiki::OopsException with {
my $e = shift;
$log .= '<blockquote>' . $e->stringify($session) . "</blockquote>\n";
return $log . "$b1 Registration failed\n";
};
#-- Generation of the page is done from the {form} subhash,
# so align the two
$row->{form} = _makeFormFieldOrderMatch( $fieldNames, $row );
my $users = $session->{users};
try {
# Add the user to the user management system. May throw an exception
my $cUID = $users->addUser(
$row->{LoginName}, $row->{WikiName},
$session->inContext("passwords_modifyable") ? $row->{Password} : undef,
$row->{Email}
);
$log .=
"$b1 $row->{WikiName} has been added to the password and user mapping managers\n";
if ( $settings->{doOverwriteTopics}
|| !$session->topicExists( $row->{webName}, $row->{WikiName} ) )
{
$log .= _createUserTopic( $session, $row );
}
else {
$log .= "$b1 Not writing user topic $row->{WikiName}\n";
}
$users->setEmails( $cUID, $row->{Email} );
$session->logEvent( 'bulkregister',
$row->{webName} . '.' . $row->{WikiName},
$row->{Email}, $row->{WikiName} );
}
catch Error::Simple with {
my $e = shift;
$log .= "$b1 Failed to add user: " . $e->stringify() . "\n";
};
#if ($Foswiki::cfg{EmailUserDetails}) {
# If you want it, write it.
# sendEmail($session, 'registernotifybulk', $data );
# $log .= $b1.' Password email disabled\n';
#}
return $log;
}
# ensures all named fields exist in hash
# returns array containing any that are missing
sub _missingElements {
my ( $presentArrRef, $requiredArrRef ) = @_;
my %present;
my @missing;
$present{$_} = 1 for @$presentArrRef;
foreach my $required (@$requiredArrRef) {
if ( !$present{$required} ) {
push @missing, $required;
}
}
return @missing;
}
# rearranges the fields in $data so that they match settings
# returns a new ordered form
sub _makeFormFieldOrderMatch {
my ( $fieldNames, $data ) = @_;
my @form = ();
foreach my $field (@$fieldNames) {
push @form, { name => $field, value => $data->{$field} };
}
return \@form;
}
=begin TML
---++ StaticMethod registerAndNext($session)
This is called when action = register. It either completes the registration,
or redirects to verification, depending on the configuration.
=cut
sub registerAndNext {
my ($session) = @_;
_innerRegister($session);
if ( $Foswiki::cfg{Register}{NeedVerification} ) {
_requireVerification($session);
}
else {
_complete($session);
}
}
# Get registration data from the CGI query and validate it
sub _innerRegister {
my ($session) = @_;
my $query = $session->{request};
my $data = _getDataFromQuery( $session->{users}, $query, $query->param() );
$data->{webName} = $session->{webName};
my $oldName = $data->{WikiName};
$data->{WikiName} =
Foswiki::Sandbox::untaint( $data->{WikiName},
\&Foswiki::Sandbox::validateTopicName );
unless ( $data->{WikiName} ) {
$session->logger->log( 'warning',
"Registration rejected: validateTopicName failed for $oldName"
);
throw Foswiki::OopsException(
'attention',
def => 'bad_wikiname',
web => $data->{webName},
topic => $session->{topicName},
params => [$oldName]
);
}
_validateRegistration( $session, $data, 1 );
}
# Generate a registration record, and mail the registrant with the code.
# Redirects the browser to the confirmation screen.
sub _requireVerification {
my ($session) = @_;
my $query = $session->{request};
my $topic = $session->{topicName};
my $web = $session->{webName};
my $data = _getDataFromQuery( $session->{users}, $query, $query->param() );
my $oldName = $data->{WikiName};
$data->{WikiName} =
Foswiki::Sandbox::untaint( $data->{WikiName},
\&Foswiki::Sandbox::validateTopicName );
unless ( $data->{WikiName} ) {
$session->logger->log( 'warning',
"Verification rejected: validateTopicName failed for $oldName"
);
throw Foswiki::OopsException(
'attention',
def => 'bad_wikiname',
web => $data->{webName},
topic => $session->{topicName},
params => [$oldName]
);
}
$data->{LoginName} ||= $data->{WikiName};
$data->{webName} = $web;
$data->{VerificationCode} = $data->{WikiName} . '.' . int( rand(99999999) );
#SMELL: used for Register unit tests
$session->{DebugVerificationCode} = $data->{VerificationCode};
require Data::Dumper;
my $file = _codeFile( $data->{VerificationCode} );
my $F;
open( $F, '>', $file )
or throw Error::Simple( 'Failed to open file: ' . $! );
print $F '# Verification code', "\n";
# SMELL: wierd jiggery-pokery required, otherwise Data::Dumper screws
# up the form fields when it saves. Perl bug? Probably to do with
# chucking around arrays, instead of references to them.
my $form = $data->{form};
$data->{form} = undef;
print $F Data::Dumper->Dump( [ $data, $form ], [ 'data', 'form' ] );
$data->{form} = $form;
close($F);
$session->logEvent( 'regstart',
$Foswiki::cfg{UsersWebName} . '.' . $data->{WikiName},
$data->{Email}, $data->{WikiName} );
my $em = $data->{Email};
if ( $Foswiki::cfg{EnableEmail} ) {
my $err = sendEmail( $session, 'registerconfirm', $data );
if ($err) {
$session->logger->log( 'warning',
"Registration rejected: registration_mail_failed - Email: $em, Error $err"
);
throw Foswiki::OopsException(
'attention',
def => 'registration_mail_failed',
web => $data->{webName},
topic => $topic,
params => [ $em, $err ]
);
}
}
else {
my $err = $session->i18n->maketext(
'Email has been disabled for this Foswiki installation');
throw Foswiki::OopsException(
'attention',
def => 'send_mail_error',
web => $data->{webName},
topic => $topic,
params => [ $em, $err ]
);
}
throw Foswiki::OopsException(
'attention',
status => 200,
def => 'confirm',
web => $data->{webName},
topic => $topic,
params => [$em]
);
}
=begin TML
---++ StaticMethod verifyEmailAddress($session)
This is invoked on receipt of the activation password. It reloads a
pending registration, and verifies the email address in it.
=cut
sub verifyEmailAddress {
my ($session) = @_;
my $code = $session->{request}->param('code');
unless ($code) {
throw Error::Simple('verifyEmailAddress: no verification code!');
}
my $data = _loadPendingRegistration( $session, $code );
if ( !exists $data->{Email} ) {
throw Error::Simple('verifyEmailAddress: no email address!');
}
}
=begin TML
---++ StaticMethod deleteUser($session)
CGI function that deletes the current user
Renames the *current* user's topic (with renaming all links) and
removes user entry from passwords.
=cut
sub deleteUser {
my $session = shift;
my $webName = $session->{webName};
my $topic = $session->{topicName};
my $query = $session->{request};
my $cUID = $session->{user};
my $password = $query->param('password');
# check if user entry exists
my $users = $session->{users};
if ( !$users->userExists($cUID) ) {
throw Foswiki::OopsException(
'attention',
web => $webName,
topic => $topic,
def => 'not_a_user',
params => [ $session->{users}->getWikiName($cUID) ]
);
}
#check to see it the user we are trying to remove is a member of a group.
#initially we refuse to delete the user
#in a later implementation we will remove the from the group
#(if Access.pm implements it..)
my $git = $users->eachMembership($cUID);
if ( $git->hasNext() ) {
my $list = '';
while ( $git->hasNext() ) {
$list .= ' ' . $git->next();
}
throw Foswiki::OopsException(
'attention',
web => $webName,
topic => $topic,
def => 'in_a_group',
params => [ $session->{users}->getWikiName($cUID), $list ]
);
}
# Check the validation key
Foswiki::UI::checkValidationKey($session);
unless (
$users->checkPassword(
$session->{users}->getLoginName($cUID), $password
)
)
{
throw Foswiki::OopsException(
'attention',
web => $webName,
topic => $topic,
def => 'wrong_password'
);
}
$users->removeUser($cUID);
throw Foswiki::OopsException(
'attention',
status => 200,
def => 'remove_user_done',
web => $webName,
topic => $topic,
params => [ $users->getWikiName($cUID) ]
);
}
=begin TML
---++ StaticMethod addUserToGroup($session)
adds users to a group
* groupname parameter must a a single groupname (group does not
have to exist)
* username can be a single login/wikiname/(!cuid?), a URLParam
list, or a comma separated list.
=cut
sub addUserToGroup {
my $session = shift;
my $query = $session->{request};
my $topic = $session->{topicName};
my $web = $session->{webName};
my $user = $session->{user};
my @userNames = $query->param('username');
my $groupName = $query->param('groupname');
my $create = Foswiki::isTrue( $query->param('create'), 0 );
if ( !$groupName or $groupName eq '' ) {
my $userNames = scalar @userNames ? join( ',', @userNames ) : '';
throw Foswiki::OopsException(
'attention',
def => 'no_group_specified_for_add_to_group',
web => $web,
topic => $topic,
params => [$userNames]
);
}
Foswiki::UI::checkValidationKey($session);
if ( ( $#userNames < 0 )
or ( $userNames[0] eq '' ) )
{
# if $create is set, and there are no users in the list, and the group exists
# then we're trying to upgrade the user topic.
# I'm not sure what other mappers might make of this..
if ( $create and Foswiki::Func::isGroup($groupName) ) {
try {
$session->{users}->addUserToGroup( undef, $groupName, $create );
}
catch Error::Simple with {
my $e = shift;
# Log the error
$session->logger->log( 'warning',
"catch: Failed to upgrade $groupName " . $e->stringify() );
};
throw Foswiki::OopsException(
'attention',
status => 200,
def => 'group_upgraded',
web => $web,
topic => $topic,
params => [$groupName]
);
}
}
if ( !Foswiki::Func::isGroup($groupName)
&& !$create )
{
throw Foswiki::OopsException(
'attention',
def => 'no_group_and_no_create',
web => $web,
topic => $topic,
);
}
if ( $#userNames == 0 ) {
@userNames = split( /,\s*/, $userNames[0] );
}
# If a users create a new group, make sure he is in the group
# Otherwise he will not be able to touch the group after the
# first user is added because this code saves once per user - and the
# group will be restricted to that group. It is also good to prevent the
# user from shooting himself in the foot.
# He can afterwards remove himself if needed
# We make an exception if you are an admin as they can always edit anything
if ( !Foswiki::Func::isGroup($groupName)
and !$session->{users}->isAdmin($user)
and $create )
{
unshift( @userNames, $session->{users}->getLoginName($user) );
}
my @failed;
my @succeeded;
push @userNames, '<none>' if ( scalar @userNames == 0 );
foreach my $u (@userNames) {
$u =~ s/^\s+//;
$u =~ s/\s+$//;
# We strip off any usersweb prefix
$u =~ s/^($Foswiki::cfg{UsersWebName}|%USERSWEB%|%MAINWEB%)\.//;
#next if ( $u eq '' );
$u = '' if ( $u eq '<none>' );
next
if ( Foswiki::Func::isGroup($groupName)
&& Foswiki::Func::isGroupMember( $groupName, $u, { expand => 0 } )
);
try {
$u = $session->{users}->validateRegistrationField( 'username', $u );
$session->{users}->addUserToGroup( $u, $groupName, $create );
push( @succeeded, $u );
}
catch Error::Simple with {
my $e = shift;
my $mess = $e->stringify();
$mess =~ s/ at .*$//s;
push( @failed, "$u : $mess" );
# Log the error
$session->logger->log( 'warning', $e->stringify() );
};
}
if ( @failed || !@succeeded ) {
$session->logger->log( 'warning',
"failed: " . scalar @failed . " Succeeded " . scalar @succeeded );
throw Foswiki::OopsException(
'attention',
web => $web,
topic => $topic,
def => 'problem_adding_to_group',
params => [ join( ', ', @failed ), $groupName ]
);
}
my $url = $session->redirectto();
unless ( $url ) {
throw Foswiki::OopsException(
'attention',
status => 200,
def => 'added_users_to_group',
web => $web,
topic => $topic,
params => [ join( ', ', @succeeded ), $groupName ]
);
} else {
$session->redirect( $url );
}
}
=begin TML
---++ StaticMethod removeUserFromGroup($session)
Removes users from a group
* groupname parameter must a a single groupname (group does not have
to exist)
* username can be a single login/wikiname/(cuid?), a URLParam list,
or a comma separated list.
=cut
sub removeUserFromGroup {
my $session = shift;
my $query = $session->{request};
my $topic = $session->{topicName};
my $web = $session->{webName};
my $user = $session->{user};
my @userNames = $query->param('username');
my $groupName = $query->param('groupname');
if ( ( $#userNames < 0 )
or ( $userNames[0] eq '' ) )
{
throw Foswiki::OopsException( 'attention',
def => 'no_users_to_remove_from_group' );
}
if ( $#userNames == 0 ) {
@userNames = split( /,\s+/, $userNames[0] );
}
if ( !$groupName or $groupName eq '' ) {
throw Foswiki::OopsException( 'attention',
def => 'no_group_specified_for_remove_from_group' );
}
unless ( Foswiki::Func::isGroup($groupName) ) {
throw Foswiki::OopsException( 'attention',
def => 'problem_removing_from_group' );
}
# Validate
Foswiki::UI::checkValidationKey($session);
my @failed;
my @succeeded;
foreach my $u (@userNames) {
$u =~ s/^\s+//;
$u =~ s/\s+$//;
next if ( $u eq '' );
try {
Foswiki::Func::removeUserFromGroup( $u, $groupName );
push( @succeeded, $u );
}
catch Error::Simple with {
my $e = shift;
my $mess = $e->stringify();
$mess =~ s/ at .*$//s;
push( @failed, "$u: $mess" );
# Log the error
$session->logger->log( 'warning', $e->stringify() );
};
}
if (@failed) {
throw Foswiki::OopsException(
'attention',
web => $web,
topic => $topic,
def => 'problem_removing_from_group',
params => [ join( ', ', @failed ), $groupName ]
);
}
my $url = $session->redirectto();
unless ( $url ) {
throw Foswiki::OopsException(
'attention',
status => 200,
def => 'removed_users_from_group',
web => $web,
topic => $topic,
params => [ join( ', ', @succeeded ), $groupName ]
);
} else {
$session->redirect( $url );
}
}
# Complete a registration (commit it to the DB)
sub _complete {
my ($session) = @_;
my $topic = $session->{topicName};
my $web = $session->{webName};
my $query = $session->{request};
my $code = $query->param('code');
my $data;
if ( $Foswiki::cfg{Register}{NeedVerification} ) {
$data = _loadPendingRegistration( $session, $code );
_clearPendingRegistrationsForUser($code);
}
else {
$data = _getDataFromQuery( $session->{users}, $query, $query->param() );
$data->{webName} = $web;
}
$data->{WikiName} =
Foswiki::Sandbox::untaint( $data->{WikiName},
\&Foswiki::Sandbox::validateTopicName );
throw Error::Simple('Bad WikiName') unless $data->{WikiName};
if ( !exists $data->{LoginName} ) {
if ( $Foswiki::cfg{Register}{AllowLoginName} ) {
# This should have been populated
throw Error::Simple('no LoginName after reload');
}
$data->{LoginName} ||= $data->{WikiName};
}
my $users = $session->{users};
try {
unless ( !$session->inContext("passwords_modifyable") ||
defined( $data->{Password} ) ) {
# SMELL: should give consideration to disabling
# $Foswiki::cfg{Register}{HidePasswd} though that may
# reduce the conf options an admin has..
# OR, a better option would be that the rego email would
# thus point the user to the resetPasswd url.
$data->{Password} = Foswiki::Users::randomPassword();
#add data to the form so it can go out in the registration emails.
push(
@{ $data->{form} },
{ name => 'Password', value => $data->{Password} }
);
}
my $cUID = $users->addUser(
$data->{LoginName}, $data->{WikiName},
$session->inContext("passwords_modifyable") ? $data->{Password} : undef,
$data->{Email}
);
my $log = _createUserTopic( $session, $data );
$users->setEmails( $cUID, $data->{Email} );
# convert to rego agent user copied from
# _writeRegistrationDetailsToTopic
my $safe = $session->{user};
my $regoAgent = $session->{user};
my $enableAddToGroup = 1;
if ( Foswiki::Func::isGuest($regoAgent) ) {
$session->{user} =
$session->{users}->getCanonicalUserID(
$Foswiki::cfg{Register}{RegistrationAgentWikiName} );
$regoAgent = $session->{user};
# SECURITY ISSUE:
# When upgrading an existing Wiki, the RegistrationUser is
# in the AdminGroup. Thus newly registering users would be
# able to join the AdminGroup. So disable the
# AddUserToGroupOnRegistration if the agent is still admin :(
$enableAddToGroup = !$session->{users}->isAdmin($regoAgent);
if ( !$enableAddToGroup ) {
# TODO: should really tell the user too?
$session->logger->log( 'warning',
'Registration failed: can\'t add user to groups ('
. $data->{AddToGroups}
. ' because '
. $Foswiki::cfg{Register}{RegistrationAgentWikiName}
. 'is in the '
. $Foswiki::cfg{SuperAdminGroup} );
}
}
my @addedTo;
if ( ($enableAddToGroup) and ( $data->{AddToGroups} ) ) {
foreach my $groupName ( split( /,/, $data->{AddToGroups} ) ) {
$session->{user} = $regoAgent;
try {
$users->addUserToGroup( $cUID, $groupName );
push @addedTo, $groupName;
}
catch Error::Simple with {
my $e = shift;
$session->logger->log( 'warning',
"Registration: Failure adding $cUID to $groupName" );
}
finally {
$session->{user} = $safe;
};
}
}
$data->{AddToGroups} = join( ',', @addedTo );
}
catch Foswiki::OopsException with {
my $e = shift;
$users->removeUser( $data->{LoginName}, $data->{WikiName} )
if ( $users->userExists( $data->{WikiName} ) );
$e->throw();
#throw Foswiki::OopsException ( @_ ); # Propagate
}
catch Error::Simple with {
my $e = shift;
$users->removeUser( $data->{LoginName}, $data->{WikiName} )
if ( $users->userExists( $data->{WikiName} ) );
# Log the error
$session->logger->log( 'warning',
'Registration failed: ' . $e->stringify() );
throw Foswiki::OopsException(
'attention',
web => $data->{webName},
topic => $topic,
def => 'problem_adding',
params => [ $data->{WikiName}, $e->stringify() ]
);
};
# Plugin to do some other post processing of the user.
# for legacy, (callback to set cookies - now should use LoginHandler)
$session->{plugins}