Admin: Anlegen, Bearbeiten und Löschen von Mandanten implementiert
[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 nologin_file_name db_cfg all_dateformats all_numberformats all_countrycodes all_stylesheets all_menustyles all_clients all_groups all_users) ],
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     CLIENTS => SL::DB::Manager::AuthClient->get_all_sorted,
99     USERS   => SL::DB::Manager::AuthUser->get_all_sorted,
100     LOCKED  => (-e $self->nologin_file_name),
101     title   => "kivitendo " . t8('Administration'),
102   );
103 }
104
105 sub action_new_user {
106   my ($self) = @_;
107
108   $self->user(SL::DB::AuthUser->new(
109     config_values => {
110       vclimit      => 200,
111       countrycode  => "de",
112       numberformat => "1.000,00",
113       dateformat   => "dd.mm.yy",
114       stylesheet   => "kivitendo.css",
115       menustyle    => "neu",
116     },
117   ));
118
119   $self->edit_user_form(title => t8('Create a new user'));
120 }
121
122 sub action_edit_user {
123   my ($self) = @_;
124   $self->edit_user_form(title => t8('Edit User'));
125 }
126
127 sub action_save_user {
128   my ($self) = @_;
129   my $params = delete($::form->{user})          || { };
130   my $props  = delete($params->{config_values}) || { };
131   my $is_new = !$params->{id};
132
133   $self->user($is_new ? SL::DB::AuthUser->new : SL::DB::AuthUser->new(id => $params->{id})->load)
134     ->assign_attributes(%{ $params })
135     ->config_values({ %{ $self->user->config_values }, %{ $props } });
136
137   my @errors = $self->user->validate;
138
139   if (@errors) {
140     flash('error', @errors);
141     $self->edit_user_form(title => $is_new ? t8('Create a new user') : t8('Edit User'));
142     return;
143   }
144
145   $self->user->save;
146
147   if ($::auth->can_change_password && $::form->{new_password}) {
148     $::auth->change_password($self->user->login, $::form->{new_password});
149   }
150
151   flash_later('info', $is_new ? t8('The user has been created.') : t8('The user has been saved.'));
152   $self->redirect_to(action => 'show');
153 }
154
155 sub action_delete_user {
156   my ($self) = @_;
157
158   if (!$self->user->delete) {
159     flash('error', t8('The user could not be deleted.'));
160     $self->edit_user_form(title => t8('Edit User'));
161     return;
162   }
163
164   flash_later('info', t8('The user has been deleted.'));
165   $self->redirect_to(action => 'show');
166 }
167
168 #
169 # actions: clients
170 #
171
172 sub action_new_client {
173   my ($self) = @_;
174
175   $self->client(SL::DB::AuthClient->new(
176     dbhost   => $::auth->{DB_config}->{host},
177     dbport   => $::auth->{DB_config}->{port},
178     dbuser   => $::auth->{DB_config}->{user},
179     dbpasswd => $::auth->{DB_config}->{password},
180   ));
181
182   $self->edit_client_form(title => t8('Create a new client'));
183 }
184
185 sub action_edit_client {
186   my ($self) = @_;
187   $self->edit_client_form(title => t8('Edit Client'));
188 }
189
190 sub action_save_client {
191   my ($self) = @_;
192   my $params = delete($::form->{client}) || { };
193   my $is_new = !$params->{id};
194
195   $self->client($is_new ? SL::DB::AuthClient->new : SL::DB::AuthClient->new(id => $params->{id})->load)->assign_attributes(%{ $params });
196
197   my @errors = $self->client->validate;
198
199   if (@errors) {
200     flash('error', @errors);
201     $self->edit_client_form(title => $is_new ? t8('Create a new client') : t8('Edit Client'));
202     return;
203   }
204
205   $self->client->save;
206   if ($self->client->is_default) {
207     SL::DB::Manager::AuthClient->update_all(set => { is_default => 0 }, where => [ '!id' => $self->client->id ]);
208   }
209
210   flash_later('info', $is_new ? t8('The client has been created.') : t8('The client has been saved.'));
211   $self->redirect_to(action => 'show');
212 }
213
214 sub action_delete_client {
215   my ($self) = @_;
216
217   if (!$self->client->delete) {
218     flash('error', t8('The client could not be deleted.'));
219     $self->edit_client_form(title => t8('Edit Client'));
220     return;
221   }
222
223   flash_later('info', t8('The client has been deleted.'));
224   $self->redirect_to(action => 'show');
225 }
226
227 sub action_test_database_connectivity {
228   my ($self)    = @_;
229
230   my %cfg       = %{ $::form->{client} || {} };
231   my $dbconnect = 'dbi:Pg:dbname=' . $cfg{dbname} . ';host=' . $cfg{dbhost} . ';port=' . $cfg{dbport};
232   my $dbh       = DBI->connect($dbconnect, $cfg{dbuser}, $cfg{dbpasswd});
233
234   my $ok        = !!$dbh;
235   my $error     = $DBI::errstr;
236
237   $dbh->disconnect if $dbh;
238
239   $self->render('admin/test_db_connection',
240                 title => t8('Database Connection Test'),
241                 ok    => $ok,
242                 error => $error);
243 }
244
245 #
246 # actions: locking, unlocking
247 #
248
249 sub action_unlock_system {
250   my ($self) = @_;
251   unlink $self->nologin_file_name;
252   flash_later('info', t8('Lockfile removed!'));
253   $self->redirect_to(action => 'show');
254 }
255
256 sub action_lock_system {
257   my ($self) = @_;
258
259   my $fh = IO::File->new($self->nologin_file_name, "w");
260   if (!$fh) {
261     $::form->error(t8('Cannot create Lock!'));
262
263   } else {
264     $fh->close;
265     flash_later('info', t8('Lockfile created!'));
266     $self->redirect_to(action => 'show');
267   }
268 }
269
270 #
271 # initializers
272 #
273
274 sub init_db_cfg            { $::lx_office_conf{'authentication/database'}                                            }
275 sub init_nologin_file_name { $::lx_office_conf{paths}->{userspath} . '/nologin';                                     }
276 sub init_client            { SL::DB::AuthClient->new(id => ($::form->{id} || ($::form->{client} || {})->{id}))->load }
277 sub init_user              { SL::DB::AuthUser  ->new(id => ($::form->{id} || ($::form->{user}   || {})->{id}))->load }
278 sub init_all_clients       { SL::DB::Manager::AuthClient->get_all_sorted                                             }
279 sub init_all_users         { SL::DB::Manager::AuthUser->get_all_sorted                                               }
280 sub init_all_groups        { SL::DB::Manager::AuthGroup->get_all_sorted                                              }
281 sub init_all_dateformats   { [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd)      ]                                      }
282 sub init_all_numberformats { [ qw(1,000.00 1000.00 1.000,00 1000,00)          ]                                      }
283 sub init_all_stylesheets   { [ qw(lx-office-erp.css Mobile.css kivitendo.css) ]                                      }
284 sub init_all_menustyles    {
285   return [
286     { id => 'old', title => $::locale->text('Old (on the side)') },
287     { id => 'v3',  title => $::locale->text('Top (CSS)') },
288     { id => 'neu', title => $::locale->text('Top (Javascript)') },
289   ];
290 }
291
292 sub init_all_countrycodes {
293   my %cc = User->country_codes;
294   return [ map { id => $_, title => $cc{$_} }, sort { $cc{$a} cmp $cc{$b} } keys %cc ];
295 }
296
297 #
298 # filters
299 #
300
301 sub setup_layout {
302   my ($self, $action) = @_;
303
304   $::request->layout(SL::Layout::Dispatcher->new(style => 'admin'));
305   $::request->layout->use_stylesheet("lx-office-erp.css");
306   $::form->{favicon} = "favicon.ico";
307 }
308
309 #
310 # displaying forms
311 #
312
313 sub login_form {
314   my ($self, %params) = @_;
315   $::request->layout->focus('#admin_password');
316   $self->render('admin/adminlogin', title => t8('kivitendo v#1 administration', $::form->{version}), %params);
317 }
318
319 sub edit_user_form {
320   my ($self, %params) = @_;
321
322   $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
323   $self->render('admin/edit_user', %params);
324 }
325
326 sub edit_client_form {
327   my ($self, %params) = @_;
328
329   $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
330   $self->render('admin/edit_client', %params);
331 }
332
333 #
334 # helpers
335 #
336
337 sub check_auth_db_and_tables {
338   my ($self) = @_;
339
340   if (!$::auth->check_database) {
341     $self->render('admin/check_auth_database', title => t8('Authentification database creation'));
342     return 0;
343   }
344
345   if (!$::auth->check_tables) {
346     $self->render('admin/check_auth_tables', title => t8('Authentification tables creation'));
347     return 0;
348   }
349
350   return 1;
351 }
352
353 sub apply_dbupgrade_scripts {
354   return SL::DBUpgrade2->new(form => $::form, dbdriver => 'Pg', auth => 1)->apply_admin_dbupgrade_scripts(1);
355 }
356
357 sub authenticate_root {
358   my ($self) = @_;
359
360   return 1 if $::auth->authenticate_root($::form->{'{AUTH}admin_password'}) == $::auth->OK();
361
362   $::auth->punish_wrong_login;
363   $::auth->delete_session_value('admin_password');
364
365   $self->login_form(error => t8('Incorrect Password!'));
366
367   return undef;
368 }
369
370 1;