Showing 1 changed files with 45 additions and 39 deletions
+45 -39
templates/auto/_start.html.ep
... ...
@@ -14,50 +14,56 @@
14 14
   my $errors;
15 15
   if ($op eq 'create') {
16 16
     
17
-    # Check existence admin user
18
-    my $admin_user = app->manager->admin_user;
19
-    if (defined $admin_user) { $errors = ['Admin user already exists.'] }
20
-    else {
21
-      # Validation
22
-      my $params = $api->params;
23
-      my $rule = [
24
-        password => [
25
-          ['not_blank' => 'Password is empty.'],
26
-          ['ascii' => 'Password contains invalid character.']
27
-        ],
28
-        {password_check => [qw/password password2/]}
29
-          => {copy => 0}
30
-          => [
31
-            ['duplication' => "Two password don't match."]
32
-          ]
33
-      ];
34
-      my $vresult = $self->app->vc->validate($params, $rule);
17
+    # Parameters
18
+    my $password = param('password');
19
+    my $password2 = param('password2');
20
+    
21
+    # Validator
22
+    my $vc = app->vc;
23
+    
24
+    # Validation
25
+    my $validation = $vc->validation;
26
+    
27
+    # "user"
28
+    if (defined $admin_user) {
29
+      $validation->add_failed('Admin user already exists.');
30
+    }
31
+    
32
+    # "password"
33
+    if (!(defined $password && length $password)) {
34
+      $validation->add_failed('Password is empty.');
35
+    }
36
+    elsif (!$vc->check($password, 'ascii_graphic')) {
37
+      $validation->add_failed("Password contains invalid character.");
38
+    }
39
+    elsif ($password ne $password2) {
40
+      $validation->add_failed("Two password don't match.");
41
+    }
42
+    
43
+    if ($validation->is_valid) {
35 44
       
36
-      if ($vresult->is_ok) {
37
-        
38
-        # Valida parameters
39
-        my $params = $vresult->data;
40
-        my $user = 'admin';
41
-        $params->{admin} = 1;
42
-        my ($password_encryped, $salt)
43
-          = $api->encrypt_password($params->{password});
44
-        $params->{password} = $password_encryped;
45
-        $params->{salt} = $salt;
46
-        
47
-        # Create admin user
48
-        $self->app->dbi->model('user')->insert($params, id => $user);
49
-        
50
-        # Redirect
51
-        $self->flash(admin_user_created => 1);
52
-        $self->redirect_to('/_login');
53
-        return;
54
-      }
55
-      else { $errors = $vresult->messages }
45
+      # Valida parameters
46
+      my $new_user = {};
47
+      $new_user->{id} = 'admin';
48
+      $new_user->{admin} = 1;
49
+      my ($password_encryped, $salt) = $api->encrypt_password($password);
50
+      $new_user->{password} = $password_encryped;
51
+      $new_user->{salt} = $salt;
52
+      
53
+      # Create admin user
54
+      $self->app->dbi->model('user')->insert($new_user);
55
+      
56
+      # Redirect
57
+      $self->flash(admin_user_created => 1);
58
+      $self->redirect_to('/_login');
59
+      return;
56 60
     }
61
+    else { $errors = $validation->messages }
57 62
   }
63
+
64
+  layout 'common', title => 'Start page';
58 65
 %>
59 66
 
60
-% layout 'common', title => 'Start page';
61 67
 
62 68
   %= include '/include/header';
63 69
   <!-- Start page -->