Admin: Anlegen, Bearbeiten und Löschen von Usern im Admin-Controller
[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) ],
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: locking, unlocking
170 #
171
172 sub action_unlock_system {
173   my ($self) = @_;
174   unlink $self->nologin_file_name;
175   flash_later('info', t8('Lockfile removed!'));
176   $self->redirect_to(action => 'show');
177 }
178
179 sub action_lock_system {
180   my ($self) = @_;
181
182   my $fh = IO::File->new($self->nologin_file_name, "w");
183   if (!$fh) {
184     $::form->error(t8('Cannot create Lock!'));
185
186   } else {
187     $fh->close;
188     flash_later('info', t8('Lockfile created!'));
189     $self->redirect_to(action => 'show');
190   }
191 }
192
193 #
194 # initializers
195 #
196
197 sub init_db_cfg            { $::lx_office_conf{'authentication/database'}                                            }
198 sub init_nologin_file_name { $::lx_office_conf{paths}->{userspath} . '/nologin';                                     }
199 sub init_client            { SL::DB::AuthClient->new(id => ($::form->{id} || ($::form->{client} || {})->{id}))->load }
200 sub init_user              { SL::DB::AuthUser  ->new(id => ($::form->{id} || ($::form->{user}   || {})->{id}))->load }
201 sub init_all_clients       { SL::DB::Manager::AuthClient->get_all_sorted                                             }
202 sub init_all_groups        { SL::DB::Manager::AuthGroup->get_all_sorted                                              }
203 sub init_all_dateformats   { [ qw(mm/dd/yy dd/mm/yy dd.mm.yy yyyy-mm-dd)      ]                                      }
204 sub init_all_numberformats { [ qw(1,000.00 1000.00 1.000,00 1000,00)          ]                                      }
205 sub init_all_stylesheets   { [ qw(lx-office-erp.css Mobile.css kivitendo.css) ]                                      }
206 sub init_all_menustyles    {
207   return [
208     { id => 'old', title => $::locale->text('Old (on the side)') },
209     { id => 'v3',  title => $::locale->text('Top (CSS)') },
210     { id => 'neu', title => $::locale->text('Top (Javascript)') },
211   ];
212 }
213
214 sub init_all_countrycodes {
215   my %cc = User->country_codes;
216   return [ map { id => $_, title => $cc{$_} }, sort { $cc{$a} cmp $cc{$b} } keys %cc ];
217 }
218
219 #
220 # filters
221 #
222
223 sub setup_layout {
224   my ($self, $action) = @_;
225
226   $::request->layout(SL::Layout::Dispatcher->new(style => 'admin'));
227   $::request->layout->use_stylesheet("lx-office-erp.css");
228   $::form->{favicon} = "favicon.ico";
229 }
230
231 #
232 # displaying forms
233 #
234
235 sub login_form {
236   my ($self, %params) = @_;
237   $::request->layout->focus('#admin_password');
238   $self->render('admin/adminlogin', title => t8('kivitendo v#1 administration', $::form->{version}), %params);
239 }
240
241 sub edit_user_form {
242   my ($self, %params) = @_;
243
244   $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
245   $self->render('admin/edit_user', %params);
246 }
247
248 #
249 # helpers
250 #
251
252 sub check_auth_db_and_tables {
253   my ($self) = @_;
254
255   if (!$::auth->check_database) {
256     $self->render('admin/check_auth_database', title => t8('Authentification database creation'));
257     return 0;
258   }
259
260   if (!$::auth->check_tables) {
261     $self->render('admin/check_auth_tables', title => t8('Authentification tables creation'));
262     return 0;
263   }
264
265   return 1;
266 }
267
268 sub apply_dbupgrade_scripts {
269   return SL::DBUpgrade2->new(form => $::form, dbdriver => 'Pg', auth => 1)->apply_admin_dbupgrade_scripts(1);
270 }
271
272 sub authenticate_root {
273   my ($self) = @_;
274
275   return 1 if $::auth->authenticate_root($::form->{'{AUTH}admin_password'}) == $::auth->OK();
276
277   $::auth->punish_wrong_login;
278   $::auth->delete_session_value('admin_password');
279
280   $self->login_form(error => t8('Incorrect Password!'));
281
282   return undef;
283 }
284
285 1;