#!/usr/bin/perl -wT

# vim: ft=xml ts=4 sw=4 et:
#----------------------------------------------------------------------
# heading     : Collaboration
# description : Users
# navigation  : 2000 2100
#----------------------------------------------------------------------
#----------------------------------------------------------------------
# copyright (C) 2002 Mitel Networks Corporation
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
# 
# Technical support for this program is available from Mitel Networks 
# Please visit our web site www.mitel.com/sme/ for details.
#----------------------------------------------------------------------

use strict;
use esmith::TestUtils;
use esmith::FormMagick::Panel::useraccounts;

my $fm = esmith::FormMagick::Panel::useraccounts->new();

# XXX: need to print custom http headers, so bypass FormMagick
use CGI;
my $q = new CGI;
if ($q->param('action') && $q->param('action') eq 'getCert')
{
    $q->delete('action');
    $fm->get_ipsec_client_cert($q);
}
else
{
    $fm->display();
}

=pod

=head1 NAME

useraccounts -- create/modify/delete user accounts

=head2 DESCRIPTION

This screen allows the administrator to create, modify or delete user
accounts on the system.

=begin testing

use esmith::FormMagick::Tester;
use esmith::TestUtils;
use esmith::ConfigDB;
use esmith::AccountsDB;

my $panel = $Original_File;
my $ua = esmith::FormMagick::Tester->new();

my $c = esmith::ConfigDB->open();
my $a = esmith::AccountsDB->open();

is  (mode($panel), '4755',              "Check permissions on script");
ok  ($ua->get_panel($panel),            "ABOUT TO RUN L10N TESTS");
is  ($ua->{status}, 200,                "200 OK");
like($ua->{content}, qr/FORM_TITLE/,    "Saw untranslated form title");
ok  ($ua->set_language("en-us"),        "Set language to U.S. English");
ok  ($ua->get_panel($panel),            "Get panel");
is  ($ua->{status}, 200,                "200 OK");
like($ua->{content}, qr/User accounts/, "Saw translated form title");


#
# Testing creating users
#

ok  ($ua->get_panel($panel),            "ABOUT TO TEST CREATING USER");
ok  ($ua->follow("Click here"),         "Follow 'create user' link");
is  ($ua->{status}, 200,                "200 OK");
like($ua->{content}, qr/Account name/,  "Saw form fields");

#
# Check that address details are picked up from LDAP
#

my $ldap_record = $c->get('ldap');
my $city = $ldap_record->prop('DefaultCity');

like($ua->{content}, qr/$city/, "Pick up address from LDAP");

#
# Creating a new user
#

my $new_username = new_random_username();

can_ok($ua, "field");
ok  ($ua->{form}->find_input('acctName'), 
                                        "Find acctName field to fill in");
$ua->field("acctName" => $new_username);
ok  ($ua->click("Save"),                "Click Save");
is  ($ua->{status}, 200,                "200 OK");
like($ua->{content}, qr/must not be left blank/, "Saw validation messages");

$ua->field("FirstName" => "Fred");
$ua->field("LastName"  => "Foonly");

ok  ($ua->click("Save"),                "Click Save");
is  ($ua->{status}, 200,                "200 OK");

#
# Testing modify user
#

ok  ($ua->get_panel($panel),            "ABOUT TO TEST MODIFYING A USER");
is  ($ua->{status}, 200,                "200 OK");
ok  ($ua->follow('Modify'),             "Follow modify link");
is  ($ua->{status}, 200,                "200 OK");
like($ua->{content}, qr/value="Save"/,  "Saw 'Save' on the button");

#
# Testing removal of a user
#

ok  ($ua->get_panel($panel),            "ABOUT TO TEST USER REMOVAL");
is  ($ua->{status}, 200,                "200 OK");
ok  ($ua->follow('Remove'),             "Follow remove link");
is  ($ua->{status}, 200,                "200 OK");
like($ua->{content}, qr/Remove/,        "Saw 'Remove'");
like($ua->{content}, qr/value="Remove"/, "Saw 'Remove' on the button");

#
# Testing password reset
#

ok  ($ua->get_panel($panel),            "ABOUT TO TEST PASSWORD RESET");
is  ($ua->{status}, 200,                "200 OK");
ok  ($ua->follow('Reset password'),     "Follow reset password link");
like($ua->{content}, qr/Reset password/,"Saw 'Reset password'");

SKIP:
{
    skip 3, "Unsafe!" unless destruction_ok();
    $ua->field(password1 => "test");
    $ua->field(password2 => "test");
    ok($ua->click('Save'), "Set password to 'test', click Save");
    is($ua->{status}, 200, "200 OK");
    like($ua->{content}, qr/Click here/, "Returned to first page");
}

=end testing

=cut


__DATA__
<form title="FORM_TITLE" header="/etc/e-smith/web/common/head.tmpl" footer="/etc/e-smith/web/common/foot.tmpl">
    <page name="First" pre-event="print_status_message()">
        <description>FIRSTPAGE_DESC</description>
        <subroutine src="print_user_table()" />
    </page>
    <page name="CreateModify" pre-event="turn_off_buttons()" post-event="handle_user_accounts()">
        <title>P2_TITLE</title> 
        <description>CREATE_MODIFY_DESC</description>
        <subroutine src="print_acctName_field()" />
        <field type="text" id="FirstName" validation="nonblank, pseudonym_clash">
            <label>FIRSTNAME</label>
        </field>
        <field type="text" id="LastName" validation="nonblank">
            <label>LASTNAME</label>
        </field>
        <field type="text" id="Dept" 
            value="get_ldap_value('Dept')">
            <label>DEPARTMENT</label>
        </field>
        <field type="text" id="Company"
            value="get_ldap_value('Company')">
            <label>COMPANY</label>
        </field>
        <field type="text" id="Street"
            value="get_ldap_value('Street')">
            <label>STREET_ADDRESS</label>
        </field>
        <field type="text" id="City"
            value="get_ldap_value('City')">
            <label>CITY</label>
        </field>
        <field type="text" id="Phone"
            value="get_ldap_value('Phone')">
            <label>PHONE_NUMBER</label>
        </field>
        <field type="select" id="EmailForward" options="'local' =>
            'DELIVER_EMAIL_LOCALLY', 'forward' => 'FORWARD_EMAIL',
            'both' => 'DELIVER_AND_FORWARD'" validation="nonblank" value='local'
            display="display_email_forwarding()"
            >
            <label>EMAIL_DELIVERY</label>
        </field>
        <field type="text" id="ForwardAddress" validation="emailforward()"
            display="display_email_forwarding()"
            >
            <label>FORWARDING_ADDRESS</label>
        </field>
        <field type="select" id="VPNClientAccess" options="'yes' => 'YES',
            'no' => 'NO'" validation="nonblank" value="get_vpn_value()">
            <label>VPN_CLIENT_ACCESS</label>
        </field>
        <subroutine src="print_ipsec_client_section()" />
        <subroutine src="print_groupMemberships_field()" />
        <subroutine src="print_save_or_add_button()" />
    </page>
    <page name="ModifyAdmin" pre-event="turn_off_buttons()" post-event="modify_admin()">
        <title>MODIFY_ADMIN_TITLE</title> 
        <subroutine src="print_acctName_field()" />
        <field type="text" id="FirstName" validation="nonblank, pseudonym_clash">
            <label>FIRSTNAME</label>
        </field>
        <field type="text" id="LastName" validation="nonblank">
            <label>LASTNAME</label>
        </field>
        <field type="select" id="EmailForward" options="'local' =>
            'DELIVER_EMAIL_LOCALLY', 'forward' => 'FORWARD_EMAIL',
            'both' => 'DELIVER_AND_FORWARD'" validation="nonblank" value='local'
            display="display_email_forwarding()"
            >
            <label>EMAIL_DELIVERY</label>
        </field>
        <field type="text" id="ForwardAddress" validation="emailforward()"
            display="display_email_forwarding()"
            >
            <label>FORWARDING_ADDRESS</label>
        </field>
        <field type="select" id="VPNClientAccess" options="'yes' => 'YES',
            'no' => 'NO'" validation="nonblank" value="get_vpn_value()">
            <label>VPN_CLIENT_ACCESS</label>
        </field>  
        <subroutine src="print_ipsec_client_section()" />
        <subroutine src="print_button('SAVE')" />
    </page>
    <page name="ResetPasswordDummy">
    </page>
    <page name="ResetPassword" pre-event="turn_off_buttons()" post-event="reset_password()">
        <title>RESET_PASSWORD_TITLE</title>
        <subroutine src="print_page_description('reset')" />
        <field type="password" id="password1" validation="nonblank, check_password">
            <label>PASSWORD_NEW</label>
        </field>
        <field type="password" id="password2" validation="verifyPasswords">
            <label>PASSWORD_VERIFY_NEW</label>
        </field>
        <subroutine src="print_button('SAVE')" />
    </page>
    <page name="LockAccount" pre-event="turn_off_buttons()" post-event="lock_account()">
        <title>LOCK_ACCOUNT_TITLE</title>
        <subroutine src="print_page_description('lock')" />
        <subroutine src="print_button('LOCK')" />
    </page>
    <page name="RemoveAccount" pre-event="turn_off_buttons()" post-event="remove_account()">
        <title>REMOVE_ACCOUNT_TITLE</title>
        <subroutine src="print_page_description('remove')" />
        <subroutine src="print_button('REMOVE')" />
    </page>

    <page name="SystemPasswordDummy">
    </page>
    <page name="SystemPassword" pre-event="turn_off_buttons()" 
        post-event="system_change_password" >
        <description>SYSTEM_PASSWORD_DESCRIPTION</description>

        <field type="password" id="curpass" validation="nonblank, system_authenticate_password">
            <label>CURRENT_SYSTEM_PASSWORD</label>
        </field>
        <field type="password" id="pass" validation="nonblank, system_check_password">
            <label>NEW_SYSTEM_PASSWORD</label>
        </field>
        <field type="password" id="passVerify" validation="system_password_compare">
            <label>NEW_SYSTEM_PASSWORD_VERIFY</label>
        </field>
        <subroutine src="print_button('SAVE')" />
    </page>
</form>