Layouts eingeführt
[kivitendo-erp.git] / SL / Controller / LoginScreen.pm
1 package SL::Controller::LoginScreen;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::Dispatcher::AuthHandler::User;
8 use SL::User;
9
10 #
11 # actions
12 #
13
14 sub action_user_login {
15   my ($self) = @_;
16
17   # If the user is already logged in then redirect to the proper menu
18   # script.
19   return if $self->_redirect_to_main_script_if_already_logged_in;
20
21   # Otherwise show the login form.
22   $self->render('login_screen/user_login', { no_menu => 1 }, error => error_state($::form->{error}));
23 }
24
25 sub action_logout {
26   my ($self) = @_;
27
28   $::auth->destroy_session;
29   $::auth->create_or_refresh_session;
30   $self->render('login_screen/user_login', { no_menu => 1 }, error => $::locale->text('You are logged out!'));
31 }
32
33 sub action_login {
34   my ($self) = @_;
35
36   %::myconfig      = $::form->{'{AUTH}login'} ? $::auth->read_user(login => $::form->{'{AUTH}login'}) : ();
37   %::myconfig      = SL::Dispatcher::AuthHandler::User->new->handle(countrycode => $::myconfig{countrycode});
38   $::form->{login} = $::myconfig{login};
39   $::locale        = Locale->new($::myconfig{countrycode}) if $::myconfig{countrycode};
40   my $user         = User->new(login => $::myconfig{login});
41   $::request->{layout} = SL::Controller::Layout->new(style => $user->{menustyle});
42
43   # if we get an error back, bale out
44   my $result = $user->login($::form);
45
46   # Database update available?
47   ::end_of_request() if -2 == $result;
48
49   # Auth DB needs update? If so log the user out forcefully.
50   if (-3 == $result) {
51     $::auth->destroy_session;
52     return $self->render('login_screen/auth_db_needs_update');
53   }
54
55   # Other login errors.
56   if (0 > $result) {
57     $::auth->punish_wrong_login;
58     return $self->render('login_screen/user_login', { no_menu => 1 }, error => $::locale->text('Incorrect username or password!'));
59   }
60
61   # Everything is fine.
62   $::auth->set_cookie_environment_variable();
63
64   $self->_redirect_to_main_script($user);
65 }
66
67 #
68 # settings
69 #
70 sub get_auth_level {
71   return 'none';
72 }
73
74 sub keep_auth_vars_in_form {
75   return 1;
76 }
77
78 #
79 # private methods
80 #
81
82 sub _redirect_to_main_script {
83   my ($self, $user) = @_;
84
85   return $self->redirect_to($::form->{callback}) if $::form->{callback};
86
87   $self->redirect_to(controller => "login.pl", action => 'company_logo');
88 }
89
90 sub _redirect_to_main_script_if_already_logged_in {
91   my ($self) = @_;
92
93   # Get 'login' from valid session.
94   my $login = $::auth->get_session_value('login');
95   return unless $login;
96
97   # See whether or not the user exists in the database.
98   my %user = $::auth->read_user(login => $login);
99   return if ($user{login} || '') ne $login;
100
101   # Check if the session is logged in correctly.
102   return if SL::Auth::OK() != $::auth->authenticate($login, undef);
103
104   $::auth->create_or_refresh_session;
105   $::auth->delete_session_value('FLASH');
106
107   $self->_redirect_to_main_script(\%user);
108
109   return 1;
110 }
111
112 sub error_state {
113   return {
114     session  => $::locale->text('The session is invalid or has expired.'),
115     password => $::locale->text('Incorrect password!'),
116   }->{$_[0]};
117 }
118
119 1;