|
Line 0
Link Here
|
|
|
1 |
use Modern::Perl; |
| 2 |
use Koha::Installer::Output qw(say_warning say_success say_info); |
| 3 |
use XML::Simple; |
| 4 |
|
| 5 |
return { |
| 6 |
bug_number => "39224", |
| 7 |
description => "Add tables for storing Shibboleth configs,mappings and migration to db based storage", |
| 8 |
up => sub { |
| 9 |
my ($args) = @_; |
| 10 |
my ( $dbh, $out ) = @$args{qw(dbh out)}; |
| 11 |
|
| 12 |
my $xml_config; |
| 13 |
my $koha_conf = $ENV{KOHA_CONF}; |
| 14 |
if ( $koha_conf && -f $koha_conf ) { |
| 15 |
$xml_config = XML::Simple->new->XMLin($koha_conf); |
| 16 |
} |
| 17 |
|
| 18 |
unless ($xml_config) { |
| 19 |
say_warning( $out, "Could not load XML config from KOHA_CONF. XML settings will not be migrated." ); |
| 20 |
} |
| 21 |
|
| 22 |
unless ( TableExists('shibboleth_field_mappings') ) { |
| 23 |
$dbh->do( |
| 24 |
q{ |
| 25 |
CREATE TABLE IF NOT EXISTS shibboleth_field_mappings ( |
| 26 |
mapping_id int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key', |
| 27 |
idp_field varchar(255) COMMENT 'Field name from the identity provider', |
| 28 |
koha_field varchar(255) NOT NULL COMMENT 'Corresponding field in Koha borrowers table', |
| 29 |
is_matchpoint tinyint(1) NOT NULL DEFAULT 0 COMMENT 'If this field is used to match existing users', |
| 30 |
default_content varchar(255) DEFAULT NULL COMMENT 'Default content for this field if not provided by the IdP', |
| 31 |
PRIMARY KEY (mapping_id), |
| 32 |
UNIQUE KEY koha_field_idx (koha_field), |
| 33 |
KEY idp_field_idx (idp_field) |
| 34 |
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; |
| 35 |
} |
| 36 |
); |
| 37 |
say_success( $out, "Added new table 'shibboleth_field_mappings'" ); |
| 38 |
} |
| 39 |
|
| 40 |
unless ( TableExists('shibboleth_config') ) { |
| 41 |
$dbh->do( |
| 42 |
q{ |
| 43 |
CREATE TABLE IF NOT EXISTS shibboleth_config ( |
| 44 |
shibboleth_config_id int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key', |
| 45 |
force_opac_sso tinyint(1) NOT NULL DEFAULT 0 COMMENT 'Force Shibboleth SSO for OPAC', |
| 46 |
force_staff_sso tinyint(1) NOT NULL DEFAULT 0 COMMENT 'Force Shibboleth SSO for staff interface', |
| 47 |
autocreate tinyint(1) NOT NULL DEFAULT 0 COMMENT 'Automatically create new patrons', |
| 48 |
sync tinyint(1) NOT NULL DEFAULT 0 COMMENT 'Sync patron attributes on login', |
| 49 |
welcome tinyint(1) NOT NULL DEFAULT 0 COMMENT 'Send welcome email to new patrons', |
| 50 |
PRIMARY KEY (shibboleth_config_id) |
| 51 |
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; |
| 52 |
} |
| 53 |
); |
| 54 |
say_success( $out, "Added new table 'shibboleth_config'" ); |
| 55 |
} |
| 56 |
|
| 57 |
my $use_shibboleth = 0; |
| 58 |
if ( $xml_config && $xml_config->{config} && defined $xml_config->{config}->{useshibboleth} ) { |
| 59 |
$use_shibboleth = $xml_config->{config}->{useshibboleth}; |
| 60 |
} |
| 61 |
|
| 62 |
$dbh->do( |
| 63 |
q{ |
| 64 |
INSERT IGNORE INTO systempreferences (variable,value,explanation,options,type) |
| 65 |
VALUES ('ShibbolethAuthentication',?,'Enable or disable Shibboleth authentication integration','0|1','YesNo') |
| 66 |
}, undef, $use_shibboleth |
| 67 |
); |
| 68 |
|
| 69 |
my $sth = $dbh->prepare( |
| 70 |
"SELECT variable, value FROM systempreferences WHERE variable IN ('staffShibOnly', 'OPACShibOnly')"); |
| 71 |
$sth->execute(); |
| 72 |
my $old_prefs = {}; |
| 73 |
while ( my $row = $sth->fetchrow_hashref ) { |
| 74 |
$old_prefs->{ $row->{variable} } = $row->{value}; |
| 75 |
} |
| 76 |
|
| 77 |
if (%$old_prefs) { |
| 78 |
my ($config_exists) = $dbh->selectrow_array("SELECT COUNT(*) FROM shibboleth_config"); |
| 79 |
|
| 80 |
if ( !$config_exists ) { |
| 81 |
$dbh->do( |
| 82 |
q{ |
| 83 |
INSERT INTO shibboleth_config |
| 84 |
(force_opac_sso, force_staff_sso) VALUES (?,?) |
| 85 |
}, undef, |
| 86 |
( $old_prefs->{OPACShibOnly} || 0 ), |
| 87 |
( $old_prefs->{staffShibOnly} || 0 ) |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
$dbh->do( |
| 92 |
q{ |
| 93 |
DELETE FROM systempreferences |
| 94 |
WHERE variable IN ('staffShibOnly','OPACShibOnly') |
| 95 |
} |
| 96 |
); |
| 97 |
say_success( $out, "Migrated and removed deprecated Shibboleth system preferences" ); |
| 98 |
} |
| 99 |
|
| 100 |
if ( $xml_config && $xml_config->{config} && $xml_config->{config}->{shibboleth} ) { |
| 101 |
my ($config_exists) = $dbh->selectrow_array("SELECT COUNT(*) FROM shibboleth_config"); |
| 102 |
|
| 103 |
if ( !$config_exists ) { |
| 104 |
$dbh->do( |
| 105 |
q{ |
| 106 |
INSERT INTO shibboleth_config |
| 107 |
(autocreate, sync, welcome) VALUES (?,?,?) |
| 108 |
}, undef, |
| 109 |
( $xml_config->{config}->{shibboleth}->{autocreate} || 0 ), |
| 110 |
( $xml_config->{config}->{shibboleth}->{sync} || 0 ), |
| 111 |
( $xml_config->{config}->{shibboleth}->{welcome} || 0 ) |
| 112 |
); |
| 113 |
} else { |
| 114 |
$dbh->do( |
| 115 |
q{ |
| 116 |
UPDATE shibboleth_config SET |
| 117 |
autocreate = ?, |
| 118 |
sync = ?, |
| 119 |
welcome = ? |
| 120 |
}, undef, |
| 121 |
( $xml_config->{config}->{shibboleth}->{autocreate} || 0 ), |
| 122 |
( $xml_config->{config}->{shibboleth}->{sync} || 0 ), |
| 123 |
( $xml_config->{config}->{shibboleth}->{welcome} || 0 ) |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
my $mapping = $xml_config->{config}->{shibboleth}->{mapping}; |
| 128 |
foreach my $koha_field ( keys %$mapping ) { |
| 129 |
my $idp_field = $mapping->{$koha_field}->{is} || undef; |
| 130 |
my $default = $mapping->{$koha_field}->{content} || undef; |
| 131 |
my $is_matchpoint = ( $koha_field eq $xml_config->{config}->{shibboleth}->{matchpoint} ) ? 1 : 0; |
| 132 |
|
| 133 |
next unless ( $idp_field || $default ); |
| 134 |
|
| 135 |
$dbh->do( |
| 136 |
q{ |
| 137 |
INSERT IGNORE INTO shibboleth_field_mappings |
| 138 |
(koha_field, idp_field, default_content, is_matchpoint) |
| 139 |
VALUES (?,?,?,?) |
| 140 |
}, undef, |
| 141 |
$koha_field, |
| 142 |
$idp_field, |
| 143 |
$default, |
| 144 |
$is_matchpoint |
| 145 |
); |
| 146 |
} |
| 147 |
say_success( $out, "Migrated Shibboleth XML configuration" ); |
| 148 |
} |
| 149 |
|
| 150 |
return 1; |
| 151 |
}, |
| 152 |
}; |