Keine Passwörter mehr in Sessions speichern.
[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     %::myconfig = $auth->read_user(login => $form->{login}) if ($form->{login});
65     $::locale   = Locale->new($::myconfig{countrycode}) if $::myconfig{countrycode};
66
67     if (SL::Auth::OK != $auth->authenticate($::myconfig{login}, $form->{password})) {
68       $form->{error_message} = $::locale->text('Incorrect username or password!');
69       login_screen();
70     } else {
71       $auth->create_or_refresh_session();
72       delete $form->{password};
73
74       $form->{titlebar} .= " - $::myconfig{name} - $::myconfig{dbname}";
75       call_sub($::locale->findsub($action));
76     }
77   } else {
78     login_screen();
79   }
80
81   $::lxdebug->leave_sub;
82 }
83
84 sub login_screen {
85   $main::lxdebug->enter_sub();
86   my ($msg) = @_;
87
88   if (-f "css/lx-office-erp.css") {
89     $form->{stylesheet} = "lx-office-erp.css";
90   }
91
92   $form->{msg} = $msg;
93   $form->header();
94
95   print $form->parse_html_template('login/login_screen');
96
97   $main::lxdebug->leave_sub();
98 }
99
100 sub login {
101   $main::lxdebug->enter_sub();
102
103   unless ($form->{login}) {
104     login_screen($::locale->text('You did not enter a name!'));
105     ::end_of_request();
106   }
107
108   my $user = User->new(login => $form->{login});
109
110   # if we get an error back, bale out
111   my $result;
112   if (($result = $user->login($form)) <= -1) {
113     if ($result == -3) {
114       show_error('login/auth_db_needs_update');
115       $::auth->destroy_session;
116       ::end_of_request();
117     }
118
119     ::end_of_request() if $result == -2;
120     login_screen($::locale->text('Incorrect username or password!'));
121     ::end_of_request();
122   }
123
124   my %style_to_script_map = (
125     v3  => 'v3',
126     neu => 'new',
127     v4  => 'v4',
128   );
129
130   my $menu_script = $style_to_script_map{$user->{menustyle}} || '';
131
132   # made it this far, execute the menu
133   # standard redirect does not seem to work for this invocation, (infinite loops?)
134   # do a manual invocation instead
135 #  $form->{callback} = build_std_url("script=menu${menu_script}.pl", 'action=display', "callback=" . $form->escape($form->{callback}));
136
137   $main::auth->set_cookie_environment_variable();
138
139   $::form->{script}   = "menu${menu_script}.pl";
140   $::form->{action}   = 'display';
141   $::form->{callback} = $::form->escape($::form->{callback});
142
143   require "bin/mozilla/$::form->{script}";
144   display();
145
146 #  $form->redirect();
147
148   $main::lxdebug->leave_sub();
149 }
150
151 sub logout {
152   $main::lxdebug->enter_sub();
153
154   $main::auth->destroy_session();
155
156   # remove the callback to display the message
157   $form->{callback} = "login.pl?action=";
158   $form->redirect($::locale->text('You are logged out!'));
159
160   $main::lxdebug->leave_sub();
161 }
162
163 sub company_logo {
164   $main::lxdebug->enter_sub();
165
166   my %myconfig = %main::myconfig;
167   $form->{todo_list}  =  create_todo_list('login_screen' => 1) if (!$form->{no_todo_list});
168
169   $form->{stylesheet} =  $myconfig{stylesheet};
170   $form->{title}      =  $::locale->text('kivitendo');
171   $form->{interface}  = $::dispatcher->interface_type;
172
173   # create the logo screen
174   $form->header() unless $form->{noheader};
175
176   print $form->parse_html_template('login/company_logo');
177
178   $main::lxdebug->leave_sub();
179 }
180
181 sub show_error {
182   my $template           = shift;
183   my %myconfig           = %main::myconfig;
184   $myconfig{countrycode} = $::lx_office_conf{system}->{language};
185   $form->{stylesheet}    = 'css/lx-office-erp.css';
186
187   $form->header();
188   print $form->parse_html_template($template);
189
190   # $form->parse_html_template('login/auth_db_unreachable');
191   # $form->parse_html_template('login/auth_db_needs_update');
192   # $form->parse_html_template('login/authentication_pl_missing');
193
194   ::end_of_request();
195 }
196
197 1;
198
199 __END__