From afc8b1199a6f99f9619ea42efee9c2ae7924f80f Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 29 Jul 2020 17:35:50 -0300 Subject: [PATCH] Bug 22343: Add CRUD page for SMTP servers This patch introduces a way to define SMTP servers either globally or per-library. To test: 1. Apply this patch 2. Find the SMTP servers entry in the admin page 3. Play with adding/removing SMTP servers => SUCCESS: All works as expected 4. Sign off :-D Signed-off-by: Kyle M Hall Signed-off-by: Martin Renvoize --- admin/smtp_servers.pl | 182 +++++++++ .../prog/en/includes/admin-menu.inc | 5 +- .../prog/en/includes/permissions.inc | 5 + .../prog/en/modules/admin/admin-home.tt | 6 +- .../prog/en/modules/admin/smtp_servers.tt | 382 ++++++++++++++++++ 5 files changed, 578 insertions(+), 2 deletions(-) create mode 100755 admin/smtp_servers.pl create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/admin/smtp_servers.tt diff --git a/admin/smtp_servers.pl b/admin/smtp_servers.pl new file mode 100755 index 0000000000..9dff83d0a5 --- /dev/null +++ b/admin/smtp_servers.pl @@ -0,0 +1,182 @@ +#!/usr/bin/perl + +# Copyright 2020 Theke Solutions +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use CGI qw ( -utf8 ); +use Scalar::Util qw(blessed); +use Try::Tiny; + +use C4::Auth qw(get_template_and_user); +use C4::Output qw(output_html_with_http_headers); + +use Koha::Libraries; +use Koha::SMTP::Servers; + +my $input = CGI->new; +my $op = $input->param('op') || 'list'; + +my ( $template, $borrowernumber, $cookie ) = get_template_and_user( + { template_name => "admin/smtp_servers.tt", + query => $input, + type => "intranet", + flagsrequired => { parameters => 'manage_smtp_servers' }, + } +); + +my @messages; + +if ( $op eq 'add' ) { + + my $name = $input->param('smtp_name'); + my $host = $input->param('smtp_host'); + my $port = $input->param('smtp_port') || 25; + my $timeout = $input->param('smtp_timeout') || 120; + my $ssl_mode = $input->param('smtp_ssl_mode'); + my $user_name = $input->param('smtp_user_name') || undef; + my $password = $input->param('smtp_password') || undef; + my $debug = ( scalar $input->param('smtp_debug_mode') ) ? 1 : 0; + + try { + Koha::SMTP::Server->new( + { + name => $name, + host => $host, + port => $port, + timeout => $timeout, + ssl_mode => $ssl_mode, + user_name => $user_name, + password => $password, + debug => $debug, + } + )->store; + + push @messages, { type => 'message', code => 'success_on_insert' }; + } + catch { + if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) { + push @messages, + { + type => 'alert', + code => 'error_on_insert', + reason => 'duplicate_id' + }; + } + }; + + # list servers after adding + $op = 'list'; +} +elsif ( $op eq 'edit_form' ) { + my $smtp_server_id = $input->param('smtp_server_id'); + my $smtp_server; + + $smtp_server = Koha::SMTP::Servers->find($smtp_server_id) + unless !$smtp_server_id; + + if ( $smtp_server ) { + $template->param( + smtp_server => $smtp_server + ); + } + else { + push @messages, + { + type => 'alert', + code => 'error_on_edit', + reason => 'invalid_id' + }; + } +} +elsif ( $op eq 'edit_save' ) { + + my $smtp_server_id = $input->param('smtp_server_id'); + my $smtp_server; + + $smtp_server = Koha::SMTP::Servers->find($smtp_server_id) + unless !$smtp_server_id; + + if ( $smtp_server ) { + + my $name = $input->param('smtp_name'); + my $host = $input->param('smtp_host'); + my $port = $input->param('smtp_port') || 25; + my $timeout = $input->param('smtp_timeout') || 120; + my $ssl_mode = $input->param('smtp_ssl_mode'); + my $user_name = $input->param('smtp_user_name') || undef; + my $password = $input->param('smtp_password') || undef; + my $debug = ( scalar $input->param('smtp_debug_mode') ) ? 1 : 0; + + try { + $smtp_server->password( $password ) + if $password; + + $smtp_server->set( + { + name => $name, + host => $host, + port => $port, + timeout => $timeout, + ssl_mode => $ssl_mode, + user_name => $user_name, + debug => $debug + } + )->store; + + push @messages, + { + type => 'message', + code => 'success_on_update' + }; + } + catch { + push @messages, + { + type => 'alert', + code => 'error_on_update' + }; + }; + + # list servers after adding + $op = 'list'; + } + else { + push @messages, + { + type => 'alert', + code => 'error_on_update', + reason => 'invalid_id' + }; + } +} + +if ( $op eq 'list' ) { + my $smtp_servers = Koha::SMTP::Servers->search; + $template->param( + servers_count => $smtp_servers->count, + default_config => $smtp_servers->default_setting + ); +} + +$template->param( + op => $op, + messages => \@messages, +); + +output_html_with_http_headers $input, $cookie, $template->output; diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/admin-menu.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/admin-menu.inc index 8f95f3ee05..44d1f4cc0e 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/admin-menu.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/admin-menu.inc @@ -123,12 +123,15 @@ [% END %] - [% IF ( CAN_user_parameters_manage_search_targets || CAN_user_parameters_manage_didyoumean || CAN_user_parameters_manage_column_config || CAN_user_parameters_manage_audio_alerts || ( CAN_user_parameters_manage_sms_providers && Koha.Preference('SMSSendDriver') == 'Email' ) || CAN_user_parameters_manage_usage_stats || CAN_user_parameters_manage_additional_fields || ( Koha.Preference('EnableAdvancedCatalogingEditor') && CAN_user_parameters_manage_keyboard_shortcuts ) ) %] + [% IF ( CAN_user_parameters_manage_smtp_servers || CAN_user_parameters_manage_search_targets || CAN_user_parameters_manage_didyoumean || CAN_user_parameters_manage_column_config || CAN_user_parameters_manage_audio_alerts || ( CAN_user_parameters_manage_sms_providers && Koha.Preference('SMSSendDriver') == 'Email' ) || CAN_user_parameters_manage_usage_stats || CAN_user_parameters_manage_additional_fields || ( Koha.Preference('EnableAdvancedCatalogingEditor') && CAN_user_parameters_manage_keyboard_shortcuts ) ) %]
Additional parameters
    [% IF ( CAN_user_parameters_manage_search_targets ) %]
  • Z39.50/SRU servers
  • [% END %] + [% IF ( CAN_user_parameters_manage_smtp_servers ) %] +
  • SMTP servers
  • + [% END %] [% IF ( CAN_user_parameters_manage_didyoumean ) %]
  • Did you mean?
  • [% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/permissions.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/permissions.inc index b8beb111ee..e5335d74f4 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/permissions.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/permissions.inc @@ -220,6 +220,11 @@ Manage Did you mean? configuration ([% name | html %]) + [%- CASE 'manage_smtp_servers' -%] + + Manage SMTP servers + + ([% name | html %]) [%- CASE 'manage_column_config' -%] Manage column configuration diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt index 0c02f258ab..1f084358fb 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt @@ -199,7 +199,7 @@ [% END %] - [% IF ( ( CAN_user_parameters_manage_search_targets || CAN_user_parameters_manage_didyoumean || CAN_user_parameters_manage_column_config || CAN_user_parameters_manage_audio_alerts || CAN_user_parameters_manage_sms_providers && Koha.Preference('SMSSendDriver') == 'Email' ) || CAN_user_parameters_manage_usage_stats || CAN_user_parameters_manage_additional_fields || CAN_user_parameters_manage_mana || (Koha.Preference('EnableAdvancedCatalogingEditor') && CAN_user_parameters_manage_keyboard_shortcuts) ) %] + [% IF ( ( CAN_user_parameters_manage_smtp_servers || CAN_user_parameters_manage_search_targets || CAN_user_parameters_manage_didyoumean || CAN_user_parameters_manage_column_config || CAN_user_parameters_manage_audio_alerts || CAN_user_parameters_manage_sms_providers && Koha.Preference('SMSSendDriver') == 'Email' ) || CAN_user_parameters_manage_usage_stats || CAN_user_parameters_manage_additional_fields || CAN_user_parameters_manage_mana || (Koha.Preference('EnableAdvancedCatalogingEditor') && CAN_user_parameters_manage_keyboard_shortcuts) ) %]

    Additional parameters

    + + + + + + +
    + +
    + + + +[% MACRO jsinclude BLOCK %] + [% Asset.js("js/admin-menu.js") | $raw %] + [% INCLUDE 'datatables.inc' %] + +[% END %] + +[% INCLUDE 'intranet-bottom.inc' %] -- 2.20.1