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