... | ... |
@@ -167,6 +167,11 @@ sub startup { |
167 | 167 |
|
168 | 168 |
return ($value || '') =~ /[a-zA-Z0-9_\-\.]+$/; |
169 | 169 |
}); |
170 |
+ $vc->add_check(user_name => sub { |
|
171 |
+ my ($vc, $value) = @_; |
|
172 |
+ |
|
173 |
+ return ($value || '') =~ /^[a-zA-Z0-9_\-]+$/; |
|
174 |
+ }); |
|
170 | 175 |
|
171 | 176 |
# Basic auth plugin |
172 | 177 |
$self->plugin('BasicAuth'); |
... | ... |
@@ -5,35 +5,52 @@ |
5 | 5 |
|
6 | 6 |
my $errors; |
7 | 7 |
if ($op eq 'create') { |
8 |
+ |
|
9 |
+ # Parameters |
|
10 |
+ my $id = param('id'); |
|
11 |
+ my $password = param('password'); |
|
12 |
+ my $password2 = param('password2'); |
|
13 |
+ |
|
14 |
+ # Validator |
|
15 |
+ my $vc = app->vc; |
|
16 |
+ |
|
17 |
+ # Validation result |
|
18 |
+ my $validation = $vc->validation; |
|
19 |
+ |
|
20 |
+ # "id" check |
|
21 |
+ if (!(defined $id && length $id)) { |
|
22 |
+ $validation->add_failed(id => 'User name is empty.'); |
|
23 |
+ } |
|
24 |
+ elsif (!$vc->check($id, 'user_name')) { |
|
25 |
+ $validation->add_failed(id => 'User name contain invalid character.'); |
|
26 |
+ } |
|
27 |
+ elsif (length $id > 20) { |
|
28 |
+ $validation->add_failed(id => 'User name is too long.'); |
|
29 |
+ } |
|
30 |
+ else { |
|
31 |
+ my $row = app->dbi->model('user')->select(where => {id => $id})->one; |
|
32 |
+ if ($row) { |
|
33 |
+ $validation->add_failed(id => "User $id already exists"); |
|
34 |
+ } |
|
35 |
+ } |
|
8 | 36 |
|
9 |
- # Validation |
|
10 |
- my $params = $api->params; |
|
11 |
- my $vc = $self->app->vc; |
|
12 |
- my $rule = [ |
|
13 |
- id => [ |
|
14 |
- ['not_blank' => 'User name is empty.'], |
|
15 |
- [user_name => 'User name contain invalid character.'], |
|
16 |
- [{'length' => {max => 20}} => 'User name is too long.'] |
|
17 |
- ], |
|
18 |
- password => [ |
|
19 |
- ['not_blank' => 'Password is empty.'], |
|
20 |
- ['ascii' => 'Password contain invalid character.'] |
|
21 |
- ], |
|
22 |
- {password_check => [qw/password password2/]} |
|
23 |
- => {copy => 0} |
|
24 |
- => [ |
|
25 |
- ['duplication' => "Two password don't match"] |
|
26 |
- ] |
|
27 |
- ]; |
|
28 |
- my $vresult = $vc->validate($params, $rule); |
|
37 |
+ # "password" check |
|
38 |
+ $password2 ||= ''; |
|
39 |
+ if (!(defined $password && length $password)) { |
|
40 |
+ $validation->add_failed(password => 'Password is empty.'); |
|
41 |
+ } |
|
42 |
+ elsif (!$vc->check($password, 'ascii_graphic')) { |
|
43 |
+ $validation->add_failed(password => 'Password contain invalid character.'); |
|
44 |
+ } |
|
45 |
+ elsif ($password ne $password2) { |
|
46 |
+ $validation->add_failed(password => "Two password don't match"); |
|
47 |
+ } |
|
29 | 48 |
|
30 |
- if ($vresult->is_ok) { |
|
49 |
+ if ($validation->is_valid) { |
|
31 | 50 |
|
32 |
- # Valid parameters |
|
33 |
- my $params = $vresult->data; |
|
34 |
- my $id = delete $params->{id}; |
|
35 |
- my ($password_encrypted, $salt) |
|
36 |
- = $api->encrypt_password($params->{password}); |
|
51 |
+ # Encrypt password |
|
52 |
+ my ($password_encrypted, $salt) = $api->encrypt_password($password); |
|
53 |
+ my $params = {}; |
|
37 | 54 |
$params->{password} = $password_encrypted; |
38 | 55 |
$params->{salt} = $salt; |
39 | 56 |
|
... | ... |
@@ -49,7 +66,7 @@ |
49 | 66 |
$self->redirect_to('current'); |
50 | 67 |
} |
51 | 68 |
} |
52 |
- else { $errors = $vresult->messages } |
|
69 |
+ else { $errors = $validation->messages } |
|
53 | 70 |
} |
54 | 71 |
%> |
55 | 72 |
|
... | ... |
@@ -29,7 +29,7 @@ |
29 | 29 |
my $validation = $vc->validation; |
30 | 30 |
|
31 | 31 |
# "to-project" check |
32 |
- if (!(defined $project && length $to_project)) { |
|
32 |
+ if (!(defined $to_project && length $to_project)) { |
|
33 | 33 |
$validation->add_failed('to-project' => 'Repository name is empty.'); |
34 | 34 |
} |
35 | 35 |
elsif (!$vc->check($to_project, 'project_name')) { |