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   if (!$self->user->delete) {
 
 161     flash('error', t8('The user could not be deleted.'));
 
 162     $self->edit_user_form(title => t8('Edit User'));
 
 166   flash_later('info', t8('The user has been deleted.'));
 
 167   $self->redirect_to(action => 'show');
 
 174 sub action_new_client {
 
 177   $self->client(SL::DB::AuthClient->new(
 
 178     dbhost   => $::auth->{DB_config}->{host},
 
 179     dbport   => $::auth->{DB_config}->{port},
 
 180     dbuser   => $::auth->{DB_config}->{user},
 
 181     dbpasswd => $::auth->{DB_config}->{password},
 
 184   $self->edit_client_form(title => t8('Create a new client'));
 
 187 sub action_edit_client {
 
 189   $self->edit_client_form(title => t8('Edit Client'));
 
 192 sub action_save_client {
 
 194   my $params = delete($::form->{client}) || { };
 
 195   my $is_new = !$params->{id};
 
 197   $self->client($is_new ? SL::DB::AuthClient->new : SL::DB::AuthClient->new(id => $params->{id})->load)->assign_attributes(%{ $params });
 
 199   my @errors = $self->client->validate;
 
 202     flash('error', @errors);
 
 203     $self->edit_client_form(title => $is_new ? t8('Create a new client') : t8('Edit Client'));
 
 208   if ($self->client->is_default) {
 
 209     SL::DB::Manager::AuthClient->update_all(set => { is_default => 0 }, where => [ '!id' => $self->client->id ]);
 
 212   flash_later('info', $is_new ? t8('The client has been created.') : t8('The client has been saved.'));
 
 213   $self->redirect_to(action => 'show');
 
 216 sub action_delete_client {
 
 219   if (!$self->client->delete) {
 
 220     flash('error', t8('The client could not be deleted.'));
 
 221     $self->edit_client_form(title => t8('Edit Client'));
 
 225   flash_later('info', t8('The client has been deleted.'));
 
 226   $self->redirect_to(action => 'show');
 
 229 sub action_test_database_connectivity {
 
 232   my %cfg       = %{ $::form->{client} || {} };
 
 233   my $dbconnect = 'dbi:Pg:dbname=' . $cfg{dbname} . ';host=' . $cfg{dbhost} . ';port=' . $cfg{dbport};
 
 234   my $dbh       = DBI->connect($dbconnect, $cfg{dbuser}, $cfg{dbpasswd});
 
 237   my $error     = $DBI::errstr;
 
 239   $dbh->disconnect if $dbh;
 
 241   $self->render('admin/test_db_connection',
 
 242                 title => t8('Database Connection Test'),
 
 251 sub action_new_group {
 
 254   $self->group(SL::DB::AuthGroup->new);
 
 255   $self->edit_group_form(title => t8('Create a new group'));
 
 258 sub action_edit_group {
 
 260   $self->edit_group_form(title => t8('Edit User Group'));
 
 263 sub action_save_group {
 
 266   my $params = delete($::form->{group}) || { };
 
 267   my $is_new = !$params->{id};
 
 269   $self->group($is_new ? SL::DB::AuthGroup->new : SL::DB::AuthGroup->new(id => $params->{id})->load)->assign_attributes(%{ $params });
 
 271   my @errors = $self->group->validate;
 
 274     flash('error', @errors);
 
 275     $self->edit_group_form(title => $is_new ? t8('Create a new user group') : t8('Edit User Group'));
 
 281   flash_later('info', $is_new ? t8('The user group has been created.') : t8('The user group has been saved.'));
 
 282   $self->redirect_to(action => 'show');
 
 285 sub action_delete_group {
 
 288   if (!$self->group->delete) {
 
 289     flash('error', t8('The user group could not be deleted.'));
 
 290     $self->edit_group_form(title => t8('Edit User Group'));
 
 294   flash_later('info', t8('The user group has been deleted.'));
 
 295   $self->redirect_to(action => 'show');
 
 302 sub action_list_printers {
 
 304   $self->render('admin/list_printers', title => t8('Printer management'));
 
 307 sub action_new_printer {
 
 310   $self->printer(SL::DB::Printer->new);
 
 311   $self->edit_printer_form(title => t8('Create a new printer'));
 
 314 sub action_edit_printer {
 
 316   $self->edit_printer_form(title => t8('Edit Printer'));
 
 319 sub action_save_printer {
 
 321   my $params = delete($::form->{printer}) || { };
 
 322   my $is_new = !$params->{id};
 
 324   $self->printer($is_new ? SL::DB::Printer->new : SL::DB::Printer->new(id => $params->{id})->load)->assign_attributes(%{ $params });
 
 326   my @errors = $self->printer->validate;
 
 329     flash('error', @errors);
 
 330     $self->edit_printer_form(title => $is_new ? t8('Create a new printer') : t8('Edit Printer'));
 
 334   $self->printer->save;
 
 336   flash_later('info', $is_new ? t8('The printer has been created.') : t8('The printer has been saved.'));
 
 337   $self->redirect_to(action => 'list_printers', 'client.id' => $self->client->id);
 
 340 sub action_delete_printer {
 
 343   if (!$self->printer->delete) {
 
 344     flash('error', t8('The printer could not be deleted.'));
 
 345     $self->edit_printer_form(title => t8('Edit Printer'));
 
 349   flash_later('info', t8('The printer has been deleted.'));
 
 350   $self->redirect_to(action => 'list_printers', 'client.id' => $self->client->id);
 
 354 # actions: locking, unlocking
 
 357 sub action_unlock_system {
 
 360   SL::System::InstallationLock->unlock;
 
 361   flash_later('info', t8('Lockfile removed!'));
 
 362   $self->redirect_to(action => 'show');
 
 365 sub action_lock_system {
 
 368   SL::System::InstallationLock->unlock;
 
 369   flash_later('info', t8('Lockfile created!'));
 
 370   $self->redirect_to(action => 'show');
 
 377 sub init_db_cfg            { $::lx_office_conf{'authentication/database'}                                                    }
 
 378 sub init_is_locked         { SL::System::InstallationLock->is_locked                                                         }
 
 379 sub init_client            { SL::DB::Manager::AuthClient->find_by(id => ($::form->{id} || ($::form->{client}  || {})->{id})) }
 
 380 sub init_user              { SL::DB::AuthUser  ->new(id => ($::form->{id} || ($::form->{user}    || {})->{id}))->load        }
 
 381 sub init_group             { SL::DB::AuthGroup ->new(id => ($::form->{id} || ($::form->{group}   || {})->{id}))->load        }
 
 382 sub init_printer           { SL::DB::Printer   ->new(id => ($::form->{id} || ($::form->{printer} || {})->{id}))->load        }
 
 383 sub init_all_clients       { SL::DB::Manager::AuthClient->get_all_sorted                                                     }
 
 384 sub init_all_users         { SL::DB::Manager::AuthUser  ->get_all_sorted                                                     }
 
 385 sub init_all_groups        { SL::DB::Manager::AuthGroup ->get_all_sorted                                                     }
 
 386 sub init_all_printers      { SL::DB::Manager::Printer   ->get_all_sorted                                                     }
 
 387 sub init_all_dateformats   { [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd)      ]                                              }
 
 388 sub init_all_numberformats { [ qw(1,000.00 1000.00 1.000,00 1000,00)          ]                                              }
 
 389 sub init_all_stylesheets   { [ qw(lx-office-erp.css Mobile.css kivitendo.css) ]                                              }
 
 390 sub init_all_menustyles    {
 
 392     { id => 'old', title => $::locale->text('Old (on the side)') },
 
 393     { id => 'v3',  title => $::locale->text('Top (CSS)') },
 
 394     { id => 'neu', title => $::locale->text('Top (Javascript)') },
 
 398 sub init_all_rights {
 
 399   my (@sections, $current_section);
 
 401   foreach my $entry ($::auth->all_rights_full) {
 
 402     if ($entry->[0] =~ m/^--/) {
 
 403       push @sections, { description => $entry->[1], rights => [] };
 
 405     } elsif (@sections) {
 
 406       push @{ $sections[-1]->{rights} }, {
 
 408         description => $entry->[1],
 
 412       die "Right without sections: " . join('::', @{ $entry });
 
 419 sub init_all_countrycodes {
 
 420   my %cc = User->country_codes;
 
 421   return [ map { id => $_, title => $cc{$_} }, sort { $cc{$a} cmp $cc{$b} } keys %cc ];
 
 429   my ($self, $action) = @_;
 
 431   $::request->layout(SL::Layout::Dispatcher->new(style => 'admin'));
 
 432   $::request->layout->use_stylesheet("lx-office-erp.css");
 
 433   $::form->{favicon} = "favicon.ico";
 
 439   $self->client((first { $_->is_default } @{ $self->all_clients }) || $self->all_clients->[0]) if !$self->client;
 
 440   $::auth->set_client($self->client->id);
 
 448 sub use_multiselect_js {
 
 451   $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
 
 456   my ($self, %params) = @_;
 
 457   $::request->layout->focus('#admin_password');
 
 458   $self->render('admin/adminlogin', title => t8('kivitendo v#1 administration', $::form->{version}), %params);
 
 462   my ($self, %params) = @_;
 
 463   $self->use_multiselect_js->render('admin/edit_user', %params);
 
 466 sub edit_client_form {
 
 467   my ($self, %params) = @_;
 
 468   $self->use_multiselect_js->render('admin/edit_client', %params);
 
 471 sub edit_group_form {
 
 472   my ($self, %params) = @_;
 
 473   $self->use_multiselect_js->render('admin/edit_group', %params);
 
 476 sub edit_printer_form {
 
 477   my ($self, %params) = @_;
 
 478   $self->render('admin/edit_printer', %params);
 
 485 sub check_auth_db_and_tables {
 
 488   if (!$::auth->check_database) {
 
 489     $self->render('admin/check_auth_database', title => t8('Authentification database creation'));
 
 493   if (!$::auth->check_tables) {
 
 494     $self->render('admin/check_auth_tables', title => t8('Authentification tables creation'));
 
 501 sub apply_dbupgrade_scripts {
 
 502   return SL::DBUpgrade2->new(form => $::form, auth => 1)->apply_admin_dbupgrade_scripts(1);
 
 505 sub authenticate_root {
 
 508   return 1 if $::auth->authenticate_root($::form->{'{AUTH}admin_password'}) == $::auth->OK();
 
 510   $::auth->punish_wrong_login;
 
 511   $::auth->delete_session_value('admin_password');
 
 513   $self->login_form(error => t8('Incorrect password!'));