Skip to content

Commit

Permalink
Item2172: add User To Group inputbox and button to WikiGroups topic …
Browse files Browse the repository at this point in the history
…- now we have a URL based way to add to groups (atm it will serialise into META:PREFERENCES, which will need to change.

UI Guys, please take a look and make the UI useable by normal users - next i'll add removeUserFromGroup.... and then finally the add user to group on registration (um 3 lines of code?)


git-svn-id: http://svn.foswiki.org/trunk@5169 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
SvenDowideit authored and SvenDowideit committed Sep 29, 2009
1 parent 2249af4 commit 63f418f
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 35 deletions.
12 changes: 4 additions & 8 deletions TopicUserMappingContrib/lib/Foswiki/Users/TopicUserMapping.pm
Expand Up @@ -677,12 +677,12 @@ cuid _cannot_ be a groupname
=cut

sub addUserToGroup {
my ( $this, $cuid, $groupName, $create ) = @_;
$groupName = Foswiki::Sandbox::untaint( $groupName,
my ( $this, $cuid, $Group, $create ) = @_;
$Group = Foswiki::Sandbox::untaint( $Group,
\&Foswiki::Sandbox::validateTopicName );
my ( $groupWeb, $groupTopic ) =
my ( $groupWeb, $groupName ) =
$this->{session}
->normalizeWebTopicName( $Foswiki::cfg{UsersWebName}, $groupName );
->normalizeWebTopicName( $Foswiki::cfg{UsersWebName}, $Group );

my $user = $this->{session}->{user};

Expand All @@ -703,8 +703,6 @@ sub addUserToGroup {
$user =
$usersObj->findUserByWikiName(
$Foswiki::cfg{Register}{RegistrationAgentWikiName} );

#$this->{session}->writeDebug("using $user") if DEBUG;
}

if (
Expand Down Expand Up @@ -736,7 +734,6 @@ sub addUserToGroup {
return 1;
}
else {

#see if we have permission to add a topic, or to edit the existing topic, etc..
return 0 unless ($create);
return 0
Expand All @@ -757,7 +754,6 @@ sub addUserToGroup {
value => $usersObj->getWikiName($cuid)
}
);

#TODO: should also consider securing the new topic?
$groupTopicObject->saveAs( $Foswiki::cfg{UsersWebName},
$groupName, -author => $user );
Expand Down
42 changes: 21 additions & 21 deletions UnitTestContrib/test/unit/FuncUsersTests.pm
Expand Up @@ -1395,7 +1395,7 @@ sub verify_addToGroup {
$this->assert( $Foswiki::Plugins::SESSION->{user} );

$this->assert( !Foswiki::Func::isGroupMember( 'ZeeGroup', 'UserZ' ) );
$this->assert( !Foswiki::Func::addUserToGroup( 'ZeeGroup', 'UserZ' ) );
$this->assert( !Foswiki::Func::addUserToGroup('UserZ', 'ZeeGroup') );

# Force a re-read
$this->{session}->finish();
Expand All @@ -1405,7 +1405,7 @@ sub verify_addToGroup {

#TODO: need to test who the topic was saved by

$this->assert( Foswiki::Func::addUserToGroup( 'ZeeGroup', 'UserZ', 1 ) );
$this->assert( Foswiki::Func::addUserToGroup('UserZ', 'ZeeGroup', 1 ) );

# Force a re-read
$this->{session}->finish();
Expand All @@ -1414,7 +1414,7 @@ sub verify_addToGroup {
$this->assert( Foswiki::Func::isGroupMember( 'ZeeGroup', 'UserZ' ) );

$this->assert( !Foswiki::Func::isGroupMember( 'ZeeGroup', 'UserA' ) );
$this->assert( Foswiki::Func::addUserToGroup( 'ZeeGroup', 'UserA' ) );
$this->assert( Foswiki::Func::addUserToGroup( 'UserA' , 'ZeeGroup') );

# Force a re-read
$this->{session}->finish();
Expand All @@ -1429,7 +1429,7 @@ sub verify_addToGroup {
);
$this->assert(
Foswiki::Func::addUserToGroup(
'ZeeGroup', $Foswiki::cfg{DefaultUserLogin}
$Foswiki::cfg{DefaultUserLogin}, 'ZeeGroup'
)
);

Expand All @@ -1446,14 +1446,14 @@ sub verify_addToGroup {
$this->assert(
!Foswiki::Func::isGroupMember( 'ZeeGroup', 'WiseGuyDoesntExist' ) );
$this->assert(
!Foswiki::Func::addUserToGroup( 'ZeeGroup', 'WiseGuyDoesntExist' ) );
!Foswiki::Func::addUserToGroup('WiseGuyDoesntExist', 'ZeeGroup' ) );

# Force a re-read
$this->{session}->finish();
$this->{session} = new Foswiki();
$Foswiki::Plugins::SESSION = $this->{session};
$this->assert(
!Foswiki::Func::isGroupMember( 'ZeeGroup', 'WiseGuyDoesntExist' ) );
!Foswiki::Func::isGroupMember('WiseGuyDoesntExist', 'ZeeGroup') );
}

sub DISABLEDverify_addGroupToGroup {
Expand All @@ -1462,8 +1462,8 @@ sub DISABLEDverify_addGroupToGroup {
return if ( $this->noUsersRegistered() );

#test nested groups
$this->assert( Foswiki::Func::addUserToGroup( 'TeeGroup', 'UserB', 1 ) );
$this->assert( Foswiki::Func::addUserToGroup( 'TeeGroup', 'ZeeGroup', 1 ) );
$this->assert( Foswiki::Func::addUserToGroup('UserB', 'TeeGroup', 1 ) );
$this->assert( Foswiki::Func::addUserToGroup('ZeeGroup', 'TeeGroup', 1 ) );

# Force a re-read
$this->{session}->finish();
Expand All @@ -1482,11 +1482,11 @@ sub verify_removeFromGroup {
$this->assert( !Foswiki::Func::isGroupMember( 'ZeeGroup', 'UserA' ) );
$this->assert( !Foswiki::Func::isGroupMember( 'ZeeGroup', 'UserB' ) );
$this->assert(
!Foswiki::Func::isGroupMember( 'ZeeGroup', 'WiseGuyDoesntExist' ) );
!Foswiki::Func::isGroupMember('WiseGuyDoesntExist' , 'ZeeGroup') );

$this->assert( Foswiki::Func::addUserToGroup( 'ZeeGroup', 'UserZ', 1 ) );
$this->assert( Foswiki::Func::addUserToGroup( 'ZeeGroup', 'UserA', 1 ) );
$this->assert( Foswiki::Func::addUserToGroup( 'ZeeGroup', 'UserB', 1 ) );
$this->assert( Foswiki::Func::addUserToGroup('UserZ', 'ZeeGroup', 1 ) );
$this->assert( Foswiki::Func::addUserToGroup('UserA', 'ZeeGroup', 1 ) );
$this->assert( Foswiki::Func::addUserToGroup('UserB', 'ZeeGroup', 1 ) );

# Force a re-read
$this->{session}->finish();
Expand All @@ -1496,11 +1496,11 @@ sub verify_removeFromGroup {
$this->assert( Foswiki::Func::isGroupMember( 'ZeeGroup', 'UserA' ) );
$this->assert( Foswiki::Func::isGroupMember( 'ZeeGroup', 'UserB' ) );
$this->assert(
!Foswiki::Func::isGroupMember( 'ZeeGroup', 'WiseGuyDoesntExist' ) );
!Foswiki::Func::isGroupMember('WiseGuyDoesntExist', 'ZeeGroup' ) );

$this->assert( Foswiki::Func::removeUserFromGroup( 'ZeeGroup', 'UserA' ) );
$this->assert( Foswiki::Func::removeUserFromGroup('UserA', 'ZeeGroup' ) );
$this->assert(
!Foswiki::Func::removeUserFromGroup( 'ZeeGroup', 'WiseGuyDoesntExist' )
!Foswiki::Func::removeUserFromGroup( 'WiseGuyDoesntExist', 'ZeeGroup' )
);

# Force a re-read
Expand All @@ -1521,9 +1521,9 @@ sub DISABLEDverify_removeFromGroup {
return if ( $this->noUsersRegistered() );

#test nested groups
$this->assert( Foswiki::Func::addUserToGroup( 'TeeGroup', 'UserB', 1 ) );
$this->assert( Foswiki::Func::addUserToGroup( 'TeeGroup', 'UserC', 1 ) );
$this->assert( Foswiki::Func::addUserToGroup( 'TeeGroup', 'ZeeGroup', 1 ) );
$this->assert( Foswiki::Func::addUserToGroup( 'UserB', 'TeeGroup', 1 ) );
$this->assert( Foswiki::Func::addUserToGroup( 'UserC', 'TeeGroup', 1 ) );
$this->assert( Foswiki::Func::addUserToGroup('ZeeGroup', 'TeeGroup', 1 ) );

# Force a re-read
$this->{session}->finish();
Expand All @@ -1532,9 +1532,9 @@ sub DISABLEDverify_removeFromGroup {
$this->assert( Foswiki::Func::isGroupMember( 'TeeGroup', 'UserB' ) );
$this->assert( Foswiki::Func::isGroupMember( 'TeeGroup', 'UserA' ) );

$this->assert( !Foswiki::Func::removeUserFromGroup( 'TeeGroup', 'UserA' ) )
$this->assert( !Foswiki::Func::removeUserFromGroup( 'UserA' , 'TeeGroup') )
; #can't remove user as they come from a subgroup..
$this->assert( Foswiki::Func::removeUserFromGroup( 'TeeGroup', 'UserB' ) );
$this->assert( Foswiki::Func::removeUserFromGroup( 'UserB', 'TeeGroup' ) );

# Force a re-read
$this->{session}->finish();
Expand All @@ -1545,7 +1545,7 @@ sub DISABLEDverify_removeFromGroup {
$this->assert( Foswiki::Func::isGroupMember( 'TeeGroup', 'UserC' ) );

$this->assert(
Foswiki::Func::removeUserFromGroup( 'TeeGroup', 'ZeeGroup' ) );
Foswiki::Func::removeUserFromGroup('ZeeGroup' , 'TeeGroup') );

# Force a re-read
$this->{session}->finish();
Expand Down
15 changes: 13 additions & 2 deletions core/data/Main/WikiGroups.txt
Expand Up @@ -4,9 +4,10 @@

These groups can be used to define fine grained [[%SYSTEMWEB%.AccessControl][AccessControl]] in <nop>%WIKITOOLNAME%:

%TABLE{databg="" cellborder="0" tableborder="0" tablewidth="99%"}%
%GROUPINFO{
header="| *Group* | *Members* |$n"
format="| $name | $percntGROUPINFO{$quot$name$quot format=$quot$dollarwikiname$quot separator=$quot $quot}$percnt |"
header="| *Group* | *Members* | *edit* |$n"
format="| $name | $percntGROUPINFO{$quot$name$quot format=$quot$dollarwikiname$quot separator=$quot $quot}$percnt | $percntINCLUDE{$quot%WEB%.%TOPIC%$quot section=$quoteditgroup$quot GROUPNAME=$quot$name$quot}$percnt |"
separator="$n"
}%

Expand All @@ -33,3 +34,13 @@ These groups can be used to define fine grained [[%SYSTEMWEB%.AccessControl][Acc

---
*Related topics:* [[%SYSTEMWEB%.AccessControl][AccessControl]], [[%WEBPREFSTOPIC%]], [[%WIKIUSERSTOPIC%]], [[%SYSTEMWEB%.WikiCulture][WikiCulture]]

<!--
%STARTSECTION{"editgroup"}%<form action="%SCRIPTURLPATH{"manage"}%/%WEB%/%TOPIC%" method="POST"> \
<input type="text" name="username" value="" size="20" class="foswikiInputField" /> \
<input class="foswikiSubmit" type="submit" value="add User"/> \
<input type="hidden" name="groupname" value="%GROUPNAME%"/> \
<input type="hidden" name="action" value="addUserToGroup"/> \
</form>%ENDSECTION{"editgroup"}%

-->
13 changes: 13 additions & 0 deletions core/data/System/CommandAndCGIScripts.txt
Expand Up @@ -159,6 +159,19 @@ Unregisters (removes) the currently logged-in user.
---++++ =action=restoreRevision=
Alternative entry point for =edit=, used by screens that support several actions using =manage. Parameters are as for =edit=.

---++++ =action=addUserToGroup=
add a user / list of users to a group
| *Parameter* | *Description* | *Default* |
| =username= | *list* of usernames/wikinames to add to group | none - error if not set |
| =groupname= |groupname to change | none - error if not set |

---++++ =action=removeUserFromGroup=
remove a user / list of users to a group
| *Parameter* | *Description* | *Default* |
| =username= | *list* of usernames/wikinames to add to group | none - error if not set |
| =groupname= |groupname to change | none - error if not set |


---+++ =oops=
This script is mainly used for rendering pages containing error messages, though it is also used for some functional actions such as manage pages (move topic etc).

Expand Down
6 changes: 2 additions & 4 deletions core/lib/Foswiki/Func.pm
Expand Up @@ -964,15 +964,13 @@ sub eachGroupMember {
=cut

sub addUserToGroup {
my ( $group, $user, $create ) = @_;
my ( $user, $group, $create ) = @_;
my $users = $Foswiki::Plugins::SESSION->{users};

return () unless $users->isGroup($group);

# if (!$users->isGroup($user)) { #requires isInGroup to also work on nested groupnames
$user = getCanonicalUserID($user);
return unless ( defined($user) and ( $users->userExists($user) ) );

# }
return $users->addUserToGroup( $user, $group, $create );
}
Expand All @@ -986,7 +984,7 @@ sub addUserToGroup {
=cut

sub removeUserFromGroup {
my ( $group, $user, $create ) = @_;
my ( $user, $group, $create ) = @_;
my $users = $Foswiki::Plugins::SESSION->{users};

return () unless $users->isGroup($group);
Expand Down
14 changes: 14 additions & 0 deletions core/lib/Foswiki/UI/Manage.pm
Expand Up @@ -81,6 +81,20 @@ sub _action_deleteUserAccount {
Foswiki::UI::Register::deleteUser($session);
}

sub _action_addUserToGroup {
my $session = shift;

require Foswiki::UI::Register;
Foswiki::UI::Register::addUserToGroup($session);
}

sub _action_removeUserFromGroup {
my $session = shift;

require Foswiki::UI::Register;
Foswiki::UI::Register::removeUserFromGroup($session);
}

sub _isValidHTMLColor {
my $c = shift;
return $c =~
Expand Down
67 changes: 67 additions & 0 deletions core/lib/Foswiki/UI/Register.pm
Expand Up @@ -549,6 +549,73 @@ sub deleteUser {
);
}

=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');
if (
(length(@userNames) <= 0) or
($userNames[0] eq '')){
throw Foswiki::OopsException( 'attention', def => 'no_users_to_add_to_group' );
}
if (length(@userNames) == 1) {
@userNames = split(/,\s+/, $userNames[0]);
}
my $groupName = $query->param('groupname');
if (!$groupName or $groupName eq '') {
throw Foswiki::OopsException( 'attention', def => 'no_group_specified_for_add_to_group' );
}
my @failed;
foreach my $u (@userNames) {
try {
if (!Foswiki::Func::addUserToGroup($u, $groupName)) {
push(@failed, $u);
# Log the error
$session->logger->log( 'warning',
"'Failed to add $u to $groupName " );
}
}
catch Error::Simple with {
my $e = shift;

push(@failed, $u);
# Log the error
$session->logger->log( 'warning',
"catch: Failed to add $u to $groupName " . $e->stringify() );
};
}
if (@failed) {
throw Foswiki::OopsException(
'attention',
web => $web,
topic => $topic,
def => 'problem_adding_to_group',
params => [ join(', ', @failed), $groupName ]
);
}
throw Foswiki::OopsException(
'attention',
status => 200,
def => 'added_users_to_group',
web => $web,
topic => $topic,
params => [ join(', ',@userNames), $groupName ]
);
}

# Complete a registration
sub _complete {
my ($session) = @_;
Expand Down
30 changes: 30 additions & 0 deletions core/templates/messages.tmpl
Expand Up @@ -429,6 +429,36 @@
%MAKETEXT{"If you are developing a Foswiki application, see [_1] for a description of the correct parameters." args="%SYSTEMWEB%.CommandAndCGIScripts#%PARAM1%"}%

%TMPL:END%

%{-----------------------------------}%
%TMPL:DEF{"no_users_to_add_to_group"}%
---+++ %MAKETEXT{"No username specified"}%

%MAKETEXT{"You need to specify a valid user to add to a Group."}%
%TMPL:END%

%TMPL:DEF{"no_group_specified_for_add_to_group"}%
---+++ %MAKETEXT{"No GroupName specified"}%
%MAKETEXT{"You need to specify a valid Group name to add users to a Group."}%

%TMPL:END%

%TMPL:DEF{"problem_adding_to_group"}%
---+++ %MAKETEXT{"Errors adding Users to Groups"}%

%MAKETEXT{"There were errors adding [_1] to the [_2] group. " args="%PARAM1%, %PARAM2%"}%

%MAKETEXT{"Please check to ensure you have permssion to modify the group."}%

%TMPL:END%

%TMPL:DEF{"added_users_to_group"}%
---+++ %MAKETEXT{"Success adding Users to Groups"}%

%MAKETEXT{"[_1] added to the [_2] group. " args="%PARAM1%, %PARAM2%"}%

%MAKETEXT{"See %MAINWEB%.WikiGroups to see the result."}%%TMPL:END%

%{
Messages for oopsleaseconflict
}%
Expand Down

0 comments on commit 63f418f

Please sign in to comment.