From 88377096494efddb86d76721a1353330e9addc56 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 --- admin/smtp_servers.pl | 236 +++++++++++ .../prog/en/modules/admin/admin-home.tt | 4 + .../prog/en/modules/admin/smtp_servers.tt | 368 ++++++++++++++++++ 3 files changed, 608 insertions(+) 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..afc19dd281 --- /dev/null +++ b/admin/smtp_servers.pl @@ -0,0 +1,236 @@ +#!/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 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", + authnotrequired => 0, + flagsrequired => { parameters => 1 }, + debug => 1, + } +); + +my @messages; + +if ( $op eq 'add' ) { + + my $library_id = $input->param('library_id'); + $library_id = undef + if defined $library_id and $library_id eq '*'; + + 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 = (scalar $input->param('smtp_ssl')) ? 1 : 0; + my $user_name = $input->param('smtp_user_name') || undef; + my $password = $input->param('smtp_password') || undef; + + try { + Koha::SMTP::Server->new( + { + library_id => $library_id, + name => $name, + host => $host, + port => $port, + timeout => $timeout, + ssl => $ssl, + user_name => $user_name, + password => $password + } + )->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 'delete' ) { + 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 ) { + try { + my $name = $smtp_server->name; + $smtp_server->delete; + push @messages, + { + type => 'message', + code => 'success_on_delete' + }; + } + catch { + push @messages, + { + type => 'alert', + code => 'error_on_delete' + }; + }; + } + else { + push @messages, + { + type => 'alert', + code => 'error_on_delete', + reason => 'invalid_id' + }; + } + + # list servers after delete + $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 = (scalar $input->param('smtp_ssl')) ? 1 : 0; + my $user_name = $input->param('smtp_user_name') || undef; + my $password = $input->param('smtp_password') || undef; + + try { + $smtp_server->set( + { + name => $name, + host => $host, + port => $port, + timeout => $timeout, + ssl => $ssl, + user_name => $user_name, + password => $password + + } + )->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 + ); +} + +my @already_set_library_ids = Koha::SMTP::Servers->search( + { + library_id => { '!=' => undef } + } +)->get_column('library_id'); + +my $libraries = Koha::Libraries->search( + { + 'me.branchcode' => { -not_in => \@already_set_library_ids } + } +); + +my $default = Koha::SMTP::Servers->search({ library_id => undef })->count; + +$template->param( + op => $op, + default_set => ( $default ) ? 1 : 0, + unset_libraries => $libraries, + messages => \@messages, +); + +output_html_with_http_headers $input, $cookie, $template->output; 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..9388dbe2d7 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 @@ -208,6 +208,10 @@
Z39.50/SRU servers
Define which external servers to query for MARC data.
[% END %] + [% IF ( CAN_user_parameters ) %] +
SMTP servers
+
Define which SMTP servers to use.
+ [% END %] [% IF ( CAN_user_parameters_manage_didyoumean ) %]
Did you mean?
Choose which plugins to use to suggest searches to patrons and staff.
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smtp_servers.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smtp_servers.tt new file mode 100644 index 0000000000..1d90213b38 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smtp_servers.tt @@ -0,0 +1,368 @@ +[% USE raw %] +[% USE Asset %] +[% SET footerjs = 1 %] +[% USE TablesSettings %] +[% INCLUDE 'doc-head-open.inc' %] +Koha › Administration › SMTP servers +[% IF op == 'add_form' %] + ›[% IF library %]Modify SMTP server[% ELSE %]New SMTP server[% END %] +[% ELSIF op == 'delete_confirm' %] + › Confirm deletion of SMTP server '[% server.name | html %]' +[% END %] + +[% INCLUDE 'doc-head-close.inc' %] + + + +[% INCLUDE 'header.inc' %] +[% INCLUDE 'prefs-admin-search.inc' %] + + + +
+
+
+
+ +[% FOREACH m IN messages %] +
+ [% SWITCH m.code %] + [% CASE 'error_on_edit' %] + An error occurred trying to open the server for editing. The passed id is invalid. + [% CASE 'error_on_insert' %] + An error occurred when adding the server. The library already has an SMTP server set. + [% CASE 'error_on_delete' %] + An error occurred when deleting this server. Check the logs. + [% CASE 'success_on_update' %] + Server updated successfully. + [% CASE 'success_on_insert' %] + Server added successfully. + [% CASE 'success_on_delete' %] + Server deleted successfully. + [% CASE %] + [% m.code | html %] + [% END %] +
+[% END %] + +[% IF op == 'add_form' %] +

New SMTP server

+
+ +
+
    +
  1. + + + Required +
  2. + +
  3. + + + Required +
  4. +
+
+ +
+
    +
  1. + + + Required +
  2. +
  3. + + + Required +
  4. +
  5. + + +
  6. +
  7. + + +
  8. +
  9. + + +
  10. +
  11. + + +
  12. +
+
+
+ + Cancel +
+
+[% END %] + +[% IF op == 'edit_form' %] +

Edit SMTP server

+
+ + +
+
    +
  1. + + [% IF smtp_server.library %] + [%- smtp_server.library.branchname | html -%] + [%- ELSE -%] + All libraries + [%- END -%] +
  2. + +
  3. + + + Required +
  4. +
+
+ +
+
    +
  1. + + + Required +
  2. +
  3. + + + Required +
  4. +
  5. + + +
  6. +
  7. + + +
  8. +
  9. + + +
  10. +
  11. + + +
  12. +
+
+
+ + Cancel +
+
+[% END %] + +[% IF op == 'list' %] + + [%- IF unset_libraries.count > 0 -%] + + [%- END -%] + +

SMTP servers

+ + [% IF servers_count > 0 %] + + + + + + + + + + + + + +
LibraryNameHostPortTimeout (secs)SSLAuthenticatedActions
+ [% ELSE %] +
No user-defined SMTP servers. Using the default configuration: +
    +
  • Host: [%- default_config.host | html -%]
  • +
  • Port: [%- default_config.port | html -%]
  • +
  • Timeout (secs): [%- default_config.timeout | html -%]
  • +
  • SSL: [%- IF default_config.ssl -%]Yes[%- ELSE -%]No[%- END -%]
  • +
+ +
+ [% END %] +[% END %] + + + +
+
+ +
+ +
+
+ + +[% MACRO jsinclude BLOCK %] + [% Asset.js("js/admin-menu.js") | $raw %] + [% INCLUDE 'datatables.inc' %] + [% INCLUDE 'columns_settings.inc' %] + [% Asset.js("lib/tiny_mce/tinymce.min.js") | $raw %] + [% INCLUDE 'str/tinymce_i18n.inc' %] + +[% END %] + +[% INCLUDE 'intranet-bottom.inc' %] -- 2.28.0