package TTXLogin;
#
# This module is a part of Trouble Ticket Express package
# http://www.troubleticketexpress.com
#
# COPYRIGHT: 2002-2005, United Web Coders
# http://www.unitedwebcoders.com
#
# $Revision: 224 $
# $Date: 2006-07-24 11:32:24 -0400 (Mon, 24 Jul 2006) $
#

$TTXLogin::VERSION='2.22';
BEGIN {
  $TTXLogin::REVISION = '$Revision: 224 $';
  if ($TTXLogin::REVISION =~ /(\d+)/) {
    $TTXLogin::REVISION = $1;
  }
};
use strict;
use TTXCommon;
require TTXUser;
require TTXSession;

# ======================================================================== login

sub login {
  my ($cfg, $query, $data) = @_;
  $data->{PAGEHEADING} = '[%Login%]';
  return undef if !$query->param('dologin');
  if (TTXCommon::cleanit($query, 'login') eq undef) {
    $data->{ERROR_MESSAGE} = '[%Missing User ID%]';
    return undef;
  }
  if (TTXCommon::cleanit($query, 'passwd') eq undef) {
    $data->{ERROR_MESSAGE} = '[%Missing Password%]';
    return undef;
  }
  my $user = TTXUser->new($query->param('login'));
  if ($user eq undef || ($user->get('passwd') eq undef) || ($user->get('passwd') ne $query->param('passwd'))) {
    $data->{ERROR_MESSAGE} = '[%Wrong User ID or Password%]';
    return undef;
  }
  $cfg->set('_USER', $user);
  my $session = TTXSession->new();
  $session->login($query->param('login'));
  $user->set('session', $session);
  $query->param(-name => 'checkupdate', -value => '1');
  $query->param(-name => 'sid', -value => $session->sid());
  if ($query->param('nextcmd') ne undef) {
    $query->param(-name => 'cmd', -value => $query->param('nextcmd'));
    return $query->param('nextcmd');
  }
  return 'helpdesk';
}

1;
#

