1 package SL::Controller::Admin;
5 use parent qw(SL::Controller::Base);
8 use List::Util qw(first);
12 use SL::DB::AuthGroup;
14 use SL::Helper::Flash;
15 use SL::Locale::String qw(t8);
16 use SL::System::InstallationLock;
19 use Rose::Object::MakeMethods::Generic
21 'scalar --get_set_init' => [ qw(client user group printer db_cfg is_locked
22 all_dateformats all_numberformats all_countrycodes all_stylesheets all_menustyles all_clients all_groups all_users all_rights all_printers
23 all_dbsources all_used_dbsources all_accounting_methods all_inventory_systems all_profit_determinations all_charts) ],
26 __PACKAGE__->run_before(\&setup_layout);
27 __PACKAGE__->run_before(\&setup_client, only => [ qw(list_printers new_printer edit_printer save_printer delete_printer) ]);
29 sub get_auth_level { "admin" };
31 my ($class, %params) = @_;
32 return $params{action} eq 'login';
36 # actions: login, logout
42 return $self->login_form if !$::form->{do_login};
43 return if !$self->authenticate_root;
44 return if !$self->check_auth_db_and_tables;
45 return if $self->apply_dbupgrade_scripts;
47 $self->redirect_to(action => 'show');
52 $::auth->destroy_session;
53 $self->redirect_to(action => 'login');
57 # actions: creating the authentication database & tables, applying database ugprades
60 sub action_apply_dbupgrade_scripts {
63 return if $self->apply_dbupgrade_scripts;
64 $self->redirect_to(action => 'show');
67 sub action_create_auth_db {
70 $::auth->create_database(superuser => $::form->{db_superuser},
71 superuser_password => $::form->{db_superuser_password},
72 template => $::form->{db_template});
73 $self->check_auth_db_and_tables;
76 sub action_create_auth_tables {
79 $::auth->create_tables;
80 $::auth->set_session_value('admin_password', $::lx_office_conf{authentication}->{admin_password});
81 $::auth->create_or_refresh_session;
83 my $group = (SL::DB::Manager::AuthGroup->get_all(limit => 1))[0];
85 SL::DB::AuthGroup->new(
86 name => t8('Full Access'),
87 description => t8('Full access to all functions'),
88 rights => [ map { SL::DB::AuthGroupRight->new(right => $_, granted => 1) } SL::Auth::all_rights() ],
92 if (!$self->apply_dbupgrade_scripts) {
106 title => "kivitendo " . t8('Administration'),
110 sub action_new_user {
113 $self->user(SL::DB::AuthUser->new(
117 numberformat => "1.000,00",
118 dateformat => "dd.mm.yy",
119 stylesheet => "kivitendo.css",
124 $self->edit_user_form(title => t8('Create a new user'));
127 sub action_edit_user {
129 $self->edit_user_form(title => t8('Edit User'));
132 sub action_save_newuser {
134 $::form->{user}{clients} = 0;
135 $self->action_save_user();
138 sub action_save_user {
140 my $params = delete($::form->{user}) || { };
141 my $props = delete($params->{config_values}) || { };
142 my $is_new = !$params->{id};
144 # Assign empty arrays if the browser doesn't send those controls.
145 $params->{clients} ||= [];
146 $params->{groups} ||= [];
148 $self->user($is_new ? SL::DB::AuthUser->new : SL::DB::AuthUser->new(id => $params->{id})->load)
149 ->assign_attributes(%{ $params })
150 ->config_values({ %{ $self->user->config_values }, %{ $props } });
152 my @errors = $self->user->validate;
155 flash('error', @errors);
156 $self->edit_user_form(title => $is_new ? t8('Create a new user') : t8('Edit User'));
162 if ($::auth->can_change_password && $::form->{new_password}) {
163 $::auth->change_password($self->user->login, $::form->{new_password});
166 flash_later('info', $is_new ? t8('The user has been created.') : t8('The user has been saved.'));
167 $self->redirect_to(action => 'show');
170 sub action_delete_user {
173 my @clients = @{ $self->user->clients || [] };
175 if (!$self->user->delete) {
176 flash('error', t8('The user could not be deleted.'));
177 $self->edit_user_form(title => t8('Edit User'));
181 # Flag corresponding entries in 'employee' as deleted.
182 foreach my $client (@clients) {
183 my $dbh = $client->dbconnect(AutoCommit => 1) || next;
184 $dbh->do(qq|UPDATE employee SET deleted = TRUE WHERE login = ?|, undef, $self->user->login);
188 flash_later('info', t8('The user has been deleted.'));
189 $self->redirect_to(action => 'show');
196 sub action_new_client {
199 $self->client(SL::DB::AuthClient->new(
200 dbhost => $::auth->{DB_config}->{host},
201 dbport => $::auth->{DB_config}->{port},
202 dbuser => $::auth->{DB_config}->{user},
203 dbpasswd => $::auth->{DB_config}->{password},
206 $self->edit_client_form(title => t8('Create a new client'));
209 sub action_edit_client {
211 $self->edit_client_form(title => t8('Edit Client'));
214 sub action_save_client {
216 my $params = delete($::form->{client}) || { };
217 my $is_new = !$params->{id};
219 # Assign empty arrays if the browser doesn't send those controls.
220 $params->{groups} ||= [];
221 $params->{users} ||= [];
223 $self->client($is_new ? SL::DB::AuthClient->new : SL::DB::AuthClient->new(id => $params->{id})->load)->assign_attributes(%{ $params });
225 my @errors = $self->client->validate;
228 flash('error', @errors);
229 $self->edit_client_form(title => $is_new ? t8('Create a new client') : t8('Edit Client'));
234 if ($self->client->is_default) {
235 SL::DB::Manager::AuthClient->update_all(set => { is_default => 0 }, where => [ '!id' => $self->client->id ]);
238 flash_later('info', $is_new ? t8('The client has been created.') : t8('The client has been saved.'));
239 $self->redirect_to(action => 'show');
242 sub action_delete_client {
245 if (!$self->client->delete) {
246 flash('error', t8('The client could not be deleted.'));
247 $self->edit_client_form(title => t8('Edit Client'));
251 flash_later('info', t8('The client has been deleted.'));
252 $self->redirect_to(action => 'show');
255 sub action_test_database_connectivity {
258 my %cfg = %{ $::form->{client} || {} };
259 my $dbconnect = 'dbi:Pg:dbname=' . $cfg{dbname} . ';host=' . $cfg{dbhost} . ';port=' . $cfg{dbport};
260 my $dbh = DBI->connect($dbconnect, $cfg{dbuser}, $cfg{dbpasswd});
263 my $error = $DBI::errstr;
265 $dbh->disconnect if $dbh;
267 $self->render('admin/test_db_connection', { layout => 0 },
268 title => t8('Database Connection Test'),
277 sub action_new_group {
280 $self->group(SL::DB::AuthGroup->new);
281 $self->edit_group_form(title => t8('Create a new group'));
284 sub action_edit_group {
286 $self->edit_group_form(title => t8('Edit User Group'));
289 sub action_save_group {
292 my $params = delete($::form->{group}) || { };
293 my $is_new = !$params->{id};
295 # Assign empty arrays if the browser doesn't send those controls.
296 $params->{clients} ||= [];
297 $params->{users} ||= [];
299 $self->group($is_new ? SL::DB::AuthGroup->new : SL::DB::AuthGroup->new(id => $params->{id})->load)->assign_attributes(%{ $params });
301 my @errors = $self->group->validate;
304 flash('error', @errors);
305 $self->edit_group_form(title => $is_new ? t8('Create a new user group') : t8('Edit User Group'));
311 flash_later('info', $is_new ? t8('The user group has been created.') : t8('The user group has been saved.'));
312 $self->redirect_to(action => 'show');
315 sub action_delete_group {
318 if (!$self->group->delete) {
319 flash('error', t8('The user group could not be deleted.'));
320 $self->edit_group_form(title => t8('Edit User Group'));
324 flash_later('info', t8('The user group has been deleted.'));
325 $self->redirect_to(action => 'show');
332 sub action_list_printers {
334 $self->render('admin/list_printers', title => t8('Printer management'));
337 sub action_new_printer {
340 $self->printer(SL::DB::Printer->new);
341 $self->edit_printer_form(title => t8('Create a new printer'));
344 sub action_edit_printer {
346 $self->edit_printer_form(title => t8('Edit Printer'));
349 sub action_save_printer {
351 my $params = delete($::form->{printer}) || { };
352 my $is_new = !$params->{id};
354 $self->printer($is_new ? SL::DB::Printer->new : SL::DB::Printer->new(id => $params->{id})->load)->assign_attributes(%{ $params });
356 my @errors = $self->printer->validate;
359 flash('error', @errors);
360 $self->edit_printer_form(title => $is_new ? t8('Create a new printer') : t8('Edit Printer'));
364 $self->printer->save;
366 flash_later('info', $is_new ? t8('The printer has been created.') : t8('The printer has been saved.'));
367 $self->redirect_to(action => 'list_printers', 'client.id' => $self->client->id);
370 sub action_delete_printer {
373 if (!$self->printer->delete) {
374 flash('error', t8('The printer could not be deleted.'));
375 $self->edit_printer_form(title => t8('Edit Printer'));
379 flash_later('info', t8('The printer has been deleted.'));
380 $self->redirect_to(action => 'list_printers', 'client.id' => $self->client->id);
384 # actions: database administration
387 sub action_create_dataset_login {
390 $self->database_administration_login_form(
391 title => t8('Create Dataset'),
392 next_action => 'create_dataset',
396 sub action_create_dataset {
398 $self->create_dataset_form;
401 sub action_do_create_dataset {
405 push @errors, t8("Dataset missing!") if !$::form->{db};
406 push @errors, t8("Default currency missing!") if !$::form->{defaultcurrency};
409 flash('error', @errors);
410 return $self->create_dataset_form;
413 $::form->{encoding} = 'UNICODE';
414 User->new->dbcreate($::form);
416 flash_later('info', t8("The dataset #1 has been created.", $::form->{db}));
417 $self->redirect_to(action => 'show');
420 sub action_delete_dataset_login {
423 $self->database_administration_login_form(
424 title => t8('Delete Dataset'),
425 next_action => 'delete_dataset',
429 sub action_delete_dataset {
431 $self->delete_dataset_form;
434 sub action_do_delete_dataset {
438 push @errors, t8("Dataset missing!") if !$::form->{db};
441 flash('error', @errors);
442 return $self->create_dataset_form;
445 User->new->dbdelete($::form);
447 flash_later('info', t8("The dataset #1 has been deleted.", $::form->{db}));
448 $self->redirect_to(action => 'show');
452 # actions: locking, unlocking
455 sub action_show_lock {
460 title => "kivitendo " . t8('Administration'),
464 sub action_unlock_system {
467 SL::System::InstallationLock->unlock;
468 flash_later('info', t8('Lockfile removed!'));
469 $self->redirect_to(action => 'show');
472 sub action_lock_system {
475 SL::System::InstallationLock->lock;
476 flash_later('info', t8('Lockfile created!'));
477 $self->redirect_to(action => 'show');
484 sub init_db_cfg { $::lx_office_conf{'authentication/database'} }
485 sub init_is_locked { SL::System::InstallationLock->is_locked }
486 sub init_client { SL::DB::Manager::AuthClient->find_by(id => ($::form->{id} || ($::form->{client} || {})->{id})) }
487 sub init_user { SL::DB::AuthUser ->new(id => ($::form->{id} || ($::form->{user} || {})->{id}))->load }
488 sub init_group { SL::DB::AuthGroup ->new(id => ($::form->{id} || ($::form->{group} || {})->{id}))->load }
489 sub init_printer { SL::DB::Printer ->new(id => ($::form->{id} || ($::form->{printer} || {})->{id}))->load }
490 sub init_all_clients { SL::DB::Manager::AuthClient->get_all_sorted }
491 sub init_all_users { SL::DB::Manager::AuthUser ->get_all_sorted }
492 sub init_all_groups { SL::DB::Manager::AuthGroup ->get_all_sorted }
493 sub init_all_printers { SL::DB::Manager::Printer ->get_all_sorted }
494 sub init_all_dateformats { [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd) ] }
495 sub init_all_numberformats { [ '1,000.00', '1000.00', '1.000,00', '1000,00' ] }
496 sub init_all_stylesheets { [ qw(lx-office-erp.css Mobile.css kivitendo.css) ] }
497 sub init_all_dbsources { [ sort User->dbsources($::form) ] }
498 sub init_all_used_dbsources { { map { (join(':', $_->dbhost || 'localhost', $_->dbport || 5432, $_->dbname) => $_->name) } @{ $_[0]->all_clients } } }
499 sub init_all_accounting_methods { [ { id => 'accrual', name => t8('Accrual accounting') }, { id => 'cash', name => t8('Cash accounting') } ] }
500 sub init_all_inventory_systems { [ { id => 'perpetual', name => t8('Perpetual inventory') }, { id => 'periodic', name => t8('Periodic inventory') } ] }
501 sub init_all_profit_determinations { [ { id => 'balance', name => t8('Balancing') }, { id => 'income', name => t8('Cash basis accounting') } ] }
503 sub init_all_charts {
504 tie my %dir_h, 'IO::Dir', 'sql/';
507 map { s/-chart\.sql$//; +{ id => $_ } }
509 grep { /-chart\.sql\z/ && !/Default-chart.sql\z/ }
514 sub init_all_menustyles {
516 { id => 'old', title => $::locale->text('Old (on the side)') },
517 { id => 'v3', title => $::locale->text('Top (CSS)') },
518 { id => 'neu', title => $::locale->text('Top (Javascript)') },
522 sub init_all_rights {
523 my (@sections, $current_section);
525 foreach my $entry ($::auth->all_rights_full) {
526 if ($entry->[0] =~ m/^--/) {
527 push @sections, { description => $entry->[1], rights => [] };
529 } elsif (@sections) {
530 push @{ $sections[-1]->{rights} }, {
532 description => $entry->[1],
536 die "Right without sections: " . join('::', @{ $entry });
543 sub init_all_countrycodes {
544 my %cc = User->country_codes;
545 return [ map { id => $_, title => $cc{$_} }, sort { $cc{$a} cmp $cc{$b} } keys %cc ];
553 my ($self, $action) = @_;
555 $::request->layout(SL::Layout::Dispatcher->new(style => 'admin'));
556 $::form->{favicon} = "favicon.ico";
559 numberformat => '1.000,00',
560 dateformat => 'dd.mm.yy',
567 $self->client(SL::DB::Manager::AuthClient->get_default || $self->all_clients->[0]) if !$self->client;
568 $::auth->set_client($self->client->id);
575 sub use_multiselect_js {
578 $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
583 my ($self, %params) = @_;
584 my $version = $::form->read_version;
585 $::request->layout->no_menu(1);
586 $self->render('admin/adminlogin', title => t8('kivitendo v#1 administration', $version), %params, version => $version);
590 my ($self, %params) = @_;
591 $self->use_multiselect_js->render('admin/edit_user', %params);
594 sub edit_client_form {
595 my ($self, %params) = @_;
596 $self->use_multiselect_js->render('admin/edit_client', %params);
599 sub edit_group_form {
600 my ($self, %params) = @_;
601 $self->use_multiselect_js->render('admin/edit_group', %params);
604 sub edit_printer_form {
605 my ($self, %params) = @_;
606 $self->render('admin/edit_printer', %params);
609 sub database_administration_login_form {
610 my ($self, %params) = @_;
614 dbhost => $::form->{dbhost} || $::auth->{DB_config}->{host} || 'localhost',
615 dbport => $::form->{dbport} || $::auth->{DB_config}->{port} || 5432,
616 dbuser => $::form->{dbuser} || $::auth->{DB_config}->{user} || 'kivitendo',
617 dbpasswd => $::form->{dbpasswd} || $::auth->{DB_config}->{password},
618 dbdefault => $::form->{dbdefault} || 'template1',
623 sub create_dataset_form {
624 my ($self, %params) = @_;
625 $self->render('admin/create_dataset', title => (t8('Database Administration') . " / " . t8('Create Dataset')));
628 sub delete_dataset_form {
629 my ($self, %params) = @_;
630 $self->render('admin/delete_dataset', title => (t8('Database Administration') . " / " . t8('Delete Dataset')));
637 sub check_auth_db_and_tables {
640 if (!$::auth->check_database) {
641 $self->render('admin/check_auth_database', title => t8('Authentification database creation'));
645 if (!$::auth->check_tables) {
646 $self->render('admin/check_auth_tables', title => t8('Authentification tables creation'));
653 sub apply_dbupgrade_scripts {
654 return SL::DBUpgrade2->new(form => $::form, auth => 1)->apply_admin_dbupgrade_scripts(1);
657 sub authenticate_root {
660 return 1 if $::auth->authenticate_root($::form->{'{AUTH}admin_password'}) == $::auth->OK();
662 $::auth->punish_wrong_login;
663 $::auth->delete_session_value('admin_password');
665 $self->login_form(error => t8('Incorrect password!'));