Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: share machine #2021

Merged
merged 15 commits into from
Feb 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip: allow also shutdown
frankiejol committed Jun 22, 2023
commit a99b0a17b6a8d1ed2a69898c4216d727be2e3b5f
4 changes: 4 additions & 0 deletions lib/Ravada/Auth/SQL.pm
Original file line number Diff line number Diff line change
@@ -682,6 +682,8 @@ sub can_do_domain($self, $grant, $domain) {
my %valid_grant = map { $_ => 1 } qw(change_settings shutdown reboot rename expose_ports);
confess "Invalid grant here '$grant'" if !$valid_grant{$grant};

return 1 if $grant eq 'shutdown' && $self->can_shutdown_machine($domain);

return 0 if !$self->can_do($grant) && !$self->_domain_id_base($domain);

return 1 if $self->can_do("${grant}_all");
@@ -1143,6 +1145,8 @@ sub can_shutdown_machine($self, $domain) {

return 1 if $self->id == $domain->id_owner;

return 1 if $self->_machine_shared($domain->id);

if ($domain->id_base && $self->can_shutdown_clone()) {
my $base = Ravada::Front::Domain->open($domain->id_base);
return 1 if $base->id_owner == $self->id;
2 changes: 1 addition & 1 deletion lib/Ravada/Front.pm
Original file line number Diff line number Diff line change
@@ -212,9 +212,9 @@ sub _get_clone_info($user, $base, $clone = Ravada::Front::Domain->open($base->{i
$c->{is_locked} = $clone->is_locked;
$c->{description} = ( $clone->_data('description')
or $base->{description});
$c->{can_remove} = 0;

$c->{can_remove} = ( $user->can_remove() && $user->id == $clone->_data('id_owner'));
$c->{can_remove} = 0 if !$c->{can_remove};

if ($clone->is_active && !$clone->is_locked
&& $user->can_screenshot) {
4 changes: 3 additions & 1 deletion script/rvd_front
Original file line number Diff line number Diff line change
@@ -843,7 +843,9 @@ get '/machine/view/(:id).(:type)' => sub {

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

return view_machine($c);
};
51 changes: 51 additions & 0 deletions t/mojo/10_login.t
Original file line number Diff line number Diff line change
@@ -973,6 +973,55 @@ sub test_clone_same_name($t, $base) {

}

sub _create_clone($t, $base) {
mojo_check_login($t);
wait_request();

$base->is_public(1);

my ($name, $pass) = (new_domain_name(),"$$ $$");
my $user = _create_user($name, $pass);
login($name, $pass);

$t->get_ok("/machine/clone/".$base->id.".html")
->status_is(200);
like($t->tx->res->code(),qr/^(200|302)$/)
or die $t->tx->res->body;

wait_request(debug => 1, check_error => 1, background => 1, timeout => 120);
mojo_check_login($t, $name, $pass);

my ($clone) = grep { $_->{id_owner} == $user->id } $base->clones;

return $clone;

}

sub _create_user($name, $pass) {
my $user = Ravada::Auth::SQL->new(name => $name);
$user->remove();
return create_user($name, $pass);
}

sub test_grant_access($t, $base) {
my $clone0 = _create_clone($t, $base);

my ($name, $pass) = (new_domain_name(),"$$ $$");
my $user2 = _create_user($name, $pass);

my $clone = Ravada::Front::Domain->open($clone0->{id});
$clone->share($user2);
login($name, $pass);

$t->get_ok("/machine/view/".$clone->id.".html")->status_is(200);

like($t->tx->res->code(),qr/^(200|302)$/)
or die $t->tx->res->body;

$t->get_ok("/machine/shutdown/".$clone->id.".json")->status_is(200);

}

########################################################################################

$ENV{MOJO_MODE} = 'development';
@@ -1028,6 +1077,8 @@ for my $vm_name (reverse @{rvd_front->list_vm_types} ) {

test_clone_same_name($t, $base0);

test_grant_access($t, $base0);

if ($vm_name eq 'KVM') {
test_new_machine_default($t, $vm_name);
test_new_machine_default($t, $vm_name, 1); # with empty iso file
19 changes: 18 additions & 1 deletion t/user/35_share.t
Original file line number Diff line number Diff line change
@@ -43,18 +43,35 @@ sub test_share($vm) {

$clone->share($user2);

is($user2->can_shutdown($clone),1);

my $req2 = Ravada::Request->start_domain(
uid => $user2->id
,id_domain => $clone->id
);
wait_request();
is($req2->status,'done');
is($req2->error,'');


$list_bases_u2 = rvd_front->list_machines_user($user2);
($clone_user2) = grep { $_->{name } eq $base->name } @$list_bases_u2;
is(scalar(@{$clone_user2->{list_clones}}),1);
is($clone_user2->{list_clones}->[0]->{can_remove},0);
is($clone_user2->{list_clones}->[0]->{can_shutdown},1);

is($user2->can_view_all,undef);
is($user2->can_start_machine($clone->id),1) or exit;

my $req2 = Ravada::Request->start_domain(
my $req3 = Ravada::Request->start_domain(
uid => $user2->id
,id_domain => $clone->id
);
wait_request();
is($req3->status,'done');
is($req3->error,'');


}

##############################################################