View | Details | Raw Unified | Return to bug 40596
Collapse All | Expand All

(-)a/installer/data/mysql/atomicupdate/bug_40596.pl (+102 lines)
Line 0 Link Here
1
use Modern::Perl;
2
use Koha::Installer::Output qw(say_warning say_success say_info);
3
use JSON;
4
5
return {
6
    bug_number  => "40596",
7
    description => "Migrate CAS authentication to identity providers",
8
    up          => sub {
9
        my ($args) = @_;
10
        my ( $dbh, $out ) = @$args{qw(dbh out)};
11
12
        # ── 1. Add CAS to identity_providers.protocol enum ─────────────────────
13
14
        if ( column_exists( 'identity_providers', 'protocol' ) ) {
15
            my ($current_type) = $dbh->selectrow_array(
16
                "SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
17
                 WHERE TABLE_SCHEMA = DATABASE()
18
                 AND TABLE_NAME = 'identity_providers'
19
                 AND COLUMN_NAME = 'protocol'"
20
            );
21
22
            unless ( $current_type && $current_type eq "enum('CAS','OAuth','OIDC','SAML2')" ) {
23
                $dbh->do(
24
                    q{
25
                    ALTER TABLE identity_providers
26
                        MODIFY COLUMN `protocol`
27
                        enum('CAS','OAuth','OIDC','SAML2')
28
                        COLLATE utf8mb4_unicode_ci NOT NULL
29
                        COMMENT 'Protocol provider speaks'
30
                }
31
                );
32
                say_success( $out, "Added CAS to identity_providers.protocol enum" );
33
            }
34
        }
35
36
        # ── 2. Migrate existing CAS configuration to identity providers ────────
37
38
        my $version = C4::Context->preference('casServerVersion') || '2';
39
40
        # Check for a multi-server YAML config file first
41
        my $yamlauthfile = C4::Context->config('intranetdir') . "/C4/Auth_cas_servers.yaml";
42
43
        if ( -e $yamlauthfile ) {
44
            require YAML::XS;
45
            my ( $default_hash, $servers ) = YAML::XS::LoadFile($yamlauthfile);
46
47
            for my $code ( sort keys %$servers ) {
48
                my $server_url = $servers->{$code};
49
                next unless $server_url;
50
51
                my ($exists) = $dbh->selectrow_array(
52
                    "SELECT COUNT(*) FROM identity_providers WHERE code = ? AND protocol = 'CAS'",
53
                    undef, $code
54
                );
55
                if ($exists) {
56
                    say_info( $out, "CAS provider '$code' already exists - skipping" );
57
                    next;
58
                }
59
60
                $dbh->do(
61
                    "INSERT INTO identity_providers (code, description, protocol, config, enabled)
62
                     VALUES (?, ?, 'CAS', ?, 1)",
63
                    undef,
64
                    $code,
65
                    $code,
66
                    encode_json( { server_url => $server_url, version => $version } )
67
                );
68
                say_success( $out, "Migrated CAS server '$code' ($server_url) to identity providers" );
69
            }
70
71
            say_info(
72
                $out,
73
                "CAS servers migrated from $yamlauthfile - "
74
                    . "the file can be removed once you have verified the configuration"
75
            );
76
77
        } elsif ( C4::Context->preference('casAuthentication') && C4::Context->preference('casServerUrl') ) {
78
79
            # Single CAS server from system preferences
80
            my $server_url = C4::Context->preference('casServerUrl');
81
82
            my ($exists) = $dbh->selectrow_array("SELECT COUNT(*) FROM identity_providers WHERE protocol = 'CAS'");
83
84
            if ($exists) {
85
                say_info( $out, "CAS identity provider already exists - skipping syspref migration" );
86
            } else {
87
                $dbh->do(
88
                    "INSERT INTO identity_providers (code, description, protocol, config, enabled)
89
                     VALUES ('cas', 'CAS', 'CAS', ?, 1)",
90
                    undef,
91
                    encode_json( { server_url => $server_url, version => $version } )
92
                );
93
                say_success( $out, "Migrated CAS server ($server_url) to identity providers (code: 'cas')" );
94
                say_info(
95
                    $out,
96
                    "The casServerUrl and casServerVersion system preferences are now superseded "
97
                        . "by the Identity Providers configuration (Admin > Identity providers)"
98
                );
99
            }
100
        }
101
    },
102
};
(-)a/installer/data/mysql/kohastructure.sql (-2 / +1 lines)
Lines 3742-3748 CREATE TABLE `identity_providers` ( Link Here
3742
  `identity_provider_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'unique key, used to identify the provider',
3742
  `identity_provider_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'unique key, used to identify the provider',
3743
  `code` varchar(20) NOT NULL COMMENT 'Provider code',
3743
  `code` varchar(20) NOT NULL COMMENT 'Provider code',
3744
  `description` varchar(255) NOT NULL COMMENT 'Description for the provider',
3744
  `description` varchar(255) NOT NULL COMMENT 'Description for the provider',
3745
  `protocol` enum('OAuth','OIDC','SAML2') NOT NULL COMMENT 'Protocol provider speaks',
3745
  `protocol` enum('CAS','OAuth','OIDC','SAML2') NOT NULL COMMENT 'Protocol provider speaks',
3746
  `config` longtext NOT NULL COMMENT 'Configuration of the provider in JSON format',
3746
  `config` longtext NOT NULL COMMENT 'Configuration of the provider in JSON format',
3747
  `enabled` tinyint(1) NOT NULL DEFAULT 1 COMMENT 'Whether this provider is active',
3747
  `enabled` tinyint(1) NOT NULL DEFAULT 1 COMMENT 'Whether this provider is active',
3748
  `icon_url` varchar(255) DEFAULT NULL COMMENT 'Provider icon URL',
3748
  `icon_url` varchar(255) DEFAULT NULL COMMENT 'Provider icon URL',
3749
- 

Return to bug 40596