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;
 
  44   $self->redirect_to(action => 'show');
 
  49   $::auth->destroy_session;
 
  50   $self->redirect_to(action => 'login');
 
  54 # actions: creating the authentication database & tables, applying database ugprades
 
  57 sub action_apply_dbupgrade_scripts {
 
  60   return if $self->apply_dbupgrade_scripts;
 
  64 sub action_create_auth_db {
 
  67   $::auth->create_database(superuser          => $::form->{db_superuser},
 
  68                            superuser_password => $::form->{db_superuser_password},
 
  69                            template           => $::form->{db_template});
 
  70   $self->check_auth_db_and_tables;
 
  73 sub action_create_auth_tables {
 
  76   $::auth->create_tables;
 
  77   $::auth->set_session_value('admin_password', $::lx_office_conf{authentication}->{admin_password});
 
  78   $::auth->create_or_refresh_session;
 
  80   my $group = (SL::DB::Manager::AuthGroup->get_all(limit => 1))[0];
 
  82     SL::DB::AuthGroup->new(
 
  83       name        => t8('Full Access'),
 
  84       description => t8('Full access to all functions'),
 
  85       rights      => [ map { SL::DB::AuthGroupRight->new(right => $_, granted => 1) } SL::Auth::all_rights() ],
 
  89   if (!$self->apply_dbupgrade_scripts) {
 
 103     title => "kivitendo " . t8('Administration'),
 
 107 sub action_new_user {
 
 110   $self->user(SL::DB::AuthUser->new(
 
 114       numberformat => "1.000,00",
 
 115       dateformat   => "dd.mm.yy",
 
 116       stylesheet   => "kivitendo.css",
 
 121   $self->edit_user_form(title => t8('Create a new user'));
 
 124 sub action_edit_user {
 
 126   $self->edit_user_form(title => t8('Edit User'));
 
 129 sub action_save_user {
 
 131   my $params = delete($::form->{user})          || { };
 
 132   my $props  = delete($params->{config_values}) || { };
 
 133   my $is_new = !$params->{id};
 
 135   $self->user($is_new ? SL::DB::AuthUser->new : SL::DB::AuthUser->new(id => $params->{id})->load)
 
 136     ->assign_attributes(%{ $params })
 
 137     ->config_values({ %{ $self->user->config_values }, %{ $props } });
 
 139   my @errors = $self->user->validate;
 
 142     flash('error', @errors);
 
 143     $self->edit_user_form(title => $is_new ? t8('Create a new user') : t8('Edit User'));
 
 149   if ($::auth->can_change_password && $::form->{new_password}) {
 
 150     $::auth->change_password($self->user->login, $::form->{new_password});
 
 153   flash_later('info', $is_new ? t8('The user has been created.') : t8('The user has been saved.'));
 
 154   $self->redirect_to(action => 'show');
 
 157 sub action_delete_user {
 
 160   my @clients = @{ $self->user->clients || [] };
 
 162   if (!$self->user->delete) {
 
 163     flash('error', t8('The user could not be deleted.'));
 
 164     $self->edit_user_form(title => t8('Edit User'));
 
 168   # Flag corresponding entries in 'employee' as deleted.
 
 169   foreach my $client (@clients) {
 
 170     my $dbh = $client->dbconnect(AutoCommit => 1) || next;
 
 171     $dbh->do(qq|UPDATE employee SET deleted = TRUE WHERE login = ?|, undef, $self->user->login);
 
 175   flash_later('info', t8('The user has been deleted.'));
 
 176   $self->redirect_to(action => 'show');
 
 183 sub action_new_client {
 
 186   $self->client(SL::DB::AuthClient->new(
 
 187     dbhost   => $::auth->{DB_config}->{host},
 
 188     dbport   => $::auth->{DB_config}->{port},
 
 189     dbuser   => $::auth->{DB_config}->{user},
 
 190     dbpasswd => $::auth->{DB_config}->{password},
 
 193   $self->edit_client_form(title => t8('Create a new client'));
 
 196 sub action_edit_client {
 
 198   $self->edit_client_form(title => t8('Edit Client'));
 
 201 sub action_save_client {
 
 203   my $params = delete($::form->{client}) || { };
 
 204   my $is_new = !$params->{id};
 
 206   $self->client($is_new ? SL::DB::AuthClient->new : SL::DB::AuthClient->new(id => $params->{id})->load)->assign_attributes(%{ $params });
 
 208   my @errors = $self->client->validate;
 
 211     flash('error', @errors);
 
 212     $self->edit_client_form(title => $is_new ? t8('Create a new client') : t8('Edit Client'));
 
 217   if ($self->client->is_default) {
 
 218     SL::DB::Manager::AuthClient->update_all(set => { is_default => 0 }, where => [ '!id' => $self->client->id ]);
 
 221   flash_later('info', $is_new ? t8('The client has been created.') : t8('The client has been saved.'));
 
 222   $self->redirect_to(action => 'show');
 
 225 sub action_delete_client {
 
 228   if (!$self->client->delete) {
 
 229     flash('error', t8('The client could not be deleted.'));
 
 230     $self->edit_client_form(title => t8('Edit Client'));
 
 234   flash_later('info', t8('The client has been deleted.'));
 
 235   $self->redirect_to(action => 'show');
 
 238 sub action_test_database_connectivity {
 
 241   my %cfg       = %{ $::form->{client} || {} };
 
 242   my $dbconnect = 'dbi:Pg:dbname=' . $cfg{dbname} . ';host=' . $cfg{dbhost} . ';port=' . $cfg{dbport};
 
 243   my $dbh       = DBI->connect($dbconnect, $cfg{dbuser}, $cfg{dbpasswd});
 
 246   my $error     = $DBI::errstr;
 
 248   $dbh->disconnect if $dbh;
 
 250   $self->render('admin/test_db_connection',
 
 251                 title => t8('Database Connection Test'),
 
 260 sub action_new_group {
 
 263   $self->group(SL::DB::AuthGroup->new);
 
 264   $self->edit_group_form(title => t8('Create a new group'));
 
 267 sub action_edit_group {
 
 269   $self->edit_group_form(title => t8('Edit User Group'));
 
 272 sub action_save_group {
 
 275   my $params = delete($::form->{group}) || { };
 
 276   my $is_new = !$params->{id};
 
 278   $self->group($is_new ? SL::DB::AuthGroup->new : SL::DB::AuthGroup->new(id => $params->{id})->load)->assign_attributes(%{ $params });
 
 280   my @errors = $self->group->validate;
 
 283     flash('error', @errors);
 
 284     $self->edit_group_form(title => $is_new ? t8('Create a new user group') : t8('Edit User Group'));
 
 290   flash_later('info', $is_new ? t8('The user group has been created.') : t8('The user group has been saved.'));
 
 291   $self->redirect_to(action => 'show');
 
 294 sub action_delete_group {
 
 297   if (!$self->group->delete) {
 
 298     flash('error', t8('The user group could not be deleted.'));
 
 299     $self->edit_group_form(title => t8('Edit User Group'));
 
 303   flash_later('info', t8('The user group has been deleted.'));
 
 304   $self->redirect_to(action => 'show');
 
 311 sub action_list_printers {
 
 313   $self->render('admin/list_printers', title => t8('Printer management'));
 
 316 sub action_new_printer {
 
 319   $self->printer(SL::DB::Printer->new);
 
 320   $self->edit_printer_form(title => t8('Create a new printer'));
 
 323 sub action_edit_printer {
 
 325   $self->edit_printer_form(title => t8('Edit Printer'));
 
 328 sub action_save_printer {
 
 330   my $params = delete($::form->{printer}) || { };
 
 331   my $is_new = !$params->{id};
 
 333   $self->printer($is_new ? SL::DB::Printer->new : SL::DB::Printer->new(id => $params->{id})->load)->assign_attributes(%{ $params });
 
 335   my @errors = $self->printer->validate;
 
 338     flash('error', @errors);
 
 339     $self->edit_printer_form(title => $is_new ? t8('Create a new printer') : t8('Edit Printer'));
 
 343   $self->printer->save;
 
 345   flash_later('info', $is_new ? t8('The printer has been created.') : t8('The printer has been saved.'));
 
 346   $self->redirect_to(action => 'list_printers', 'client.id' => $self->client->id);
 
 349 sub action_delete_printer {
 
 352   if (!$self->printer->delete) {
 
 353     flash('error', t8('The printer could not be deleted.'));
 
 354     $self->edit_printer_form(title => t8('Edit Printer'));
 
 358   flash_later('info', t8('The printer has been deleted.'));
 
 359   $self->redirect_to(action => 'list_printers', 'client.id' => $self->client->id);
 
 363 # actions: locking, unlocking
 
 366 sub action_unlock_system {
 
 369   SL::System::InstallationLock->unlock;
 
 370   flash_later('info', t8('Lockfile removed!'));
 
 371   $self->redirect_to(action => 'show');
 
 374 sub action_lock_system {
 
 377   SL::System::InstallationLock->unlock;
 
 378   flash_later('info', t8('Lockfile created!'));
 
 379   $self->redirect_to(action => 'show');
 
 386 sub init_db_cfg            { $::lx_office_conf{'authentication/database'}                                                    }
 
 387 sub init_is_locked         { SL::System::InstallationLock->is_locked                                                         }
 
 388 sub init_client            { SL::DB::Manager::AuthClient->find_by(id => ($::form->{id} || ($::form->{client}  || {})->{id})) }
 
 389 sub init_user              { SL::DB::AuthUser  ->new(id => ($::form->{id} || ($::form->{user}    || {})->{id}))->load        }
 
 390 sub init_group             { SL::DB::AuthGroup ->new(id => ($::form->{id} || ($::form->{group}   || {})->{id}))->load        }
 
 391 sub init_printer           { SL::DB::Printer   ->new(id => ($::form->{id} || ($::form->{printer} || {})->{id}))->load        }
 
 392 sub init_all_clients       { SL::DB::Manager::AuthClient->get_all_sorted                                                     }
 
 393 sub init_all_users         { SL::DB::Manager::AuthUser  ->get_all_sorted                                                     }
 
 394 sub init_all_groups        { SL::DB::Manager::AuthGroup ->get_all_sorted                                                     }
 
 395 sub init_all_printers      { SL::DB::Manager::Printer   ->get_all_sorted                                                     }
 
 396 sub init_all_dateformats   { [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd)      ]                                              }
 
 397 sub init_all_numberformats { [ qw(1,000.00 1000.00 1.000,00 1000,00)          ]                                              }
 
 398 sub init_all_stylesheets   { [ qw(lx-office-erp.css Mobile.css kivitendo.css) ]                                              }
 
 399 sub init_all_menustyles    {
 
 401     { id => 'old', title => $::locale->text('Old (on the side)') },
 
 402     { id => 'v3',  title => $::locale->text('Top (CSS)') },
 
 403     { id => 'neu', title => $::locale->text('Top (Javascript)') },
 
 407 sub init_all_rights {
 
 408   my (@sections, $current_section);
 
 410   foreach my $entry ($::auth->all_rights_full) {
 
 411     if ($entry->[0] =~ m/^--/) {
 
 412       push @sections, { description => $entry->[1], rights => [] };
 
 414     } elsif (@sections) {
 
 415       push @{ $sections[-1]->{rights} }, {
 
 417         description => $entry->[1],
 
 421       die "Right without sections: " . join('::', @{ $entry });
 
 428 sub init_all_countrycodes {
 
 429   my %cc = User->country_codes;
 
 430   return [ map { id => $_, title => $cc{$_} }, sort { $cc{$a} cmp $cc{$b} } keys %cc ];
 
 438   my ($self, $action) = @_;
 
 440   $::request->layout(SL::Layout::Dispatcher->new(style => 'admin'));
 
 441   $::request->layout->use_stylesheet("lx-office-erp.css");
 
 442   $::form->{favicon} = "favicon.ico";
 
 448   $self->client((first { $_->is_default } @{ $self->all_clients }) || $self->all_clients->[0]) if !$self->client;
 
 449   $::auth->set_client($self->client->id);
 
 457 sub use_multiselect_js {
 
 460   $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
 
 465   my ($self, %params) = @_;
 
 466   $::request->layout->focus('#admin_password');
 
 467   $self->render('admin/adminlogin', title => t8('kivitendo v#1 administration', $::form->read_version), %params);
 
 471   my ($self, %params) = @_;
 
 472   $self->use_multiselect_js->render('admin/edit_user', %params);
 
 475 sub edit_client_form {
 
 476   my ($self, %params) = @_;
 
 477   $self->use_multiselect_js->render('admin/edit_client', %params);
 
 480 sub edit_group_form {
 
 481   my ($self, %params) = @_;
 
 482   $self->use_multiselect_js->render('admin/edit_group', %params);
 
 485 sub edit_printer_form {
 
 486   my ($self, %params) = @_;
 
 487   $self->render('admin/edit_printer', %params);
 
 494 sub check_auth_db_and_tables {
 
 497   if (!$::auth->check_database) {
 
 498     $self->render('admin/check_auth_database', title => t8('Authentification database creation'));
 
 502   if (!$::auth->check_tables) {
 
 503     $self->render('admin/check_auth_tables', title => t8('Authentification tables creation'));
 
 510 sub apply_dbupgrade_scripts {
 
 511   return SL::DBUpgrade2->new(form => $::form, auth => 1)->apply_admin_dbupgrade_scripts(1);
 
 514 sub authenticate_root {
 
 517   return 1 if $::auth->authenticate_root($::form->{'{AUTH}admin_password'}) == $::auth->OK();
 
 519   $::auth->punish_wrong_login;
 
 520   $::auth->delete_session_value('admin_password');
 
 522   $self->login_form(error => t8('Incorrect password!'));