Umstellung der Benutzerverwaltung von Dateien im Verzeichnis "users" auf die Verwendu...
[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
37 # This is required because the am.pl in the root directory
38 # is not scanned by locales.pl:
39 # $form->parse_html_template('login/password_error')
40
41 $form = new Form;
42
43 if (! -f 'config/authentication.pl') {
44   show_error('login/authentication_pl_missing');
45 }
46
47 $locale = new Locale $language, "login";
48
49 our $auth = SL::Auth->new();
50 if (!$auth->session_tables_present()) {
51   show_error('login/auth_db_unreachable');
52 }
53 $auth->expire_sessions();
54 $auth->restore_session();
55
56 # customization
57 if (-f "bin/mozilla/custom_$form->{script}") {
58   eval { require "bin/mozilla/custom_$form->{script}"; };
59   $form->error($@) if ($@);
60 }
61
62 # per login customization
63 if (-f "bin/mozilla/$form->{login}_$form->{script}") {
64   eval { require "bin/mozilla/$form->{login}_$form->{script}"; };
65   $form->error($@) if ($@);
66 }
67
68 # window title bar, user info
69 $form->{titlebar} = "Lx-Office " . $locale->text('Version') . " $form->{version}";
70
71 if ($form->{action}) {
72   our %myconfig = $auth->read_user($form->{login}) if ($form->{login});
73
74   if (!$myconfig{login} || (SL::Auth::OK != $auth->authenticate($form->{login}, $form->{password}, 0))) {
75     $form->{error_message} = $locale->text('Incorrect Password!');
76     login_screen();
77     exit;
78   }
79
80   $auth->set_session_value('login', $form->{login}, 'password', $form->{password});
81   $auth->create_or_refresh_session();
82
83   $form->{titlebar} .= " - $myconfig{name} - $myconfig{dbname}";
84   call_sub($locale->findsub($form->{action}));
85
86 } else {
87   login_screen();
88 }
89
90 1;
91
92 sub login_screen {
93   $lxdebug->enter_sub();
94   my ($msg) = @_;
95
96   if (-f "css/lx-office-erp.css") {
97     $form->{stylesheet} = "lx-office-erp.css";
98   }
99
100   $form->{msg}   = $msg;
101   $form->{fokus} = "loginscreen.login";
102   $form->header;
103
104   print $form->parse_html_template('login/login_screen');
105
106   $lxdebug->leave_sub();
107 }
108
109 sub login {
110   $lxdebug->enter_sub();
111
112   unless ($form->{login}) {
113     login_screen($locale->text('You did not enter a name!'));
114     exit;
115   }
116
117   $user = new User $form->{login};
118
119   # if we get an error back, bale out
120   if (($result = $user->login($form)) <= -1) {
121     exit if $result == -2;
122     login_screen($locale->text('Incorrect username or password!'));
123     exit;
124   }
125
126   my %style_to_script_map = ( 'v3'  => 'v3',
127                               'neu' => 'new',
128                               'xml' => 'XML',
129     );
130
131   my $menu_script = $style_to_script_map{$user->{menustyle}} || '';
132
133   # made it this far, execute the menu
134   $form->{callback} = build_std_url("script=menu${menu_script}.pl", 'action=display');
135
136   $auth->set_cookie_environment_variable();
137
138   $form->redirect();
139
140   $lxdebug->leave_sub();
141 }
142
143 sub logout {
144   $lxdebug->enter_sub();
145
146   $auth->destroy_session();
147
148   # remove the callback to display the message
149   $form->{callback} = "login.pl?action=";
150   $form->redirect($locale->text('You are logged out!'));
151
152   $lxdebug->leave_sub();
153 }
154
155 sub company_logo {
156   $lxdebug->enter_sub();
157
158   $locale             =  new Locale $myconfig{countrycode}, "login" if ($language ne $myconfig{countrycode});
159
160   $form->{stylesheet} =  $myconfig{stylesheet};
161   $form->{title}      =  $locale->text('About');
162
163   # create the logo screen
164   $form->header() unless $form->{noheader};
165
166   print $form->parse_html_template('login/company_logo');
167
168   $lxdebug->leave_sub();
169 }
170
171 sub show_error {
172   my $template           = shift;
173   $locale                = Locale->new($language, 'all');
174   $myconfig{countrycode} = $language;
175   $form->{stylesheet}    = 'css/lx-office-erp.css';
176
177   $form->header();
178   print $form->parse_html_template($template);
179
180   # $form->parse_html_template('login/auth_db_unreachable');
181   # $form->parse_html_template('login/authentication_pl_missing');
182
183   exit;
184 }