Locking in eigenes Modul verschieben
[kivitendo-erp.git] / SL / Controller / Admin.pm
1 package SL::Controller::Admin;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use IO::File;
8 use List::Util qw(first);
9
10 use SL::DB::AuthUser;
11 use SL::DB::AuthGroup;
12 use SL::DB::Printer;
13 use SL::Helper::Flash;
14 use SL::Locale::String qw(t8);
15 use SL::System::InstallationLock;
16 use SL::User;
17
18 use Rose::Object::MakeMethods::Generic
19 (
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) ],
22 );
23
24 __PACKAGE__->run_before(\&setup_layout);
25 __PACKAGE__->run_before(\&setup_client, only => [ qw(list_printers new_printer edit_printer save_printer delete_printer) ]);
26
27 sub get_auth_level { "admin" };
28 sub keep_auth_vars {
29   my ($class, %params) = @_;
30   return $params{action} eq 'login';
31 }
32
33 #
34 # actions: login, logout
35 #
36
37 sub action_login {
38   my ($self) = @_;
39
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');
45 }
46
47 sub action_logout {
48   my ($self) = @_;
49   $::auth->destroy_session;
50   $self->redirect_to(action => 'login');
51 }
52
53 #
54 # actions: creating the authentication database & tables, applying database ugprades
55 #
56
57 sub action_apply_dbupgrade_scripts {
58   my ($self) = @_;
59
60   return if $self->apply_dbupgrade_scripts;
61   $self->action_show;
62 }
63
64 sub action_create_auth_db {
65   my ($self) = @_;
66
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;
71 }
72
73 sub action_create_auth_tables {
74   my ($self) = @_;
75
76   $::auth->create_tables;
77   $::auth->set_session_value('admin_password', $::lx_office_conf{authentication}->{admin_password});
78   $::auth->create_or_refresh_session;
79
80   my $group = (SL::DB::Manager::AuthGroup->get_all(limit => 1))[0];
81   if (!$group) {
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() ],
86     )->save;
87   }
88
89   if (!$self->apply_dbupgrade_scripts) {
90     $self->action_login;
91   }
92 }
93
94 #
95 # actions: users
96 #
97
98 sub action_show {
99   my ($self) = @_;
100
101   $self->render(
102     "admin/show",
103     title => "kivitendo " . t8('Administration'),
104   );
105 }
106
107 sub action_new_user {
108   my ($self) = @_;
109
110   $self->user(SL::DB::AuthUser->new(
111     config_values => {
112       vclimit      => 200,
113       countrycode  => "de",
114       numberformat => "1.000,00",
115       dateformat   => "dd.mm.yy",
116       stylesheet   => "kivitendo.css",
117       menustyle    => "neu",
118     },
119   ));
120
121   $self->edit_user_form(title => t8('Create a new user'));
122 }
123
124 sub action_edit_user {
125   my ($self) = @_;
126   $self->edit_user_form(title => t8('Edit User'));
127 }
128
129 sub action_save_user {
130   my ($self) = @_;
131   my $params = delete($::form->{user})          || { };
132   my $props  = delete($params->{config_values}) || { };
133   my $is_new = !$params->{id};
134
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 } });
138
139   my @errors = $self->user->validate;
140
141   if (@errors) {
142     flash('error', @errors);
143     $self->edit_user_form(title => $is_new ? t8('Create a new user') : t8('Edit User'));
144     return;
145   }
146
147   $self->user->save;
148
149   if ($::auth->can_change_password && $::form->{new_password}) {
150     $::auth->change_password($self->user->login, $::form->{new_password});
151   }
152
153   flash_later('info', $is_new ? t8('The user has been created.') : t8('The user has been saved.'));
154   $self->redirect_to(action => 'show');
155 }
156
157 sub action_delete_user {
158   my ($self) = @_;
159
160   if (!$self->user->delete) {
161     flash('error', t8('The user could not be deleted.'));
162     $self->edit_user_form(title => t8('Edit User'));
163     return;
164   }
165
166   flash_later('info', t8('The user has been deleted.'));
167   $self->redirect_to(action => 'show');
168 }
169
170 #
171 # actions: clients
172 #
173
174 sub action_new_client {
175   my ($self) = @_;
176
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},
182   ));
183
184   $self->edit_client_form(title => t8('Create a new client'));
185 }
186
187 sub action_edit_client {
188   my ($self) = @_;
189   $self->edit_client_form(title => t8('Edit Client'));
190 }
191
192 sub action_save_client {
193   my ($self) = @_;
194   my $params = delete($::form->{client}) || { };
195   my $is_new = !$params->{id};
196
197   $self->client($is_new ? SL::DB::AuthClient->new : SL::DB::AuthClient->new(id => $params->{id})->load)->assign_attributes(%{ $params });
198
199   my @errors = $self->client->validate;
200
201   if (@errors) {
202     flash('error', @errors);
203     $self->edit_client_form(title => $is_new ? t8('Create a new client') : t8('Edit Client'));
204     return;
205   }
206
207   $self->client->save;
208   if ($self->client->is_default) {
209     SL::DB::Manager::AuthClient->update_all(set => { is_default => 0 }, where => [ '!id' => $self->client->id ]);
210   }
211
212   flash_later('info', $is_new ? t8('The client has been created.') : t8('The client has been saved.'));
213   $self->redirect_to(action => 'show');
214 }
215
216 sub action_delete_client {
217   my ($self) = @_;
218
219   if (!$self->client->delete) {
220     flash('error', t8('The client could not be deleted.'));
221     $self->edit_client_form(title => t8('Edit Client'));
222     return;
223   }
224
225   flash_later('info', t8('The client has been deleted.'));
226   $self->redirect_to(action => 'show');
227 }
228
229 sub action_test_database_connectivity {
230   my ($self)    = @_;
231
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});
235
236   my $ok        = !!$dbh;
237   my $error     = $DBI::errstr;
238
239   $dbh->disconnect if $dbh;
240
241   $self->render('admin/test_db_connection',
242                 title => t8('Database Connection Test'),
243                 ok    => $ok,
244                 error => $error);
245 }
246
247 #
248 # actions: groups
249 #
250
251 sub action_new_group {
252   my ($self) = @_;
253
254   $self->group(SL::DB::AuthGroup->new);
255   $self->edit_group_form(title => t8('Create a new group'));
256 }
257
258 sub action_edit_group {
259   my ($self) = @_;
260   $self->edit_group_form(title => t8('Edit User Group'));
261 }
262
263 sub action_save_group {
264   my ($self) = @_;
265
266   my $params = delete($::form->{group}) || { };
267   my $is_new = !$params->{id};
268
269   $self->group($is_new ? SL::DB::AuthGroup->new : SL::DB::AuthGroup->new(id => $params->{id})->load)->assign_attributes(%{ $params });
270
271   my @errors = $self->group->validate;
272
273   if (@errors) {
274     flash('error', @errors);
275     $self->edit_group_form(title => $is_new ? t8('Create a new user group') : t8('Edit User Group'));
276     return;
277   }
278
279   $self->group->save;
280
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');
283 }
284
285 sub action_delete_group {
286   my ($self) = @_;
287
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'));
291     return;
292   }
293
294   flash_later('info', t8('The user group has been deleted.'));
295   $self->redirect_to(action => 'show');
296 }
297
298 #
299 # actions: printers
300 #
301
302 sub action_list_printers {
303   my ($self) = @_;
304   $self->render('admin/list_printers', title => t8('Printer management'));
305 }
306
307 sub action_new_printer {
308   my ($self) = @_;
309
310   $self->printer(SL::DB::Printer->new);
311   $self->edit_printer_form(title => t8('Create a new printer'));
312 }
313
314 sub action_edit_printer {
315   my ($self) = @_;
316   $self->edit_printer_form(title => t8('Edit Printer'));
317 }
318
319 sub action_save_printer {
320   my ($self) = @_;
321   my $params = delete($::form->{printer}) || { };
322   my $is_new = !$params->{id};
323
324   $self->printer($is_new ? SL::DB::Printer->new : SL::DB::Printer->new(id => $params->{id})->load)->assign_attributes(%{ $params });
325
326   my @errors = $self->printer->validate;
327
328   if (@errors) {
329     flash('error', @errors);
330     $self->edit_printer_form(title => $is_new ? t8('Create a new printer') : t8('Edit Printer'));
331     return;
332   }
333
334   $self->printer->save;
335
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);
338 }
339
340 sub action_delete_printer {
341   my ($self) = @_;
342
343   if (!$self->printer->delete) {
344     flash('error', t8('The printer could not be deleted.'));
345     $self->edit_printer_form(title => t8('Edit Printer'));
346     return;
347   }
348
349   flash_later('info', t8('The printer has been deleted.'));
350   $self->redirect_to(action => 'list_printers', 'client.id' => $self->client->id);
351 }
352
353 #
354 # actions: locking, unlocking
355 #
356
357 sub action_unlock_system {
358   my ($self) = @_;
359
360   SL::System::InstallationLock->unlock;
361   flash_later('info', t8('Lockfile removed!'));
362   $self->redirect_to(action => 'show');
363 }
364
365 sub action_lock_system {
366   my ($self) = @_;
367
368   SL::System::InstallationLock->unlock;
369   flash_later('info', t8('Lockfile created!'));
370   $self->redirect_to(action => 'show');
371 }
372
373 #
374 # initializers
375 #
376
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    {
391   return [
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)') },
395   ];
396 }
397
398 sub init_all_rights {
399   my (@sections, $current_section);
400
401   foreach my $entry ($::auth->all_rights_full) {
402     if ($entry->[0] =~ m/^--/) {
403       push @sections, { description => $entry->[1], rights => [] };
404
405     } elsif (@sections) {
406       push @{ $sections[-1]->{rights} }, {
407         name        => $entry->[0],
408         description => $entry->[1],
409       };
410
411     } else {
412       die "Right without sections: " . join('::', @{ $entry });
413     }
414   }
415
416   return \@sections;
417 }
418
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 ];
422 }
423
424 #
425 # filters
426 #
427
428 sub setup_layout {
429   my ($self, $action) = @_;
430
431   $::request->layout(SL::Layout::Dispatcher->new(style => 'admin'));
432   $::request->layout->use_stylesheet("lx-office-erp.css");
433   $::form->{favicon} = "favicon.ico";
434 }
435
436 sub setup_client {
437   my ($self) = @_;
438
439   $self->client((first { $_->is_default } @{ $self->all_clients }) || $self->all_clients->[0]) if !$self->client;
440   $::auth->set_client($self->client->id);
441 }
442
443
444 #
445 # displaying forms
446 #
447
448 sub use_multiselect_js {
449   my ($self) = @_;
450
451   $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
452   return $self;
453 }
454
455 sub login_form {
456   my ($self, %params) = @_;
457   $::request->layout->focus('#admin_password');
458   $self->render('admin/adminlogin', title => t8('kivitendo v#1 administration', $::form->{version}), %params);
459 }
460
461 sub edit_user_form {
462   my ($self, %params) = @_;
463   $self->use_multiselect_js->render('admin/edit_user', %params);
464 }
465
466 sub edit_client_form {
467   my ($self, %params) = @_;
468   $self->use_multiselect_js->render('admin/edit_client', %params);
469 }
470
471 sub edit_group_form {
472   my ($self, %params) = @_;
473   $self->use_multiselect_js->render('admin/edit_group', %params);
474 }
475
476 sub edit_printer_form {
477   my ($self, %params) = @_;
478   $self->render('admin/edit_printer', %params);
479 }
480
481 #
482 # helpers
483 #
484
485 sub check_auth_db_and_tables {
486   my ($self) = @_;
487
488   if (!$::auth->check_database) {
489     $self->render('admin/check_auth_database', title => t8('Authentification database creation'));
490     return 0;
491   }
492
493   if (!$::auth->check_tables) {
494     $self->render('admin/check_auth_tables', title => t8('Authentification tables creation'));
495     return 0;
496   }
497
498   return 1;
499 }
500
501 sub apply_dbupgrade_scripts {
502   return SL::DBUpgrade2->new(form => $::form, auth => 1)->apply_admin_dbupgrade_scripts(1);
503 }
504
505 sub authenticate_root {
506   my ($self) = @_;
507
508   return 1 if $::auth->authenticate_root($::form->{'{AUTH}admin_password'}) == $::auth->OK();
509
510   $::auth->punish_wrong_login;
511   $::auth->delete_session_value('admin_password');
512
513   $self->login_form(error => t8('Incorrect password!'));
514
515   return undef;
516 }
517
518 1;