-
Notifications
You must be signed in to change notification settings - Fork 38
/
TopicUserMapping.pm
executable file
·1836 lines (1385 loc) · 52.7 KB
/
TopicUserMapping.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::Users::TopicUserMapping @isa Foswiki::UserMapping');
use
The User mapping is the process by which Foswiki maps from a username (a login name)
to a wikiname and back. It is also where groups are defined.
By default Foswiki maintains user topics and group topics in the %MAINWEB% that
define users and group. These topics are
* !WikiUsers - stores a mapping from usernames to Wiki names
* !WikiName - for each user, stores info about the user
* !GroupNameGroup - for each group, a topic ending with "Group" stores a list of users who are part of that group.
Many sites will want to override this behaviour, for example to get users and groups from a corporate database.
This class implements the basic Foswiki behaviour using topics to store users,
but is also designed to be subclassed so that other services can be used.
Subclasses should be named 'XxxxUserMapping' so that configure can find them.
=cut
package Foswiki::Users::TopicUserMapping;
use strict;
use warnings;
use Foswiki::UserMapping ();
our @ISA = ('Foswiki::UserMapping');
use Assert;
use Error qw( :try );
use Foswiki::ListIterator ();
use Foswiki::Func ();
use Foswiki::Time ();
use Foswiki::Users ();
#use Monitor;
#Monitor::MonitorMethod('Foswiki::Users::TopicUserMapping');
=begin TML
---++ ClassMethod new ($session, $impl)
Constructs a new user mapping handler of this type, referring to $session
for any required Foswiki services.
=cut
# The null mapping name is reserved for Foswiki for backward-compatibility.
# We declare this as a global variable so we can override it during testing.
our $FOSWIKI_USER_MAPPING_ID = '';
#our $FOSWIKI_USER_MAPPING_ID = 'TestMapping_';
sub new {
my ( $class, $session ) = @_;
my $this = $class->SUPER::new( $session, $FOSWIKI_USER_MAPPING_ID );
my $implPasswordManager = $Foswiki::cfg{PasswordManager};
$implPasswordManager = 'Foswiki::Users::Password'
if ( $implPasswordManager eq 'none' );
eval "require $implPasswordManager";
die $@ if $@;
$this->{passwords} = $implPasswordManager->new($session);
unless ( $this->{passwords}->readOnly() ) {
$this->{session}->enterContext('passwords_modifyable');
}
#SMELL: and this is a second user object
#TODO: combine with the one in Foswiki::Users
#$this->{U2L} = {};
$this->{L2U} = {};
$this->{U2W} = {};
$this->{W2U} = {};
$this->{eachGroupMember} = {};
$this->{singleGroupMembers} = ();
return $this;
}
=begin TML
---++ ObjectMethod finish()
Break circular references.
=cut
# Note to developers; please undef *all* fields in the object explicitly,
# whether they are references or not. That way this method is "golden
# documentation" of the live fields in the object.
sub finish {
my $this = shift;
$this->{passwords}->finish() if $this->{passwords};
undef $this->{L2U};
undef $this->{U2W};
undef $this->{W2U};
undef $this->{passwords};
undef $this->{eachGroupMember};
undef $this->{singleGroupMembers};
undef $this->{isGroup};
$this->SUPER::finish();
}
=begin TML
---++ ObjectMethod supportsRegistration () -> false
return 1 if the UserMapper supports registration (ie can create new users)
=cut
sub supportsRegistration {
return 1;
}
=begin TML
---++ ObjectMethod handlesUser ( $cUID, $login, $wikiname) -> $boolean
Called by the Foswiki::Users object to determine which loaded mapping
to use for a given user.
The user can be identified by any of $cUID, $login or $wikiname. Any of
these parameters may be undef, and they should be tested in order; cUID
first, then login, then wikiname. This mapping is special - for backwards
compatibility, it assumes responsibility for _all_ non BaseMapping users.
If you're needing to mix the TopicUserMapping with other mappings,
define $this->{mapping_id} = 'TopicUserMapping_';
=cut
sub handlesUser {
my ( $this, $cUID, $login, $wikiname ) = @_;
if ( defined $cUID && !length( $this->{mapping_id} ) ) {
# Handle all cUIDs if the mapping ID is not defined
return 1;
}
else {
# Used when (if) TopicUserMapping is subclassed
return 1 if ( defined $cUID && $cUID =~ m/^($this->{mapping_id})/ );
}
# Check the login id to see if we know it
return 1 if ( $login && $this->_userReallyExists($login) );
# Or the wiki name
if ($wikiname) {
$this->_loadMapping(); # Sorry Sven, has to be done
return 1 if defined $this->{W2U}->{$wikiname};
}
return 0;
}
=begin TML
---++ ObjectMethod login2cUID ($login, $dontcheck) -> $cUID
Convert a login name to the corresponding canonical user name. The
canonical name can be any string of 7-bit alphanumeric and underscore
characters, and must correspond 1:1 to the login name.
(undef on failure)
(if dontcheck is true, return a cUID for a nonexistant user too.
This is used for registration)
=cut
sub login2cUID {
my ( $this, $login, $dontcheck ) = @_;
unless ($dontcheck) {
return unless ( _userReallyExists( $this, $login ) );
}
return $this->{mapping_id} . Foswiki::Users::mapLogin2cUID($login);
}
=begin TML
---++ ObjectMethod getLoginName ($cUID) -> login
Converts an internal cUID to that user's login
(undef on failure)
=cut
sub getLoginName {
my ( $this, $cUID ) = @_;
ASSERT($cUID) if DEBUG;
my $login = $cUID;
#can't call userExists - its recursive
#return unless (userExists($this, $user));
# Remove the mapping id in case this is a subclass
$login =~ s/$this->{mapping_id}// if $this->{mapping_id};
use bytes;
# Reverse the encoding used to generate cUIDs in login2cUID
# use bytes to ignore character encoding
$login =~ s/_([0-9a-f][0-9a-f])/chr(hex($1))/gei;
no bytes;
return unless _userReallyExists( $this, $login );
return unless ( $cUID eq $this->login2cUID($login) );
# Validated
return Foswiki::Sandbox::untaintUnchecked($login);
}
# test if the login is in the WikiUsers topic, or in the password file
# depending on the AllowLoginNames setting
sub _userReallyExists {
my ( $this, $login ) = @_;
if ( $Foswiki::cfg{Register}{AllowLoginName}
|| $Foswiki::cfg{PasswordManager} eq 'none' )
{
# need to use the WikiUsers file
$this->_loadMapping();
return 1 if ( defined( $this->{L2U}->{$login} ) );
}
if ( $this->{passwords}->canFetchUsers() ) {
# AllowLoginName mapping failed, maybe the user is however
# present in the Wiki managed pwd file
# can use the password file if available
my $pass = $this->{passwords}->fetchPass($login);
return unless ( defined($pass) );
return if ( $pass eq '0' ); # login invalid... (SMELL: what
# does that really mean)
return 1;
}
else {
return 0;
}
return 0;
}
=begin TML
---++ ObjectMethod addUser ($login, $wikiname, $password, $emails) -> $cUID
throws an Error::Simple
Add a user to the persistent mapping that maps from usernames to wikinames
and vice-versa. The default implementation uses a special topic called
"WikiUsers" in the users web. Subclasses will provide other implementations
(usually stubs if they have other ways of mapping usernames to wikinames).
Names must be acceptable to $Foswiki::cfg{NameFilter}
$login must *always* be specified. $wikiname may be undef, in which case
the user mapper should make one up.
This function must return a *canonical user id* that it uses to uniquely
identify the user. This can be the login name, or the wikiname if they
are all guaranteed unigue, or some other string consisting only of 7-bit
alphanumerics and underscores.
if you fail to create a new user (for eg your Mapper has read only access),
throw Error::Simple(
'Failed to add user: '.$ph->error());
=cut
sub addUser {
my ( $this, $login, $wikiname, $password, $emails ) = @_;
ASSERT($login) if DEBUG;
# SMELL: really ought to be smarter about this e.g. make a wikiword
$wikiname ||= $login;
if ( $this->{passwords}->fetchPass($login) ) {
# They exist; their password must match
unless ( $this->{passwords}->checkPassword( $login, $password ) ) {
throw Error::Simple(
$this->{session}->i18n->maketext(
'User exists in the Password Manager, and the password you provided is different from the users current password. You cannot add a user and change the password at the same time.'
)
);
}
# User exists, and the password was good.
}
else {
# add a new user
unless ( defined($password) ) {
$password = Foswiki::Users::randomPassword();
}
unless ( $this->{passwords}->setPassword( $login, $password ) == 1 ) {
throw Error::Simple(
$this->{session}->i18n->maketext('Failed to add user: ')
. $this->{passwords}->error() );
}
}
$this->{CACHED} = 0;
my $user = $this->_maintainUsersTopic( 'add', $login, $wikiname );
#can't call setEmails here - user may be in the process of being registered
#TODO; when registration is moved into the mapping, setEmails will happend after the createUserTOpic
#$this->setEmails( $user, $emails );
return $user;
}
=begin TML
---++ ObjectMethod _maintainUsersTopic ( $action, $login, $wikiname )
throws an Error::Simple
Add or remove a user to/from the persistent mapping that maps from usernames to wikinames
and vice-versa. The default implementation uses a special topic called
"WikiUsers" in the users web. =cut
=cut
sub _maintainUsersTopic {
my ( $this, $action, $login, $wikiname ) = @_;
my $usersTopicObject;
if (
$this->{session}->topicExists(
$Foswiki::cfg{UsersWebName},
$Foswiki::cfg{UsersTopicName}
)
)
{
# Load existing users topic
$usersTopicObject = Foswiki::Meta->load(
$this->{session},
$Foswiki::cfg{UsersWebName},
$Foswiki::cfg{UsersTopicName}
);
}
else {
return if ( $action eq 'del' );
# Construct a new users topic from the template
my $templateTopicObject =
Foswiki::Meta->load( $this->{session}, $Foswiki::cfg{SystemWebName},
'UsersTemplate' );
$usersTopicObject = Foswiki::Meta->new(
$this->{session}, $Foswiki::cfg{UsersWebName},
$Foswiki::cfg{UsersTopicName}, $templateTopicObject->text()
);
$usersTopicObject->copyFrom($templateTopicObject);
$usersTopicObject->put( "TOPICPARENT",
{ name => $Foswiki::cfg{HomeTopicName} } );
}
my $entry = " * $wikiname - ";
$entry .= $login . " - " if $login;
my $today =
Foswiki::Time::formatTime( time(), '$day $mon $year', 'gmtime' );
my $user;
# add to the mapping caches unless removing a user
unless ( $action eq 'del' ) {
$user = _cacheUser( $this, $wikiname, $login );
ASSERT($user) if DEBUG;
}
# add name alphabetically to list
# insidelist is used to see if we are before the first record or after the last
# 0 before, 1 inside, 2 after
my $insidelist = 0;
my $input = $usersTopicObject->text();
my $output = '';
foreach my $line ( split( /\r?\n/, $input || '' ) ) {
# TODO: I18N fix here once basic auth problem with 8-bit user names is
# solved
if ($entry) {
my ( $web, $name, $odate ) = ( '', '', '' );
if ( $line =~
m/^\s+\*\s($Foswiki::regex{webNameRegex}\.)?($Foswiki::regex{wikiWordRegex})\s*(?:-\s*\w+\s*)?-\s*(.*)/
)
{
$web = $1 || $Foswiki::cfg{UsersWebName};
$name = $2;
$odate = $3;
# Filter-in date format matching {DefaultDateFormat}
# The admin may have changed the format at some point of time
# so we test with a generic format that matches all 4 formats.
$odate = ''
unless $odate =~ m/^\d+[- .\/]+[A-Za-z0-9]+[- .\/]+\d+$/;
$insidelist = 1;
#print STDERR "1: Found $web.$name.$odate $insidelist\n";
}
elsif ( $line =~ m/^\s+\*\s([[:upper:]]) - / ) {
# * A - <a name="A">- - - -</a>^M
$name = $1;
$insidelist = 1;
#print STDERR "2: Found $name $insidelist\n";
}
elsif ( $insidelist == 1 ) {
# After last entry we have a blank line or some comment
# We assume no blank lines inside the list of users
# We cannot look for last after Z because Z is not the last letter
# in all alphabets
$insidelist = 2;
$name = '';
#print STDERR "3: Found $name $insidelist\n";
}
if ( ( $name && ( $wikiname le $name ) ) || $insidelist == 2 ) {
#print STDERR "4: Found $wikiname le $name || $insidelist\n";
# found alphabetical position or last record
if ( $wikiname eq $name ) {
#print STDERR "5: Found $wikiname eq $name\n";
next if ( $action eq 'del' );
# adjusting existing user - keep original registration date
$entry .= $odate;
}
else {
#print STDERR "6: Appending $today to $entry NL $line \n";
$entry .= $today . "\n" . $line;
}
# don't adjust if unchanged
return $user if ( $entry eq $line && $action eq 'add' );
$line = $entry if ( $action eq 'add' );
$entry = '';
}
}
$output .= $line . "\n";
}
if ( $entry && $action eq 'add' ) {
# brand new file - add to end
$output .= "$entry$today\n";
}
$usersTopicObject->text($output);
$this->{CACHED} = 0;
try {
$usersTopicObject->save(
author =>
# SMELL: why is this Admin and not the RegoAgent??
$this->{session}->{users}
->getCanonicalUserID( $Foswiki::cfg{AdminUserLogin} )
);
}
catch Error with {
# Failed to add user; must remove them from the password system too,
# otherwise their next registration attempt will be blocked
my $e = shift;
$this->{passwords}->removeUser($login);
throw $e;
};
return $user;
}
=begin TML
---++ ObjectMethod removeUser( $cUID ) -> $boolean
Delete the users entry. Removes the user from the password
manager and user mapping manager. Does *not* remove their personal
topics, which may still be linked.
Note that this must be called with the cUID. If any doubt, resolve the cUID
by $this->{session}->{users}->getCanonicalUserID($identity).
=cut
sub removeUser {
my ( $this, $cUID ) = @_;
my $ln = $this->getLoginName($cUID);
my $wikiname = $this->getWikiName($cUID);
$this->{passwords}->removeUser($ln) if ($ln);
# SMELL: If for some reason the login or wikiname is not found in the mapping
# Then the WikiUsers topic will not be maintained.
$this->_maintainUsersTopic( 'del', $ln, $wikiname ) if ( $ln && $wikiname );
# SMELL: does not update the internal caches,
# needs someone to implement it
# Brutal update - invalidate them all
$this->{CACHED} = 0;
$this->{L2U} = {};
$this->{U2W} = {};
$this->{W2U} = {};
$this->{eachGroupMember} = {};
$this->{singleGroupMembers} = ();
return 1;
}
=begin TML
---++ ObjectMethod getWikiName ($cUID) -> $wikiname
Map a canonical user name to a wikiname. If it fails to find a
WikiName, it will attempt to find a matching loginname, and use
an escaped version of that.
If there is no matching WikiName or LoginName, it returns undef.
=cut
sub getWikiName {
my ( $this, $cUID ) = @_;
ASSERT($cUID) if DEBUG;
ASSERT( $cUID =~ m/^$this->{mapping_id}/ ) if DEBUG;
my $wikiname;
if ( $Foswiki::cfg{Register}{AllowLoginName} ) {
$this->_loadMapping();
$wikiname = $this->{U2W}->{$cUID};
}
else {
# If the mapping isn't enabled there's no point in loading it
}
unless ($wikiname) {
$wikiname = $this->getLoginName($cUID);
if ($wikiname) {
# sanitise the generated WikiName
$wikiname =~ s/$Foswiki::cfg{NameFilter}//g;
}
}
return $wikiname;
}
=begin TML
---++ ObjectMethod userExists($cUID) -> $boolean
Determine if the user already exists or not. Whether a user exists
or not is determined by the password manager.
=cut
sub userExists {
my ( $this, $cUID ) = @_;
ASSERT($cUID) if DEBUG;
# Do this to avoid a password manager lookup
return 1 if $this->{session}->{user} && $cUID eq $this->{session}->{user};
my $loginName = $this->getLoginName($cUID);
return 0 unless defined($loginName);
return 1 if ( $loginName eq $Foswiki::cfg{DefaultUserLogin} );
# Foswiki allows *groups* to log in
return 1 if ( $this->isGroup($loginName) );
# Look them up in the password manager (can be slow).
return 1
if ( $this->{passwords}->canFetchUsers()
&& $this->{passwords}->fetchPass($loginName) );
unless ( $Foswiki::cfg{Register}{AllowLoginName}
&& $this->{passwords}->canFetchUsers() )
{
#if there is no pwd file, then its external auth
#and if AllowLoginName is also off, then the only way to know if
#the user has registered is to test for user topic?
my $wikiname = $this->{session}->{users}->getWikiName($cUID);
if (
Foswiki::Func::topicExists(
$Foswiki::cfg{UsersWebName}, $wikiname
)
)
{
return 1;
}
}
return 0;
}
=begin TML
---++ ObjectMethod eachUser () -> Foswiki::Iterator of cUIDs
See baseclass for documentation
=cut
sub eachUser {
my ($this) = @_;
$this->_loadMapping();
my @list = keys( %{ $this->{U2W} } );
my $iter = new Foswiki::ListIterator( \@list );
$iter->{filter} = sub {
# don't claim users that are handled by the basemapping
my $cUID = $_[0] || '';
my $login = $this->{session}->{users}->getLoginName($cUID);
my $wikiname = $this->{session}->{users}->getWikiName($cUID);
return !( $this->{session}->{users}->{basemapping}
->handlesUser( undef, $login, $wikiname ) );
};
return $iter;
}
=begin TML
---++ ObjectMethod eachGroupMember ($group) -> listIterator of cUIDs
See baseclass for documentation
=cut
my %expanding; # Prevents loops in nested groups
sub eachGroupMember {
my ( $this, $group, $options ) = @_;
my $expand = $options->{expand};
if ( Scalar::Util::tainted($group) ) {
$group = Foswiki::Sandbox::untaint( $group,
\&Foswiki::Sandbox::validateTopicName );
}
$expand = 1 unless ( defined $expand );
# print STDERR "eachGroupMember called for $group - expand $expand \n";
if ( !$expand && defined( $this->{singleGroupMembers}->{$group} ) ) {
# print STDERR "Returning cached unexpanded list for $group\n";
return new Foswiki::ListIterator(
$this->{singleGroupMembers}->{$group} );
}
if ( $expand && defined( $this->{eachGroupMember}->{$group} ) ) {
# print STDERR "Returning cached expanded list for $group\n";
return new Foswiki::ListIterator( $this->{eachGroupMember}->{$group} );
}
# print "Cache miss for $group expand $expand \n";
my $session = $this->{session};
my $users = $session->{users};
my $members = [];
my $singleGroupMembers = [];
# Determine if we are called recursively, either directly, or by the _expandUserList routine
unless ( ( caller(1) )[3] eq ( caller(0) )[3]
|| ( caller(2) )[3] eq ( caller(0) )[3] )
{
# print "eachGroupMember $group - TOP LEVEL \n";
%expanding = ();
}
if ( !$expanding{$group}
&& $session->topicExists( $Foswiki::cfg{UsersWebName}, $group ) )
{
$expanding{$group} = 1;
# print "Expanding $group \n";
my $groupTopicObject =
Foswiki::Meta->load( $this->{session}, $Foswiki::cfg{UsersWebName},
$group );
if ( !$expand ) {
$singleGroupMembers =
_expandUserList( $this,
$groupTopicObject->getPreference('GROUP'), 0 );
$this->{singleGroupMembers}->{$group} = $singleGroupMembers;
# print "Returning iterator for singleGroupMembers $group, members $singleGroupMembers \n";
return new Foswiki::ListIterator(
$this->{singleGroupMembers}->{$group} );
}
else {
$members =
_expandUserList( $this,
$groupTopicObject->getPreference('GROUP') );
$this->{eachGroupMember}->{$group} = $members;
}
delete $expanding{$group};
}
# print "Returning iterator for eachGroupMember $group \n";
return new Foswiki::ListIterator( $this->{eachGroupMember}->{$group} );
}
=begin TML
---++ ObjectMethod isGroup ($user) -> boolean
See baseclass for documentation
=cut
sub isGroup {
my ( $this, $user ) = @_;
return $this->{isGroup}{$user} if defined $this->{isGroup}{$user};
# Groups have the same username as wikiname as canonical name
return 1 if $user eq $Foswiki::cfg{SuperAdminGroup};
return 0 unless ( $user =~ m/Group$/ );
#actually test for the existance of this group
#TODO: SMELL: this is still a lie, because it will claim that a
#Group which the currently logged in user does _not_
#have VIEW permission for simply is non-existant.
#however, this may be desirable for security reasons.
#SMELL: this is why we should not use topicExist to test for createability...
my $iterator = $this->eachGroup();
while ( $iterator->hasNext() ) {
my $groupname = $iterator->next();
if ( $groupname eq $user ) {
$this->{isGroup}{$user} = 1;
last;
}
}
return $this->{isGroup}{$user};
}
=begin TML
---++ ObjectMethod eachGroup () -> ListIterator of groupnames
See baseclass for documentation
=cut
sub eachGroup {
my ($this) = @_;
_getListOfGroups($this);
return new Foswiki::ListIterator( \@{ $this->{groupsList} } );
}
=begin TML
---++ ObjectMethod eachMembership ($cUID) -> ListIterator of groups this user is in
See baseclass for documentation
=cut
sub eachMembership {
my ( $this, $user ) = @_;
_getListOfGroups($this);
my $it = new Foswiki::ListIterator( \@{ $this->{groupsList} } );
$it->{filter} = sub {
$this->isInGroup( $user, $_[0] );
};
return $it;
}
=begin TML
---++ ObjectMethod groupAllowsView($group) -> boolean
returns 1 if the group is able to be viewed by the current logged in user
implemented using topic VIEW permissions
=cut
sub groupAllowsView {
my $this = shift;
my $Group = shift;
my $user = $this->{session}->{user};
return 1 if $this->{session}->{users}->isAdmin($user);
$Group = Foswiki::Sandbox::untaint( $Group,
\&Foswiki::Sandbox::validateTopicName );
my ( $groupWeb, $groupName ) =
$this->{session}
->normalizeWebTopicName( $Foswiki::cfg{UsersWebName}, $Group );
# If a Group or User topic normalized somewhere else, doesn't make sense, so ignore the Webname
$groupWeb = $Foswiki::cfg{UsersWebName};
$groupName = undef
if ( not $this->{session}->topicExists( $groupWeb, $groupName ) );
return Foswiki::Func::checkAccessPermission( 'VIEW', $user, undef,
$groupName, $groupWeb );
}
=begin TML
---++ ObjectMethod groupAllowsChange($group, $cuid) -> boolean
returns 1 if the group is able to be modified by $cuid
implemented using topic CHANGE permissions
=cut
sub groupAllowsChange {
my $this = shift;
my $Group = shift;
my $user = shift;
ASSERT( defined $user ) if DEBUG;
$Group = Foswiki::Sandbox::untaint( $Group,
\&Foswiki::Sandbox::validateTopicName );
my ( $groupWeb, $groupName ) =
$this->{session}
->normalizeWebTopicName( $Foswiki::cfg{UsersWebName}, $Group );
# SMELL: Should NobodyGroup be configurable?
return 0 if $groupName eq 'NobodyGroup';
return 1 if $this->{session}->{users}->isAdmin($user);
# If a Group or User topic normalized somewhere else, doesn't make sense, so ignore the Webname
$groupWeb = $Foswiki::cfg{UsersWebName};
$groupName = undef
if ( not $this->{session}->topicExists( $groupWeb, $groupName ) );
return Foswiki::Func::checkAccessPermission( 'CHANGE', $user, undef,
$groupName, $groupWeb );
}
=begin TML
---++ ObjectMethod addToGroup( $cuid, $group, $create ) -> $boolean
adds the user specified by the cuid to the group.
If the group does not exist, it will return false and do nothing, unless the create flag is set.
cuid be a groupname which is added like it was an unknown user
=cut
sub addUserToGroup {
my ( $this, $cuid, $Group, $create ) = @_;
$Group = Foswiki::Sandbox::untaint( $Group,
\&Foswiki::Sandbox::validateTopicName );
my ( $groupWeb, $groupName ) =
$this->{session}
->normalizeWebTopicName( $Foswiki::cfg{UsersWebName}, $Group );
throw Error::Simple( $this->{session}
->i18n->maketext( 'Users cannot be added to [_1]', $Group ) )
if ( $Group eq 'NobodyGroup' || $Group eq 'BaseGroup' );
throw Error::Simple(
$this->{session}->i18n->maketext('Group names must end in Group') )
unless ( $Group =~ m/Group$/ );
# the registration code will call this function using the rego agent
my $user = $this->{session}->{user};
my $usersObj = $this->{session}->{users};
print STDERR "$user, aka("
. $usersObj->getWikiName($user)
. ") is TRYING to add $cuid aka("
. $usersObj->getWikiName($cuid)
. ") to $groupName\n"
if ( $cuid && DEBUG );
my $membersString = '';
my $allowChangeString;
my $groupTopicObject;
if ( $usersObj->isGroup($groupName) ) {
$groupTopicObject =
Foswiki::Meta->load( $this->{session}, $groupWeb, $groupName );
if ( !$groupTopicObject->haveAccess( 'CHANGE', $user ) ) {
throw Error::Simple( $this->{session}
->i18n->maketext( 'CHANGE not permitted by [_1]', $user ) );
}
$membersString = $groupTopicObject->getPreference('GROUP') || '';
my @l;
foreach my $ident ( split( /[\,\s]+/, $membersString ) ) {
$ident =~ s/^($Foswiki::cfg{UsersWebName}|%USERSWEB%|%MAINWEB%)\.//;
push( @l, $ident ) if $ident;
}
$membersString = join( ', ', @l );
if ( $create and !defined($cuid) ) {
#upgrade group topic.
$this->_writeGroupTopic(
$groupTopicObject, $groupWeb, $groupName,
$membersString, $allowChangeString
);
return 1;
}
}
else {
# see if we have permission to add a topic, or to edit the existing topic, etc..
throw Error::Simple( $this->{session}
->i18n->maketext('Group does not exist and create not permitted')
) unless ($create);
throw Error::Simple(
$this->{session}->i18n->maketext(
'CHANGE not permitted for [_1] by [_2]',
( $groupName, $user )
)
)
unless (
Foswiki::Func::checkAccessPermission(
'CHANGE', $user, '', $groupName, $groupWeb
)
);
if ( Foswiki::Func::topicExists( $groupWeb, 'GroupTemplate' ) ) {
$groupTopicObject =
Foswiki::Meta->load( $this->{session}, $groupWeb,
'GroupTemplate' );
}
else {
$groupTopicObject =
Foswiki::Meta->load( $this->{session},
$Foswiki::cfg{SystemWebName},
'GroupTemplate' );
}
# expand the GroupTemplate as best we can.
$this->{session}->{request}
->param( -name => 'topic', -value => $groupName );
$groupTopicObject->expandNewTopic();
$allowChangeString = $groupName;
}
my $wikiName = '';
$wikiName = $usersObj->getWikiName($cuid) if ($cuid);
if ( $membersString !~ m/\b$wikiName\b/ ) {
$membersString .= ', ' if ( $membersString ne '' );
$membersString .= $wikiName;
}
Foswiki::Func::writeEvent( 'addUserToGroup',
"$groupName: $wikiName added by $user" );
$this->_clearGroupCache($groupName);
$this->_writeGroupTopic(
$groupTopicObject, $groupWeb, $groupName,