-
Notifications
You must be signed in to change notification settings - Fork 38
/
Func.pm
3018 lines (2234 loc) · 92.2 KB
/
Func.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::Func
_Interface for Foswiki extensions developers_
This module defines the main interfaces that extensions
can use to interact with the Foswiki engine and content.
Refer to =lib/Foswiki/Plugins/EmptyPlugin.pm= for a template Plugin
and starter documentation on how to write a Plugin.
Plugins should *only* call methods in packages documented in
System.DevelopingPlugins. If you use
functions in other Foswiki libraries you risk creating a security hole, and
you will probably need to change your plugin when you upgrade Foswiki.
%TOC%
API version $Date$ (revision $Rev$)
*Since* _date_ indicates where functions or parameters have been added since
the baseline of the API (Foswiki 1.0.0). The _date_ indicates the
earliest date of a Foswiki release that will support that function or
parameter.
*Deprecated* _date_ indicates where a function or parameters has been
[[http://en.wikipedia.org/wiki/Deprecation][deprecated]]. Deprecated
functions will still work, though they should
_not_ be called in new plugins and should be replaced in older plugins
as soon as possible. Deprecated parameters are simply ignored in Foswiki
releases after _date_.
*Until* _date_ indicates where a function or parameter has been removed.
The _date_ indicates the latest date at which Foswiki releases still supported
the function or parameter.
=cut
# THIS PACKAGE IS PART OF THE PUBLISHED API USED BY EXTENSION AUTHORS.
# DO NOT CHANGE THE EXISTING APIS (well thought out extensions are OK)
# AND ENSURE ALL POD DOCUMENTATION IS COMPLETE AND ACCURATE.
#
# Deprecated functions should not be removed, but should be moved to to the
# deprecated functions section.
package Foswiki::Func;
use strict;
use Error qw( :try );
use Assert;
use Foswiki ();
use Foswiki::Plugins ();
use Foswiki::Meta ();
=begin TML
---++ Environment
=cut
=begin TML
---+++ getSkin( ) -> $skin
Get the skin path, set by the =SKIN= and =COVER= preferences variables or the =skin= and =cover= CGI parameters
Return: =$skin= Comma-separated list of skins, e.g. ='gnu,tartan'=. Empty string if none.
=cut
sub getSkin {
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
return $Foswiki::Plugins::SESSION->getSkin();
}
=begin TML
---+++ getUrlHost( ) -> $host
Get protocol, domain and optional port of script URL
Return: =$host= URL host, e.g. ="http://example.com:80"=
=cut
sub getUrlHost {
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
return $Foswiki::Plugins::SESSION->{urlHost};
}
=begin TML
---+++ getScriptUrl( $web, $topic, $script, ... ) -> $url
Compose fully qualified URL
* =$web= - Web name, e.g. ='Main'=
* =$topic= - Topic name, e.g. ='WebNotify'=
* =$script= - Script name, e.g. ='view'=
* =...= - an arbitrary number of name=>value parameter pairs that will be url-encoded and added to the url. The special parameter name '#' is reserved for specifying an anchor. e.g. <tt>getScriptUrl('x','y','view','#'=>'XXX',a=>1,b=>2)</tt> will give <tt>.../view/x/y?a=1&b=2#XXX</tt>
Return: =$url= URL, e.g. ="http://example.com:80/cgi-bin/view.pl/Main/WebNotify"=
=cut
sub getScriptUrl {
my $web = shift;
my $topic = shift;
my $script = shift;
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
return $Foswiki::Plugins::SESSION->getScriptUrl( 1, $script, $web, $topic,
@_ );
}
=begin TML
---+++ getViewUrl( $web, $topic ) -> $url
Compose fully qualified view URL
* =$web= - Web name, e.g. ='Main'=. The current web is taken if empty
* =$topic= - Topic name, e.g. ='WebNotify'=
Return: =$url= URL, e.g. ="http://example.com:80/cgi-bin/view.pl/Main/WebNotify"=
=cut
sub getViewUrl {
my ( $web, $topic ) = @_;
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
$web ||= $Foswiki::Plugins::SESSION->{webName}
|| $Foswiki::cfg{UsersWebName};
return getScriptUrl( $web, $topic, 'view' );
}
=begin TML
---+++ getPubUrlPath( ) -> $path
Get pub URL path
Return: =$path= URL path of pub directory, e.g. ="/pub"=
=cut
sub getPubUrlPath {
return $Foswiki::cfg{PubUrlPath};
}
=begin TML
---+++ getExternalResource( $url ) -> $response
Get whatever is at the other end of a URL (using an HTTP GET request). Will
only work for encrypted protocols such as =https= if the =LWP= CPAN module is
installed.
Note that the =$url= may have an optional user and password, as specified by
the relevant RFC. Any proxy set in =configure= is honoured.
The =$response= is an object that is known to implement the following subset of
the methods of =LWP::Response=. It may in fact be an =LWP::Response= object,
but it may also not be if =LWP= is not available, so callers may only assume
the following subset of methods is available:
| =code()= |
| =message()= |
| =header($field)= |
| =content()= |
| =is_error()= |
| =is_redirect()= |
Note that if LWP is *not* available, this function:
1 can only really be trusted for HTTP/1.0 urls. If HTTP/1.1 or another
protocol is required, you are *strongly* recommended to =require LWP=.
1 Will not parse multipart content
In the event of the server returning an error, then =is_error()= will return
true, =code()= will return a valid HTTP status code
as specified in RFC 2616 and RFC 2518, and =message()= will return the
message that was received from
the server. In the event of a client-side error (e.g. an unparseable URL)
then =is_error()= will return true and =message()= will return an explanatory
message. =code()= will return 400 (BAD REQUEST).
Note: Callers can easily check the availability of other HTTP::Response methods
as follows:
<verbatim>
my $response = Foswiki::Func::getExternalResource($url);
if (!$response->is_error() && $response->isa('HTTP::Response')) {
... other methods of HTTP::Response may be called
} else {
... only the methods listed above may be called
}
</verbatim>
=cut
sub getExternalResource {
my ($url) = @_;
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
ASSERT( defined $url ) if DEBUG;
return $Foswiki::Plugins::SESSION->net->getExternalResource($url);
}
=begin TML
---+++ getRequestObject( ) -> $query
Get the request object. This is a subclass of Foswiki::Request. The request
object can be used to get the parameters passed to the request, either
via CGI or on the command ine (depending on how the script was called).
*Since:* 31 Mar 2009
=cut
sub getRequestObject {
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
return $Foswiki::Plugins::SESSION->{request};
}
=begin TML
---+++ getSessionKeys() -> @keys
Get a list of all the names of session variables. The list is unsorted.
Session keys are stored and retrieved using =setSessionValue= and
=getSessionValue=.
=cut
sub getSessionKeys {
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
my $hash =
$Foswiki::Plugins::SESSION->{users}->{loginManager}->getSessionValues();
return keys %{$hash};
}
=begin TML
---+++ getSessionValue( $key ) -> $value
Get a session value from the client session module
* =$key= - Session key
Return: =$value= Value associated with key; empty string if not set
=cut
sub getSessionValue {
# my( $key ) = @_;
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
return $Foswiki::Plugins::SESSION->{users}->{loginManager}
->getSessionValue(@_);
}
=begin TML
---+++ setSessionValue( $key, $value ) -> $boolean
Set a session value.
* =$key= - Session key
* =$value= - Value associated with key
Return: true if function succeeded
=cut
sub setSessionValue {
# my( $key, $value ) = @_;
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
$Foswiki::Plugins::SESSION->{users}->{loginManager}->setSessionValue(@_);
}
=begin TML
---+++ clearSessionValue( $key ) -> $boolean
Clear a session value that was set using =setSessionValue=.
* =$key= - name of value stored in session to be cleared. Note that
you *cannot* clear =AUTHUSER=.
Return: true if the session value was cleared
=cut
sub clearSessionValue {
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
return $Foswiki::Plugins::SESSION->{users}->{loginManager}
->clearSessionValue(@_);
}
=begin TML
---+++ getContext() -> \%hash
Get a hash of context identifiers representing the currently active
context.
The context is a set of identifiers that are set
during specific phases of processing. For example, each of
the standard scripts in the 'bin' directory each has a context
identifier - the view script has 'view', the edit script has 'edit'
etc. So you can easily tell what 'type' of script your Plugin is
being called within. The core context identifiers are listed
in the %SYSTEMWEB%.IfStatements topic. Please be careful not to
overwrite any of these identifiers!
Context identifiers can be used to communicate between Plugins, and between
Plugins and templates. For example, in FirstPlugin.pm, you might write:
<verbatim>
sub initPlugin {
Foswiki::Func::getContext()->{'MyID'} = 1;
...
</verbatim>
This can be used in !SecondPlugin.pm like this:
<verbatim>
sub initPlugin {
if( Foswiki::Func::getContext()->{'MyID'} ) {
...
}
...
</verbatim>
or in a template, like this:
<verbatim>
%TMPL:DEF{"ON"}% Not off %TMPL:END%
%TMPL:DEF{"OFF"}% Not on %TMPL:END%
%TMPL:P{context="MyID" then="ON" else="OFF"}%
</verbatim>
or in a topic:
<verbatim>
%IF{"context MyID" then="MyID is ON" else="MyID is OFF"}%
</verbatim>
__Note__: *all* plugins have an *automatically generated* context identifier
if they are installed and initialised. For example, if the FirstPlugin is
working, the context ID 'FirstPlugin' will be set.
=cut
sub getContext {
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
return $Foswiki::Plugins::SESSION->{context};
}
=begin TML
---+++ pushTopicContext($web, $topic)
* =$web= - new web
* =$topic= - new topic
Change the Foswiki context so it behaves as if it was processing =$web.$topic=
from now on. All the preferences will be reset to those of the new topic.
Note that if the new topic is not readable by the logged in user due to
access control considerations, there will *not* be an exception. It is the
duty of the caller to check access permissions before changing the topic.
It is the duty of the caller to restore the original context by calling
=popTopicContext=.
Note that this call does *not* re-initialise plugins, so if you have used
global variables to remember the web and topic in =initPlugin=, then those
values will be unchanged.
=cut
sub pushTopicContext {
my $session = $Foswiki::Plugins::SESSION;
ASSERT($session) if DEBUG;
my ( $web, $topic ) = $session->normalizeWebTopicName(@_);
$session->{prefs}->pushTopicContext( $web, $topic );
$session->{webName} = $web;
$session->{topicName} = $topic;
}
=begin TML
---+++ popTopicContext()
Returns the Foswiki context to the state it was in before the
=pushTopicContext= was called.
=cut
sub popTopicContext {
my $session = $Foswiki::Plugins::SESSION;
ASSERT($session) if DEBUG;
( $session->{webName}, $session->{topicName} ) =
$session->{prefs}->popTopicContext();
}
=begin TML
---++ Preferences
=cut
=begin TML
---+++ getPreferencesValue( $key, $web ) -> $value
Get a preferences value for the currently requested context, from the currently request topic, its web and the site.
* =$key= - Preference name
* =$web= - Name of web, optional. if defined, we shortcircuit to the WebPreferences (and its Sitewide defaults)
Return: =$value= Preferences value; empty string if not set
* Example for preferences setting:
* WebPreferences topic has: =* Set WEBBGCOLOR = #FFFFC0=
* =my $webColor = Foswiki::Func::getPreferencesValue( 'WEBBGCOLOR', 'Sandbox' );=
* Example for MyPlugin setting:
* if the %SYSTEMWEB%.MyPlugin topic has: =* Set COLOR = red=
* Use ="MYPLUGIN_COLOR"= for =$key=
* =my $color = Foswiki::Func::getPreferencesValue( "MYPLUGIN_COLOR" );=
*NOTE:* If =$NO_PREFS_IN_TOPIC= is enabled in the plugin, then
preferences set in the plugin topic will be ignored.
=cut
sub getPreferencesValue {
my ( $key, $web ) = @_;
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
if ($web) {
# Web preference
my $webObject = Foswiki::Meta->new( $Foswiki::Plugins::SESSION, $web );
return $webObject->getPreference($key);
}
else {
# Global preference
return $Foswiki::Plugins::SESSION->{prefs}->getPreference($key);
}
}
=begin TML
---+++ getPluginPreferencesValue( $key ) -> $value
Get a preferences value from your Plugin
* =$key= - Plugin Preferences key w/o PLUGINNAME_ prefix.
Return: =$value= Preferences value; empty string if not set
__Note__: This function will will *only* work when called from the Plugin.pm file itself. it *will not work* if called from a sub-package (e.g. Foswiki::Plugins::MyPlugin::MyModule)
*NOTE:* If =$NO_PREFS_IN_TOPIC= is enabled in the plugin, then
preferences set in the plugin topic will be ignored.
=cut
sub getPluginPreferencesValue {
my ($key) = @_;
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
my $package = caller;
$package =~ s/.*:://; # strip off Foswiki::Plugins:: prefix
return $Foswiki::Plugins::SESSION->{prefs}
->getPreference("\U$package\E_$key");
}
=begin TML
---+++ getPreferencesFlag( $key, $web ) -> $value
Get a preferences flag from Foswiki or from a Plugin
* =$key= - Preferences key
* =$web= - Name of web, optional. Current web if not specified; does not apply to settings of Plugin topics
Return: =$value= Preferences flag ='1'= (if set), or ="0"= (for preferences values ="off"=, ="no"= and ="0"=)
* Example for Plugin setting:
* MyPlugin topic has: =* Set SHOWHELP = off=
* Use ="MYPLUGIN_SHOWHELP"= for =$key=
* =my $showHelp = Foswiki::Func::getPreferencesFlag( "MYPLUGIN_SHOWHELP" );=
*NOTE:* If =$NO_PREFS_IN_TOPIC= is enabled in the plugin, then
preferences set in the plugin topic will be ignored.
=cut
sub getPreferencesFlag {
# my( $key, $web ) = @_;
my $t = getPreferencesValue(@_);
return Foswiki::isTrue($t);
}
=begin TML
---+++ getPluginPreferencesFlag( $key ) -> $boolean
Get a preferences flag from your Plugin
* =$key= - Plugin Preferences key w/o PLUGINNAME_ prefix.
Return: false for preferences values ="off"=, ="no"= and ="0"=, or values not set at all. True otherwise.
__Note__: This function will will *only* work when called from the Plugin.pm file itself. it *will not work* if called from a sub-package (e.g. Foswiki::Plugins::MyPlugin::MyModule)
*NOTE:* If =$NO_PREFS_IN_TOPIC= is enabled in the plugin, then
preferences set in the plugin topic will be ignored.
=cut
sub getPluginPreferencesFlag {
my ($key) = @_;
my $package = caller;
$package =~ s/.*:://; # strip off Foswiki::Plugins:: prefix
return getPreferencesFlag("\U$package\E_$key");
}
=begin TML
---+++ setPreferencesValue($name, $val)
Set the preferences value so that future calls to getPreferencesValue will
return this value, and =%$name%= will expand to the preference when used in
future variable expansions.
The preference only persists for the rest of this request. Finalised
preferences cannot be redefined using this function.
=cut
sub setPreferencesValue {
my ( $name, $value ) = @_;
return $Foswiki::Plugins::SESSION->{prefs}
->setSessionPreferences( $name => $value );
}
=begin TML
---++ User Handling and Access Control
---+++ getDefaultUserName( ) -> $loginName
Get default user name as defined in the configuration as =DefaultUserLogin=
Return: =$loginName= Default user name, e.g. ='guest'=
=cut
sub getDefaultUserName {
return $Foswiki::cfg{DefaultUserLogin};
}
=begin TML
---+++ getCanonicalUserID( $user ) -> $cUID
* =$user= can be a login, wikiname or web.wikiname
Return the cUID of the specified user. A cUID is a unique identifier which
is assigned by Foswiki for each user.
BEWARE: While the default TopicUserMapping uses a cUID that looks like a user's
LoginName, some characters are modified to make them compatible with rcs.
Other usermappings may use other conventions - the !JoomlaUserMapping
for example, has cUIDs like 'JoomlaeUserMapping_1234'.
If $user is undefined, it assumes the currently logged-in user.
Return: =$cUID=, an internal unique and portable escaped identifier for
registered users. This may be autogenerated for an authenticated but
unregistered user.
=cut
sub getCanonicalUserID {
my $user = shift;
return $Foswiki::Plugins::SESSION->{user} unless ($user);
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
my $cUID;
if ($user) {
$cUID = $Foswiki::Plugins::SESSION->{users}->getCanonicalUserID($user);
if ( !$cUID ) {
# Not a login name or a wiki name. Is it a valid cUID?
my $ln = $Foswiki::Plugins::SESSION->{users}->getLoginName($user);
$cUID = $user if defined $ln && $ln ne 'unknown';
}
}
else {
$cUID = $Foswiki::Plugins::SESSION->{user};
}
return $cUID;
}
=begin TML
---+++ getWikiName( $user ) -> $wikiName
return the WikiName of the specified user
if $user is undefined Get Wiki name of logged in user
* $user can be a cUID, login, wikiname or web.wikiname
Return: =$wikiName= Wiki Name, e.g. ='JohnDoe'=
=cut
sub getWikiName {
my $user = shift;
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
my $cUID = getCanonicalUserID($user);
unless ( defined $cUID ) {
my ( $w, $u ) =
normalizeWebTopicName( $Foswiki::cfg{UsersWebName}, $user );
return $u;
}
return $Foswiki::Plugins::SESSION->{users}->getWikiName($cUID);
}
=begin TML
---+++ getWikiUserName( $user ) -> $wikiName
return the userWeb.WikiName of the specified user
if $user is undefined Get Wiki name of logged in user
* $user can be a cUID, login, wikiname or web.wikiname
Return: =$wikiName= Wiki Name, e.g. ="Main.JohnDoe"=
=cut
sub getWikiUserName {
my $user = shift;
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
my $cUID = getCanonicalUserID($user);
unless ( defined $cUID ) {
my ( $w, $u ) =
normalizeWebTopicName( $Foswiki::cfg{UsersWebName}, $user );
return "$w.$u";
}
return $Foswiki::Plugins::SESSION->{users}->webDotWikiName($cUID);
}
=begin TML
---+++ wikiToUserName( $id ) -> $loginName
Translate a Wiki name to a login name.
* =$id= - Wiki name, e.g. ='Main.JohnDoe'= or ='JohnDoe'=.
$id may also be a login name. This will normally
be transparent, but should be borne in mind if you have login names
that are also legal wiki names.
Return: =$loginName= Login name of user, e.g. ='jdoe'=, or undef if not
matched.
Note that it is possible for several login names to map to the same wikiname.
This function will only return the *first* login name that maps to the
wikiname.
returns undef if the WikiName is not found.
=cut
sub wikiToUserName {
my ($wiki) = @_;
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
return '' unless $wiki;
my $cUID = getCanonicalUserID($wiki);
if ($cUID) {
my $login = $Foswiki::Plugins::SESSION->{users}->getLoginName($cUID);
return if !$login || $login eq 'unknown';
return $login;
}
return;
}
=begin TML
---+++ userToWikiName( $loginName, $dontAddWeb ) -> $wikiName
Translate a login name to a Wiki name
* =$loginName= - Login name, e.g. ='jdoe'=. This may
also be a wiki name. This will normally be transparent, but may be
relevant if you have login names that are also valid wiki names.
* =$dontAddWeb= - Do not add web prefix if ="1"=
Return: =$wikiName= Wiki name of user, e.g. ='Main.JohnDoe'= or ='JohnDoe'=
userToWikiName will always return a name. If the user does not
exist in the mapping, the $loginName parameter is returned. (backward compatibility)
=cut
sub userToWikiName {
my ( $login, $dontAddWeb ) = @_;
return '' unless $login;
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
my $users = $Foswiki::Plugins::SESSION->{users};
my $user = getCanonicalUserID($login);
return (
$dontAddWeb
? $login
: ( $Foswiki::cfg{UsersWebName} . '.' . $login )
) unless $users->userExists($user);
return $users->getWikiName($user) if $dontAddWeb;
return $users->webDotWikiName($user);
}
=begin TML
---+++ emailToWikiNames( $email, $dontAddWeb ) -> @wikiNames
* =$email= - email address to look up
* =$dontAddWeb= - Do not add web prefix if ="1"=
Find the wikinames of all users who have the given email address as their
registered address. Since several users could register with the same email
address, this returns a list of wikinames rather than a single wikiname.
=cut
sub emailToWikiNames {
my ( $email, $dontAddWeb ) = @_;
ASSERT($email) if DEBUG;
my %matches;
my $users = $Foswiki::Plugins::SESSION->{users};
my $ua = $users->findUserByEmail($email);
if ($ua) {
foreach my $user (@$ua) {
if ($dontAddWeb) {
$matches{ $users->getWikiName($user) } = 1;
}
else {
$matches{ $users->webDotWikiName($user) } = 1;
}
}
}
return sort keys %matches;
}
=begin TML
---+++ wikinameToEmails( $user ) -> @emails
* =$user= - wikiname of user to look up
Returns the registered email addresses of the named user. If $user is
undef, returns the registered email addresses for the logged-in user.
$user may also be a group.
=cut
sub wikinameToEmails {
my ($wikiname) = @_;
if ($wikiname) {
if ( isGroup($wikiname) ) {
return $Foswiki::Plugins::SESSION->{users}->getEmails($wikiname);
}
else {
my $uids =
$Foswiki::Plugins::SESSION->{users}
->findUserByWikiName($wikiname);
my @em = ();
foreach my $user (@$uids) {
push( @em,
$Foswiki::Plugins::SESSION->{users}->getEmails($user) );
}
return @em;
}
}
else {
my $user = $Foswiki::Plugins::SESSION->{user};
return $Foswiki::Plugins::SESSION->{users}->getEmails($user);
}
}
=begin TML
---+++ isGuest( ) -> $boolean
Test if logged in user is a guest (WikiGuest)
=cut
sub isGuest {
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
return $Foswiki::Plugins::SESSION->{user} eq
$Foswiki::Plugins::SESSION->{users}
->getCanonicalUserID( $Foswiki::cfg{DefaultUserLogin} );
}
=begin TML
---+++ isAnAdmin( $id ) -> $boolean
Find out if the user is an admin or not. If the user is not given,
the currently logged-in user is assumed.
* $id can be either a login name or a WikiName
=cut
sub isAnAdmin {
my $user = shift;
return $Foswiki::Plugins::SESSION->{users}
->isAdmin( getCanonicalUserID($user) );
}
=begin TML
---+++ isGroupMember( $group, $id ) -> $boolean
Find out if $id is in the named group. e.g.
<verbatim>
if( Foswiki::Func::isGroupMember( "HesperionXXGroup", "jordi" )) {
...
}
</verbatim>
If =$user= is =undef=, it defaults to the currently logged-in user.
* $id can be a login name or a WikiName
=cut
sub isGroupMember {
my ( $group, $user ) = @_;
my $users = $Foswiki::Plugins::SESSION->{users};
return () unless $users->isGroup($group);
if ($user) {
#my $login = wikiToUserName( $user );
#return 0 unless $login;
$user = getCanonicalUserID($user);
}
else {
$user = $Foswiki::Plugins::SESSION->{user};
}
return $users->isInGroup( $user, $group );
}
=begin TML
---+++ eachUser() -> $iterator
Get an iterator over the list of all the registered users *not* including
groups. The iterator will return each wiki name in turn (e.g. 'FredBloggs').
Use it as follows:
<verbatim>
my $iterator = Foswiki::Func::eachUser();
while ($it->hasNext()) {
my $user = $it->next();
# $user is a wikiname
}
</verbatim>
*WARNING* on large sites, this could be a long list!
=cut
sub eachUser {
my $it = $Foswiki::Plugins::SESSION->{users}->eachUser();
$it->{process} = sub {
return $Foswiki::Plugins::SESSION->{users}->getWikiName( $_[0] );
};
return $it;
}
=begin TML
---+++ eachMembership($id) -> $iterator
* =$id= - WikiName or login name of the user.
If =$id= is =undef=, defaults to the currently logged-in user.
Get an iterator over the names of all groups that the user is a member of.
=cut
sub eachMembership {
my ($user) = @_;
my $users = $Foswiki::Plugins::SESSION->{users};
if ($user) {
my $login = wikiToUserName($user);
return 0 unless $login;
$user = getCanonicalUserID($login);
}
else {
$user = $Foswiki::Plugins::SESSION->{user};
}
return $users->eachMembership($user);
}
=begin TML
---+++ eachGroup() -> $iterator
Get an iterator over all groups.
Use it as follows:
<verbatim>
my $iterator = Foswiki::Func::eachGroup();
while ($it->hasNext()) {
my $group = $it->next();
# $group is a group name e.g. AdminGroup
}
</verbatim>
*WARNING* on large sites, this could be a long list!
=cut
sub eachGroup {
my $session = $Foswiki::Plugins::SESSION;
my $it = $session->{users}->eachGroup();
return $it;
}
=begin TML
---+++ isGroup( $group ) -> $boolean
Checks if =$group= is the name of a user group.
=cut
sub isGroup {
my ($group) = @_;
return $Foswiki::Plugins::SESSION->{users}->isGroup($group);
}
=begin TML
---+++ eachGroupMember($group) -> $iterator
Get an iterator over all the members of the named group. Returns undef if
$group is not a valid group.
Use it as follows:
<verbatim>
my $iterator = Foswiki::Func::eachGroupMember('RadioheadGroup');
while ($it->hasNext()) {
my $user = $it->next();
# $user is a wiki name e.g. 'TomYorke', 'PhilSelway'
}
</verbatim>
*WARNING* on large sites, this could be a long list!
=cut
sub eachGroupMember {
my $user = shift;
my $session = $Foswiki::Plugins::SESSION;
return
unless $Foswiki::Plugins::SESSION->{users}->isGroup($user);
my $it = $Foswiki::Plugins::SESSION->{users}->eachGroupMember($user);
$it->{process} = sub {
return $Foswiki::Plugins::SESSION->{users}->getWikiName( $_[0] );
};
return $it;
}
=begin TML
---+++ checkAccessPermission( $type, $id, $text, $topic, $web, $meta ) -> $boolean
Check access permission for a topic based on the
[[%SYSTEMWEB%.AccessControl]] rules
* =$type= - Access type, required, e.g. ='VIEW'=, ='CHANGE'=.
* =$id= - WikiName of remote user, required, e.g. ="RickShaw"=.
$id may also be a login name.
If =$id= is '', 0 or =undef= then access is *always permitted*.
* =$text= - Topic text, optional. If 'perl false' (undef, 0 or ''),
topic =$web.$topic= is consulted. =$text= may optionally contain embedded
=%META:PREFERENCE= tags. Provide this parameter if:
1 You are setting different access controls in the text to those defined
in the stored topic,
1 You already have the topic text in hand, and want to help avoid
having to read it again,
1 You are providing a =$meta= parameter.
* =$topic= - Topic name, required, e.g. ='PrivateStuff'=
* =$web= - Web name, required, e.g. ='Sandbox'=
* =$meta= - Meta-data object, as returned by =readTopic=. Optional.
If =undef=, but =$text= is defined, then access controls will be parsed
from =$text=. If defined, then metadata embedded in =$text= will be
ignored. This parameter is always ignored if =$text= is undefined.
Settings in =$meta= override =Set= settings in $text.
A perl true result indicates that access is permitted.
*Note* the weird parameter order is due to compatibility constraints with
earlier releases.
*Tip* if you want, you can use this method to check your own access control types. For example, if you:
* Set ALLOWTOPICSPIN = IncyWincy
in =ThatWeb.ThisTopic=, then a call to =checkAccessPermissions('SPIN', 'IncyWincy', undef, 'ThisTopic', 'ThatWeb', undef)= will return =true=.
=cut
sub checkAccessPermission {
my ( $type, $user, $text, $topic, $web, $meta ) = @_;
return 1 unless ($user);
ASSERT($Foswiki::Plugins::SESSION) if DEBUG;
$text = undef unless $text;
my $cUID = getCanonicalUserID($user)
|| getCanonicalUserID( $Foswiki::cfg{DefaultUserLogin} );
if ( !defined($meta) ) {
if ($text) {
$meta = Foswiki::Meta->new( $Foswiki::Plugins::SESSION,
$web, $topic, $text );