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