Formularfelder und Session-Keys für Logins umbenannt
[kivitendo-erp.git] / bin / mozilla / login.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 ######################################################################
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #######################################################################
29
30 use DBI;
31 use SL::Auth;
32 use SL::User;
33 use SL::Form;
34
35 require "bin/mozilla/common.pl";
36 require "bin/mozilla/todo.pl";
37
38 use strict;
39
40 our $cgi;
41 our $form;
42 our $auth;
43
44 sub run {
45   $::lxdebug->enter_sub;
46   my $session_result = shift;
47
48   $form   = $::form;
49   $auth   = $::auth;
50
51   $form->{stylesheet} = "lx-office-erp.css";
52   $form->{favicon}    = "favicon.ico";
53
54   if (SL::Auth::SESSION_EXPIRED == $session_result) {
55     $form->{error_message} = $::locale->text('The session is invalid or has expired.');
56     login_screen();
57     ::end_of_request();
58   }
59   my $action = $form->{action};
60   if (!$action && $auth->{SESSION}->{login}) {
61     $action = 'login';
62   }
63   if ($action) {
64     $form->{login} = $form->{'{AUTH}login'} || $form->{login};
65     %::myconfig    = $auth->read_user(login => $form->{login}) if $form->{login};
66
67     $::locale   = Locale->new($::myconfig{countrycode}) if $::myconfig{countrycode};
68
69     if (SL::Auth::OK != $auth->authenticate($::myconfig{login}, $form->{'{AUTH}password'})) {
70       $form->{error_message} = $::locale->text('Incorrect username or password!');
71       login_screen();
72     } else {
73       $auth->create_or_refresh_session();
74       delete $form->{'{AUTH}password'};
75
76       $form->{titlebar} .= " - $::myconfig{name} - $::myconfig{dbname}";
77       call_sub($::locale->findsub($action));
78     }
79   } else {
80     login_screen();
81   }
82
83   $::lxdebug->leave_sub;
84 }
85
86 sub login_screen {
87   $main::lxdebug->enter_sub();
88   my ($msg) = @_;
89
90   if (-f "css/lx-office-erp.css") {
91     $form->{stylesheet} = "lx-office-erp.css";
92   }
93
94   $form->{msg} = $msg;
95   $form->header();
96
97   print $form->parse_html_template('login/login_screen');
98
99   $main::lxdebug->leave_sub();
100 }
101
102 sub login {
103   $main::lxdebug->enter_sub();
104
105   unless ($form->{login}) {
106     login_screen($::locale->text('You did not enter a name!'));
107     ::end_of_request();
108   }
109
110   my $user = User->new(login => $form->{login});
111
112   # if we get an error back, bale out
113   my $result;
114   if (($result = $user->login($form)) <= -1) {
115     if ($result == -3) {
116       show_error('login/auth_db_needs_update');
117       $::auth->destroy_session;
118       ::end_of_request();
119     }
120
121     ::end_of_request() if $result == -2;
122     login_screen($::locale->text('Incorrect username or password!'));
123     ::end_of_request();
124   }
125
126   my %style_to_script_map = (
127     v3  => 'v3',
128     neu => 'new',
129     v4  => 'v4',
130   );
131
132   my $menu_script = $style_to_script_map{$user->{menustyle}} || '';
133
134   # made it this far, execute the menu
135   # standard redirect does not seem to work for this invocation, (infinite loops?)
136   # do a manual invocation instead
137 #  $form->{callback} = build_std_url("script=menu${menu_script}.pl", 'action=display', "callback=" . $form->escape($form->{callback}));
138
139   $main::auth->set_cookie_environment_variable();
140
141   $::form->{script}   = "menu${menu_script}.pl";
142   $::form->{action}   = 'display';
143   $::form->{callback} = $::form->escape($::form->{callback});
144
145   require "bin/mozilla/$::form->{script}";
146   display();
147
148 #  $form->redirect();
149
150   $main::lxdebug->leave_sub();
151 }
152
153 sub logout {
154   $main::lxdebug->enter_sub();
155
156   $main::auth->destroy_session();
157
158   # remove the callback to display the message
159   $form->{callback} = "login.pl?action=";
160   $form->redirect($::locale->text('You are logged out!'));
161
162   $main::lxdebug->leave_sub();
163 }
164
165 sub company_logo {
166   $main::lxdebug->enter_sub();
167
168   my %myconfig = %main::myconfig;
169   $form->{todo_list}  =  create_todo_list('login_screen' => 1) if (!$form->{no_todo_list});
170
171   $form->{stylesheet} =  $myconfig{stylesheet};
172   $form->{title}      =  $::locale->text('kivitendo');
173   $form->{interface}  = $::dispatcher->interface_type;
174
175   # create the logo screen
176   $form->header() unless $form->{noheader};
177
178   print $form->parse_html_template('login/company_logo');
179
180   $main::lxdebug->leave_sub();
181 }
182
183 sub show_error {
184   my $template           = shift;
185   my %myconfig           = %main::myconfig;
186   $myconfig{countrycode} = $::lx_office_conf{system}->{language};
187   $form->{stylesheet}    = 'css/lx-office-erp.css';
188
189   $form->header();
190   print $form->parse_html_template($template);
191
192   # $form->parse_html_template('login/auth_db_unreachable');
193   # $form->parse_html_template('login/auth_db_needs_update');
194   # $form->parse_html_template('login/authentication_pl_missing');
195
196   ::end_of_request();
197 }
198
199 1;
200
201 __END__