1 package SL::Controller::LoginScreen;
5 use parent qw(SL::Controller::Base);
7 use List::Util qw(first);
9 use SL::Dispatcher::AuthHandler::User;
10 use SL::DB::AuthClient;
11 use SL::DB::AuthGroup;
14 use SL::Locale::String qw(t8);
17 use Rose::Object::MakeMethods::Generic (
18 'scalar --get_set_init' => [ qw(clients default_client_id) ],
21 __PACKAGE__->run_before('set_layout');
27 sub action_user_login {
30 # If the user is already logged in then redirect to the proper menu
32 return if $self->_redirect_to_main_script_if_already_logged_in;
34 # Otherwise show the login form.
35 $self->show_login_form(error => error_state($::form->{error}));
41 $::auth->destroy_session;
42 $::auth->create_or_refresh_session;
43 $self->show_login_form(info => $::locale->text('You are logged out!'));
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!');
53 if (!$::auth->set_client($client_id)) {
54 $::auth->punish_wrong_login;
55 return $self->show_login_form(error => $error);
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});
63 $::request->layout(SL::Layout::Dispatcher->new(style => $::myconfig{menustyle}));
65 # if we get an error back, bale out
66 my $result = User->new(login => $::myconfig{login})->login($::form);
68 # Database update available?
69 ::end_of_request() if -2 == $result;
71 # Auth DB needs update? If so log the user out forcefully.
73 $::auth->destroy_session;
74 return $self->render('login_screen/auth_db_needs_update');
79 $::auth->punish_wrong_login;
80 return $self->show_login_form(error => $error);
84 $::auth->set_cookie_environment_variable();
86 $self->_redirect_to_main_script;
96 sub keep_auth_vars_in_form {
104 sub _redirect_to_main_script {
107 $self->_ensure_employees_for_authorized_users_exist;
109 return $self->redirect_to($::form->{callback}) if $::form->{callback};
111 $self->redirect_to(controller => "login.pl", action => 'company_logo');
114 sub _redirect_to_main_script_if_already_logged_in {
117 # Get 'login' from valid session.
118 my $login = $::auth->get_session_value('login');
119 return unless $login;
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;
125 # Check if the session is logged in correctly.
126 return if SL::Auth::OK() != $::auth->authenticate($login, undef);
128 $::auth->create_or_refresh_session;
129 $::auth->delete_session_value('FLASH');
131 $self->_redirect_to_main_script(\%user);
136 sub _ensure_employees_for_authorized_users_exist {
139 my %employees_by_login = map { ($_->login => $_) } @{ SL::DB::Manager::Employee->get_all };
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);
145 $employee->update_attributes(
146 name => $user_config->{name},
147 workphone => $user_config->{tel},
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!'),
161 $::request->{layout} = SL::Layout::Dispatcher->new(style => 'login');
165 return SL::DB::Manager::AuthClient->get_all_sorted;
168 sub init_default_client_id {
170 my $default_client = first { $_->is_default } @{ $self->clients };
171 return $default_client ? $default_client->id : undef;
174 sub show_login_form {
175 my ($self, %params) = @_;
177 $::request->layout->focus('#auth_login');
178 $self->render('login_screen/user_login', %params);