diff --git a/lib/GLPI/Agent/HTTP/Server/ToolBox/Credentials.pm b/lib/GLPI/Agent/HTTP/Server/ToolBox/Credentials.pm index 4b3fd1311..662cfeb9e 100644 --- a/lib/GLPI/Agent/HTTP/Server/ToolBox/Credentials.pm +++ b/lib/GLPI/Agent/HTTP/Server/ToolBox/Credentials.pm @@ -71,6 +71,8 @@ sub update_template_hash { return unless $hash; my $yaml = $self->yaml() || {}; + my $credentials = $self->yaml('credentials') || {}; + my $yaml_config = $self->yaml('configuration') || {}; # Update Text::Template HASH but protect some values by encoding html entities foreach my $base (qw(credentials)) { @@ -88,6 +90,56 @@ sub update_template_hash { } } $hash->{title} = "Credentials"; + + # Don't include listing datas when editing + return if $self->edit(); + + $hash->{columns} = [ + [ name => "Credentials name" ], + [ type => "Type" ], + [ config => "Configuration" ], + [ description => "Description" ] + ]; + $hash->{order} = $self->get_from_session('credentials_order') || "ascend"; + my $asc = $hash->{order} eq 'ascend'; + my $ordering = $hash->{ordering_column} = $self->get_from_session('credentials_ordering_column') || 'name'; + my %type_order = qw( unknown 0 esx 1 snmp_v1 2 snmp_v2c 3 snmp_v3 4 ssh 5 winrm 6); + $hash->{credentials_order} = [ + sort { + my ($A, $B) = $asc ? ( $a, $b ) : ( $b, $a ); + if ($ordering eq 'config') { + ($credentials->{$A}->{community} || $credentials->{$A}->{username} || '') cmp ($credentials->{$B}->{community} || $credentials->{$B}->{username} || '') + || $A cmp $B + } elsif ($ordering eq 'type') { + my ($typeA, $typeB) = map { + if (!$credentials->{$_}->{type} || $credentials->{$_}->{type} eq 'snmp') { + $credentials->{$_}->{snmpversion} ? 'snmp_'.$credentials->{$_}->{snmpversion} : 'unknown'; + } else { + $credentials->{$_}->{type}; + } + } ($A, $B); + ($type_order{$typeA} || 0) <=> ($type_order{$typeB} || 0) + || $A cmp $B + } else { + ($credentials->{$A}->{$ordering} || '') cmp ($credentials->{$B}->{$ordering} || '') + || $A cmp $B + } + } keys(%{$credentials}) + ]; + my @display_options = grep { /^\d+$/ } split(/[|]/,$yaml_config->{display_options} || '30|0|5|10|20|40|50|100|500'); + $hash->{display_options} = [ sort { $a <=> $b } keys(%{{map { $_ => 1 } @display_options}}) ]; + my $display = $self->get_from_session('display'); + $hash->{display} = length($display) ? $display : $display_options[0]; + $hash->{list_count} = scalar(@{$hash->{credentials_order}}); + $self->delete_in_session('credentials_start') unless $hash->{display}; + $hash->{start} = $self->get_from_session('credentials_start') || 1; + $hash->{start} = $hash->{list_count} if $hash->{start} > $hash->{list_count}; + $hash->{page} = $hash->{display} ? int(($hash->{start}-1)/$hash->{display})+1 : 1; + $hash->{pages} = $hash->{display} ? int(($hash->{list_count}-1)/$hash->{display})+1 : 1; + $hash->{start} = $hash->{display} ? $hash->{start} - $hash->{start}%$hash->{display} : 0; + # Handle case we are indexing the last element + $hash->{start} -= $hash->{display} if $hash->{start} == $hash->{list_count}; + $hash->{start} = 0 if $hash->{start} < 0; } sub _submit_add { @@ -176,40 +228,12 @@ sub _submit_add { $self->need_save(credentials); delete $form->{empty}; } elsif (!$name) { - $self->errors("New credential: Can't create entry without name"); + $self->errors("New credential: Can't create entry without name") if $form->{empty}; + # We still should return an empty add form + $form->{empty} = 1; } } -sub _submit_add_v1_v2c { - my ($self, $form) = @_; - - return unless $form; - - # We should return an empty add form with name edition allowed - $form->{empty} = 1; - $form->{snmpversion} = "v2c"; -} - -sub _submit_add_v3 { - my ($self, $form) = @_; - - return unless $form; - - # We should return an empty add form with name edition allowed - $form->{empty} = 1; - $form->{snmpversion} = "v3"; -} - -sub _submit_add_remotecred { - my ($self, $form) = @_; - - return unless $form; - - # We should return an empty add form with name edition allowed - $form->{empty} = 1; - $form->{type} = "ssh"; -} - sub _submit_update { my ($self, $form, $credentials) = @_; @@ -329,55 +353,13 @@ sub _submit_update { } } -sub _submit_delete_v1_v2c { - my ($self, $form, $credentials) = @_; - - return unless $form && $credentials; - - my @delete = map { m{^checkbox-v1-v2c/(.*)$} } - grep { /^checkbox-v1-v2c\// && $form->{$_} eq 'on' } keys(%{$form}); - - return $self->errors("Delete credential: No credential selected") - unless @delete; - - my $used = $self->_used_credentials(\@delete); - return $self->errors("Delete credential: Can't delete used credential: ".$used) - if $used; - - foreach my $name (@delete) { - delete $credentials->{$name}; - $self->need_save(credentials); - } -} - -sub _submit_delete_v3 { +sub _submit_delete { my ($self, $form, $credentials) = @_; return unless $form && $credentials; - my @delete = map { m{^checkbox-v3/(.*)$} } - grep { /^checkbox-v3\// && $form->{$_} eq 'on' } keys(%{$form}); - - return $self->errors("Delete credential: No credential selected") - unless @delete; - - my $used = $self->_used_credentials(\@delete); - return $self->errors("Delete credential: Can't delete used credential: ".$used) - if $used; - - foreach my $name (@delete) { - delete $credentials->{$name}; - $self->need_save(credentials); - } -} - -sub _submit_delete_remotes { - my ($self, $form, $credentials) = @_; - - return unless $form && $credentials; - - my @delete = map { m{^checkbox-remotes/(.*)$} } - grep { /^checkbox-remotes\// && $form->{$_} eq 'on' } keys(%{$form}); + my @delete = map { m{^checkbox/(.*)$} } + grep { /^checkbox\// && $form->{$_} eq 'on' } keys(%{$form}); return $self->errors("Delete credential: No credential selected") unless @delete; @@ -425,13 +407,8 @@ sub _submit_back_to_list { my %handlers = ( 'submit/add' => \&_submit_add, - 'submit/add-v1-v2c' => \&_submit_add_v1_v2c, - 'submit/add-v3' => \&_submit_add_v3, - 'submit/add-remotecred' => \&_submit_add_remotecred, 'submit/update' => \&_submit_update, - 'submit/delete-v1-v2c' => \&_submit_delete_v1_v2c, - 'submit/delete-v3' => \&_submit_delete_v3, - 'submit/delete-remote' => \&_submit_delete_remotes, + 'submit/delete' => \&_submit_delete, 'submit/back-to-list' => \&_submit_back_to_list, ); @@ -447,6 +424,19 @@ sub handle_form { return unless defined($yaml_config->{'credentials_navbar'}) && $yaml_config->{'credentials_navbar'} =~ /^1|yes$/i; + # Save few values in session + $self->store_in_session( 'credentials_ordering_column' => $form->{'col'} ) + if $form->{'col'} && $form->{'col'} =~ /^name|type|config|description$/; + + $self->store_in_session( 'credentials_order' => $form->{'order'} ) + if $form->{'order'} && $form->{'order'} =~ /^ascend|descend$/; + + $self->store_in_session( 'credentials_start' => int($form->{'start'}) ) + if defined($form->{'start'}) && $form->{'start'} =~ /^\d+$/; + + $self->store_in_session( 'display' => $form->{'display'} =~ /^\d+$/ ? $form->{'display'} : 0 ) + if defined($form->{'display'}); + $self->edit($form->{'edit'}) if defined($form->{'edit'}); my $credentials = $yaml->{credentials} || {}; diff --git a/share/html/toolbox/credentials-language-fr.txt b/share/html/toolbox/credentials-language-fr.txt index f611b80b2..5f55a7536 100644 --- a/share/html/toolbox/credentials-language-fr.txt +++ b/share/html/toolbox/credentials-language-fr.txt @@ -5,7 +5,7 @@ SNMP Credentials v3: Identifiants SNMP v3 Credential name: Nom de l'identifiant Community: Communauté -Username: Nom d'utilisateur +Username: Utilisateur Authentication: Authentification Privacy: Chiffrement diff --git a/share/html/toolbox/credentials-list.tpl b/share/html/toolbox/credentials-list.tpl index 520d6ddd7..12d09457c 100644 --- a/share/html/toolbox/credentials-list.tpl +++ b/share/html/toolbox/credentials-list.tpl @@ -1,258 +1,177 @@ -
- - - -
+{ # This comment to keep a line feed at the beginning of this template inclusion }
- - -
- - - - - - - - - - - - - { - use Encode qw(encode); - use HTML::Entities; - use URI::Escape; - $count = 0; - foreach my $entry (sort keys(%credentials)) { - my $version = $credentials{$entry}->{snmpversion}; - next unless $version && $version =~ /^v1|v2c$/; - $count++; - my $this = encode('UTF-8', encode_entities($entry)); - my $id = $credentials{$entry}->{id}; - my $community = $credentials{$entry}->{community}; - my $port = $credentials{$entry}->{port} || ""; - my $protocol = $credentials{$entry}->{protocol} || ""; - my $description = $credentials{$entry}->{description} || ""; - my $name = $credentials{$entry}->{name} || $this ; + { +use Encode qw(encode); +use HTML::Entities; +use URI::Escape; + +if (@credentials_order) { + my $listnav = Text::Template::fill_in_file($template_path."/list-navigation.tpl", HASH => $hash); + chomp($listnav) if defined($listnav); + $OUT .= $listnav ? $listnav : " +

Error loading list-navigation.tpl template: $Text::Template::ERROR

"; + $OUT .= " +
- - {_"Credential name"}{_"Version"}{_"Community"}{_"Port"}{_"Protocol"}{"Description"}
+ + + "; + $other_order = $order eq 'ascend' ? 'descend' : 'ascend'; + foreach my $column (@columns) { + my ($name, $text) = @{$column}; + my $order_req = $name eq $ordering_column ? $other_order : $order; $OUT .= " - - - - - - - - - "; + + + "; } - # Handle empty list case - unless ($count) { + $OUT .= " + + + "; + my $count = -$start; + my $listed = 0; + foreach my $entry (@credentials_order) { + next unless $count++>=0; + $listed++; + my $credential = $credentials{$entry}; + my $this = encode('UTF-8', encode_entities($entry)); + my $name = $credential->{name} || $this ; + my $tips = $credential->{id} ? " title='id=".$credential->{id}."'": ""; + my $type = $credential->{type} || 'snmp'; + my $port = int($credential->{port}) || 0; + # Don't show port in config if it's the default one + undef $port if $type eq 'snmp' && $port == 161; + undef $port if $type eq 'ssh' && $port == 222; + undef $port if $type eq 'winrm' && (($port == 5985 && (!$credential->{mode} || $credential->{mode} !~ /ssl/)) + || ($port == 5986 && $credential->{mode} && $credential->{mode} =~ /ssl/)); + if ($type eq 'snmp' && $credential->{snmpversion}) { + $type = $credential->{snmpversion} eq 'v3' ? "snmp v3" : + $credential->{snmpversion} eq 'v2c' ? "snmp v2c" : "snmp v1"; + } + my $community = $type =~ /^snmp v(?:1|2c)$/ && $credential->{community} || ""; + my $username = $credential->{username} // ""; + my $protocol = $type =~ /^snmp/ && $credential->{protocol} && $credential->{protocol} ne 'udp' ? $credential->{protocol} : ""; + my $mode = $credential->{mode} // ""; + my $authprotocol = $type eq "snmp v3" && $credential->{authprotocol} || ""; + my $privprotocol = $type eq "snmp v3" && $credential->{privprotocol} || ""; + my $checked = $form{"checkbox/$entry"} eq "on" ? " checked" : ""; + my $description = $credential->{description} || ""; $OUT .= " - - - + + + + + + "; - } -} - -
+ +
- - $name$version$community$port$protocol$description
 "._("empty list")."
+ + $name$type +
    +
  • +
    + ".($community ? _("Community").": ".$community : _("Username").": ".$username)." +
    +

    ".($community ? _("Community").": ".$community : _("Username").": ".$username)."

    ".($authprotocol ? " +

    "._("Authentication protocol").": ".$authprotocol."

    " : "").($privprotocol ? " +

    "._("Privacy protocol").": ".$privprotocol."

    " : "").($port ? " +

    "._("Port").": ".$port."

    " : "").($protocol ? " +

    "._("Protocol").": ".$protocol."

    " : "").($mode ? " +

    "._("Mode").": ".$mode."

    " : "")." + +
    +
    +
  • +
+
$description
-
- - -
-
- -
-
- - - - - - - - - - - - - - { - $count = 0; - foreach my $entry (sort keys(%credentials)) { - my $version = $credentials{$entry}->{snmpversion}; - next unless $version && $version =~ /^v3$/; - $count++; - my $this = encode('UTF-8', encode_entities($entry)); - my $id = $credentials{$entry}->{id}; - my $user = $credentials{$entry}->{username}; - my $aproto = $credentials{$entry}->{authprotocol}; - my $apass = $credentials{$entry}->{authpassword} || ""; - my $pproto = $credentials{$entry}->{privprotocol}; - my $ppass = $credentials{$entry}->{privpassword} || ""; - my $port = $credentials{$entry}->{port} || ""; - my $protocol = $credentials{$entry}->{protocol} || ""; - my $description = $credentials{$entry}->{description} || ""; - my $name = $credentials{$entry}->{name} || $this ; - $apass = "****" if length($apass); - $ppass = "****" if length($ppass); + last if $display && $count >= $display; - $OUT .= " - - - - - - - - - - "; } - # Handle empty list case - unless ($count) { + $OUT .= " + "; + if ($listed >= 50) { $OUT .= " - - - - "; - } -} - -
- - {_"Credential name"}{_"Username"}{_"Authentication"}{_"Privacy"}{_"Port"}{_"Protocol"}{_"Description"}
- - $name$user".($aproto?"[$aproto] ":"")."$apass".($pproto?"[$pproto] ":"")."$ppass$port$protocol$description
 "._("empty list")."
-
- - -
-
- -
-
- - - - - - - - - - - - - - { - $count = 0; - foreach my $entry (sort keys(%credentials)) { - my $type = $credentials{$entry}->{type} - or next; - next if $type eq 'snmp'; - $count++; - my $this = encode('UTF-8', encode_entities($entry)); - my $id = $credentials{$entry}->{id}; - my $remoteuser = $credentials{$entry}->{username}; - my $remotepass = $credentials{$entry}->{password}; - my $port = $credentials{$entry}->{port} || ""; - my $mode = $credentials{$entry}->{mode} || ""; - my $description = $credentials{$entry}->{description} || ""; - my $name = $credentials{$entry}->{name} || $this ; - $remotepass = "****" if length($remotepass); + + + "; + foreach my $column (@columns) { + my ($name, $text) = @{$column}; + my $other_order = $order eq 'ascend' ? 'descend' : 'ascend'; + my $order_req = $name eq $ordering_column ? $other_order : $order; + $OUT .= " + + "; + } $OUT .= " - - - - - - - - - - "; + + "; } - # Handle empty list case - unless ($count) { + $OUT .= " + "; + if ($listed >= 50) { $OUT .= " - - - - "; + + + "; + foreach my $column (@columns) { + my ($name, $text) = @{$column}; + my $other_order = $order eq 'ascend' ? 'descend' : 'ascend'; + my $order_req = $name eq $ordering_column ? $other_order : $order; + $OUT .= " + + "; + } + $OUT .= " + + "; } -} - -
- - {_"Credential name"}{_"Type"}{_"Username"}{_"Password"}{_"Port"}{_"Mode"}{_"Description"}
+ +
- - $name$type$remoteuser$remotepass$port$mode$description
 "._("empty list")."
+ +
-
- - -
+ $OUT .= " + +
+ + +
"; + $OUT .= $listnav if $listed >= 50 && $listnav; +} else { + # Handle empty list case + $OUT .= " +
+

"._("No credential defined")."

+
"; +}}
- +
diff --git a/share/html/toolbox/toolbox.css b/share/html/toolbox/toolbox.css index 27782c39f..c058fc064 100644 --- a/share/html/toolbox/toolbox.css +++ b/share/html/toolbox/toolbox.css @@ -25,10 +25,6 @@ table { padding-top: 0.5em; } -table.credentials { - padding-top: 0; -} - th { background-color: #e6e6e6; border-bottom: 1px solid #eee;