Focus & Tabreihenfolge im User-Loginscreen fixen
[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 List::Util qw(first);
8
9 use SL::Dispatcher::AuthHandler::User;
10 use SL::DB::AuthClient;
11 use SL::DB::AuthGroup;
12 use SL::DB::AuthUser;
13 use SL::User;
14
15 use Rose::Object::MakeMethods::Generic (
16   'scalar --get_set_init' => [ qw(clients default_client_id) ],
17 );
18
19 __PACKAGE__->run_before('set_layout');
20
21 #
22 # actions
23 #
24
25 sub action_user_login {
26   my ($self) = @_;
27
28   # If the user is already logged in then redirect to the proper menu
29   # script.
30   return if $self->_redirect_to_main_script_if_already_logged_in;
31
32   # Otherwise show the login form.
33   $self->show_login_form(error => error_state($::form->{error}));
34 }
35
36 sub action_logout {
37   my ($self) = @_;
38
39   $::auth->destroy_session;
40   $::auth->create_or_refresh_session;
41   $self->show_login_form(error => $::locale->text('You are logged out!'));
42 }
43
44 sub action_login {
45   my ($self) = @_;
46
47   my $login     = $::form->{'{AUTH}login'}     || $::auth->get_session_value('login');
48   my $client_id = $::form->{'{AUTH}client_id'} || $::auth->get_session_value('client_id');
49   my $error     = t8('Incorrect username or password or no access to selected client!');
50
51   if (!$::auth->set_client($client_id)) {
52     $::auth->punish_wrong_login;
53     return $self->show_login_form(error => $error);
54   }
55
56   %::myconfig      = $login ? $::auth->read_user(login => $login) : ();
57   SL::Dispatcher::AuthHandler::User->new->handle(countrycode => $::myconfig{countrycode});
58   $::form->{login} = $::myconfig{login};
59   $::locale        = Locale->new($::myconfig{countrycode}) if $::myconfig{countrycode};
60   my $user         = User->new(login => $::myconfig{login});
61   $::request->{layout} = SL::Layout::Dispatcher->new(style => $user->{menustyle});
62
63   # if we get an error back, bale out
64   my $result = $user->login($::form);
65
66   # Database update available?
67   ::end_of_request() if -2 == $result;
68
69   # Auth DB needs update? If so log the user out forcefully.
70   if (-3 == $result) {
71     $::auth->destroy_session;
72     return $self->render('login_screen/auth_db_needs_update');
73   }
74
75   # Other login errors.
76   if (0 > $result) {
77     $::auth->punish_wrong_login;
78     return $self->show_login_form(error => $error);
79   }
80
81   # Everything is fine.
82   $::auth->set_cookie_environment_variable();
83
84   # TODO: Employees anlegen/checken
85   # $self->_ensure_employees_for_authorized_users_exist;
86
87   $self->_redirect_to_main_script($user);
88 }
89
90 #
91 # settings
92 #
93 sub get_auth_level {
94   return 'none';
95 }
96
97 sub keep_auth_vars_in_form {
98   return 1;
99 }
100
101 #
102 # private methods
103 #
104
105 sub _redirect_to_main_script {
106   my ($self, $user) = @_;
107
108   return $self->redirect_to($::form->{callback}) if $::form->{callback};
109
110   $self->redirect_to(controller => "login.pl", action => 'company_logo');
111 }
112
113 sub _redirect_to_main_script_if_already_logged_in {
114   my ($self) = @_;
115
116   # Get 'login' from valid session.
117   my $login = $::auth->get_session_value('login');
118   return unless $login;
119
120   # See whether or not the user exists in the database.
121   my %user = $::auth->read_user(login => $login);
122   return if ($user{login} || '') ne $login;
123
124   # Check if the session is logged in correctly.
125   return if SL::Auth::OK() != $::auth->authenticate($login, undef);
126
127   $::auth->create_or_refresh_session;
128   $::auth->delete_session_value('FLASH');
129
130   $self->_redirect_to_main_script(\%user);
131
132   return 1;
133 }
134
135 sub error_state {
136   return {
137     session  => $::locale->text('The session is invalid or has expired.'),
138     password => $::locale->text('Incorrect password!'),
139   }->{$_[0]};
140 }
141
142 sub set_layout {
143   $::request->{layout} = SL::Layout::Dispatcher->new(style => 'login');
144 }
145
146 sub init_clients {
147   return SL::DB::Manager::AuthClient->get_all_sorted;
148 }
149
150 sub init_default_client_id {
151   my ($self)         = @_;
152   my $default_client = first { $_->is_default } @{ $self->clients };
153   return $default_client ? $default_client->id : undef;
154 }
155
156 sub show_login_form {
157   my ($self, %params) = @_;
158
159   $::request->layout->focus('#auth_login');
160   $self->render('login_screen/user_login', %params);
161 }
162
163 1;