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