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;
18 use SL::Layout::AdminLogin;
20 use Rose::Object::MakeMethods::Generic
22 'scalar --get_set_init' => [ qw(client user group printer db_cfg is_locked
23 all_dateformats all_numberformats all_countrycodes all_stylesheets all_menustyles all_clients all_groups all_users all_rights all_printers
24 all_dbsources all_used_dbsources all_accounting_methods all_inventory_systems all_profit_determinations all_charts) ],
27 __PACKAGE__->run_before(\&setup_layout);
28 __PACKAGE__->run_before(\&setup_client, only => [ qw(list_printers new_printer edit_printer save_printer delete_printer) ]);
30 sub get_auth_level { "admin" };
32 my ($class, %params) = @_;
33 return $params{action} eq 'login';
37 # actions: login, logout
43 return $self->login_form if !$::form->{do_login};
44 return if !$self->authenticate_root;
45 return if !$self->check_auth_db_and_tables;
46 return if $self->apply_dbupgrade_scripts;
48 $self->redirect_to(action => 'show');
53 $::auth->destroy_session;
54 $self->redirect_to(action => 'login');
58 # actions: creating the authentication database & tables, applying database ugprades
61 sub action_apply_dbupgrade_scripts {
64 return if $self->apply_dbupgrade_scripts;
65 $self->redirect_to(action => 'show');
68 sub action_create_auth_db {
71 $::auth->create_database(superuser => $::form->{db_superuser},
72 superuser_password => $::form->{db_superuser_password},
73 template => $::form->{db_template});
74 $self->check_auth_db_and_tables;
77 sub action_create_auth_tables {
80 $::auth->create_tables;
81 $::auth->set_session_value('admin_password', $::lx_office_conf{authentication}->{admin_password});
82 $::auth->create_or_refresh_session;
84 my $group = (SL::DB::Manager::AuthGroup->get_all(limit => 1))[0];
86 SL::DB::AuthGroup->new(
87 name => t8('Full Access'),
88 description => t8('Full access to all functions'),
89 rights => [ map { SL::DB::AuthGroupRight->new(right => $_, granted => 1) } SL::Auth::all_rights() ],
93 if (!$self->apply_dbupgrade_scripts) {
107 title => "kivitendo " . t8('Administration'),
111 sub action_new_user {
114 $self->user(SL::DB::AuthUser->new(
118 numberformat => "1.000,00",
119 dateformat => "dd.mm.yy",
120 stylesheet => "kivitendo.css",
125 $self->edit_user_form(title => t8('Create a new user'));
128 sub action_edit_user {
130 $self->edit_user_form(title => t8('Edit User'));
133 sub action_save_user {
135 my $params = delete($::form->{user}) || { };
136 my $props = delete($params->{config_values}) || { };
137 my $is_new = !$params->{id};
139 # Assign empty arrays if the browser doesn't send those controls.
140 $params->{clients} ||= [];
141 $params->{groups} ||= [];
143 $self->user($is_new ? SL::DB::AuthUser->new : SL::DB::AuthUser->new(id => $params->{id})->load)
144 ->assign_attributes(%{ $params })
145 ->config_values({ %{ $self->user->config_values }, %{ $props } });
147 my @errors = $self->user->validate;
150 flash('error', @errors);
151 $self->edit_user_form(title => $is_new ? t8('Create a new user') : t8('Edit User'));
157 if ($::auth->can_change_password && $::form->{new_password}) {
158 $::auth->change_password($self->user->login, $::form->{new_password});
161 flash_later('info', $is_new ? t8('The user has been created.') : t8('The user has been saved.'));
162 $self->redirect_to(action => 'show');
165 sub action_delete_user {
168 my @clients = @{ $self->user->clients || [] };
170 # backup user metadata (email, name, etc)
171 my $user_config_values_ref = $self->user->config_values();
172 my $login =$self->user->login;
174 if (!$self->user->delete) {
175 flash('error', t8('The user could not be deleted.'));
176 $self->edit_user_form(title => t8('Edit User'));
180 # Flag corresponding entries in 'employee' as deleted.
181 # and restore the most important user data in employee
182 # TODO try and catch the whole transaction {user->delete; update employee} {exception}
183 foreach my $client (@clients) {
184 my $dbh = $client->dbconnect(AutoCommit => 1) || next;
185 $dbh->do(qq|UPDATE employee SET deleted = TRUE, name = ?, deleted_email = ?,
186 deleted_tel = ?, deleted_fax = ?, deleted_signature = ? WHERE login = ?|,undef,
187 $user_config_values_ref->{name}, $user_config_values_ref->{email},
188 $user_config_values_ref->{tel}, $user_config_values_ref->{fax},
189 $user_config_values_ref->{signature}, $self->user->login);
193 flash_later('info', t8('The user has been deleted.'));
194 $self->redirect_to(action => 'show');
201 sub action_new_client {
204 $self->client(SL::DB::AuthClient->new(
205 dbhost => $::auth->{DB_config}->{host},
206 dbport => $::auth->{DB_config}->{port},
207 dbuser => $::auth->{DB_config}->{user},
208 dbpasswd => $::auth->{DB_config}->{password},
211 $self->edit_client_form(title => t8('Create a new client'));
214 sub action_edit_client {
216 $self->edit_client_form(title => t8('Edit Client'));
219 sub action_save_client {
221 my $params = delete($::form->{client}) || { };
222 my $is_new = !$params->{id};
224 # Assign empty arrays if the browser doesn't send those controls.
225 $params->{groups} ||= [];
226 $params->{users} ||= [];
228 $self->client($is_new ? SL::DB::AuthClient->new : SL::DB::AuthClient->new(id => $params->{id})->load)->assign_attributes(%{ $params });
230 my @errors = $self->client->validate;
233 flash('error', @errors);
234 $self->edit_client_form(title => $is_new ? t8('Create a new client') : t8('Edit Client'));
239 if ($self->client->is_default) {
240 SL::DB::Manager::AuthClient->update_all(set => { is_default => 0 }, where => [ '!id' => $self->client->id ]);
243 flash_later('info', $is_new ? t8('The client has been created.') : t8('The client has been saved.'));
244 $self->redirect_to(action => 'show');
247 sub action_delete_client {
250 if (!$self->client->delete) {
251 flash('error', t8('The client could not be deleted.'));
252 $self->edit_client_form(title => t8('Edit Client'));
256 flash_later('info', t8('The client has been deleted.'));
257 $self->redirect_to(action => 'show');
260 sub action_test_database_connectivity {
263 my %cfg = %{ $::form->{client} || {} };
264 my $dbconnect = 'dbi:Pg:dbname=' . $cfg{dbname} . ';host=' . $cfg{dbhost} . ';port=' . $cfg{dbport};
265 my $dbh = DBI->connect($dbconnect, $cfg{dbuser}, $cfg{dbpasswd});
268 my $error = $DBI::errstr;
270 $dbh->disconnect if $dbh;
272 $self->render('admin/test_db_connection', { layout => 0 },
273 title => t8('Database Connection Test'),
282 sub action_new_group {
285 $self->group(SL::DB::AuthGroup->new);
286 $self->edit_group_form(title => t8('Create a new group'));
289 sub action_edit_group {
291 $self->edit_group_form(title => t8('Edit User Group'));
294 sub action_save_group {
297 my $params = delete($::form->{group}) || { };
298 my $is_new = !$params->{id};
300 # Assign empty arrays if the browser doesn't send those controls.
301 $params->{clients} ||= [];
302 $params->{users} ||= [];
304 $self->group($is_new ? SL::DB::AuthGroup->new : SL::DB::AuthGroup->new(id => $params->{id})->load)->assign_attributes(%{ $params });
306 my @errors = $self->group->validate;
309 flash('error', @errors);
310 $self->edit_group_form(title => $is_new ? t8('Create a new user group') : t8('Edit User Group'));
316 flash_later('info', $is_new ? t8('The user group has been created.') : t8('The user group has been saved.'));
317 $self->redirect_to(action => 'show');
320 sub action_delete_group {
323 if (!$self->group->delete) {
324 flash('error', t8('The user group could not be deleted.'));
325 $self->edit_group_form(title => t8('Edit User Group'));
329 flash_later('info', t8('The user group has been deleted.'));
330 $self->redirect_to(action => 'show');
337 sub action_list_printers {
339 $self->render('admin/list_printers', title => t8('Printer management'));
342 sub action_new_printer {
345 $self->printer(SL::DB::Printer->new);
346 $self->edit_printer_form(title => t8('Create a new printer'));
349 sub action_edit_printer {
351 $self->edit_printer_form(title => t8('Edit Printer'));
354 sub action_save_printer {
356 my $params = delete($::form->{printer}) || { };
357 my $is_new = !$params->{id};
359 $self->printer($is_new ? SL::DB::Printer->new : SL::DB::Printer->new(id => $params->{id})->load)->assign_attributes(%{ $params });
361 my @errors = $self->printer->validate;
364 flash('error', @errors);
365 $self->edit_printer_form(title => $is_new ? t8('Create a new printer') : t8('Edit Printer'));
369 $self->printer->save;
371 flash_later('info', $is_new ? t8('The printer has been created.') : t8('The printer has been saved.'));
372 $self->redirect_to(action => 'list_printers', 'client.id' => $self->client->id);
375 sub action_delete_printer {
378 if (!$self->printer->delete) {
379 flash('error', t8('The printer could not be deleted.'));
380 $self->edit_printer_form(title => t8('Edit Printer'));
384 flash_later('info', t8('The printer has been deleted.'));
385 $self->redirect_to(action => 'list_printers', 'client.id' => $self->client->id);
389 # actions: database administration
392 sub action_create_dataset_login {
395 $self->database_administration_login_form(
396 title => t8('Create Dataset'),
397 next_action => 'create_dataset',
401 sub action_create_dataset {
403 $self->create_dataset_form;
406 sub action_do_create_dataset {
410 push @errors, t8("Dataset missing!") if !$::form->{db};
411 push @errors, t8("Default currency missing!") if !$::form->{defaultcurrency};
414 flash('error', @errors);
415 return $self->create_dataset_form;
418 $::form->{encoding} = 'UNICODE';
419 User->new->dbcreate($::form);
421 flash_later('info', t8("The dataset #1 has been created.", $::form->{db}));
422 $self->redirect_to(action => 'show');
425 sub action_delete_dataset_login {
428 $self->database_administration_login_form(
429 title => t8('Delete Dataset'),
430 next_action => 'delete_dataset',
434 sub action_delete_dataset {
436 $self->delete_dataset_form;
439 sub action_do_delete_dataset {
443 push @errors, t8("Dataset missing!") if !$::form->{db};
446 flash('error', @errors);
447 return $self->create_dataset_form;
450 User->new->dbdelete($::form);
452 flash_later('info', t8("The dataset #1 has been deleted.", $::form->{db}));
453 $self->redirect_to(action => 'show');
457 # actions: locking, unlocking
460 sub action_show_lock {
465 title => "kivitendo " . t8('Administration'),
469 sub action_unlock_system {
472 SL::System::InstallationLock->unlock;
473 flash_later('info', t8('Lockfile removed!'));
474 $self->redirect_to(action => 'show');
477 sub action_lock_system {
480 SL::System::InstallationLock->lock;
481 flash_later('info', t8('Lockfile created!'));
482 $self->redirect_to(action => 'show');
489 sub init_db_cfg { $::lx_office_conf{'authentication/database'} }
490 sub init_is_locked { SL::System::InstallationLock->is_locked }
491 sub init_client { SL::DB::Manager::AuthClient->find_by(id => ($::form->{id} || ($::form->{client} || {})->{id})) }
492 sub init_user { SL::DB::AuthUser ->new(id => ($::form->{id} || ($::form->{user} || {})->{id}))->load }
493 sub init_group { SL::DB::AuthGroup ->new(id => ($::form->{id} || ($::form->{group} || {})->{id}))->load }
494 sub init_printer { SL::DB::Printer ->new(id => ($::form->{id} || ($::form->{printer} || {})->{id}))->load }
495 sub init_all_clients { SL::DB::Manager::AuthClient->get_all_sorted }
496 sub init_all_users { SL::DB::Manager::AuthUser ->get_all_sorted }
497 sub init_all_groups { SL::DB::Manager::AuthGroup ->get_all_sorted }
498 sub init_all_printers { SL::DB::Manager::Printer ->get_all_sorted }
499 sub init_all_dateformats { [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd) ] }
500 sub init_all_numberformats { [ '1,000.00', '1000.00', '1.000,00', '1000,00' ] }
501 sub init_all_stylesheets { [ qw(lx-office-erp.css kivitendo.css) ] }
502 sub init_all_dbsources { [ sort User->dbsources($::form) ] }
503 sub init_all_used_dbsources { { map { (join(':', $_->dbhost || 'localhost', $_->dbport || 5432, $_->dbname) => $_->name) } @{ $_[0]->all_clients } } }
504 sub init_all_accounting_methods { [ { id => 'accrual', name => t8('Accrual accounting') }, { id => 'cash', name => t8('Cash accounting') } ] }
505 sub init_all_inventory_systems { [ { id => 'perpetual', name => t8('Perpetual inventory') }, { id => 'periodic', name => t8('Periodic inventory') } ] }
506 sub init_all_profit_determinations { [ { id => 'balance', name => t8('Balancing') }, { id => 'income', name => t8('Cash basis accounting') } ] }
508 sub init_all_charts {
509 tie my %dir_h, 'IO::Dir', 'sql/';
512 map { s/-chart\.sql$//; +{ id => $_ } }
514 grep { /-chart\.sql\z/ && !/Default-chart.sql\z/ }
519 sub init_all_menustyles {
521 { id => 'old', title => $::locale->text('Old (on the side)') },
522 { id => 'v3', title => $::locale->text('Top (CSS)') },
523 { id => 'neu', title => $::locale->text('Top (Javascript)') },
527 sub init_all_rights {
528 my (@sections, $current_section);
530 foreach my $entry ($::auth->all_rights_full) {
531 if ($entry->[0] =~ m/^--/) {
532 push @sections, { description => $entry->[1], rights => [] };
534 } elsif (@sections) {
535 push @{ $sections[-1]->{rights} }, {
537 description => $entry->[1],
541 die "Right without sections: " . join('::', @{ $entry });
548 sub init_all_countrycodes {
549 my %cc = User->country_codes;
550 return [ map { id => $_, title => $cc{$_} }, sort { $cc{$a} cmp $cc{$b} } keys %cc ];
558 my ($self, $action) = @_;
560 $::request->layout(SL::Layout::Dispatcher->new(style => 'admin'));
561 $::form->{favicon} = "favicon.ico";
564 numberformat => '1.000,00',
565 dateformat => 'dd.mm.yy',
572 $self->client(SL::DB::Manager::AuthClient->get_default || $self->all_clients->[0]) if !$self->client;
573 $::auth->set_client($self->client->id);
580 sub use_multiselect_js {
583 $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
588 my ($self, %params) = @_;
589 $::request->layout(SL::Layout::AdminLogin->new);
590 my $version = $::form->read_version;
591 $self->render('admin/adminlogin', title => t8('kivitendo v#1 administration', $version), %params, version => $version);
595 my ($self, %params) = @_;
596 $self->use_multiselect_js->render('admin/edit_user', %params);
599 sub edit_client_form {
600 my ($self, %params) = @_;
601 $self->use_multiselect_js->render('admin/edit_client', %params);
604 sub edit_group_form {
605 my ($self, %params) = @_;
606 $self->use_multiselect_js->render('admin/edit_group', %params);
609 sub edit_printer_form {
610 my ($self, %params) = @_;
611 $self->render('admin/edit_printer', %params);
614 sub database_administration_login_form {
615 my ($self, %params) = @_;
619 dbhost => $::form->{dbhost} || $::auth->{DB_config}->{host} || 'localhost',
620 dbport => $::form->{dbport} || $::auth->{DB_config}->{port} || 5432,
621 dbuser => $::form->{dbuser} || $::auth->{DB_config}->{user} || 'kivitendo',
622 dbpasswd => $::form->{dbpasswd} || $::auth->{DB_config}->{password},
623 dbdefault => $::form->{dbdefault} || 'template1',
628 sub create_dataset_form {
629 my ($self, %params) = @_;
630 $self->render('admin/create_dataset', title => (t8('Database Administration') . " / " . t8('Create Dataset')));
633 sub delete_dataset_form {
634 my ($self, %params) = @_;
635 $self->render('admin/delete_dataset', title => (t8('Database Administration') . " / " . t8('Delete Dataset')));
642 sub check_auth_db_and_tables {
645 if (!$::auth->check_database) {
646 $self->render('admin/check_auth_database', title => t8('Authentification database creation'));
650 if (!$::auth->check_tables) {
651 $self->render('admin/check_auth_tables', title => t8('Authentification tables creation'));
658 sub apply_dbupgrade_scripts {
659 return SL::DBUpgrade2->new(form => $::form, auth => 1)->apply_admin_dbupgrade_scripts(1);
662 sub authenticate_root {
665 return 1 if $::auth->authenticate_root($::form->{'{AUTH}admin_password'}) == $::auth->OK();
667 $::auth->punish_wrong_login;
668 $::auth->delete_session_value('admin_password');
670 $self->login_form(error => t8('Incorrect password!'));