| 1 | package Yukki::Web::Controller::Admin::Repository; | ||
| 2 | $Yukki::Web::Controller::Admin::Repository::VERSION = '0.991_007'; # TRIAL | ||
| 3 | $Yukki::Web::Controller::Admin::Repository::VERSION = '0.991007'; | ||
| 4 | use v5.24; | ||
| 5 | use utf8; | ||
| 6 | use Moo; | ||
| 7 | |||
| 8 | use FormValidator::Tiny ':all'; | ||
| 9 | use Types::Standard qw( Int ); | ||
| 10 | use Yukki::Error qw( http_throw ); | ||
| 11 | |||
| 12 | with 'Yukki::Web::Controller'; | ||
| 13 | |||
| 14 | use namespace::clean; | ||
| 15 | |||
| 16 | # ABSTRACT: controller for adminsitrator your repositories | ||
| 17 | |||
| 18 | |||
| 19 | sub fire { | ||
| 20 | my ($self, $ctx) = @_; | ||
| 21 | |||
| 22 | my $action = $ctx->request->path_parameters->{action}; | ||
| 23 | |||
| 24 | if ($action eq 'list') { $self->list_repositories($ctx) } | ||
| 25 | elsif ($action eq 'add') { $self->add_repository($ctx) } | ||
| 26 | elsif ($action eq 'initialize') { $self->initialize_repository($ctx) } | ||
| 27 | elsif ($action eq 'remove') { $self->remove_repository($ctx) } | ||
| 28 | elsif ($action eq 'kill') { $self->kill_repository($ctx) } | ||
| 29 | else { | ||
| 30 | http_throw('No action found matching that URL.', { | ||
| 31 | status => 'NotFound', | ||
| 32 | }); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | sub list_repositories { | ||
| 38 | my ($self, $ctx) = @_; | ||
| 39 | |||
| 40 | my $repos = $self->model('Root'); | ||
| 41 | my @repos = $repos->list_repositories; | ||
| 42 | |||
| 43 | my $body = $self->view('Admin::Repository')->list($ctx, { | ||
| 44 | repositories => \@repos, | ||
| 45 | }); | ||
| 46 | |||
| 47 | $ctx->response->body($body); | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | sub _none_and_any_groups_are_special { | ||
| 52 | my ($value) = @_; | ||
| 53 | my @groups = @$value; | ||
| 54 | if (@groups > 1 && any { $_ eq 'ANY' || $_ eq 'NONE' } @groups) { | ||
| 55 | return (0, 'ANY or NONE must appear alone or not at all.'); | ||
| 56 | } | ||
| 57 | else { | ||
| 58 | return (1, ''); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | sub _unbox_none_and_any { | ||
| 63 | my ($value) = @_; | ||
| 64 | my @groups = @$value; | ||
| 65 | if (@groups == 1 && ($groups[0] eq 'ANY' || $groups[0] eq 'NONE')) { | ||
| 66 | return $groups[0]; | ||
| 67 | } | ||
| 68 | else { | ||
| 69 | return $value; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | validation_spec add_repository => [ | ||
| 74 | name => [ | ||
| 75 | required => 1, | ||
| 76 | must => limit_character_set('a-z', 'A-Z', '0-9', '_', '-', '.'), | ||
| 77 | must => length_in_range(3, 50), | ||
| 78 | ], | ||
| 79 | title => [ | ||
| 80 | required => 1, | ||
| 81 | ], | ||
| 82 | branch => [ | ||
| 83 | required => 1, | ||
| 84 | must => limit_character_set('a-z', 'A-Z', '0-9', '_', '-', '.', '/'), | ||
| 85 | must => length_in_range('*', 300), | ||
| 86 | ], | ||
| 87 | default_page => [ | ||
| 88 | required => 1, | ||
| 89 | must => limit_character_set('a-z', 'A-Z', '0-9', '_', '-', '.'), | ||
| 90 | must => length_in_range('*', 100), | ||
| 91 | ], | ||
| 92 | sort => [ | ||
| 93 | required => 1, | ||
| 94 | must => Int, | ||
| 95 | with_error => 'Must be a number.', | ||
| 96 | into => '+', | ||
| 97 | ], | ||
| 98 | anonymous_access_level => [ | ||
| 99 | required => 1, | ||
| 100 | must => one_of(qw( none read write )), | ||
| 101 | ], | ||
| 102 | read_groups => [ | ||
| 103 | optional => 1, | ||
| 104 | must => limit_character_set('a-z', 'A-Z', '0-9', '_', '-'), | ||
| 105 | into => split_by(qr/\s+/), | ||
| 106 | must => \&_none_and_any_groups_are_special, | ||
| 107 | into => \&_unbox_none_and_any, | ||
| 108 | ], | ||
| 109 | write_groups => [ | ||
| 110 | optional => 1, | ||
| 111 | must => limit_character_set('a-z', 'A-Z', '0-9', '_', '-'), | ||
| 112 | into => split_by(qr/\s+/), | ||
| 113 | must => \&_none_and_any_groups_are_special, | ||
| 114 | into => \&_unbox_none_and_any, | ||
| 115 | ], | ||
| 116 | ]; | ||
| 117 | |||
| 118 | |||
| 119 | sub add_repository { | ||
| 120 | my ($self, $ctx) = @_; | ||
| 121 | |||
| 122 | my $form_errors; | ||
| 123 | if ($ctx->request->method eq 'POST') { | ||
| 124 | my $repo_params; | ||
| 125 | ($repo_params, $form_errors) | ||
| 126 | = validate_form add_repository => $ctx->request->body_parameters; | ||
| 127 | |||
| 128 | if (!defined $form_errors) { | ||
| 129 | $self->model('Root')->attach_repository( | ||
| 130 | key => $repo_params->{name}, | ||
| 131 | repository => "$repo_params->{name}.git", | ||
| 132 | site_branch => $repo_params->{branch}, | ||
| 133 | name => $repo_params->{title}, | ||
| 134 | default_page => $repo_params->{default_page}, | ||
| 135 | sort => $repo_params->{sort}, | ||
| 136 | anonymous_access_level => $repo_params->{anonymous_access_level}, | ||
| 137 | read_groups => $repo_params->{read_groups}, | ||
| 138 | write_groups => $repo_params->{write_groups}, | ||
| 139 | ); | ||
| 140 | |||
| 141 | $ctx->add_info("Saved $repo_params->{name}."); | ||
| 142 | |||
| 143 | $ctx->response->redirect('/admin/repository/list'); | ||
| 144 | return; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | my %default = ( | ||
| 149 | branch => 'refs/heads/master', | ||
| 150 | default_page => 'home.yukki', | ||
| 151 | sort => '50', | ||
| 152 | anonymous_access_level => 'none', | ||
| 153 | read_groups => 'NONE', | ||
| 154 | write_groups => 'NONE', | ||
| 155 | ); | ||
| 156 | |||
| 157 | my @breadcrumb = ( | ||
| 158 | { | ||
| 159 | label => 'List', | ||
| 160 | href => '/admin/user/repository', | ||
| 161 | }, | ||
| 162 | ); | ||
| 163 | |||
| 164 | my $body = $self->view('Admin::Repository')->edit($ctx, { | ||
| 165 | form => $ctx->request->body_parameters->as_hashref, | ||
| 166 | form_errors => $form_errors, | ||
| 167 | default => \%default, | ||
| 168 | breadcrumb => \@breadcrumb, | ||
| 169 | }); | ||
| 170 | |||
| 171 | $ctx->response->body($body); | ||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | sub initialize_repository { | ||
| 176 | my ($self, $ctx) = @_; | ||
| 177 | |||
| 178 | my $repo_name = $ctx->request->path_parameters->{repository}; | ||
| 179 | my $repo = $self->model('Root')->repository($repo_name); | ||
| 180 | |||
| 181 | http_throw("Repository named $repo_name does not exist.", { | ||
| 182 | status => 'NotFound', | ||
| 183 | }) unless $repo; | ||
| 184 | |||
| 185 | my $confirmed = $ctx->request->body_parameters->{confirmed}; | ||
| 186 | if ($ctx->request->method eq 'POST' && $confirmed) { | ||
| 187 | $self->model('Root')->init_repository( | ||
| 188 | key => $repo_name, | ||
| 189 | ); | ||
| 190 | |||
| 191 | $ctx->add_info("Initialized repository named ".$repo->title."."); | ||
| 192 | $ctx->response->redirect('/admin/repository/list'); | ||
| 193 | return; | ||
| 194 | } | ||
| 195 | |||
| 196 | my @breadcrumb = ( | ||
| 197 | { | ||
| 198 | label => 'List', | ||
| 199 | href => 'admin/repository/list', | ||
| 200 | } | ||
| 201 | ); | ||
| 202 | |||
| 203 | my $body = $self->view('Admin::Repository')->initialize($ctx, { | ||
| 204 | title => 'Initialize ' . $repo->title, | ||
| 205 | repo => $repo, | ||
| 206 | breadcrumb => \@breadcrumb, | ||
| 207 | return_link => $ctx->rebase_url('admin/repository/list'), | ||
| 208 | }); | ||
| 209 | |||
| 210 | $ctx->response->body($body); | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | sub kill_repository { | ||
| 215 | my ($self, $ctx) = @_; | ||
| 216 | |||
| 217 | my $repo_name = $ctx->request->path_parameters->{repository}; | ||
| 218 | my $repo = $self->model('Root')->repository($repo_name); | ||
| 219 | |||
| 220 | http_throw("Repository named $repo_name does not exist.", { | ||
| 221 | status => 'NotFound', | ||
| 222 | }) unless $repo; | ||
| 223 | |||
| 224 | if (!$repo->repository_exists) { | ||
| 225 | $ctx->add_warnings('Repository '.$repo->title.' has no storage associated with it.'); | ||
| 226 | $ctx->response->redirect('/admin/repository/list'); | ||
| 227 | return; | ||
| 228 | } | ||
| 229 | |||
| 230 | my $confirmed = $ctx->request->body_parameters->{confirmed}; | ||
| 231 | if ($ctx->request->method eq 'POST' && $confirmed) { | ||
| 232 | $self->model('Root')->kill_repository( | ||
| 233 | key => $repo_name, | ||
| 234 | ); | ||
| 235 | |||
| 236 | $ctx->add_info('Killed repository named '.$repo->title.', it is now safe to reinitialize or remove.'); | ||
| 237 | $ctx->response->redirect('/admin/repository/list'); | ||
| 238 | return; | ||
| 239 | } | ||
| 240 | |||
| 241 | my @breadcrumb = ( | ||
| 242 | { | ||
| 243 | label => 'List', | ||
| 244 | href => 'admin/repository/list', | ||
| 245 | }, | ||
| 246 | ); | ||
| 247 | |||
| 248 | my $body = $self->view('Admin::Repository')->kill_it_with_fire($ctx, { | ||
| 249 | title => 'Kill ' . $repo->title, | ||
| 250 | repo => $repo, | ||
| 251 | breadcrumb => \@breadcrumb, | ||
| 252 | return_link => $ctx->rebase_url('admin/repository/list'), | ||
| 253 | }); | ||
| 254 | |||
| 255 | $ctx->response->body($body); | ||
| 256 | } | ||
| 257 | |||
| 258 | |||
| 259 | sub remove_repository { | ||
| 260 | my ($self, $ctx) = @_; | ||
| 261 | |||
| 262 | my $repo_name = $ctx->request->path_parameters->{repository}; | ||
| 263 | my $repo = $self->model('Root')->repository($repo_name); | ||
| 264 | |||
| 265 | http_throw("Repository named $repo_name does not exist.", { | ||
| 266 | status => 'NotFound', | ||
| 267 | }) unless $repo; | ||
| 268 | |||
| 269 | if ($repo->is_master_repository) { | ||
| 270 | $ctx->add_warnings('Repository '.$repo->title.' cannot be removed in app. Please use the command-line tools.'); | ||
| 271 | $ctx->response->redirect('/admin/repository/list'); | ||
| 272 | return; | ||
| 273 | } | ||
| 274 | |||
| 275 | my $confirmed = $ctx->request->body_parameters->{confirmed}; | ||
| 276 | if ($ctx->request->method eq 'POST' && $confirmed) { | ||
| 277 | $self->model('Root')->detach_repository( | ||
| 278 | key => $repo_name, | ||
| 279 | ); | ||
| 280 | |||
| 281 | $ctx->add_info('Removed repository configuration.'); | ||
| 282 | $ctx->response->redirect('/admin/repository/list'); | ||
| 283 | return; | ||
| 284 | } | ||
| 285 | |||
| 286 | my @breadcrumb = ( | ||
| 287 | { | ||
| 288 | label => 'List', | ||
| 289 | href => 'admin/repository/list', | ||
| 290 | }, | ||
| 291 | ); | ||
| 292 | |||
| 293 | my $body = $self->view('Admin::Repository')->remove($ctx, { | ||
| 294 | title => 'Remove ' . $repo->title, | ||
| 295 | repo => $repo, | ||
| 296 | breadcrumb => \@breadcrumb, | ||
| 297 | return_link => $ctx->rebase_url('admin/repository/list'), | ||
| 298 | }); | ||
| 299 | |||
| 300 | $ctx->response->body($body); | ||
| 301 | } | ||
| 302 | |||
| 303 | 1; | ||
| 304 | |||
| 305 | __END__ |