rvd_front 73.7 KB
Newer Older
1
2
3
#!/usr/bin/env perl
use warnings;
use strict;
4
5
6
#####
use locale ':not_characters';
#####
7

8
use Carp qw(confess cluck);
9
use Data::Dumper;
10
use Digest::SHA qw(sha256_hex);
11
use Hash::Util qw(lock_hash);
12
use Mojolicious::Lite 'Ravada::I18N';
Francesc Guasch's avatar
Francesc Guasch committed
13
use Mojo::JSON qw(decode_json encode_json);
14
use Time::Piece;
15
#use Mojolicious::Plugin::I18N;
16
use Mojo::Home;
Francesc Guasch's avatar
Francesc Guasch committed
17
18

use I18N::LangTags::Detect;
19
#####
20
21
#my $self->plugin('I18N');
#package Ravada::I18N:en;
22
#####
23
24
25
#
no warnings "experimental::signatures";
use feature qw(signatures);
26

27

28
use Ravada::Front;
29
use Ravada::Front::Domain;
Francesc Guasch's avatar
Francesc Guasch committed
30
use Ravada::Auth;
Francesc Guasch's avatar
Francesc Guasch committed
31
use Ravada::WebSocket;
32
use POSIX qw(locale_h strftime);
Francesc Guasch's avatar
Francesc Guasch committed
33
34

my $help;
Francesc Guasch's avatar
Francesc Guasch committed
35

Francesc Guasch's avatar
Francesc Guasch committed
36
37
38
my $FILE_CONFIG = "/etc/rvd_front.conf";

my $error_file_duplicated = 0;
39
for my $file ( "/etc/rvd_front.conf" , ($ENV{HOME} or '')."/rvd_front.conf") {
Francesc Guasch's avatar
Francesc Guasch committed
40
    warn "WARNING: Found config file at $file and at $FILE_CONFIG\n"
41
        if -e $file && $FILE_CONFIG && $file ne $FILE_CONFIG;
42
    $FILE_CONFIG = $file if -e $file;
Francesc Guasch's avatar
Francesc Guasch committed
43
    $error_file_duplicated++;
44
}
Francesc Guasch's avatar
Francesc Guasch committed
45
warn "WARNING: using $FILE_CONFIG\n"    if$error_file_duplicated>2;
46
47

my $FILE_CONFIG_RAVADA;
48
for my $file ( "/etc/ravada.conf" , ($ENV{HOME} or '')."/ravada.conf") {
49
50
51
    warn "WARNING: Found config file at $file and at $FILE_CONFIG_RAVADA\n"
        if -e $file && $FILE_CONFIG_RAVADA;
    $FILE_CONFIG_RAVADA = $file if -e $file;
Francesc Guasch's avatar
Francesc Guasch committed
52
}
53

amesones's avatar
amesones committed
54
55
56
my $CONFIG_FRONT = plugin Config => { default => {
                                                hypnotoad => {
                                                pid_file => 'log/rvd_front.pid'
fv3rdugo's avatar
Indent    
fv3rdugo committed
57
58
                                                ,listen => ['http://*:8081']
                                                }
59
                                              ,login_bg_file => '/img/intro-bg.jpg'
Francesc Guasch's avatar
Francesc Guasch committed
60
                                              ,login_header => 'Welcome'
61
                                              ,login_message => ''
62
                                              ,secrets => ['changeme0']
63
                                              ,guide => 0
fv3rdugo's avatar
fv3rdugo committed
64
                                              ,login_custom => ''
65
                                              ,footer => 'bootstrap/footer'
66
                                              ,monitoring => 0
67
                                              ,fallback => 0
68
                                              ,guide_custom => ''
69
70
                                              ,admin => {
                                                    hide_clones => 15
Francesc Guasch's avatar
Francesc Guasch committed
71
                                                    ,autostart => 0
amesones's avatar
amesones committed
72
                                              }
73
                                              ,config => $FILE_CONFIG_RAVADA
74
                                              ,auto_view => 0
75
76
77
78
79
                                              ,log => {
                                                log => 0
                                                ,file => '/var/log/ravada/rvd_front.log'
                                                ,level => 'debug'
                                              }
amesones's avatar
amesones committed
80
                                              }
Francesc Guasch's avatar
Francesc Guasch committed
81
                                      ,file => $FILE_CONFIG
82
};
83

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#####
#####
#####
# Import locale-handling tool set from POSIX module.
# This example uses: setlocale -- the function call
#                    LC_CTYPE -- explained below

# query and save the old locale
my $old_locale = setlocale(LC_CTYPE);

setlocale(LC_CTYPE, "en_US.ISO8859-1");
# LC_CTYPE now in locale "English, US, codeset ISO 8859-1"

setlocale(LC_CTYPE, "");
# LC_CTYPE now reset to default defined by LC_ALL/LC_CTYPE/LANG
# environment variables.  See below for documentation.

# restore the old locale
setlocale(LC_CTYPE, $old_locale);
#####
#####
#####
106
plugin I18N => {namespace => 'Ravada::I18N', default => 'en'};
Francesc Guasch's avatar
Francesc Guasch committed
107

108
my %config;
109
%config = (config => $CONFIG_FRONT->{config}) if $CONFIG_FRONT->{config};
110
our $RAVADA = Ravada::Front->new(%config);
111

112
our $USER;
Francesc Guasch's avatar
Francesc Guasch committed
113

114
# TODO: get those from the config file
115
our $DOCUMENT_ROOT = "/var/www";
116
117

# session times out in 5 minutes
118
our $SESSION_TIMEOUT = ($CONFIG_FRONT->{session_timeout} or 5 * 60);
119
# session times out in 15 minutes for admin users
120
our $SESSION_TIMEOUT_ADMIN = ($CONFIG_FRONT->{session_timeout_admin} or 15 * 60);
121
our $SESSION_TIMEOUT_ADMIN2 = ($CONFIG_FRONT->{session_timeout_admin2} or 60 * 60);
122

Francesc Guasch's avatar
Francesc Guasch committed
123
my $WS = Ravada::WebSocket->new(ravada => $RAVADA);
124
125
my %ALLOWED_ANONYMOUS_WS = map { $_ => 1 } qw(list_bases_anonymous list_alerts);

Francesc Guasch's avatar
Francesc Guasch committed
126
127
init();
############################################################################3
128

129
130
131
132
sub _time() {
    return strftime('%Y/%m/%d:%H:%M:%S %z',localtime);
}

133
134
135
hook before_routes => sub {
  my $c = shift;

136
137
  $USER = undef;

Francesc Guasch's avatar
Francesc Guasch committed
138
  $c->stash(version => $RAVADA->version);
139
  my $url = $c->req->url->to_abs->path;
fv3rdugo's avatar
fv3rdugo committed
140
  my $host = $c->req->url->to_abs->host;
141
  $c->stash(css=>['/css/sb-admin.css']
Francesc Guasch's avatar
Francesc Guasch committed
142
143
            ,js=>[
                '/js/ravada.js'
144
145
146
                ]
            ,csssnippets => []
            ,navbar_custom => 0
Francesc Guasch's avatar
Francesc Guasch committed
147
148
149
150
            ,url => undef
            ,_logged_in => undef
            ,_anonymous => undef
            ,_user => undef
151
            ,footer=> $CONFIG_FRONT->{footer}
152
            ,monitoring => 0
fv3rdugo's avatar
fv3rdugo committed
153
            ,fallback => $CONFIG_FRONT->{fallback}
154
            ,check_netdata => 0
155
            ,guide => $CONFIG_FRONT->{guide}
fv3rdugo's avatar
fv3rdugo committed
156
            ,host => $host
157
            );
158

159
160
    $USER = _logged_in($c);

161
162
163
164
165
    my $user = '-';
    $user = $USER->name if defined $USER;
    app->log->info(_remote_ip($c)." $user ["._time()."] ".$c->req->method." ".$url)
    if $CONFIG_FRONT->{log}->{log};

166
    return if $url =~ m{^/(css|font|img|js)}
Francesc Guasch's avatar
Francesc Guasch committed
167
	   || $url =~ m{^/fallback/.*\.(css|js|map)$};
168

169
170
171
172
173
174
175
176
177
    return if $url =~ m{^/(login|logout|requirements|robots.txt)};

    if ( $RAVADA->is_in_maintenance ) {
        return login($c) if !$USER && $url =~ m{^/$};
        return maintenance($c) if !$USER || !$USER->is_operator;
    }

    return if $url =~ m{^/anonymous};

Francesc Guasch's avatar
Francesc Guasch committed
178
179
    if (($url =~ m{^/machine/(clone|display|info|view)/}
        || $url =~ m{^/(list_bases_anonymous|request/)}i
180
        || $url =~ m{^/ws/subscribe}
Francesc Guasch's avatar
Francesc Guasch committed
181
182
183
184
185
186
        ) && !_logged_in($c)) {
        $USER = _anonymous_user($c);
        return if $USER->is_temporary;
    }
    return access_denied($c)
        if $url =~ /(screenshot|\.json)/
187
        && !_logged_in($c);
188
    return login($c,401) if !_logged_in($c);
189

190
191
192
193
194
195
    if ($USER && $USER->is_admin && $CONFIG_FRONT->{monitoring}) {
        if (!defined $c->session('monitoring')) {
            $c->stash(check_netdata => "https://$host:19999/index.html");
        }
        $c->stash( monitoring => 1) if $c->session('monitoring');
    }
196
        $c->stash( fallback => 1) if $c->session('fallback');
197
198
199
200
201
};


############################################################################3

Francesc Guasch's avatar
Francesc Guasch committed
202
203
204
205
206
any '/robots.txt' => sub {
    my $c = shift;
    return $c->render(text => "User-agent: *\nDisallow: /\n", format => 'text');
};

207
any '/' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
208
    my $c = shift;
209
    return quick_start($c);
Francesc Guasch's avatar
Francesc Guasch committed
210
211
};

Francesc Guasch's avatar
Francesc Guasch committed
212
213
any '/index.html' => sub {
    my $c = shift;
214
    return quick_start($c);
Francesc Guasch's avatar
Francesc Guasch committed
215
216
};

217
218
any '/login' => sub {
    my $c = shift;
219
    return login($c);
220
221
};

Francesc Guasch's avatar
Francesc Guasch committed
222
223
224
225
226
227
228
229
230
any '/test' => sub {
    my $c = shift;
    my $logged = _logged_in($c);
    my $count = $c->session('count');
    $c->session(count => ++$count);

    my $name_mojo = $c->signed_cookie('mojolicious');

    my $dump_log = ''.(Dumper($logged) or '');
Francesc Guasch's avatar
Francesc Guasch committed
231
232
233
234
235
    return $c->render(text => "$count ".($name_mojo or '')."<br> ".$dump_log
        ."<br>"
        ."<script>alert(window.screen.availWidth"
        ."+\" \"+window.screen.availHeight)</script>"
    );
Francesc Guasch's avatar
Francesc Guasch committed
236
237
};

Francesc Guasch's avatar
Francesc Guasch committed
238
239
any '/logout' => sub {
    my $c = shift;
240
    logout($c);
Francesc Guasch's avatar
Francesc Guasch committed
241
242
243
    $c->redirect_to('/');
};

244
get '/anonymous' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
245
    my $c = shift;
246
#    $c->render(template => 'bases', base => list_bases());
247
    $USER = _anonymous_user($c);
248
    return list_bases_anonymous($c);
Francesc Guasch's avatar
Francesc Guasch committed
249
250
};

251
get '/anonymous_logout.html' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
252
    my $c = shift;
253
254
    $c->session('anonymous_user' => '');
    return $c->redirect_to('/');
Francesc Guasch's avatar
Francesc Guasch committed
255
256
};

257
get '/anonymous/(#base_id).html' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
258
259
    my $c = shift;

260
    $c->stash(_anonymous => 1 , _logged_in => 0);
261
    _init_error($c);
262
    my $base_id = $c->stash('base_id');
263
264
    my $base = $RAVADA->search_domain_by_id($base_id);

265
    $USER = _anonymous_user($c);
266
    return quick_start_domain($c,$base->id, $USER->name);
Francesc Guasch's avatar
Francesc Guasch committed
267
268
};

269
270
271
272
273
274
275
276
get '/settings_global.json' => sub($c) {
    $RAVADA->is_in_maintenance();
    return $c->render(json => $RAVADA->settings_global );
};

post '/settings_global' => sub($c) {
    my $arg = decode_json($c->req->body);

277
    $RAVADA->update_settings_global($arg, $USER);
278
279
280
281

    return $c->render(json => { ok => 1 });
};

282
any '/admin/#type' => sub {
283
  my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
284

285
286
287
  return admin($c)  if $c->stash('type') eq 'machines'
                        && $USER->is_operator;

288
289
  return access_denied($c)    if !$USER->is_operator;

amesones's avatar
amesones committed
290
  return admin($c);
Francesc Guasch's avatar
Francesc Guasch committed
291
292
};

293
any '/new_machine' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
294
    my $c = shift;
295
    return access_denied($c)    if !$USER->can_create_machine;
296
    return new_machine($c);
Francesc Guasch's avatar
Francesc Guasch committed
297
298
};

299
300
301
302
303
any '/copy_machine' => sub {
    my $c = shift;
    return new_machine_copy($c);
};

304
get '/domain/new.html' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
305
306
    my $c = shift;

307
308
    return access_denied($c) if !_logged_in($c) || !$USER->is_admin();
    $c->stash(error => []);
amesones's avatar
amesones committed
309
    return $c->render(template => "main/new_machine");
Francesc Guasch's avatar
Francesc Guasch committed
310
311
312

};

313
314
get '/list_vm_types.json' => sub {
    my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
315
    $c->render(json => $RAVADA->list_vm_types);
316
317
};

318
319
320
321
322
323
324
325
326
327
328
329
330
331
get '/list_nodes.json' => sub {
    my $c = shift;
    $c->render(json => [$RAVADA->list_vms]);
};

get '/node/enable/(:id).json' => sub {
    my $c = shift;
    return access_denied($c) if !$USER->is_admin;
    return $c->render(json => {enabled => $RAVADA->enable_node($c->stash('id'),1)});
};

get '/node/disable/(:id).json' => sub {
    my $c = shift;
    return access_denied($c) if !$USER->is_admin;
332
333
334
335
336

    my $machines = $RAVADA->_list_machines_vm($c->stash('id'));
    for ( @$machines ) {
        my $req = Ravada::Request->shutdown_domain( uid => $USER->id , id_domain => $_->{id} );
    }
337
338
    return $c->render(json => {enabled => $RAVADA->enable_node($c->stash('id'),0)});
};
339
340
341
342
343
get '/node/remove/(:id).json' => sub {
    my $c = shift;
    return access_denied($c) if !$USER->is_admin;
    return $c->render(json => {remove => $RAVADA->remove_node($c->stash('id'),1)});
};
344

Francesc Guasch's avatar
Francesc Guasch committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
get '/node/shutdown/(:id).json' => sub {
    my $c = shift;
    return access_denied($c) if !$USER->is_admin;

    my $machines = $RAVADA->_list_machines_vm($c->stash('id'));
    for ( @$machines ) {
        my $req = Ravada::Request->shutdown_domain(
                    uid => $USER->id
            , id_domain => $_->{id}
        );
    }
    my $at = 0;
    if (@$machines) {
        $at = time + 60 + scalar @$machines;
    }
    my $req = Ravada::Request->shutdown_node(
                id_node => $c->stash('id')
                ,at => $at
    );
    Ravada::Request->connect_node(
                id_node => $c->stash('id')
                ,at => $at + 10
    );
    return $c->render(json => {id_req => $req->id });
};

get '/node/start/(:id).json' => sub {
    my $c = shift;
    return access_denied($c) if !$USER->is_admin;
    my $req = Ravada::Request->start_node(
                id_node => $c->stash('id')
    );
    for my $seconds ( 30,60,90,120 ) {
        Ravada::Request->connect_node(
            id_node => $c->stash('id')
            ,at => time + $seconds
        );
    }

    return $c->render(json => {id_req => $req->id });

};

388
389
390
391
392
393
any '/new_node' => sub {
    my $c = shift;
    return access_denied($c)    if !$USER->is_admin;
    return new_node($c);
};

Francesc Guasch's avatar
Francesc Guasch committed
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
get '/node/connect/(#backend)/(#hostname)' => sub {
    my $c = shift;
    return access_denied($c)    if !$USER->is_admin;
    my $req = Ravada::Request->connect_node(
                backend => $c->stash('backend')
                    ,hostname => $c->stash('hostname')
    );
    return $c->render(json => {id_req => $req->id });
};

get '/node/connect/(#id)' => sub {
    my $c = shift;
    return access_denied($c)    if !$USER->is_admin;
    my $req = Ravada::Request->connect_node(
        id_node => $c->stash('id')
    );
    return $c->render(json => {id_req => $req->id });
};


414
415
get '/list_bases.json' => sub {
    my $c = shift;
416
417
418
419
420
421
422
423
424
425
426

    my $domains = $RAVADA->list_bases();
    my @domains_show = @$domains;
    if (!$USER->is_admin) {
        @domains_show = ();
        for (@$domains) {
            push @domains_show,($_) if $_->{is_public};
        }
    }
    $c->render(json => [@domains_show]);

427
428
429
430
};

get '/list_images.json' => sub {
    my $c = shift;
431

elodin1's avatar
elodin1 committed
432
433
434
    return access_denied($c) unless _logged_in($c)
        && $USER->can_create_machine();

435
436
437
    my $vm_name = $c->param('backend');

    $c->render(json => $RAVADA->list_iso_images($vm_name or undef));
438
439
};

440
get '/iso_file.json' => sub {
joelalju's avatar
joelalju committed
441
    my $c = shift;
elodin1's avatar
elodin1 committed
442
443
    return access_denied($c) unless _logged_in($c)
        && $USER->can_create_machine();
444
    my @isos =('<NONE>');
Fernando Verdugo's avatar
Fernando Verdugo committed
445
    push @isos,(@{$RAVADA->iso_file('KVM')});
446
    $c->render(json => \@isos);
joelalju's avatar
joelalju committed
447
448
};

449
get '/list_machines.json' => sub {
450
    my $c = shift;
451

452
    return access_denied($c) unless _logged_in($c)
453
454
455
456
457
        && (
            $USER->can_list_machines
            || $USER->can_list_own_machines()
            || $USER->can_list_clones()
            || $USER->can_list_clones_from_own_base()
458
459
460
            || $USER->is_admin()
        );

461
    return $c->render( json => $RAVADA->list_machines($USER) );
462

463
};
Francesc Guasch's avatar
Francesc Guasch committed
464

465
466
467
get '/list_machines_user.json' => sub {
    my $c = shift;
    return $c->render( json => $RAVADA->list_machines_user($USER));
468
};
Francesc Guasch's avatar
Francesc Guasch committed
469

470
471
472
473
474
475
476
get '/list_bases_anonymous.json' => sub {
    my $c = shift;

    # shouldn't this be "list_bases" ?
    $c->render(json => $RAVADA->list_bases_anonymous(_remote_ip($c)));
};

Francesc Guasch's avatar
Francesc Guasch committed
477
get '/list_lxc_templates.json' => sub {
fv3rdugo's avatar
fv3rdugo committed
478
    my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
479
    $c->render(json => $RAVADA->list_lxc_templates);
fv3rdugo's avatar
fv3rdugo committed
480
481
};

482
483
# machine commands

484
get '/machine/info/(:id).(:type)' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
485
    my $c = shift;
486
    my $id = $c->stash('id');
Francesc Guasch's avatar
Francesc Guasch committed
487
    die "No id " if !$id;
488

joelalju's avatar
joelalju committed
489
490
491
    my ($domain) = _search_requested_machine($c);
    return access_denied($c)    if !$domain;

492
    return access_denied($c,"Access denied to user ".$USER->name) unless $USER->can_manage_machine($domain->id);
Francesc Guasch's avatar
Francesc Guasch committed
493

494
495
    my $info = $domain->info($USER);
    if ($domain->is_active && !$info->{ip}) {
Francesc Guasch's avatar
Francesc Guasch committed
496
        Ravada::Request->refresh_machine(id_domain => $domain->id, uid => $USER->id);
497
498
    }
    return $c->render(json => $info);
Francesc Guasch's avatar
Francesc Guasch committed
499
500
};

Francesc Guasch's avatar
Francesc Guasch committed
501
get '/machine/requests/(:id).json' => sub {
502
    my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
503
    my $id_domain = $c->stash('id');
504
    return access_denied($c) unless $USER->can_manage_machine($id_domain);
joelalju's avatar
joelalju committed
505

Francesc Guasch's avatar
Francesc Guasch committed
506
507
    $c->render(json => $RAVADA->list_requests($id_domain,10));
};
joelalju's avatar
joelalju committed
508

509
any '/machine/manage/(:id).(:type)' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
510
   	 my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
511
     Ravada::Request->refresh_machine(id_domain => $c->stash('id'), uid => $USER->id);
Francesc Guasch's avatar
Francesc Guasch committed
512
     return manage_machine($c);
513
};
joelalju's avatar
joelalju committed
514

Francesc Guasch's avatar
Francesc Guasch committed
515
516
517
any '/hardware/(:id).(:type)' => sub {
   	 my $c = shift;
     return $c->render(template => 'main/hardware');
518
519
};

520
get '/machine/view/(:id).(:type)' => sub {
521
    my $c = shift;
522
523
    my $id = $c->stash('id');
    my $type = $c->stash('type');
Francesc Guasch's avatar
Francesc Guasch committed
524

525
526
    return $c->redirect_to('/login') if !_logged_in($c);

joelalju's avatar
joelalju committed
527
528
529
530
531
532
    my ($domain) = _search_requested_machine($c);
    return access_denied($c)    if !$domain;

    return access_denied($c) unless $USER->is_admin
                              || $domain->id_owner == $USER->id;

533
534
535
    return view_machine($c);
};

Francesc Guasch's avatar
Francesc Guasch committed
536
any '/machine/clone/(:id).(:type)' => sub {
Joel Alarcón's avatar
Joel Alarcón committed
537
    my $c = shift;
538

Francesc Guasch's avatar
Francesc Guasch committed
539
540
541
542
543
544
545
546
547
548
549
    return clone_machine($c)    if $USER && $USER->can_clone() && !$USER->is_temporary();

    my $bases_anonymous = $RAVADA->list_bases_anonymous(_remote_ip($c));
    for (@$bases_anonymous) {
        if ($_->{id} == $c->stash('id') ) {
            return clone_machine($c,1);
        }
    }

    return login($c)    if !$USER || $USER->is_temporary;
    return access_denied($c);
550
};
Francesc Guasch's avatar
Francesc Guasch committed
551

552
get '/machine/shutdown/(:id).(:type)' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
553
        my $c = shift;
554
    return access_denied($c)        if !$USER ->can_shutdown($c->stash('id'));
Francesc Guasch's avatar
Francesc Guasch committed
555
556
557
558

        return shutdown_machine($c);
};

559
any '/machine/remove/(:id).(:type)' => sub {
joansp's avatar
joansp committed
560
        my $c = shift;
561
    return access_denied($c)       if !$USER->can_remove_machine($c->stash('id'));
joansp's avatar
joansp committed
562
563
        return remove_machine($c);
};
564
565

any '/machine/remove_clones/(:id).(:type)' => sub {
566
567
568
569
570
571
    my $c = shift;

    # TODO : call to $domain->_allow_remove();
	return access_denied($c)
        unless
            $USER -> can_remove_clone_all()
Francesc Guasch's avatar
Francesc Guasch committed
572
573
	        || $USER->can_remove_clone()
            || $USER->can_remove_all();
574
    return remove_clones($c);
575
576
};

577
get '/machine/prepare/(:id).(:type)' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
578
579
580
581
        my $c = shift;
        return prepare_machine($c);
};

Francesc Guasch's avatar
Francesc Guasch committed
582
get '/machine/set_base_vm/(:id_vm)/(:id_domain).(:type)' => sub {
583
    my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
584
585
586
587
588
589
    return set_base_vm($c, 1);
};

get '/machine/remove_base_vm/(:id_vm)/(:id_domain).(:type)' => sub {
    my $c = shift;
    return set_base_vm($c, 0);
590
591
};

592
get '/machine/remove_b/(:id).(:type)' => sub {
joansp's avatar
joansp committed
593
594
595
596
        my $c = shift;
        return remove_base($c);
};

597
get '/machine/remove_base/(:id).(:type)' => sub {
598
599
600
601
    my $c = shift;
    return remove_base($c);
};

602
get '/machine/screenshot/(:id).(:type)' => sub {
603
        my $c = shift;
604
605
        my $domain = _search_requested_machine($c);
        return access_denied($c)   if (!$USER->can_screenshot() || $domain->is_base());
606
607
608
        return screenshot_machine($c);
};

609
get '/machine/copy_screenshot/(:id).(:type)' => sub {
610
611
612
        my $c = shift; 
        my $domain = _search_requested_machine($c);
        return access_denied($c) if (!$USER->is_admin() || $domain->id_base() == NULL );
613
614
615
        return copy_screenshot($c);
};

616
get '/machine/pause/(:id).(:type)' => sub {
joansp's avatar
joansp committed
617
618
619
620
        my $c = shift;
        return pause_machine($c);
};

621
get '/machine/hibernate/(:id).(:type)' => sub {
622
        my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
623
          return access_denied($c)
Francesc Guasch's avatar
Francesc Guasch committed
624
             unless $USER->is_admin() || $USER->can_shutdown($c->stash('id'));
Francesc Guasch's avatar
Francesc Guasch committed
625

626
627
628
        return hybernate_machine($c);
};

629
get '/machine/resume/(:id).(:type)' => sub {
joansp's avatar
joansp committed
630
631
632
633
        my $c = shift;
        return resume_machine($c);
};

634
get '/machine/start/(:id).(:type)' => sub {
joansp's avatar
joansp committed
635
636
637
        my $c = shift;
        return start_machine($c);
};
joansp's avatar
joansp committed
638

639
get '/machine/exists/#name' => sub {
640
    my $c = shift;
641
    my $name = $c->stash('name');
642
643
644
645
646
647
    #TODO
    # return failure if it can't find the name in the URL

    return $c->render(json => $RAVADA->domain_exists($name));

};
joansp's avatar
joansp committed
648

Francesc Guasch's avatar
Francesc Guasch committed
649
post '/machine/hardware/change' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
650
    my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
651
652
653
654
655
656
657
658
659
660
661
662
    my $arg = decode_json($c->req->body);

    my $domain = Ravada::Front::Domain->open(delete $arg->{id_domain});

    return access_denied($c)
        unless $USER->id == $domain->id_owner || $USER->is_admin;

    my $hardware = delete $arg->{hardware} or die "Missing hardware name";
    my $index = delete $arg->{index};
    my $data = delete $arg->{data};

    die "Unknown fields in request ".Dumper($arg) if keys %{$arg};
Francesc Guasch's avatar
Francesc Guasch committed
663
    return $c->render(json => { req => Ravada::Request->change_hardware(
Francesc Guasch's avatar
Francesc Guasch committed
664
665
666
667
                id_domain => $domain->id
                ,hardware => $hardware
                ,index => $index
                ,data => $data
Francesc Guasch's avatar
Francesc Guasch committed
668
669
670
671
672
                ,uid => $USER->id
            )
    });
};

673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
get '/machine/list_access/(#id_domain)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);

    my @access0 = $domain->list_access();
    my $default = {};
    my @access;
    for my $access (@access0) {
        if ($access->{value} eq '*') {
            $default = $access;
            next;
        }
        push @access,($access);
    }

    return $c->render(json => {list => \@access, default => $default} );
};

get '/machine/check_access/(#id_domain)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);

    my %client;
    for my $name (@{$c->req->headers->names}) {
        $client{$name}= $c->req->headers->header($name);
    }
    return $c->render( json => { ok => $domain->access_allowed(client => \%client)});
};

get '/machine/delete_access/(#id_domain)/(#id_access)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);
    $domain->delete_access($c->stash('id_access'));

    # delete default if it is the only one left
    my @access = $domain->list_access();
    if (scalar @access == 1 && $access[0]->{value} eq '*') {
        $domain->delete_access($access[0]->{id});
    }

    return $c->render(json => { ok => 1 });

};

get '/machine/move_access/(#id_domain)/(#id_access)/(#position)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);

    $domain->move_access($c->stash('id_access'),$c->stash('position'));

    return $c->render(json => { ok => 1 });

};

743
744
745
746
747
748
749
get '/node/exists/#name' => sub {
    my $c = shift;
    my $name = $c->stash('name');

    return $c->render(json => $RAVADA->node_exists($name));

};
750
get '/machine/rename/#id/#value' => sub {
751
    my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
752
    return access_denied($c)       if !$USER->can_manage_machine($c->stash('id'));
753
754
755
    return rename_machine($c);
};

756
757
any '/machine/copy' => sub {
    my $c = shift;
758
    return access_denied($c)    if !$USER -> can_clone_all();
759
760
761
    return copy_machine($c);
};

762
763
764
765
766
767
get '/machine/public/#id' => sub {
    my $c = shift;
    return machine_is_public($c);
};

get '/machine/public/#id/#value' => sub {
768
769
770
771
    my $c = shift;
    return machine_is_public($c);
};

Francesc Guasch's avatar
Francesc Guasch committed
772
get '/machine/set/#id/#field/#value' => sub {
773
774
    my %privileged = map { $_ => 1 } qw(timeout id_owner);

Francesc Guasch's avatar
Francesc Guasch committed
775
776
777
778
779
780
    my $c = shift;
    my $id = $c->stash('id');
    my $field = $c->stash('field');
    my $value = $c->stash('value');

    return access_denied($c)       if !$USER->can_manage_machine($c->stash('id'));
781
    return access_denied($c) if $privileged{$field} && !$USER->is_admin;
Francesc Guasch's avatar
Francesc Guasch committed
782
783

    my $domain = Ravada::Front::Domain->open($id) or die "Unkown domain $id";
Francesc Guasch's avatar
Francesc Guasch committed
784
785
    $USER->send_message("Setting $field to $value in ".$domain->name)
        if $domain->_data($field) ne $value;
Francesc Guasch's avatar
Francesc Guasch committed
786
787
788
    return $c->render(json => { $field => $domain->_data($field, $value)});
};

789
790
791
792
793
794
795
796
797
798
get '/machine/autostart/#id/#value' => sub {
    my $c = shift;
    my $req = Ravada::Request->domain_autostart(
        id_domain => $c->stash('id')
           ,value => $c->stash('value')
             ,uid => $USER->id
    );
    return $c->render(json => { request => $req->id});
};

799
get '/machine/display/(:id).vv' => sub {
800
801
802
803
804
805
806
807
808
809
810
811
    my $c = shift;

    my $id = $c->stash('id');

    my $domain = $RAVADA->search_domain_by_id($id);
    return $c->render(text => "unknown machine id=$id") if !$id;

    return access_denied($c)
        if $USER->id ne $domain->id_owner
        && !$USER->is_admin;

    $c->res->headers->content_type('application/x-virt-viewer');
812
813
        $c->res->headers->content_disposition(
        "inline;filename=".$domain->id.".vv");
814

815
    return $c->render(data => $domain->display_file($USER), format => 'vv');
816
817
};

Francesc Guasch's avatar
Francesc Guasch committed
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
get '/machine/display-tls/(:id)-tls.vv' => sub {
    my $c = shift;

    my $id = $c->stash('id');

    my $domain = $RAVADA->search_domain_by_id($id);
    return $c->render(text => "unknown machine id=$id") if !$id;

    return access_denied($c)
        if $USER->id ne $domain->id_owner
        && !$USER->is_admin;

    $c->res->headers->content_type('application/x-virt-viewer');
        $c->res->headers->content_disposition(
        "inline;filename=".$domain->id."-tls.vv");

    return $c->render(data => $domain->display_file_tls($USER), format => 'vv');
};

Francesc Guasch's avatar
Francesc Guasch committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
# Network ##########################################################3

get '/network/interfaces/(:vm_type)/(:type)' => sub {
    my $c = shift;

    my $vm_type = $c->stash('vm_type');
    my $type = $c->stash('type');

    return $c->render( json => $RAVADA->list_network_interfaces(
               user => $USER
              ,type => $type
           ,vm_type => $vm_type
       )
    );
};
Francesc Guasch's avatar
Francesc Guasch committed
852

853
854
# Users ##########################################################3

Laura Figuerola's avatar
Laura Figuerola committed
855
856
##add user

857
any '/users/register' => sub {
858

Laura Figuerola's avatar
Laura Figuerola committed
859
       my $c = shift;
860
       return access_denied($c) if !$USER->is_admin();
Laura Figuerola's avatar
Laura Figuerola committed
861
862
863
       return register($c);
};

Francesc Guasch's avatar
Francesc Guasch committed
864
865
any '/admin/user/(:id).(:type)' => sub {
    my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
866
    return access_denied($c) if !$USER->can_manage_users() && !$USER->can_grant();
Francesc Guasch's avatar
Francesc Guasch committed
867
868
869
870
871
872

    my $user = Ravada::Auth::SQL->search_by_id($c->stash('id'));

    return $c->render(text => "Unknown user id: ".$c->stash('id'))
        if !$user;

Francesc Guasch's avatar
Francesc Guasch committed
873
874
875
876
877
    if ($c->param('make_admin')) {
        $USER->make_admin($c->stash('id'))  if $c->param('is_admin');
        $USER->remove_admin($c->stash('id'))if !$c->param('is_admin');
        $user = Ravada::Auth::SQL->search_by_id($c->stash('id'));
    }
878
879
880
881
882
883
884
885
886
887
888
    if ($c->param('grant')) {
        return access_denied($c)    if !$USER->can_grant();
        my %grant;
        for my $param_name (@{$c->req->params->names}) {
            if ( $param_name =~ /^perm_(.*)/ ) {
                $grant{$1} = 1;
            } elsif ($param_name =~ /^off_perm_(.*)/) {
                $grant{$1} = 0 if !exists $grant{$1};
            }
        }
        for my $perm (keys %grant) {
889
            if ( $grant{$perm} ) {
890
891
892
893
894
895
                $USER->grant($user, $perm);
            } else {
                $USER->revoke($user, $perm);
            }
        }
    }
Francesc Guasch's avatar
Francesc Guasch committed
896
897
898
    $c->stash(user => $user);
    return $c->render(template => 'main/manage_user');
};
Laura Figuerola's avatar
Laura Figuerola committed
899

900
901
902
903
904
905
906
907
908
909
910
911
912
913
get '/list_ldap_attributes/(#cn)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $cn = $c->stash('cn');
    my $user;
    eval {
        ($user) = Ravada::Auth::LDAP::search_user($cn);
    };
    return $c->render(json => { error => $@ }) if $@;
    return $c->render(json => []) if !$user;

    $c->session(ldap_attributes_cn => $cn) if $user;
914
    return $c->render(json => {attributes => [sort $user->attributes]});
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
};

get '/count_ldap_entries/(#attribute)/(#value)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my @entries;
    eval {
        @entries = Ravada::Auth::LDAP::search_user(
            field => $c->stash('attribute')
            ,name => $c->stash('value')
            ,typesonly => 1
        );
    };
    @entries = [ 'too many' ] if $@ =~ /Sizelimit exceeded/;
    return $c->render(json => { entries => scalar @entries });
};

934
post '/machine/add_access/(#id_domain)' => sub {
935
936
937
938
939
940
941
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);

942
943
944
945
946
947
948
949
    my $args = decode_json($c->req->body);

    my $attribute = delete $args->{attribute};
    my $value = delete $args->{value};
    my $type = delete $args->{type};

    my $allowed = delete $args->{allowed};
    if (!defined $allowed || !$allowed || $allowed =~ /false|undefined/) {
950
        $allowed = 0;
951
952
    } else {
        $allowed= 1;
953
    }
954
955
    my $last = delete $args->{last};
    if (!defined $last || !$last || $last =~ /false|undefined/) {
956
        $last = 0;
957
958
    } else {
        $last = 1;
959
    }
960
961
962
963
964
965
966
967
968
969
    confess "Error: unknown args ".Dumper($args) if keys %$args;

    $domain->grant_access(type => $type
            , attribute => $attribute
            , allowed => $allowed
            , value => $value
            , last => $last
    );
    _fix_default_ldap_access($c, $domain, $allowed)
    if $type eq 'ldap';
970
971
972
973
974

    return $c->render(json => { ok => 1 });

};

975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
get '/add_ldap_access/(#id_domain)/(#attribute)/(#value)/(#allowed)/(#last)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);

    my $attribute = $c->stash('attribute');
    my $value = $c->stash('value');
    my $allowed = 1;
    if ($c->stash('allowed') eq 'false') {
        $allowed = 0;
    }
    my $last = 1;
    if ($c->stash('last') eq 'false') {
        $last = 0;
    }
    $last = 1 if !$allowed;

    eval { $domain->allow_ldap_access($attribute => $value, $allowed, $last ) };
    _fix_default_ldap_access($c,'ldap', $domain, $allowed) if !$@;
    return $c->render(json => { error => $@ }) if $@;
    return $c->render(json => { ok => 1 });

};

1002
sub _fix_default_ldap_access($c, $type, $domain, $allowed) {
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
    my @list = $domain->list_ldap_access();
    my $default_found;
    for ( @list ) {
        if ( $_->{value} eq '*' ) {
            $default_found = $_->{id};
        }
    }
    if ( $default_found ) {
        $domain->move_ldap_access($default_found, +1);
        return;
    }
    my $allowed_default = 0;
    $allowed_default = 1 if !$allowed;
1016
1017
1018
1019
1020
1021
    eval { $domain->grant_access(type => $type
            , attribute => 'DEFAULT'
            , value => '*'
            , last => $allowed_default
    ) };
    die $@ if $@;
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
}

get '/delete_ldap_access/(#id_domain)/(#id_access)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);

    $domain->delete_ldap_access($c->stash('id_access'));

1034
1035
1036
1037
1038
1039
    # delete default if it is the only one left
    my @ldap_access = $domain->list_ldap_access();
    if (scalar @ldap_access == 1 && $ldap_access[0]->{value} eq '*') {
        $domain->delete_ldap_access($ldap_access[0]->{id});
    }

1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
    return $c->render(json => { ok => 1 });
};

get '/list_ldap_access/(#id_domain)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);

    my @ldap_access = $domain->list_ldap_access();
    my $default = {};
1053
    if (scalar @ldap_access && $ldap_access[-1]->{value} eq '*') {
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
        $default = pop @ldap_access;
    }
    return $c->render(json => {list => \@ldap_access, default => $default} );
};

get '/move_ldap_access/(#id_domain)/(#id_access)/(#count)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);

    $domain->move_ldap_access($c->stash('id_access'), $c->stash('count'));

    return $c->render(json => { ok => 1});
};

1072
get '/set_ldap_access/(#id_domain)/(#id_access)/(#allowed)/(#last)' => sub {
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);

    my $allowed = $c->stash('allowed');
    if ($allowed =~ /false/ || !$allowed) {
        $allowed = 0;
    } else {
        $allowed = 1;
    }
1086
1087
1088
1089
1090
1091
1092
    my $last= $c->stash('last');
    if ($last=~ /false/ || !$last) {
        $last= 0;
    } else {
        $last= 1;
    }
    warn "last = $last";
1093

1094
    $domain->set_ldap_access($c->stash('id_access'), $allowed, $last);
1095
1096
    return $c->render(json => { ok => 1});
};
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122

get '/machine/set_access/(#id_domain)/(#id_access)/(#allowed)/(#last)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);

    my $allowed = $c->stash('allowed');
    if ($allowed =~ /false|undefined/ || !$allowed) {
        $allowed = 0;
    } else {
        $allowed = 1;
    }
    my $last= $c->stash('last');
    if ($last=~ /false|undefined/ || !$last) {
        $last= 0;
    } else {
        $last= 1;
    }

    $domain->set_access($c->stash('id_access'), $allowed, $last);
    return $c->render(json => { ok => 1});
};

1123
##############################################
Laura Figuerola's avatar
Laura Figuerola committed
1124

Francesc Guasch's avatar
Francesc Guasch committed
1125
1126
post '/request/(:name)/' => sub {
    my $c = shift;
1127
    my $name = $c->stash('name');
Francesc Guasch's avatar
Francesc Guasch committed
1128
1129
1130

    my $args = decode_json($c->req->body);

1131
1132
1133
1134
1135
1136
1137
1138
    for (qw(remote_ip uid)) {
        confess "Error: $_ should not be provided".Dumper($args)
        if exists $args->{$_};
    }
    if ($name eq 'start_clones') {
        $args->{remote_ip} = _remote_ip($c);
    }

1139
    app->log->info($USER->name." requesting $name ".encode_json($args)) if $CONFIG_FRONT->{log}->{log};
1140
1141
1142
1143
1144
1145
1146
1147
1148
    my $req;
    eval {
        $req = Ravada::Request->new_request(
            $name
            ,uid => $USER->id
            ,%$args
        );
    };
    return $c->render(json => { ok => 0, error => $@ }) if !$req;
1149
    return $c->render(json => { ok => 1, request => $req->id });
Francesc Guasch's avatar
Francesc Guasch committed
1150
};
1151

1152
get '/request/(:id).(:type)' => sub {
1153
    my $c = shift;
1154
    my $id = $c->stash('id');
1155

Francesc Guasch's avatar
Francesc Guasch committed
1156
1157
1158
    if (!$USER) {
        $USER = _get_anonymous_user($c) or access_denied($c);
    }
1159
1160
1161
1162
    if ($c->stash('type') eq 'json') {
        my $request = Ravada::Request->open($id);
        return $c->render(json => $request->info($USER));
    }
1163
1164
1165
    return _show_request($c,$id);
};

1166
1167
1168
1169
1170
1171
1172
1173
1174
get '/anonymous/request/(:id).(:type)' => sub {
    my $c = shift;
    my $id = $c->stash('id');

    $USER = _anonymous_user($c);

    return _show_request($c,$id);
};

Francesc Guasch's avatar
Francesc Guasch committed
1175
1176
get '/requests.json' => sub {
    my $c = shift;
elodin1's avatar
elodin1 committed
1177
1178
    return access_denied($c) unless _logged_in($c)
        && $USER->is_admin;
Francesc Guasch's avatar
Francesc Guasch committed
1179
1180
1181
    return list_requests($c);
};

joansp's avatar
joansp committed
1182
get '/messages.json' => sub {
joansp's avatar
joansp committed
1183
1184
1185
1186
1187
1188
    my $c = shift;


    return $c->render( json => [$USER->messages()] );
};

1189
get '/unshown_messages.json' => sub {
1190
1191
1192
1193
    my $c = shift;

    return $c->redirect_to('/login') if !_logged_in($c);

1194
    return $c->render( json => [$USER->unshown_messages()] );
Antonio's avatar
Antonio committed
1195
1196
1197
};


joansp's avatar
joansp committed
1198
1199
1200
get '/messages/read/all.html' => sub {
    my $c = shift;
    $USER->mark_all_messages_read;
1201
    return $c->render(inline => "1");
1202
1203
};

1204
get '/messages/read/(#id).json' => sub {
1205
    my $c = shift;
1206
    my $id = $c->stash('id');
joansp's avatar
joansp committed
1207
    $USER->mark_message_read($id);
1208
    return $c->render(inline => "1");
joansp's avatar
joansp committed
1209
};
1210

amesones's avatar
amesones committed
1211
get '/messages/unread/(#id).json' => sub {
joansp's avatar
joansp committed
1212
    my $c = shift;
1213
    my $id = $c->stash('id');
joansp's avatar
joansp committed
1214
    $USER->mark_message_unread($id);
1215
    return $c->render(inline => "1");
joansp's avatar
joansp committed
1216
1217
};

1218
get '/messages/view/(#id).html' => sub {
1219
    my $c = shift;
1220
1221
    my $id = $c->stash('id');
    return $c->render( json => $USER->show_message($id) );
1222
1223
};

1224
1225
1226
1227
1228
1229
any '/ng-templates/(#template).html' => sub {
  my $c = shift;
  my $id = $c->stash('template');
  return $c->render(template => 'ng-templates/'.$id);
};

1230
any '/about' => sub {
fv3rdugo's avatar
fv3rdugo committed
1231
1232
    my $c = shift;

amesones's avatar
amesones committed
1233
    $c->render(template => 'main/about');
fv3rdugo's avatar
fv3rdugo committed
1234
1235
};

1236

1237
1238
1239
any '/requirements' => sub {
    my $c = shift;

amesones's avatar
amesones committed
1240
    $c->render(template => 'main/requirements');
1241
1242
};

fv3rdugo's avatar
fv3rdugo committed
1243
1244
1245
1246
1247
1248
any '/admin/monitoring' => sub {
    my $c = shift;

    $c->render(template => 'main/monitoring');
};

1249
any '/auto_view/(#value)/' => sub {
1250
1251
1252
    my $c = shift;
    my $value = $c->stash('value');
    if ($value =~ /toggle/i) {
1253
        $value = $c->session('auto_view');
1254
1255
1256
1257
1258
1259
        if ($value) {
            $value = 0;
        } else {
            $value = 1;
        }
    }
1260
1261
    $c->session('auto_view' => $value);
    return $c->render(json => {auto_view => $c->session('auto_view') });
1262
1263
};

1264
get '/auto_view' => sub {
1265
    my $c = shift;
1266
    return $c->render(json => {auto_view => $c->session('auto_view') });
1267
1268
};

Francesc Guasch's avatar
Francesc Guasch committed
1269
get '/machine/hardware/remove/(#id_domain)/(#hardware)/(#index)' => sub {
1270
1271
1272
    my $c = shift;
    my $hardware = $c->stash('hardware');
    my $index = $c->stash('index');
Francesc Guasch's avatar
Francesc Guasch committed
1273