From 6f2fe33ff859f26d4aff761c8941936eab37cfab Mon Sep 17 00:00:00 2001 From: Agustin Moyano Date: Thu, 18 Aug 2022 16:27:06 -0300 Subject: [PATCH] Bug 31378: Add Koha::Auth::Provider(s) class(es) Signed-off-by: Lukasz Koszyk Signed-off-by: Tomas Cohen Arazi Signed-off-by: Nick Clemens Signed-off-by: Martin Renvoize --- Koha/Auth/Provider.pm | 58 ++++++ Koha/Auth/Providers.pm | 53 ++++++ Koha/Schema/Result/AuthProvider.pm | 189 +++++++++++++++++++ Koha/Schema/Result/AuthProviderDomain.pm | 226 +++++++++++++++++++++++ t/db_dependent/Koha/Auth/Provider.t | 50 +++++ 5 files changed, 576 insertions(+) create mode 100644 Koha/Auth/Provider.pm create mode 100644 Koha/Auth/Providers.pm create mode 100644 Koha/Schema/Result/AuthProvider.pm create mode 100644 Koha/Schema/Result/AuthProviderDomain.pm create mode 100755 t/db_dependent/Koha/Auth/Provider.t diff --git a/Koha/Auth/Provider.pm b/Koha/Auth/Provider.pm new file mode 100644 index 0000000000..d70a41b7bf --- /dev/null +++ b/Koha/Auth/Provider.pm @@ -0,0 +1,58 @@ +package Koha::Auth::Provider; + +# Copyright Theke Solutions 2022 +# +# 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 base qw(Koha::Object); + +use Koha::Auth::Provider::Domains; + +=head1 NAME + +Koha::Auth::Provider - Koha Auth Provider Object class + +=head1 API + +=head2 Class methods + +=head3 domains + + my $domains = $provider->domains; + +Returns the related I iterator. + +=cut + +sub domains { + my ($self) = @_; + + return Koha::Auth::Provider::Domains->_new_from_dbic( scalar $self->_result->domains ); +} + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'AuthProvider'; +} + +1; diff --git a/Koha/Auth/Providers.pm b/Koha/Auth/Providers.pm new file mode 100644 index 0000000000..51e32b7606 --- /dev/null +++ b/Koha/Auth/Providers.pm @@ -0,0 +1,53 @@ +package Koha::Auth::Providers; + +# Copyright Theke Solutions 2022 +# +# 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 Koha::Database; +use Koha::Auth::Provider; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Auth::Providers - Koha Auth Provider Object class + +=head1 API + +=head2 Internal methods + +=cut + +=head3 _type + +=cut + +sub _type { + return 'AuthProvider'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Auth::Provider'; +} + +1; diff --git a/Koha/Schema/Result/AuthProvider.pm b/Koha/Schema/Result/AuthProvider.pm new file mode 100644 index 0000000000..c62a79a365 --- /dev/null +++ b/Koha/Schema/Result/AuthProvider.pm @@ -0,0 +1,189 @@ +use utf8; +package Koha::Schema::Result::AuthProvider; + +# Created by DBIx::Class::Schema::Loader +# DO NOT MODIFY THE FIRST PART OF THIS FILE + +=head1 NAME + +Koha::Schema::Result::AuthProvider + +=cut + +use strict; +use warnings; + +use base 'DBIx::Class::Core'; + +=head1 TABLE: C + +=cut + +__PACKAGE__->table("auth_providers"); + +=head1 ACCESSORS + +=head2 auth_provider_id + + data_type: 'integer' + is_auto_increment: 1 + is_nullable: 0 + +unique key, used to identify the provider + +=head2 code + + data_type: 'varchar' + is_nullable: 0 + size: 20 + +Provider code + +=head2 description + + data_type: 'varchar' + is_nullable: 0 + size: 255 + +Description for the provider + +=head2 protocol + + data_type: 'enum' + extra: {list => ["OAuth","OIDC","LDAP","CAS"]} + is_nullable: 0 + +Protocol provider speaks + +=head2 config + + data_type: 'longtext' + default_value: ''{}'' + is_nullable: 0 + +Configuration of the provider in JSON format + +=head2 mapping + + data_type: 'longtext' + default_value: ''{}'' + is_nullable: 0 + +Configuration to map provider data to Koha user + +=head2 matchpoint + + data_type: 'enum' + extra: {list => ["email","userid","cardnumber"]} + is_nullable: 0 + +The patron attribute to be used as matchpoint + +=head2 icon_url + + data_type: 'varchar' + is_nullable: 1 + size: 255 + +Provider icon URL + +=cut + +__PACKAGE__->add_columns( + "auth_provider_id", + { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, + "code", + { data_type => "varchar", is_nullable => 0, size => 20 }, + "description", + { data_type => "varchar", is_nullable => 0, size => 255 }, + "protocol", + { + data_type => "enum", + extra => { list => ["OAuth", "OIDC", "LDAP", "CAS"] }, + is_nullable => 0, + }, + "config", + { data_type => "longtext", default_value => "'{}'", is_nullable => 0 }, + "mapping", + { data_type => "longtext", default_value => "'{}'", is_nullable => 0 }, + "matchpoint", + { + data_type => "enum", + extra => { list => ["email", "userid", "cardnumber"] }, + is_nullable => 0, + }, + "icon_url", + { data_type => "varchar", is_nullable => 1, size => 255 }, +); + +=head1 PRIMARY KEY + +=over 4 + +=item * L + +=back + +=cut + +__PACKAGE__->set_primary_key("auth_provider_id"); + +=head1 UNIQUE CONSTRAINTS + +=head2 C + +=over 4 + +=item * L + +=back + +=cut + +__PACKAGE__->add_unique_constraint("code", ["code"]); + +=head1 RELATIONS + +=head2 auth_provider_domains + +Type: has_many + +Related object: L + +=cut + +__PACKAGE__->has_many( + "auth_provider_domains", + "Koha::Schema::Result::AuthProviderDomain", + { "foreign.auth_provider_id" => "self.auth_provider_id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + + +# Created by DBIx::Class::Schema::Loader v0.07049 @ 2022-09-30 19:43:00 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ZqUo3by0ZXca5RI3QFNypw + + +=head2 domains + +Type: has_many + +Related object: L + +=cut + +__PACKAGE__->has_many( + "domains", + "Koha::Schema::Result::AuthProviderDomain", + { "foreign.auth_provider_id" => "self.auth_provider_id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + +sub koha_object_class { + 'Koha::Auth::Provider'; +} +sub koha_objects_class { + 'Koha::Auth::Providers'; +} + +1; diff --git a/Koha/Schema/Result/AuthProviderDomain.pm b/Koha/Schema/Result/AuthProviderDomain.pm new file mode 100644 index 0000000000..117627f2db --- /dev/null +++ b/Koha/Schema/Result/AuthProviderDomain.pm @@ -0,0 +1,226 @@ +use utf8; +package Koha::Schema::Result::AuthProviderDomain; + +# Created by DBIx::Class::Schema::Loader +# DO NOT MODIFY THE FIRST PART OF THIS FILE + +=head1 NAME + +Koha::Schema::Result::AuthProviderDomain + +=cut + +use strict; +use warnings; + +use base 'DBIx::Class::Core'; + +=head1 TABLE: C + +=cut + +__PACKAGE__->table("auth_provider_domains"); + +=head1 ACCESSORS + +=head2 auth_provider_domain_id + + data_type: 'integer' + is_auto_increment: 1 + is_nullable: 0 + +unique key, used to identify providers domain + +=head2 auth_provider_id + + data_type: 'integer' + is_foreign_key: 1 + is_nullable: 0 + +Reference to provider + +=head2 domain + + data_type: 'varchar' + is_nullable: 1 + size: 100 + +Domain name. If null means all domains + +=head2 auto_register + + data_type: 'tinyint' + default_value: 0 + is_nullable: 0 + +Allow user auto register + +=head2 update_on_auth + + data_type: 'tinyint' + default_value: 0 + is_nullable: 0 + +Update user data on auth login + +=head2 default_library_id + + data_type: 'varchar' + is_foreign_key: 1 + is_nullable: 1 + size: 10 + +Default library to create user if auto register is enabled + +=head2 default_category_id + + data_type: 'varchar' + is_foreign_key: 1 + is_nullable: 1 + size: 10 + +Default category to create user if auto register is enabled + +=head2 allow_opac + + data_type: 'tinyint' + default_value: 1 + is_nullable: 0 + +Allow provider from opac interface + +=head2 allow_staff + + data_type: 'tinyint' + default_value: 1 + is_nullable: 0 + +Allow provider from staff interface + +=cut + +__PACKAGE__->add_columns( + "auth_provider_domain_id", + { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, + "auth_provider_id", + { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, + "domain", + { data_type => "varchar", is_nullable => 1, size => 100 }, + "auto_register", + { data_type => "tinyint", default_value => 0, is_nullable => 0 }, + "update_on_auth", + { data_type => "tinyint", default_value => 0, is_nullable => 0 }, + "default_library_id", + { data_type => "varchar", is_foreign_key => 1, is_nullable => 1, size => 10 }, + "default_category_id", + { data_type => "varchar", is_foreign_key => 1, is_nullable => 1, size => 10 }, + "allow_opac", + { data_type => "tinyint", default_value => 1, is_nullable => 0 }, + "allow_staff", + { data_type => "tinyint", default_value => 1, is_nullable => 0 }, +); + +=head1 PRIMARY KEY + +=over 4 + +=item * L + +=back + +=cut + +__PACKAGE__->set_primary_key("auth_provider_domain_id"); + +=head1 UNIQUE CONSTRAINTS + +=head2 C + +=over 4 + +=item * L + +=item * L + +=back + +=cut + +__PACKAGE__->add_unique_constraint("auth_provider_id", ["auth_provider_id", "domain"]); + +=head1 RELATIONS + +=head2 auth_provider + +Type: belongs_to + +Related object: L + +=cut + +__PACKAGE__->belongs_to( + "auth_provider", + "Koha::Schema::Result::AuthProvider", + { auth_provider_id => "auth_provider_id" }, + { is_deferrable => 1, on_delete => "CASCADE", on_update => "RESTRICT" }, +); + +=head2 default_category + +Type: belongs_to + +Related object: L + +=cut + +__PACKAGE__->belongs_to( + "default_category", + "Koha::Schema::Result::Category", + { categorycode => "default_category_id" }, + { + is_deferrable => 1, + join_type => "LEFT", + on_delete => "CASCADE", + on_update => "RESTRICT", + }, +); + +=head2 default_library + +Type: belongs_to + +Related object: L + +=cut + +__PACKAGE__->belongs_to( + "default_library", + "Koha::Schema::Result::Branch", + { branchcode => "default_library_id" }, + { + is_deferrable => 1, + join_type => "LEFT", + on_delete => "CASCADE", + on_update => "RESTRICT", + }, +); + + +# Created by DBIx::Class::Schema::Loader v0.07049 @ 2022-08-24 15:03:07 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:1b0q+e8Ym8icJ6bYAY/Mbw + +sub koha_object_class { + 'Koha::Auth::Provider::Domain'; +} +sub koha_objects_class { + 'Koha::Auth::Providers::Domains'; +} + +__PACKAGE__->add_columns( + '+auto_register' => { is_boolean => 1 }, + '+update_on_auth' => { is_boolean => 1 }, + '+allow_opac' => { is_boolean => 1 }, + '+allow_staff' => { is_boolean => 1 }, +); + +1; diff --git a/t/db_dependent/Koha/Auth/Provider.t b/t/db_dependent/Koha/Auth/Provider.t new file mode 100755 index 0000000000..cfe2ccf37a --- /dev/null +++ b/t/db_dependent/Koha/Auth/Provider.t @@ -0,0 +1,50 @@ +#!/usr/bin/perl + +# Copyright 2022 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 Test::More tests => 1; + +use Koha::Auth::Providers; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'domains() tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $provider = $builder->build_object({ class => 'Koha::Auth::Providers' }); + my $domains = $provider->domains; + + is( ref($domains), 'Koha::Auth::Provider::Domains', 'Type is correct' ); + is( $domains->count, 0, 'No domains defined' ); + + $builder->build_object({ class => 'Koha::Auth::Provider::Domains', value => { auth_provider_id => $provider->id } }); + $builder->build_object({ class => 'Koha::Auth::Provider::Domains', value => { auth_provider_id => $provider->id } }); + + is( $provider->domains->count, 2, 'The provider has 2 domains defined' ); + + $schema->storage->txn_rollback; +}; -- 2.20.1