... | ... |
@@ -159,6 +159,15 @@ sub startup { |
159 | 159 |
} |
160 | 160 |
); |
161 | 161 |
|
162 |
+ $vc->add_check(project_name => sub { |
|
163 |
+ my ($vc, $value) = @_; |
|
164 |
+ |
|
165 |
+ return 0 unless defined $value; |
|
166 |
+ return 0 if $value eq '.' || $value eq '..'; |
|
167 |
+ |
|
168 |
+ return ($value || '') =~ /[a-zA-Z0-9_\-\.]+$/; |
|
169 |
+ }); |
|
170 |
+ |
|
162 | 171 |
# Basic auth plugin |
163 | 172 |
$self->plugin('BasicAuth'); |
164 | 173 |
|
... | ... |
@@ -18,40 +18,41 @@ |
18 | 18 |
my $errors; |
19 | 19 |
my $post = lc $self->req->method eq 'post'; |
20 | 20 |
if ($op eq 'rename-project' && $post) { |
21 |
- |
|
22 |
- # Validation |
|
23 |
- my $params = $api->params; |
|
24 |
- my $rule = [ |
|
25 |
- 'to-project' => [ |
|
26 |
- ['not_blank' => 'Repository name is empty.'], |
|
27 |
- ['project_name' => 'Repository name contains invalid charactor.'] |
|
28 |
- ] |
|
29 |
- ]; |
|
30 |
- my $vresult = app->vc->validate($params, $rule); |
|
31 | 21 |
|
32 |
- if ($vresult->is_ok) { |
|
33 |
- # Valida parameters |
|
34 |
- my $data = $vresult->data; |
|
35 |
- my $to_project = $data->{'to-project'}; |
|
36 |
- |
|
22 |
+ # Parameters |
|
23 |
+ my $to_project = param('to-project'); |
|
24 |
+ |
|
25 |
+ # Validator |
|
26 |
+ my $vc = app->vc; |
|
27 |
+ |
|
28 |
+ # Validation result |
|
29 |
+ my $validation = $vc->validation; |
|
30 |
+ |
|
31 |
+ # "to-project" check |
|
32 |
+ if (!length $to_project) { |
|
33 |
+ $validation->add_failed('to-project' => 'Repository name is empty.'); |
|
34 |
+ } |
|
35 |
+ elsif (!$vc->check($to_project, 'project_name')) { |
|
36 |
+ $validation->add_failed('to-project' => 'Repository name contains invalid charactor.'); |
|
37 |
+ } |
|
38 |
+ elsif (app->manager->exists_project($user, $to_project)) { |
|
39 |
+ $validation->add_failed('to-project' => "$to_project is already exists"); |
|
40 |
+ } |
|
41 |
+ |
|
42 |
+ if ($validation->is_valid) { |
|
37 | 43 |
# Rename |
38 |
- if (app->manager->exists_project($user, $to_project)) { |
|
39 |
- $errors = ["$to_project is already exists"]; |
|
44 |
+ eval { app->manager->rename_project($user, $project, $to_project) }; |
|
45 |
+ if (my $e = $@) { |
|
46 |
+ app->log->error($e); |
|
47 |
+ $errors = ['Internal Error']; |
|
40 | 48 |
} |
41 | 49 |
else { |
42 |
- eval { app->manager->rename_project($user, $project, $to_project) }; |
|
43 |
- if (my $e = $@) { |
|
44 |
- app->log->error($e); |
|
45 |
- $errors = ['Internal Error']; |
|
46 |
- } |
|
47 |
- else { |
|
48 |
- flash(message => "Repository name is renamed to $to_project"); |
|
49 |
- $self->redirect_to("/$user/$to_project/settings"); |
|
50 |
- return; |
|
51 |
- } |
|
50 |
+ flash(message => "Repository name is renamed to $to_project"); |
|
51 |
+ $self->redirect_to("/$user/$to_project/settings"); |
|
52 |
+ return; |
|
52 | 53 |
} |
53 | 54 |
} |
54 |
- else { $errors = $vresult->messages } |
|
55 |
+ else { $errors = $validation->messages } |
|
55 | 56 |
} |
56 | 57 |
|
57 | 58 |
# Change description |