Commit 64b1b920 authored by Joel Alarcón's avatar Joel Alarcón Committed by Francesc Guasch
Browse files

feat: owner machine (#771)

* 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

* fix(frontend): more readable settings machine

It still has room to improve: better help placement, aesthetics, labels

issue #688

* 688_owner_machine: fix: test_change_owner now doesn't require rvd_back. issue #688

* 688_owner_machine: 688_owner_machine: text strings added at en.po for translation. issue #688.

*  fix: owner's bad name error fixed. issue #688

* fix: repeated clausure in en.po. issue #688
parent dc6f553e
......@@ -953,7 +953,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;
......@@ -1006,7 +1006,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());
......@@ -1573,7 +1573,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})
) {
......@@ -1728,7 +1728,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);
}
......@@ -1756,7 +1756,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;
......@@ -1796,8 +1796,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;
......@@ -1808,11 +1808,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);
}
}
......@@ -2228,6 +2228,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";
......@@ -2376,6 +2387,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};
......@@ -2452,7 +2464,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;
......
......@@ -689,3 +689,12 @@ msgstr ""
msgid "This information will be available to the users"
msgstr ""
msgid "Volatile Clones"
msgstr ""
msgid "Owner"
msgstr ""
msgid "Change the owner of the machine"
msgstr ""
......@@ -66,6 +66,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 }
......@@ -744,7 +745,7 @@ sub copy_screenshot {
,id_domain => $args->{id_domain}
,args => $args
);
}
=head2 open_iptables
......@@ -920,6 +921,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
......
......@@ -374,7 +374,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);
};
......@@ -1315,10 +1315,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;
......@@ -1366,7 +1372,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 {
......@@ -1561,7 +1568,7 @@ sub copy_machine {
my $c = shift;
return login($c) if !_logged_in($c);
my $id_base= $c->param('id_base');
......
......@@ -27,6 +27,26 @@ 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);
rvd_back->_process_requests_dont_fork();
$domain = Ravada::Domain->open($domain->id);
is($USER2->id, $domain->id_owner) or return;
}
sub test_vm_connect {
my $vm_name = shift;
......@@ -291,7 +311,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 +328,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 +336,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 +424,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);
......
......@@ -3,7 +3,7 @@
<form class="form-inline" action="<%= $action %>" method="post">
<div class="row">
<div class="col-md-4">
<div class="col-md-2">
<label class="control-label" for="ram"><%=l 'Memory' %></label>
</div>
<div class="col-md-2">
......@@ -15,8 +15,9 @@
<%=l 'MB available' %>.
</div>
</div>
% if ($USER->is_admin){
<div class="row">
<div class="col-md-4">
<div class="col-md-2">
<label class="control-label" for="timeout">Timeout</label>
</div>
<div class="col-md-2">
......@@ -28,9 +29,11 @@
</div>
<div class="col-md-6"><%=l 'The machine will shutdown after these minutes' %></div>
</div>
% }
% if ($USER->is_admin){
<div class="row">
<div class="col-md-4">
<label class="control-label" for="volatile_clones">Enable Volatile Clones</label>
<div class="col-md-2">
<label class="control-label" for="volatile_clones"><%=l 'Volatile Clones' %></label>
</div>
<div class="col-md-2">
<input type="checkbox" name="volatile_clones"
......@@ -41,11 +44,31 @@
value="1">
</div>
<div class="col-md-6">
Clones created from this machine will be removed on shutdown.
<%=l 'Clones created from this machine will be removed on shutdown.' %>
</div>
</div>
% }
% if ($USER->is_admin){
<div class="row">
<div class="col-md-2">
<label class="control-label" for="new_owner"><%=l '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 'Change the owner of the machine' %></div>
</div>
% }
<div class="row">
<div class="col-md-4">
<input type="submit" name="submitbtn">
<button type="reset" >Cancel</button>
<button type="reset" ><%=l 'Cancel' %></button>
</div>
</div>
</form>
</div><!-- of form-group -->
</div><!-- of panel-body -->
Supports Markdown
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