From 88c865c902c5561b9fdf758a36fc55b958e1ff63 Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Tue, 15 Feb 2011 15:44:32 -0600 Subject: [PATCH] migrate Auth editUserForm and Operation User editUser to FormBuilder --- lib/WebGUI/Auth/LDAP.pm | 34 +++---- lib/WebGUI/Auth/WebGUI.pm | 2 +- lib/WebGUI/Operation/User.pm | 168 +++++++++++++++++++---------------- t/Operation/User.t | 93 +++++++++++++++++++ 4 files changed, 203 insertions(+), 94 deletions(-) create mode 100644 t/Operation/User.t diff --git a/lib/WebGUI/Auth/LDAP.pm b/lib/WebGUI/Auth/LDAP.pm index 68e189e38..7c3071018 100644 --- a/lib/WebGUI/Auth/LDAP.pm +++ b/lib/WebGUI/Auth/LDAP.pm @@ -260,7 +260,7 @@ sub editUserForm { my $connectDN = $self->session->form->process('authLDAP_connectDN') || $userData->{connectDN}; my $ldapConnection = $self->session->form->process('authLDAP_ldapConnection') || $userData->{ldapConnection}; my $ldapLinks = $self->session->db->buildHashRef("select ldapLinkId,ldapUrl from ldapLink"); - my $f = WebGUI::HTMLForm->new($self->session); + my $f = WebGUI::FormBuilder->new($self->session); my $jscript = ""; my $i18n = WebGUI::International->new($self->session,'AuthLDAP'); if(scalar(keys %{$ldapLinks}) > 0) { @@ -277,27 +277,27 @@ sub editUserForm { $jsArray //--> |; - $f->selectBox( - -name=>"authLDAP_ldapConnection", - -label=>$i18n->get("ldapConnection"), - -hoverHelp=>$i18n->get("ldapConnection description"), - -options=>WebGUI::LDAPLink->getList($self->session,), - -value=>[$ldapConnection], - -extras=>q|onchange="this.form.authLDAP_ldapUrl.value=ldapValue[this.options[this.selectedIndex].value];"| + $f->addField( "selectBox", + name=>"authLDAP_ldapConnection", + label=>$i18n->get("ldapConnection"), + hoverHelp=>$i18n->get("ldapConnection description"), + options=>WebGUI::LDAPLink->getList($self->session,), + value=>[$ldapConnection], + extras=>q|onchange="this.form.authLDAP_ldapUrl.value=ldapValue[this.options[this.selectedIndex].value];"| ); } - $f->url( - -name => "authLDAP_ldapUrl", - -label => $i18n->get(3), - -value => $ldapUrl, + $f->addField( "url", + name => "authLDAP_ldapUrl", + label => $i18n->get(3), + value => $ldapUrl, ); - $f->text( - -name => "authLDAP_connectDN", - -label => $i18n->get('LDAP User DN'), - -value => $connectDN, + $f->addField( "text", + name => "authLDAP_connectDN", + label => $i18n->get('LDAP User DN'), + value => $connectDN, ); $self->session->style->setRawHeadTags($jscript); - return $f->printRowsOnly; + return $f; } #------------------------------------------------------------------- diff --git a/lib/WebGUI/Auth/WebGUI.pm b/lib/WebGUI/Auth/WebGUI.pm index fb5dedf19..3a9a94039 100644 --- a/lib/WebGUI/Auth/WebGUI.pm +++ b/lib/WebGUI/Auth/WebGUI.pm @@ -219,7 +219,7 @@ sub editUserFormSave { } } - $self->SUPER::editUserFormSave($properties); + $self->update( $properties ); } #------------------------------------------------------------------- diff --git a/lib/WebGUI/Operation/User.pm b/lib/WebGUI/Operation/User.pm index 479917619..0c683c964 100644 --- a/lib/WebGUI/Operation/User.pm +++ b/lib/WebGUI/Operation/User.pm @@ -621,100 +621,122 @@ sub www_editUser { return $session->privilege->adminOnly() unless canAdd($session); my $error = shift; my $uid = shift || $session->form->process("uid"); - my $i18n = WebGUI::International->new($session, "WebGUI"); - my %tabs; - tie %tabs, 'Tie::IxHash'; - %tabs = ( - "account"=> { label=>$i18n->get("account")}, - "profile"=> { label=>$i18n->get("profile")}, - "groups"=> { label=>$i18n->get('89')}, - ); - my $tabform = WebGUI::TabForm->new($session,\%tabs); - $tabform->formHeader({extras=>'autocomplete="off"'}); my $u = WebGUI::User->new($session,($uid eq 'new') ? '' : $uid); #Setting uid to '' when uid is 'new' so visitor defaults prefill field for new user my $username = ($u->isVisitor && $uid ne "1") ? '' : $u->username; - $tabform->hidden({name=>"op",value=>"editUserSave"}); - $tabform->hidden({name=>"uid",value=>$uid}); - $tabform->getTab("account")->raw('  '); - $tabform->getTab("account")->readOnly(value=>$uid,label=>$i18n->get(378)); - $tabform->getTab("account")->readOnly(value=>$u->karma,label=>$i18n->get(537)) if ($session->setting->get("useKarma")); - $tabform->getTab("account")->readOnly(value=>$session->datetime->epochToHuman($u->dateCreated,"%z"),label=>$i18n->get(453)); - $tabform->getTab("account")->readOnly(value=>$session->datetime->epochToHuman($u->lastUpdated,"%z"),label=>$i18n->get(454)); - $tabform->getTab("account")->text( - -name=>"username", - -label=>$i18n->get(50), - -value=>$username - -extras=>'autocomplete="off"', + my $i18n = WebGUI::International->new($session, "WebGUI"); + my $f = WebGUI::FormBuilder->new( $session, + action => $session->url->page, + extras => 'autocomplete="off"', ); - my %status; - tie %status, 'Tie::IxHash'; - %status = ( - Active =>$i18n->get(817), - Deactivated =>$i18n->get(818), - Selfdestructed =>$i18n->get(819) - ); + $f->addField( 'csrfToken', name => 'csrfToken' ); + $f->addField( "hidden", + name => 'op', + value => 'editUserSave', + ); + $f->addField( "hidden", + name => "uid", + value => $uid, + ); + my $account = $f->addTab( name => "account", label => $i18n->get('account') ); + my $profile = $f->addTab( name => "profile", label => $i18n->get('profile') ); + my $groups = $f->addTab( name => "groups", label => $i18n->get('89') ); + + # Normal user fields + $account->addField( "readOnly", + name => "uid", + value => $uid, + label => $i18n->get(378), + ); + $account->addField( "readOnly", + name => "karma", + value => $u->karma, + label => $i18n->get(537) + ) if ($session->setting->get("useKarma")); + $account->addField( "readOnly", + name => "dateCreated", + value=>$session->datetime->epochToHuman($u->dateCreated,"%z"), + label=>$i18n->get(453) + ); + $account->addField( "readOnly", + name => "lastUpdated", + value=>$session->datetime->epochToHuman($u->lastUpdated,"%z"), + label=>$i18n->get(454) + ); + $account->addField( "text", + name=>"username", + label=>$i18n->get(50), + value=>$username, + extras=>'autocomplete="off"', + ); + if ($u->userId eq $session->user->userId) { - $tabform->getTab("account")->hidden( - -name => "status", - -value => $u->status + $account->addField( "hidden", + name => "status", + value => $u->status ); } else { - $tabform->getTab("account")->selectBox( - -name => "status", - -options => \%status, - -label => $i18n->get(816), - -value => $u->status + tie my %status, 'Tie::IxHash', ( + Active =>$i18n->get(817), + Deactivated =>$i18n->get(818), + Selfdestructed =>$i18n->get(819) + ); + $account->addField( "selectBox", + name => "status", + options => \%status, + label => $i18n->get(816), + value => $u->status ); } + + # Auth configurations my $options; foreach (@{$session->config->get("authMethods")}) { $options->{$_} = $_; } - $tabform->getTab("account")->selectBox( - -name=>"authMethod", - -options=>$options, - -label=>$i18n->get(164), - -value=>$u->authMethod, + $account->addField( "selectBox", + name=>"authMethod", + options=>$options, + label=>$i18n->get(164), + value=>$u->authMethod, ); - foreach (@{$session->config->get("authMethods")}) { - my $authInstance = WebGUI::Operation::Auth::getInstance($session,$_,$u->userId); + foreach my $auth (@{$session->config->get("authMethods")}) { + my $authInstance = WebGUI::Operation::Auth::getInstance($session,$auth,$u->userId); my $editUserForm = $authInstance->editUserForm; next unless $editUserForm; - $tabform->getTab("account")->fieldSetStart($_); - $tabform->getTab("account")->raw($editUserForm); - $tabform->getTab("account")->fieldSetEnd; + $account->addFieldset( $editUserForm, name => $auth, label => $auth ); } + + # Profile fields foreach my $category (@{WebGUI::ProfileCategory->getCategories($session)}) { - $tabform->getTab("profile")->fieldSetStart($category->getLabel); + my $fieldset = $profile->addFieldset( name => $category->getLabel, label => $category->getLabel ); foreach my $field (@{$category->getFields}) { next if $field->getId =~ /contentPositions/; my $label = $field->getLabel . ($field->isRequired ? "*" : ''); if ($u->isVisitor) { - $tabform->getTab("profile")->raw($field->formField({label=>$label},1,undef,undef,undef,undef,'useFormDefault')); + $fieldset->addField($field->formField({label=>$label},1,undef,undef,undef,1,'useFormDefault')); } else { - $tabform->getTab("profile")->raw($field->formField({label=>$label},1,$u)); + $fieldset->addField($field->formField({label=>$label},1,$u,undef,undef,1)); } } - $tabform->getTab("profile")->fieldSetEnd($category->getLabel); } + + # Groups my @groupsToAdd = $session->form->group("groupsToAdd"); my @exclude = $session->db->buildArray("select groupId from groupings where userId=?",[$u->userId]); - @exclude = (@exclude,"1","2","7"); + push @exclude,"1","2","7"; # Special groups cannot be left/joined my $secondaryAdmin = $session->user->isInGroup('11'); - my @extraExclude = (); if ($secondaryAdmin && !$session->user->isAdmin) { - @extraExclude = $session->db->buildArray('select groupId from groups where groupId not in (select groupId from groupings where userId=?)',[$session->user->userId]); + push @exclude, $session->db->buildArray('select groupId from groups where groupId not in (select groupId from groupings where userId=?)',[$session->user->userId]); } - push @extraExclude, @exclude; - $tabform->getTab("groups")->group( - -name=>"groupsToAdd", - -label=>$i18n->get("groups to add"), - -excludeGroups=>\@extraExclude, - -size=>15, - -multiple=>1, - -value=>\@groupsToAdd + $groups->addField( "group", + name=>"groupsToAdd", + label=>$i18n->get("groups to add"), + excludeGroups=>\@exclude, + size=>15, + multiple=>1, + value=>\@groupsToAdd ); my @include; foreach my $group (@exclude) { @@ -728,22 +750,16 @@ sub www_editUser { } push (@include, "0"); my @groupsToDelete = $session->form->group("groupsToDelete"); - $tabform->getTab("groups")->selectList( - -name=>"groupsToDelete", - -options=>$session->db->buildHashRef("select groupId, groupName from groups + $groups->addField( "selectList", + name=>"groupsToDelete", + options=>$session->db->buildHashRef("select groupId, groupName from groups where groupId in (".$session->db->quoteAndJoin(\@include).") and showInForms=1 order by groupName"), - -label=>$i18n->get("groups to delete"), - -multiple=>1, - -size=>15, - -value=>\@groupsToDelete + label=>$i18n->get("groups to delete"), + multiple=>1, + size=>15, + value=>\@groupsToDelete ); - my $submenu = _submenu( - $session, - { workarea => $error.$tabform->print, - title => 168, - userId => $uid, } - ); - return $submenu;; + return '

' . $i18n->get(168) . '

' . $f->toHtml; } #------------------------------------------------------------------- diff --git a/t/Operation/User.t b/t/Operation/User.t new file mode 100644 index 000000000..63a4f9f74 --- /dev/null +++ b/t/Operation/User.t @@ -0,0 +1,93 @@ +# vim:syntax=perl +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2009 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------ +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------ + +# Test the User operation +# +# + +use FindBin; +use strict; +use lib "$FindBin::Bin/lib"; +use Test::More; +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Session; +use WebGUI::Test::Mechanize; +use WebGUI::Operation::User; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; + + +#---------------------------------------------------------------------------- +# Tests + +plan tests => 17; # Increment this number for each test you create + +#---------------------------------------------------------------------------- +# Create a new user +my $mech = WebGUI::Test::Mechanize->new( config => WebGUI::Test->file ); +$mech->get_ok( '/' ); +$mech->session->user({userId => 3}); + +$mech->get_ok( '?op=editUser;uid=new' ); +my %fields = ( + username => 'AndyDufresne', + email => 'andy@shawshank.doj.gov', + alias => 'Randall Stevens', + status => 'Active', + ); +$mech->submit_form_ok({ + fields => { + %fields, + 'authWebGUI.identifier' => 'zihuatanejo', + groupsToAdd => '12', + }, + }, + "Add a new user", +); + +ok( my $user = WebGUI::User->newByUsername( $session, 'AndyDufresne' ), "user exists" ); +WebGUI::Test->addToCleanup( $user ); +is( $user->get('email'), $fields{email} ); +is( $user->get('alias'), $fields{alias} ); +is( $user->status, $fields{status} ); +ok( $user->isInGroup( 12 ) ); +my $auth = WebGUI::Auth::WebGUI->new( $session, $user ); +is( $auth->get('identifier'), $auth->hashPassword('zihuatanejo'), "password was set correctly" ); + +# Edit an existing user +$mech->get_ok( '?op=editUser;uid=' . $user->getId ); +%fields = ( + username => "EllisRedding", + email => 'red@shawshank.doj.gov', + alias => 'Red', + status => 'Active', +); +$mech->submit_form_ok({ + fields => { + %fields, + 'authWebGUI.identifier' => 'rehabilitated', + groupsToDelete => '12', + }, + }, + "Edit an existing user", +); + +ok( my $user = WebGUI::User->newByUsername( $mech->session, 'EllisRedding' ), "user exists" ); +is( $user->get('email'), $fields{email} ); +is( $user->get('alias'), $fields{alias} ); +is( $user->status, $fields{status} ); +ok( not $user->isInGroup( 12 ) ); +$auth = WebGUI::Auth::WebGUI->new( $session, $user ); +is( $auth->get('identifier'), $auth->hashPassword('rehabilitated'), "password was set correctly" ); + +#vim:ft=perl