Question
I'd like to make some portions of a page appear/disappear depending on the group membership of the user. For example, I'd like to put administrative functions on a page that only members of
TWikiAdminGroup will see.
Another option that would help me would be to show/hide page content based on the user's ability to edit that page.
Is there a way to form a %IF to test for either/both of these conditions?
Environment
--
SteveJohnson24 - 13 Jan 2007
Answer
If you answer a question - or have a question you asked answered by someone - please remember to edit the page and set the status to answered. The status is in a drop-down list below the edit box.
At this time there is no easy way to test for group membership and take conditional action based on that with an %IF.
There is however a simple solution: Simply INCLUDE a topic that has view access restriction based on a group. Those who are not in the group will not see the content. You can omit the view denied error message, see details in
VarINCLUDE.
--
PeterThoeny - 14 Jan 2007
Here's a little hack to add "group" functionality to %IF... Insert the following code into lib/TWiki/If.pm, somewhere around line 86:
$defOps{group} =
{ name => 'group',
prec => 5,
type => 0, # unary
exec => sub {
my( $twiki, $a, $b ) = @_;
my $s=$b->evaluate($twiki);
for my $group ($twiki->{user}->getGroups) {
return 1 if( $s eq $group->wikiName );
}
return 0;
}
};
Then you can use the following syntax:
%IF{ "group TWikiAdminGroup" then="*yes*" else="no"}%
Hope this helps.
--
SteveMokris - 28 Sep 2007
Useful for me, thanks
--
MartinCleaver - 12 Oct 2007
yoinc:
Bugs:Item4809
--
SvenDowideit - 13 Oct 2007
I've made the if a little more generic for 4.2, and written a set of unit tests for it:
%IF{ "SvenDowideit ingroup TWikiAdminGroup" then="*yes*" else="no"}%
--
SvenDowideit - 13 Oct 2007