1 | package Sophie::Controller::Admin; |
---|
2 | use Moose; |
---|
3 | use namespace::autoclean; |
---|
4 | use YAML qw/freeze thaw/; |
---|
5 | |
---|
6 | BEGIN {extends 'Catalyst::Controller'; } |
---|
7 | |
---|
8 | =head1 NAME |
---|
9 | |
---|
10 | Sophie::Controller::Admin - Catalyst Controller |
---|
11 | |
---|
12 | =head1 DESCRIPTION |
---|
13 | |
---|
14 | Catalyst Controller. |
---|
15 | |
---|
16 | =head1 METHODS |
---|
17 | |
---|
18 | =cut |
---|
19 | |
---|
20 | =head2 index |
---|
21 | |
---|
22 | =cut |
---|
23 | |
---|
24 | sub index :Path :Args(0) { |
---|
25 | my ( $self, $c ) = @_; |
---|
26 | |
---|
27 | $c->response->body('Matched Sophie::Controller::Admin in Admin.'); |
---|
28 | } |
---|
29 | |
---|
30 | sub create :XMLRPC { |
---|
31 | my ( $self, $c, $distribution, $version, $arch ) = @_; |
---|
32 | |
---|
33 | my $rs = $c->model('Base')->resultset('Distribution'); |
---|
34 | my $rs_d = $rs->find_or_create({ name => $distribution}) or do { |
---|
35 | $c->stash->{xmlrpc} = 'Erreur adding distrib'; |
---|
36 | return; |
---|
37 | }; |
---|
38 | |
---|
39 | my $rs_r = $rs_d->Release->find_or_create({ version => $version, }) or do { |
---|
40 | $c->stash->{xmlrpc} = 'Erreur adding release'; |
---|
41 | return; |
---|
42 | }; |
---|
43 | |
---|
44 | my $rs_a = $rs_r->Arch->find_or_create({ arch => $arch }) or do { |
---|
45 | $c->stash->{xmlrpc} = 'Erreur adding arch'; |
---|
46 | return; |
---|
47 | }; |
---|
48 | |
---|
49 | $c->stash->{xmlrpc} = 'Ok'; |
---|
50 | |
---|
51 | $c->model('Base')->storage->dbh->commit; |
---|
52 | |
---|
53 | } |
---|
54 | |
---|
55 | sub add_media :XMLRPC { |
---|
56 | my ( $self, $c, $distribspec, $mediaspec) = @_; |
---|
57 | |
---|
58 | my $d = $c->model('Base')->resultset('Distribution') |
---|
59 | ->search(name => $distribspec->{distribution}) |
---|
60 | ->search_related('Release', version => $distribspec->{release}) |
---|
61 | ->search_related('Arch', arch => $distribspec->{arch})->next; |
---|
62 | if ($d) { |
---|
63 | my $new = my $rs = $c->model('Base')->resultset('Medias') |
---|
64 | ->update_or_create({ |
---|
65 | %{ $mediaspec }, |
---|
66 | Arch => $d, |
---|
67 | }, |
---|
68 | { key => 'label' } |
---|
69 | ); |
---|
70 | if ($new) { |
---|
71 | $c->stash->{xmlrpc} = 'OK'; |
---|
72 | $c->model('Base')->storage->dbh->commit; |
---|
73 | } else { |
---|
74 | $c->stash->{xmlrpc} = 'Erreur adding media'; |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | sub remove_media :XMLRPC { |
---|
80 | my ( $self, $c, $distribspec, $medianame) = @_; |
---|
81 | |
---|
82 | my $med = $c->model('Base::Medias')->find( |
---|
83 | { |
---|
84 | label => $medianame, |
---|
85 | d_arch => $c->model('Base')->resultset('Distribution') |
---|
86 | ->search(name => $distribspec->{distribution}) |
---|
87 | ->search_related('Release', version => $distribspec->{release}) |
---|
88 | ->search_related('Arch', arch => |
---|
89 | $distribspec->{arch})->next->d_arch_key, |
---|
90 | } |
---|
91 | ); |
---|
92 | |
---|
93 | if ($med->delete) { |
---|
94 | $c->stash->{xmlrpc} = 'OK'; |
---|
95 | $c->model('Base')->storage->dbh->commit; |
---|
96 | } else { |
---|
97 | $c->stash->{xmlrpc} = "Cannot delete $medianame"; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | sub list_path :XMLRPC { |
---|
102 | my ($self, $c, $distribution, $version, $arch, $media) = @_; |
---|
103 | |
---|
104 | if (ref $distribution) { |
---|
105 | ($distribution, $version, $arch, $media) = |
---|
106 | ( |
---|
107 | $distribution->{distribution}, |
---|
108 | $distribution->{release}, |
---|
109 | $distribution->{arch}, |
---|
110 | $version, |
---|
111 | ); |
---|
112 | } |
---|
113 | |
---|
114 | $c->stash->{xmlrpc} = [ |
---|
115 | $c->model('Base')->resultset('Distribution') |
---|
116 | ->search($distribution ? (name => $distribution) : ()) |
---|
117 | ->search_related('Release', $version ? (version => $version) : ()) |
---|
118 | ->search_related('Arch', $arch ? (arch => $arch) : ()) |
---|
119 | ->search_related('Medias', $media ? (label => $media) : ()) |
---|
120 | ->search_related('MediasPaths') |
---|
121 | ->search_related('Paths')->get_column('path') |
---|
122 | ->all ]; |
---|
123 | } |
---|
124 | |
---|
125 | sub media_path :XMLRPC { |
---|
126 | my ( $self, $c, $distribution, $version, $arch, $label, $path ) = @_; |
---|
127 | |
---|
128 | if (ref $distribution) { |
---|
129 | ($distribution, $version, $arch, $label, $path) = |
---|
130 | ( |
---|
131 | $distribution->{distribution}, |
---|
132 | $distribution->{release}, |
---|
133 | $distribution->{arch}, |
---|
134 | $version, |
---|
135 | $arch, |
---|
136 | ); |
---|
137 | } |
---|
138 | |
---|
139 | $path =~ s/\/*$//; |
---|
140 | $path =~ s/\/+/\//g; |
---|
141 | |
---|
142 | my $med = $c->model('Base')->resultset('Distribution') |
---|
143 | ->search(name => $distribution) |
---|
144 | ->search_related('Release', version => $version) |
---|
145 | ->search_related('Arch', arch => $arch) |
---|
146 | ->search_related('Medias', label => $label)->next or return; |
---|
147 | |
---|
148 | my $rspath = $c->model('Base')->resultset('Paths') |
---|
149 | ->find_or_create({ path => $path }) or do { |
---|
150 | }; |
---|
151 | my $new = $c->model('Base')->resultset('MediasPaths')->new({ |
---|
152 | Medias => $med, |
---|
153 | Paths => $rspath, |
---|
154 | }); |
---|
155 | $new->insert; |
---|
156 | |
---|
157 | $c->model('Base')->storage->dbh->commit; |
---|
158 | } |
---|
159 | |
---|
160 | sub media_remove_path :XMLRPC { |
---|
161 | my ( $self, $c, $distribution, $version, $arch, $label, $path ) = @_; |
---|
162 | |
---|
163 | if (ref $distribution) { |
---|
164 | ($distribution, $version, $arch, $label, $path) = |
---|
165 | ( |
---|
166 | $distribution->{distribution}, |
---|
167 | $distribution->{release}, |
---|
168 | $distribution->{arch}, |
---|
169 | $version, |
---|
170 | $arch, |
---|
171 | ); |
---|
172 | } |
---|
173 | |
---|
174 | $path =~ s/\/*$//; |
---|
175 | $path =~ s/\/+/\//g; |
---|
176 | |
---|
177 | my $med = $c->model('Base')->resultset('Distribution') |
---|
178 | ->search(name => $distribution) |
---|
179 | ->search_related('Release', version => $version) |
---|
180 | ->search_related('Arch', arch => $arch) |
---|
181 | ->search_related('Medias', label => $label)->find or return; |
---|
182 | |
---|
183 | my $rspath = $c->model('Base')->resultset('Paths') |
---|
184 | ->find({ path => $path }) or do { |
---|
185 | return; |
---|
186 | }; |
---|
187 | my $new = $c->model('Base')->resultset('MediasPaths')->search({ |
---|
188 | d_media => $med->d_media_key, |
---|
189 | d_path => $rspath->d_path_key, |
---|
190 | })->next->delete; |
---|
191 | |
---|
192 | $c->model('Base')->storage->dbh->commit; |
---|
193 | } |
---|
194 | |
---|
195 | sub ls_local : XMLRPC { |
---|
196 | my ($self, $c, $path) = @_; |
---|
197 | |
---|
198 | $c->stash->{xmlrpc} = [ <$path*> ]; |
---|
199 | } |
---|
200 | |
---|
201 | sub replace_path : XMLRPC { |
---|
202 | my ($self, $c, $path, $newpath) = @_; |
---|
203 | |
---|
204 | my $dpath = $c->model('Base::Paths')->find({ |
---|
205 | path => $path, |
---|
206 | }) or do { |
---|
207 | return $c->stash->{xmlrpc} = 'Path not found'; |
---|
208 | }; |
---|
209 | |
---|
210 | $newpath =~ s/\/*$//; |
---|
211 | |
---|
212 | $dpath->update( |
---|
213 | { |
---|
214 | updated => undef, |
---|
215 | path => $newpath, |
---|
216 | } |
---|
217 | ) and $c->model('Base')->storage->dbh->commit; |
---|
218 | return $c->stash->{xmlrpc} = 'OK'; |
---|
219 | } |
---|
220 | |
---|
221 | sub remove_path : XMLRPC { |
---|
222 | my ($self, $c, $path) = @_; |
---|
223 | |
---|
224 | my $dpath = $c->model('Base::Paths')->find({ |
---|
225 | path => $path, |
---|
226 | }) or do { |
---|
227 | return $c->stash->{xmlrpc} = 'Path not found'; |
---|
228 | }; |
---|
229 | |
---|
230 | |
---|
231 | $dpath->delete and $c->model('Base')->storage->dbh->commit; |
---|
232 | return $c->stash->{xmlrpc} = 'OK'; |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | sub dump_distrib : XMLRPC { |
---|
237 | my ($self, $c, $distribution, $version, $arch) = @_; |
---|
238 | |
---|
239 | if (!ref $distribution) { |
---|
240 | $distribution = { |
---|
241 | distribution => $distribution, |
---|
242 | release => $version, |
---|
243 | arch => $arch, |
---|
244 | }; |
---|
245 | } |
---|
246 | |
---|
247 | $c->forward('/distrib/exists', [ $distribution ]) or do { |
---|
248 | $c->error('No such distribution'); |
---|
249 | return; |
---|
250 | }; |
---|
251 | |
---|
252 | my $ref = { |
---|
253 | distrib => $distribution, |
---|
254 | }; |
---|
255 | |
---|
256 | $ref->{media} = $c->forward('/distrib/struct', [ $distribution ]); |
---|
257 | |
---|
258 | foreach (@{ $ref->{media} || []}) { |
---|
259 | warn $_->{label}; |
---|
260 | $ref->{path}{$_->{label}} = $c->forward('list_path', [ $distribution, |
---|
261 | $_->{label} ]); |
---|
262 | } |
---|
263 | |
---|
264 | $c->stash->{xmlrpc} = freeze($ref); |
---|
265 | } |
---|
266 | |
---|
267 | sub clean_distrib : XMLRPC { |
---|
268 | my ($self, $c, $distribution, $version, $arch) = @_; |
---|
269 | |
---|
270 | if (!ref $distribution) { |
---|
271 | $distribution = { |
---|
272 | distribution => $distribution, |
---|
273 | release => $version, |
---|
274 | arch => $arch, |
---|
275 | }; |
---|
276 | } |
---|
277 | |
---|
278 | my $rsdist = $c->model('Base')->resultset('Distribution') |
---|
279 | ->search(name => $distribution->{distribution}) |
---|
280 | ->search_related('Release', version => $distribution->{release}) |
---|
281 | ->search_related('Arch', arch => $distribution->{arch}) |
---|
282 | ->search_related('Medias'); |
---|
283 | |
---|
284 | my $new = $c->model('Base')->resultset('MediasPaths')->search({ |
---|
285 | d_media => { IN => $rsdist->get_column('d_media_key')->as_query }, |
---|
286 | })->delete; |
---|
287 | |
---|
288 | # $c->model('Base')->storage->dbh->rollback; |
---|
289 | |
---|
290 | } |
---|
291 | |
---|
292 | sub load_distrib : XMLRPC { |
---|
293 | my ( $self, $c, $dump ) = @_; |
---|
294 | |
---|
295 | my $ref = thaw($dump); |
---|
296 | |
---|
297 | warn keys %{$ref->{path}}; |
---|
298 | |
---|
299 | $c->forward('clean_distrib', [ $ref->{distrib} ]); |
---|
300 | |
---|
301 | $c->forward('create', [ |
---|
302 | $ref->{distrib}{distribution}, |
---|
303 | $ref->{distrib}{release}, |
---|
304 | $ref->{distrib}{arch}, |
---|
305 | ]); |
---|
306 | |
---|
307 | foreach my $media (@{ $ref->{media} || []}) { |
---|
308 | $c->forward('add_media', [ $ref->{distrib}, $media ]); |
---|
309 | } |
---|
310 | foreach my $media (keys %{ $ref->{path} || {} }) { |
---|
311 | foreach my $path (@{ $ref->{path}{$media} || [] }) { |
---|
312 | $c->forward('media_path', [ $ref->{distrib}, $media, $path ]); |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | #$c->model('Base')->storage->dbh->rollback; |
---|
317 | } |
---|
318 | |
---|
319 | sub set_user_data : XMLRPC { |
---|
320 | my ( $self, $c, $user, $dataname, $data ) = @_; |
---|
321 | $c->forward('/user/set_user_data', [ $user, $dataname, $data ]); |
---|
322 | } |
---|
323 | |
---|
324 | sub get_user_data : XMLRPC { |
---|
325 | my ( $self, $c, $user, $dataname ) = @_; |
---|
326 | $c->forward('/user/fetch_user_data', [ $user, $dataname ]); |
---|
327 | } |
---|
328 | |
---|
329 | sub update_user_data : XMLRPC { |
---|
330 | my ( $self, $c, $user, $dataname, $data ) = @_; |
---|
331 | $c->forward('/user/update_user_data', [ $user, $dataname, $data ]); |
---|
332 | } |
---|
333 | |
---|
334 | sub set_user_password : XMLRPC { |
---|
335 | my ( $self, $c, $user, $password ) = @_; |
---|
336 | |
---|
337 | $c->forward('/user/set_user_password', $user, $password); |
---|
338 | } |
---|
339 | |
---|
340 | sub list_user : XMLRPC { |
---|
341 | my ($self, $c, $match) = @_; |
---|
342 | |
---|
343 | $c->stash->{xmlrpc} = [ |
---|
344 | $c->model('Base::Users')->search( |
---|
345 | { |
---|
346 | $match ? ( mail => { '~' => $match } ) : (), |
---|
347 | } |
---|
348 | )->get_column('mail')->all ]; |
---|
349 | } |
---|
350 | |
---|
351 | sub delete_user : XMLRPC { |
---|
352 | my ($self, $c, $mail) = @_; |
---|
353 | |
---|
354 | if (my $user = $c->model('Base::Users')->find({ mail => $mail })) { |
---|
355 | if ($user->delete) { |
---|
356 | $c->model('Base')->storage->dbh->commit; |
---|
357 | return $c->stash->{xmlrpc} = "User $mail deleted"; |
---|
358 | } |
---|
359 | } |
---|
360 | $c->stash->{xmlrpc} = "No user $mail"; |
---|
361 | } |
---|
362 | |
---|
363 | sub create_user : XMLRPC { |
---|
364 | my ($self, $c, $user, $password) = @_; |
---|
365 | |
---|
366 | if ($c->model('Base::Users')->create({ |
---|
367 | mail => $user, |
---|
368 | })) { |
---|
369 | $c->forward('set_user_password', [ $user, $password ]); |
---|
370 | return $c->stash->{xmlrpc} = "User $user created"; |
---|
371 | } else { |
---|
372 | return; |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | =head1 AUTHOR |
---|
377 | |
---|
378 | Olivier Thauvin |
---|
379 | |
---|
380 | =head1 LICENSE |
---|
381 | |
---|
382 | This library is free software. You can redistribute it and/or modify |
---|
383 | it under the same terms as Perl itself. |
---|
384 | |
---|
385 | =cut |
---|
386 | |
---|
387 | __PACKAGE__->meta->make_immutable; |
---|
388 | |
---|
389 | 1; |
---|