1 package SL::Controller::Admin;
5 use parent qw(SL::Controller::Base);
8 use List::Util qw(first);
11 use SL::DB::AuthGroup;
13 use SL::Helper::Flash;
14 use SL::Locale::String qw(t8);
15 use SL::System::InstallationLock;
18 use Rose::Object::MakeMethods::Generic
20 'scalar --get_set_init' => [ qw(client user group printer db_cfg is_locked
21 all_dateformats all_numberformats all_countrycodes all_stylesheets all_menustyles all_clients all_groups all_users all_rights all_printers) ],
24 __PACKAGE__->run_before(\&setup_layout);
25 __PACKAGE__->run_before(\&setup_client, only => [ qw(list_printers new_printer edit_printer save_printer delete_printer) ]);
27 sub get_auth_level { "admin" };
29 my ($class, %params) = @_;
30 return $params{action} eq 'login';
34 # actions: login, logout
40 return $self->login_form if !$::form->{do_login};
41 return if !$self->authenticate_root;
42 return if !$self->check_auth_db_and_tables;
43 return if $self->apply_dbupgrade_scripts;
45 $self->redirect_to(action => 'show');
50 $::auth->destroy_session;
51 $self->redirect_to(action => 'login');
55 # actions: creating the authentication database & tables, applying database ugprades
58 sub action_apply_dbupgrade_scripts {
61 return if $self->apply_dbupgrade_scripts;
62 $self->redirect_to(action => 'show');
65 sub action_create_auth_db {
68 $::auth->create_database(superuser => $::form->{db_superuser},
69 superuser_password => $::form->{db_superuser_password},
70 template => $::form->{db_template});
71 $self->check_auth_db_and_tables;
74 sub action_create_auth_tables {
77 $::auth->create_tables;
78 $::auth->set_session_value('admin_password', $::lx_office_conf{authentication}->{admin_password});
79 $::auth->create_or_refresh_session;
81 my $group = (SL::DB::Manager::AuthGroup->get_all(limit => 1))[0];
83 SL::DB::AuthGroup->new(
84 name => t8('Full Access'),
85 description => t8('Full access to all functions'),
86 rights => [ map { SL::DB::AuthGroupRight->new(right => $_, granted => 1) } SL::Auth::all_rights() ],
90 if (!$self->apply_dbupgrade_scripts) {
104 title => "kivitendo " . t8('Administration'),
108 sub action_new_user {
111 $self->user(SL::DB::AuthUser->new(
115 numberformat => "1.000,00",
116 dateformat => "dd.mm.yy",
117 stylesheet => "kivitendo.css",
122 $self->edit_user_form(title => t8('Create a new user'));
125 sub action_edit_user {
127 $self->edit_user_form(title => t8('Edit User'));
130 sub action_save_user {
132 my $params = delete($::form->{user}) || { };
133 my $props = delete($params->{config_values}) || { };
134 my $is_new = !$params->{id};
136 $self->user($is_new ? SL::DB::AuthUser->new : SL::DB::AuthUser->new(id => $params->{id})->load)
137 ->assign_attributes(%{ $params })
138 ->config_values({ %{ $self->user->config_values }, %{ $props } });
140 my @errors = $self->user->validate;
143 flash('error', @errors);
144 $self->edit_user_form(title => $is_new ? t8('Create a new user') : t8('Edit User'));
150 if ($::auth->can_change_password && $::form->{new_password}) {
151 $::auth->change_password($self->user->login, $::form->{new_password});
154 flash_later('info', $is_new ? t8('The user has been created.') : t8('The user has been saved.'));
155 $self->redirect_to(action => 'show');
158 sub action_delete_user {
161 my @clients = @{ $self->user->clients || [] };
163 if (!$self->user->delete) {
164 flash('error', t8('The user could not be deleted.'));
165 $self->edit_user_form(title => t8('Edit User'));
169 # Flag corresponding entries in 'employee' as deleted.
170 foreach my $client (@clients) {
171 my $dbh = $client->dbconnect(AutoCommit => 1) || next;
172 $dbh->do(qq|UPDATE employee SET deleted = TRUE WHERE login = ?|, undef, $self->user->login);
176 flash_later('info', t8('The user has been deleted.'));
177 $self->redirect_to(action => 'show');
184 sub action_new_client {
187 $self->client(SL::DB::AuthClient->new(
188 dbhost => $::auth->{DB_config}->{host},
189 dbport => $::auth->{DB_config}->{port},
190 dbuser => $::auth->{DB_config}->{user},
191 dbpasswd => $::auth->{DB_config}->{password},
194 $self->edit_client_form(title => t8('Create a new client'));
197 sub action_edit_client {
199 $self->edit_client_form(title => t8('Edit Client'));
202 sub action_save_client {
204 my $params = delete($::form->{client}) || { };
205 my $is_new = !$params->{id};
207 $self->client($is_new ? SL::DB::AuthClient->new : SL::DB::AuthClient->new(id => $params->{id})->load)->assign_attributes(%{ $params });
209 my @errors = $self->client->validate;
212 flash('error', @errors);
213 $self->edit_client_form(title => $is_new ? t8('Create a new client') : t8('Edit Client'));
218 if ($self->client->is_default) {
219 SL::DB::Manager::AuthClient->update_all(set => { is_default => 0 }, where => [ '!id' => $self->client->id ]);
222 flash_later('info', $is_new ? t8('The client has been created.') : t8('The client has been saved.'));
223 $self->redirect_to(action => 'show');
226 sub action_delete_client {
229 if (!$self->client->delete) {
230 flash('error', t8('The client could not be deleted.'));
231 $self->edit_client_form(title => t8('Edit Client'));
235 flash_later('info', t8('The client has been deleted.'));
236 $self->redirect_to(action => 'show');
239 sub action_test_database_connectivity {
242 my %cfg = %{ $::form->{client} || {} };
243 my $dbconnect = 'dbi:Pg:dbname=' . $cfg{dbname} . ';host=' . $cfg{dbhost} . ';port=' . $cfg{dbport};
244 my $dbh = DBI->connect($dbconnect, $cfg{dbuser}, $cfg{dbpasswd});
247 my $error = $DBI::errstr;
249 $dbh->disconnect if $dbh;
251 $self->render('admin/test_db_connection',
252 title => t8('Database Connection Test'),
261 sub action_new_group {
264 $self->group(SL::DB::AuthGroup->new);
265 $self->edit_group_form(title => t8('Create a new group'));
268 sub action_edit_group {
270 $self->edit_group_form(title => t8('Edit User Group'));
273 sub action_save_group {
276 my $params = delete($::form->{group}) || { };
277 my $is_new = !$params->{id};
279 $self->group($is_new ? SL::DB::AuthGroup->new : SL::DB::AuthGroup->new(id => $params->{id})->load)->assign_attributes(%{ $params });
281 my @errors = $self->group->validate;
284 flash('error', @errors);
285 $self->edit_group_form(title => $is_new ? t8('Create a new user group') : t8('Edit User Group'));
291 flash_later('info', $is_new ? t8('The user group has been created.') : t8('The user group has been saved.'));
292 $self->redirect_to(action => 'show');
295 sub action_delete_group {
298 if (!$self->group->delete) {
299 flash('error', t8('The user group could not be deleted.'));
300 $self->edit_group_form(title => t8('Edit User Group'));
304 flash_later('info', t8('The user group has been deleted.'));
305 $self->redirect_to(action => 'show');
312 sub action_list_printers {
314 $self->render('admin/list_printers', title => t8('Printer management'));
317 sub action_new_printer {
320 $self->printer(SL::DB::Printer->new);
321 $self->edit_printer_form(title => t8('Create a new printer'));
324 sub action_edit_printer {
326 $self->edit_printer_form(title => t8('Edit Printer'));
329 sub action_save_printer {
331 my $params = delete($::form->{printer}) || { };
332 my $is_new = !$params->{id};
334 $self->printer($is_new ? SL::DB::Printer->new : SL::DB::Printer->new(id => $params->{id})->load)->assign_attributes(%{ $params });
336 my @errors = $self->printer->validate;
339 flash('error', @errors);
340 $self->edit_printer_form(title => $is_new ? t8('Create a new printer') : t8('Edit Printer'));
344 $self->printer->save;
346 flash_later('info', $is_new ? t8('The printer has been created.') : t8('The printer has been saved.'));
347 $self->redirect_to(action => 'list_printers', 'client.id' => $self->client->id);
350 sub action_delete_printer {
353 if (!$self->printer->delete) {
354 flash('error', t8('The printer could not be deleted.'));
355 $self->edit_printer_form(title => t8('Edit Printer'));
359 flash_later('info', t8('The printer has been deleted.'));
360 $self->redirect_to(action => 'list_printers', 'client.id' => $self->client->id);
364 # actions: locking, unlocking
367 sub action_unlock_system {
370 SL::System::InstallationLock->unlock;
371 flash_later('info', t8('Lockfile removed!'));
372 $self->redirect_to(action => 'show');
375 sub action_lock_system {
378 SL::System::InstallationLock->unlock;
379 flash_later('info', t8('Lockfile created!'));
380 $self->redirect_to(action => 'show');
387 sub init_db_cfg { $::lx_office_conf{'authentication/database'} }
388 sub init_is_locked { SL::System::InstallationLock->is_locked }
389 sub init_client { SL::DB::Manager::AuthClient->find_by(id => ($::form->{id} || ($::form->{client} || {})->{id})) }
390 sub init_user { SL::DB::AuthUser ->new(id => ($::form->{id} || ($::form->{user} || {})->{id}))->load }
391 sub init_group { SL::DB::AuthGroup ->new(id => ($::form->{id} || ($::form->{group} || {})->{id}))->load }
392 sub init_printer { SL::DB::Printer ->new(id => ($::form->{id} || ($::form->{printer} || {})->{id}))->load }
393 sub init_all_clients { SL::DB::Manager::AuthClient->get_all_sorted }
394 sub init_all_users { SL::DB::Manager::AuthUser ->get_all_sorted }
395 sub init_all_groups { SL::DB::Manager::AuthGroup ->get_all_sorted }
396 sub init_all_printers { SL::DB::Manager::Printer ->get_all_sorted }
397 sub init_all_dateformats { [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd) ] }
398 sub init_all_numberformats { [ qw(1,000.00 1000.00 1.000,00 1000,00) ] }
399 sub init_all_stylesheets { [ qw(lx-office-erp.css Mobile.css kivitendo.css) ] }
400 sub init_all_menustyles {
402 { id => 'old', title => $::locale->text('Old (on the side)') },
403 { id => 'v3', title => $::locale->text('Top (CSS)') },
404 { id => 'neu', title => $::locale->text('Top (Javascript)') },
408 sub init_all_rights {
409 my (@sections, $current_section);
411 foreach my $entry ($::auth->all_rights_full) {
412 if ($entry->[0] =~ m/^--/) {
413 push @sections, { description => $entry->[1], rights => [] };
415 } elsif (@sections) {
416 push @{ $sections[-1]->{rights} }, {
418 description => $entry->[1],
422 die "Right without sections: " . join('::', @{ $entry });
429 sub init_all_countrycodes {
430 my %cc = User->country_codes;
431 return [ map { id => $_, title => $cc{$_} }, sort { $cc{$a} cmp $cc{$b} } keys %cc ];
439 my ($self, $action) = @_;
441 $::request->layout(SL::Layout::Dispatcher->new(style => 'admin'));
442 $::request->layout->use_stylesheet("lx-office-erp.css");
443 $::form->{favicon} = "favicon.ico";
449 $self->client((first { $_->is_default } @{ $self->all_clients }) || $self->all_clients->[0]) if !$self->client;
450 $::auth->set_client($self->client->id);
458 sub use_multiselect_js {
461 $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
466 my ($self, %params) = @_;
467 $::request->layout->focus('#admin_password');
468 $self->render('admin/adminlogin', title => t8('kivitendo v#1 administration', $::form->read_version), %params);
472 my ($self, %params) = @_;
473 $self->use_multiselect_js->render('admin/edit_user', %params);
476 sub edit_client_form {
477 my ($self, %params) = @_;
478 $self->use_multiselect_js->render('admin/edit_client', %params);
481 sub edit_group_form {
482 my ($self, %params) = @_;
483 $self->use_multiselect_js->render('admin/edit_group', %params);
486 sub edit_printer_form {
487 my ($self, %params) = @_;
488 $self->render('admin/edit_printer', %params);
495 sub check_auth_db_and_tables {
498 if (!$::auth->check_database) {
499 $self->render('admin/check_auth_database', title => t8('Authentification database creation'));
503 if (!$::auth->check_tables) {
504 $self->render('admin/check_auth_tables', title => t8('Authentification tables creation'));
511 sub apply_dbupgrade_scripts {
512 return SL::DBUpgrade2->new(form => $::form, auth => 1)->apply_admin_dbupgrade_scripts(1);
515 sub authenticate_root {
518 return 1 if $::auth->authenticate_root($::form->{'{AUTH}admin_password'}) == $::auth->OK();
520 $::auth->punish_wrong_login;
521 $::auth->delete_session_value('admin_password');
523 $self->login_form(error => t8('Incorrect password!'));