Gruppenverwaltung auf Controllermodel umgestellt
[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
9 use SL::DB::AuthUser;
10 use SL::DB::AuthGroup;
11 use SL::Helper::Flash;
12 use SL::Locale::String qw(t8);
13 use SL::User;
14
15 use Rose::Object::MakeMethods::Generic
16 (
17   'scalar --get_set_init' => [ qw(client user group nologin_file_name db_cfg all_dateformats all_numberformats all_countrycodes all_stylesheets all_menustyles all_clients all_groups all_users all_rights is_locked) ],
18 );
19
20 __PACKAGE__->run_before(\&setup_layout);
21
22 sub get_auth_level { "admin" };
23 sub keep_auth_vars {
24   my ($class, %params) = @_;
25   return $params{action} eq 'login';
26 }
27
28 #
29 # actions: login, logout
30 #
31
32 sub action_login {
33   my ($self) = @_;
34
35   return $self->login_form if !$::form->{do_login};
36   return                   if !$self->authenticate_root;
37   return                   if !$self->check_auth_db_and_tables;
38   return                   if  $self->apply_dbupgrade_scripts;
39   $self->redirect_to(action => 'show');
40 }
41
42 sub action_logout {
43   my ($self) = @_;
44   $::auth->destroy_session;
45   $self->redirect_to(action => 'login');
46 }
47
48 #
49 # actions: creating the authentication database & tables, applying database ugprades
50 #
51
52 sub action_apply_dbupgrade_scripts {
53   my ($self) = @_;
54
55   return if $self->apply_dbupgrade_scripts;
56   $self->action_show;
57 }
58
59 sub action_create_auth_db {
60   my ($self) = @_;
61
62   $::auth->create_database(superuser          => $::form->{db_superuser},
63                            superuser_password => $::form->{db_superuser_password},
64                            template           => $::form->{db_template});
65   $self->check_auth_db_and_tables;
66 }
67
68 sub action_create_auth_tables {
69   my ($self) = @_;
70
71   $::auth->create_tables;
72   $::auth->set_session_value('admin_password', $::lx_office_conf{authentication}->{admin_password});
73   $::auth->create_or_refresh_session;
74
75   my $group = (SL::DB::Manager::AuthGroup->get_all(limit => 1))[0];
76   if (!$group) {
77     SL::DB::AuthGroup->new(
78       name        => t8('Full Access'),
79       description => t8('Full access to all functions'),
80       rights      => [ map { SL::DB::AuthGroupRight->new(right => $_, granted => 1) } SL::Auth::all_rights() ],
81     )->save;
82   }
83
84   if (!$self->apply_dbupgrade_scripts) {
85     $self->action_login;
86   }
87 }
88
89 #
90 # actions: users
91 #
92
93 sub action_show {
94   my ($self) = @_;
95
96   $self->render(
97     "admin/show",
98     title => "kivitendo " . t8('Administration'),
99   );
100 }
101
102 sub action_new_user {
103   my ($self) = @_;
104
105   $self->user(SL::DB::AuthUser->new(
106     config_values => {
107       vclimit      => 200,
108       countrycode  => "de",
109       numberformat => "1.000,00",
110       dateformat   => "dd.mm.yy",
111       stylesheet   => "kivitendo.css",
112       menustyle    => "neu",
113     },
114   ));
115
116   $self->edit_user_form(title => t8('Create a new user'));
117 }
118
119 sub action_edit_user {
120   my ($self) = @_;
121   $self->edit_user_form(title => t8('Edit User'));
122 }
123
124 sub action_save_user {
125   my ($self) = @_;
126   my $params = delete($::form->{user})          || { };
127   my $props  = delete($params->{config_values}) || { };
128   my $is_new = !$params->{id};
129
130   $self->user($is_new ? SL::DB::AuthUser->new : SL::DB::AuthUser->new(id => $params->{id})->load)
131     ->assign_attributes(%{ $params })
132     ->config_values({ %{ $self->user->config_values }, %{ $props } });
133
134   my @errors = $self->user->validate;
135
136   if (@errors) {
137     flash('error', @errors);
138     $self->edit_user_form(title => $is_new ? t8('Create a new user') : t8('Edit User'));
139     return;
140   }
141
142   $self->user->save;
143
144   if ($::auth->can_change_password && $::form->{new_password}) {
145     $::auth->change_password($self->user->login, $::form->{new_password});
146   }
147
148   flash_later('info', $is_new ? t8('The user has been created.') : t8('The user has been saved.'));
149   $self->redirect_to(action => 'show');
150 }
151
152 sub action_delete_user {
153   my ($self) = @_;
154
155   if (!$self->user->delete) {
156     flash('error', t8('The user could not be deleted.'));
157     $self->edit_user_form(title => t8('Edit User'));
158     return;
159   }
160
161   flash_later('info', t8('The user has been deleted.'));
162   $self->redirect_to(action => 'show');
163 }
164
165 #
166 # actions: clients
167 #
168
169 sub action_new_client {
170   my ($self) = @_;
171
172   $self->client(SL::DB::AuthClient->new(
173     dbhost   => $::auth->{DB_config}->{host},
174     dbport   => $::auth->{DB_config}->{port},
175     dbuser   => $::auth->{DB_config}->{user},
176     dbpasswd => $::auth->{DB_config}->{password},
177   ));
178
179   $self->edit_client_form(title => t8('Create a new client'));
180 }
181
182 sub action_edit_client {
183   my ($self) = @_;
184   $self->edit_client_form(title => t8('Edit Client'));
185 }
186
187 sub action_save_client {
188   my ($self) = @_;
189   my $params = delete($::form->{client}) || { };
190   my $is_new = !$params->{id};
191
192   $self->client($is_new ? SL::DB::AuthClient->new : SL::DB::AuthClient->new(id => $params->{id})->load)->assign_attributes(%{ $params });
193
194   my @errors = $self->client->validate;
195
196   if (@errors) {
197     flash('error', @errors);
198     $self->edit_client_form(title => $is_new ? t8('Create a new client') : t8('Edit Client'));
199     return;
200   }
201
202   $self->client->save;
203   if ($self->client->is_default) {
204     SL::DB::Manager::AuthClient->update_all(set => { is_default => 0 }, where => [ '!id' => $self->client->id ]);
205   }
206
207   flash_later('info', $is_new ? t8('The client has been created.') : t8('The client has been saved.'));
208   $self->redirect_to(action => 'show');
209 }
210
211 sub action_delete_client {
212   my ($self) = @_;
213
214   if (!$self->client->delete) {
215     flash('error', t8('The client could not be deleted.'));
216     $self->edit_client_form(title => t8('Edit Client'));
217     return;
218   }
219
220   flash_later('info', t8('The client has been deleted.'));
221   $self->redirect_to(action => 'show');
222 }
223
224 sub action_test_database_connectivity {
225   my ($self)    = @_;
226
227   my %cfg       = %{ $::form->{client} || {} };
228   my $dbconnect = 'dbi:Pg:dbname=' . $cfg{dbname} . ';host=' . $cfg{dbhost} . ';port=' . $cfg{dbport};
229   my $dbh       = DBI->connect($dbconnect, $cfg{dbuser}, $cfg{dbpasswd});
230
231   my $ok        = !!$dbh;
232   my $error     = $DBI::errstr;
233
234   $dbh->disconnect if $dbh;
235
236   $self->render('admin/test_db_connection',
237                 title => t8('Database Connection Test'),
238                 ok    => $ok,
239                 error => $error);
240 }
241
242 #
243 # actions: groups
244 #
245
246 sub action_new_group {
247   my ($self) = @_;
248
249   $self->group(SL::DB::AuthGroup->new);
250   $self->edit_group_form(title => t8('Create a new group'));
251 }
252
253 sub action_edit_group {
254   my ($self) = @_;
255   $self->edit_group_form(title => t8('Edit User Group'));
256 }
257
258 sub action_save_group {
259   my ($self) = @_;
260
261   my $params = delete($::form->{group}) || { };
262   my $is_new = !$params->{id};
263
264   $self->group($is_new ? SL::DB::AuthGroup->new : SL::DB::AuthGroup->new(id => $params->{id})->load)->assign_attributes(%{ $params });
265
266   my @errors = $self->group->validate;
267
268   if (@errors) {
269     flash('error', @errors);
270     $self->edit_group_form(title => $is_new ? t8('Create a new user group') : t8('Edit User Group'));
271     return;
272   }
273
274   $self->group->save;
275
276   flash_later('info', $is_new ? t8('The user group has been created.') : t8('The user group has been saved.'));
277   $self->redirect_to(action => 'show');
278 }
279
280 sub action_delete_group {
281   my ($self) = @_;
282
283   if (!$self->group->delete) {
284     flash('error', t8('The user group could not be deleted.'));
285     $self->edit_group_form(title => t8('Edit User Group'));
286     return;
287   }
288
289   flash_later('info', t8('The user group has been deleted.'));
290   $self->redirect_to(action => 'show');
291 }
292
293 #
294 # actions: locking, unlocking
295 #
296
297 sub action_unlock_system {
298   my ($self) = @_;
299   unlink $self->nologin_file_name;
300   flash_later('info', t8('Lockfile removed!'));
301   $self->redirect_to(action => 'show');
302 }
303
304 sub action_lock_system {
305   my ($self) = @_;
306
307   my $fh = IO::File->new($self->nologin_file_name, "w");
308   if (!$fh) {
309     $::form->error(t8('Cannot create Lock!'));
310
311   } else {
312     $fh->close;
313     flash_later('info', t8('Lockfile created!'));
314     $self->redirect_to(action => 'show');
315   }
316 }
317
318 #
319 # initializers
320 #
321
322 sub init_db_cfg            { $::lx_office_conf{'authentication/database'}                                            }
323 sub init_nologin_file_name { $::lx_office_conf{paths}->{userspath} . '/nologin';                                     }
324 sub init_is_locked         { -e $_[0]->nologin_file_name                                                             }
325 sub init_client            { SL::DB::AuthClient->new(id => ($::form->{id} || ($::form->{client} || {})->{id}))->load }
326 sub init_user              { SL::DB::AuthUser  ->new(id => ($::form->{id} || ($::form->{user}   || {})->{id}))->load }
327 sub init_group             { SL::DB::AuthGroup ->new(id => ($::form->{id} || ($::form->{group}  || {})->{id}))->load }
328 sub init_all_clients       { SL::DB::Manager::AuthClient->get_all_sorted                                             }
329 sub init_all_users         { SL::DB::Manager::AuthUser->get_all_sorted                                               }
330 sub init_all_groups        { SL::DB::Manager::AuthGroup->get_all_sorted                                              }
331 sub init_all_dateformats   { [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd)      ]                                      }
332 sub init_all_numberformats { [ qw(1,000.00 1000.00 1.000,00 1000,00)          ]                                      }
333 sub init_all_stylesheets   { [ qw(lx-office-erp.css Mobile.css kivitendo.css) ]                                      }
334 sub init_all_menustyles    {
335   return [
336     { id => 'old', title => $::locale->text('Old (on the side)') },
337     { id => 'v3',  title => $::locale->text('Top (CSS)') },
338     { id => 'neu', title => $::locale->text('Top (Javascript)') },
339   ];
340 }
341
342 sub init_all_rights {
343   my (@sections, $current_section);
344
345   foreach my $entry ($::auth->all_rights_full) {
346     if ($entry->[0] =~ m/^--/) {
347       push @sections, { description => $entry->[1], rights => [] };
348
349     } elsif (@sections) {
350       push @{ $sections[-1]->{rights} }, {
351         name        => $entry->[0],
352         description => $entry->[1],
353       };
354
355     } else {
356       die "Right without sections: " . join('::', @{ $entry });
357     }
358   }
359
360   return \@sections;
361 }
362
363 sub init_all_countrycodes {
364   my %cc = User->country_codes;
365   return [ map { id => $_, title => $cc{$_} }, sort { $cc{$a} cmp $cc{$b} } keys %cc ];
366 }
367
368 #
369 # filters
370 #
371
372 sub setup_layout {
373   my ($self, $action) = @_;
374
375   $::request->layout(SL::Layout::Dispatcher->new(style => 'admin'));
376   $::request->layout->use_stylesheet("lx-office-erp.css");
377   $::form->{favicon} = "favicon.ico";
378 }
379
380 #
381 # displaying forms
382 #
383
384 sub use_multiselect_js {
385   my ($self) = @_;
386
387   $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
388   return $self;
389 }
390
391 sub login_form {
392   my ($self, %params) = @_;
393   $::request->layout->focus('#admin_password');
394   $self->render('admin/adminlogin', title => t8('kivitendo v#1 administration', $::form->{version}), %params);
395 }
396
397 sub edit_user_form {
398   my ($self, %params) = @_;
399   $self->use_multiselect_js->render('admin/edit_user', %params);
400 }
401
402 sub edit_client_form {
403   my ($self, %params) = @_;
404   $self->use_multiselect_js->render('admin/edit_client', %params);
405 }
406
407 sub edit_group_form {
408   my ($self, %params) = @_;
409   $self->use_multiselect_js->render('admin/edit_group', %params);
410 }
411
412 #
413 # helpers
414 #
415
416 sub check_auth_db_and_tables {
417   my ($self) = @_;
418
419   if (!$::auth->check_database) {
420     $self->render('admin/check_auth_database', title => t8('Authentification database creation'));
421     return 0;
422   }
423
424   if (!$::auth->check_tables) {
425     $self->render('admin/check_auth_tables', title => t8('Authentification tables creation'));
426     return 0;
427   }
428
429   return 1;
430 }
431
432 sub apply_dbupgrade_scripts {
433   return SL::DBUpgrade2->new(form => $::form, dbdriver => 'Pg', auth => 1)->apply_admin_dbupgrade_scripts(1);
434 }
435
436 sub authenticate_root {
437   my ($self) = @_;
438
439   return 1 if $::auth->authenticate_root($::form->{'{AUTH}admin_password'}) == $::auth->OK();
440
441   $::auth->punish_wrong_login;
442   $::auth->delete_session_value('admin_password');
443
444   $self->login_form(error => t8('Incorrect Password!'));
445
446   return undef;
447 }
448
449 1;