Commit 57d8f271 authored by Joel Alarcón's avatar Joel Alarcón Committed by Francesc Guasch
Browse files

688 owner machine (#752)

* 688_owner_machine: 688_owner_machine: feat: The owner of a domain can be changed via the options tab. issue #688

* 688_owner_machine: feat: user name's domain owner updates after changing it in the options tab. issue #688

* 688_owner_machine: fix: admin can access any settings. issue #688.

* 688_owner_machine: feat: Test for change_owner added. issue #688
parent 1543657c
......@@ -908,7 +908,7 @@ sub _connect_dbh {
my $host = $CONFIG->{db}->{host};
my $data_source = "DBI:$driver:$db";
$data_source = "DBI:$driver:database=$db;host=$host"
$data_source = "DBI:$driver:database=$db;host=$host"
if $host && $host ne 'localhost';
my $con;
......@@ -961,7 +961,7 @@ sub _init_config {
$CONFIG->{vm} = [] if !$CONFIG->{vm};
$LIMIT_PROCESS = $CONFIG->{limit_process}
$LIMIT_PROCESS = $CONFIG->{limit_process}
if $CONFIG->{limit_process} && $CONFIG->{limit_process}>1;
# $CONNECTOR = ( $connector or _connect_dbh());
......@@ -1516,7 +1516,7 @@ sub process_requests {
while (my ($id_request,$id_domain)= $sth->fetchrow) {
my $req = Ravada::Request->open($id_request);
if ( ($long_commands &&
if ( ($long_commands &&
(!$short_commands && !$LONG_COMMAND{$req->command}))
||(!$long_commands && $LONG_COMMAND{$req->command})
) {
......@@ -1671,7 +1671,7 @@ sub _execute {
my $pid = fork();
die "I can't fork" if !defined $pid;
if ( $pid == 0 ) {
$self->_do_execute_command($sub, $request)
$self->_do_execute_command($sub, $request)
} else {
$self->_add_pid($pid, $request->id);
}
......@@ -1699,7 +1699,7 @@ sub _do_execute_command {
};
my $err = ( $@ or '');
$request->error($err);
$request->status('done')
$request->status('done')
if $request->status() ne 'done'
&& $request->status() !~ /^retry/i;
exit;
......@@ -1739,8 +1739,8 @@ sub _cmd_screenshot {
sub _cmd_copy_screenshot {
my $self = shift;
my $request = shift;
my $id_domain = $request->args('id_domain');
my $id_domain = $request->args('id_domain');
my $domain = $self->search_domain_by_id($id_domain);
my $id_base = $domain->id_base;
......@@ -1751,11 +1751,11 @@ sub _cmd_copy_screenshot {
} else {
my $base_screenshot = $domain->file_screenshot();
$base_screenshot =~ s{(.*)/\d+\.(\w+)}{$1/$id_base.$2};
$base->_post_screenshot($base_screenshot);
$base->_post_screenshot($base_screenshot);
copy($domain->file_screenshot, $base_screenshot);
copy($domain->file_screenshot, $base_screenshot);
}
}
......@@ -2166,6 +2166,17 @@ sub _cmd_refresh_storage($self, $request=undef) {
$vm->refresh_storage();
}
sub _cmd_change_owner($self, $request) {
my $uid = $request->args('uid');
my $id_domain = $request->args('id_domain');
my $sth = $CONNECTOR->dbh->prepare(
"UPDATE domains SET id_owner=?"
." WHERE id=?"
);
$sth->execute($uid, $id_domain);
$sth->finish;
}
sub _cmd_domain_autostart($self, $request ) {
my $uid = $request->args('uid');
my $id_domain = $request->args('id_domain') or die "ERROR: Missing id_domain";
......@@ -2306,6 +2317,7 @@ sub _req_method {
,force_shutdown => \&_cmd_force_shutdown
,refresh_storage => \&_cmd_refresh_storage
,domain_autostart=> \&_cmd_domain_autostart
,change_owner => \&_cmd_change_owner
);
return $methods{$cmd};
......@@ -2382,7 +2394,7 @@ sub import_domain {
my $vm = $self->search_vm($vm_name) or die "ERROR: unknown VM '$vm_name'";
my $user = Ravada::Auth::SQL->new(name => $user_name);
die "ERROR: unknown user '$user_name'" if !$user || !$user->id;
my $domain;
eval { $domain = $self->search_domain($name) };
die "ERROR: Domain '$name' already in RVD" if $domain;
......
......@@ -64,6 +64,7 @@ our %VALID_ARG = (
,download => {uid => 2, id_iso => 1, id_vm => 2, delay => 2, verbose => 2}
,refresh_storage => { id_vm => 2 }
,clone => { uid => 1, id_domain => 1, name => 1, memory => 2 }
,change_owner => {uid => 1, id_domain => 1}
);
our %CMD_SEND_MESSAGE = map { $_ => 1 }
......@@ -718,7 +719,7 @@ sub copy_screenshot {
,id_domain => $args->{id_domain}
,args => $args
);
}
=head2 open_iptables
......@@ -894,6 +895,32 @@ sub clone {
);
}
=head2 change_owner
Changes the owner of a machine
my $req = Ravada::Request->change_owner(
,uid => $user->id
id_domain => $domain->id
);
=cut
sub change_owner {
my $proto = shift;
my $class = ref($proto) || $proto;
my $args = _check_args('change_owner', @_ );
my $self = {};
bless($self,$class);
return _new_request($self
, command => 'change_owner'
, args =>$args
);
}
=head2 domain_autostart
Sets the autostart flag on a domain
......
......@@ -364,7 +364,7 @@ get '/machine/view/(:id).(:type)' => sub {
};
get '/machine/clone/(:id).(:type)' => sub {
my $c = shift;
my $c = shift;
return access_denied($c) if !$USER->can_clone();
return clone_machine($c);
};
......@@ -1409,10 +1409,16 @@ sub settings_machine {
my $c = shift;
my ($domain) = _search_requested_machine($c);
return access_denied($c) if !$domain;
return access_denied($c) if !$USER->can_manage_machine($domain->id);
return access_denied($c) if !($USER->can_manage_machine($domain->id) || $USER->is_admin);
$c->stash(domain => $domain);
$c->stash(USER => $USER);
$c->stash(list_users => $RAVADA->list_users);
my $actual_owner = $domain->id_owner;
if ($c->param("new_owner") && $actual_owner != $c->param("new_owner")) {
my $req_change = Ravada::Request->change_owner(uid => $c->param("new_owner"), id_domain => $domain->id);
$actual_owner = $c->param("new_owner");
}
my $req = Ravada::Request->shutdown_domain(id_domain => $domain->id, uid => $USER->id)
if $c->param('shutdown') && $domain->is_active;
......@@ -1460,7 +1466,8 @@ sub settings_machine {
}
return $c->render(template => 'main/settings_machine'
, list_clones => [map { $_->{name} } $domain->clones]
, action => $c->req->url->to_abs->path);
, action => $c->req->url->to_abs->path
, actual_owner => $actual_owner);
}
sub _enable_buttons {
......@@ -1650,7 +1657,7 @@ sub copy_machine {
my $c = shift;
return login($c) if !_logged_in($c);
my $id_base= $c->param('id_base');
......
......@@ -27,6 +27,24 @@ ok($USER);
##########################################################
sub test_change_owner {
my $vm_name = shift;
my $USER2 = create_user("foo2","bar2", 1);
my $name = new_domain_name();
my $id_iso = search_id_iso('Debian');
diag("Testing change owner");
my $domain = rvd_back->search_vm($vm_name)->create_domain(
name => $name
,id_iso => $id_iso
,id_owner => $USER->id
,iso_file => '<NONE>'
);
is($USER->id, $domain->id_owner) or return;
my $req = Ravada::Request->change_owner(uid => $USER2->id, id_domain => $domain->id);
sleep(3); #we make sure that the sql has updated.
is($USER2->id, $domain->id_owner) or return;
}
sub test_vm_connect {
my $vm_name = shift;
......@@ -291,7 +309,7 @@ sub test_screenshot_file {
sub test_change_interface {
my ($vm_name) = @_;
return if $vm_name !~ /kvm/i;
my $domain = test_create_domain($vm_name);
set_bogus_ip($domain);
......@@ -308,7 +326,7 @@ sub set_bogus_ip {
=> $domain->domain->get_xml_description) ;
my @graphics = $doc->findnodes('/domain/devices/graphics');
is(scalar @graphics,1) or return;
my $bogus_ip = '999.999.999.999';
$graphics[0]->setAttribute('listen' => $bogus_ip);
......@@ -316,7 +334,7 @@ sub set_bogus_ip {
for my $child ( $graphics[0]->childNodes()) {
$listen = $child if $child->getName() eq 'listen';
}
ok($listen,"Expecting child node listen , got :'".($listen or ''))
ok($listen,"Expecting child node listen , got :'".($listen or ''))
or return;
$listen->setAttribute('address' => $bogus_ip);
......@@ -404,6 +422,7 @@ for my $vm_name (qw( Void KVM )) {
skip $msg,10 if !$vm;
use_ok($CLASS) or next;
test_change_owner($vm_name);
test_vm_connect($vm_name);
test_search_vm($vm_name);
......
......@@ -44,6 +44,21 @@
Clones created from this machine will be removed on shutdown.
</div>
</div>
% if ($USER->is_admin){
<div class="row">
<div class="col-md-4">
<label class="control-label" for="new_owner">Owner</label>
</div>
<div class="col-md-2">
<select id="new_owner" name="new_owner" >
% for my $user (@$list_users) {
<option <%= $user->{id} eq $actual_owner ? "selected" : "" %> value="<%= $user->{id} %>"><%= $user->{name} %></option>
% }
</select>
</div>
<div class="col-md-6"><%=l 'Select who the owner of the machine is.' %></div>
</div>
% }
<input type="submit" name="submitbtn">
<button type="reset" >Cancel</button>
</form>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment