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

(-)a/Koha/REST/Plugin/Auth/IdP.pm (-4 / +20 lines)
Lines 28-33 use Koha::Patrons; Link Here
28
use C4::Auth qw(create_basic_session);
28
use C4::Auth qw(create_basic_session);
29
29
30
use CGI;
30
use CGI;
31
use List::MoreUtils qw(any);
31
32
32
=head1 NAME
33
=head1 NAME
33
34
Lines 51-64 sub register { Link Here
51
=head3 auth.register
52
=head3 auth.register
52
53
53
    my $patron = $c->auth->register(
54
    my $patron = $c->auth->register(
54
        {   data      => $patron_data,
55
        {
56
            data      => $patron_data,
55
            domain    => $domain,
57
            domain    => $domain,
56
            interface => $interface
58
            interface => $interface
57
        }
59
        }
58
    );
60
    );
59
61
60
If no patron passed, creates a new I<Koha::Patron> if the provider is configured
62
This helper creates a new I<Koha::Patron> using the (already) mapped data
61
to do so for the domain.
63
provided in the I<data> attribute.
64
65
A check is done on the passed I<interface> and I<domain> to validate
66
the provider is configured to allow auto registration.
67
68
Valid values for B<interface> are I<opac> and I<staff>. An exception will be thrown
69
if other values or none are passed.
62
70
63
=cut
71
=cut
64
72
Lines 69-75 to do so for the domain. Link Here
69
            my $domain    = $params->{domain};
77
            my $domain    = $params->{domain};
70
            my $interface = $params->{interface};
78
            my $interface = $params->{interface};
71
79
72
            unless ( $interface eq 'opac' && $domain->auto_register ) {
80
            Koha::Exceptions::MissingParameter->throw( parameter => 'interface' )
81
                unless $interface;
82
83
            Koha::Exceptions::BadParameter->throw( parameter => 'interface' )
84
                unless any { $interface eq $_ } qw{ opac staff };
85
86
            if (   $interface eq 'opac' && !$domain->auto_register_opac
87
                || $interface eq 'staff' && !$domain->auto_register_staff )
88
            {
73
                Koha::Exceptions::Auth::Unauthorized->throw( code => 401 );
89
                Koha::Exceptions::Auth::Unauthorized->throw( code => 401 );
74
            }
90
            }
75
91
(-)a/t/db_dependent/Koha/REST/Plugin/Auth/IdP.t (-24 / +63 lines)
Lines 45-50 post '/register_user' => sub { Link Here
45
    } catch {
45
    } catch {
46
        if ( ref($_) eq 'Koha::Exceptions::Auth::Unauthorized' ) {
46
        if ( ref($_) eq 'Koha::Exceptions::Auth::Unauthorized' ) {
47
            $c->render( status => 401, json => { message => 'unauthorized' } );
47
            $c->render( status => 401, json => { message => 'unauthorized' } );
48
        } elsif ( ref($_) eq 'Koha::Exceptions::BadParameter' ) {
49
            $c->render( status => 400, json => { message => 'bad parameter: ' . $_->parameter } );
50
        } elsif ( ref($_) eq 'Koha::Exceptions::MissingParameter' ) {
51
            $c->render( status => 400, json => { message => 'missing parameter: ' . $_->parameter } );
48
        } else {
52
        } else {
49
            $c->render( status => 500, json => { message => 'other error' } );
53
            $c->render( status => 500, json => { message => 'other error' } );
50
        }
54
        }
Lines 79-141 use Koha::Database; Link Here
79
my $schema  = Koha::Database->new()->schema();
83
my $schema  = Koha::Database->new()->schema();
80
my $builder = t::lib::TestBuilder->new;
84
my $builder = t::lib::TestBuilder->new;
81
85
86
my $t = Test::Mojo->new;
87
82
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
88
# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling
83
# this affects the other REST api tests
89
# this affects the other REST api tests
84
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
90
t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
85
91
86
subtest 'auth.register helper' => sub {
92
subtest 'auth.register helper' => sub {
87
    plan tests => 6;
93
94
    plan tests => 18;
88
95
89
    $schema->storage->txn_begin;
96
    $schema->storage->txn_begin;
90
97
91
    # generate a random patron
98
    # generate a random patron
92
    my $patron_to_delete = $builder->build_object( { class => 'Koha::Patrons' } );
99
    my $patron_to_delete_1 = $builder->build_object( { class => 'Koha::Patrons' } );
93
    my $userid           = $patron_to_delete->userid;
100
    my $patron_to_delete_2 = $builder->build_object( { class => 'Koha::Patrons' } );
101
    my $userid_1           = $patron_to_delete_1->userid;
102
    my $userid_2           = $patron_to_delete_2->userid;
94
103
95
    # delete patron
104
    # delete patron
96
    $patron_to_delete->delete;
105
    $patron_to_delete_1->delete;
106
    $patron_to_delete_2->delete;
97
107
98
    my $provider =
108
    my $provider =
99
        $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } );
109
        $builder->build_object( { class => 'Koha::Auth::Identity::Providers', value => { matchpoint => 'email' } } );
100
110
101
    my $domain_with_register = $builder->build_object(
111
    my $domain_1 = $builder->build_object(
102
        {
112
        {
103
            class => 'Koha::Auth::Identity::Provider::Domains',
113
            class => 'Koha::Auth::Identity::Provider::Domains',
104
            value => { identity_provider_id => $provider->id, domain => 'domain1.com', auto_register => 1 }
114
            value => {
115
                identity_provider_id => $provider->id,
116
                domain               => 'domain1.com',
117
                auto_register_opac   => 1,
118
                auto_register_staff  => 0
119
            }
105
        }
120
        }
106
    );
121
    );
107
122
108
    my $domain_without_register = $builder->build_object(
123
    my $domain_2 = $builder->build_object(
109
        {
124
        {
110
            class => 'Koha::Auth::Identity::Provider::Domains',
125
            class => 'Koha::Auth::Identity::Provider::Domains',
111
            value => { identity_provider_id => $provider->id, domain => 'domain2.com', auto_register => 0 }
126
            value => {
127
                identity_provider_id => $provider->id,
128
                domain               => 'domain2.com',
129
                auto_register_opac   => 0,
130
                auto_register_staff  => 1
131
            }
112
        }
132
        }
113
    );
133
    );
114
134
115
    my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
135
    my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
116
    my $category = $builder->build_object( { class => 'Koha::Patron::Categories' } );
136
    my $category = $builder->build_object( { class => 'Koha::Patron::Categories' } );
117
137
118
    my $user_data = {
119
        firstname    => 'test',
120
        surname      => 'test',
121
        userid       => $userid,
122
        branchcode   => $library->branchcode,
123
        categorycode => $category->categorycode
124
    };
125
126
    my $t = Test::Mojo->new;
127
128
    $t->post_ok(
138
    $t->post_ok(
129
        '/register_user' => json => {
139
        '/register_user' => json => {
130
            data => $user_data, domain_id => $domain_with_register->identity_provider_domain_id, interface => 'opac'
140
            data => {
141
                firstname    => 'test',
142
                surname      => 'test',
143
                userid       => $userid_1,
144
                branchcode   => $library->branchcode,
145
                categorycode => $category->categorycode
146
            },
147
            domain_id => $domain_1->identity_provider_domain_id,
148
            interface => 'opac'
131
        }
149
        }
132
    )->status_is(200)->json_has( '/firstname', 'test' );
150
    )->status_is(200)->json_is( '/userid', $userid_1 );
151
152
    $t->post_ok( '/register_user' => json =>
153
            { data => {}, domain_id => $domain_2->identity_provider_domain_id, interface => 'opac' } )->status_is(401)
154
        ->json_is( '/message', 'unauthorized' );
155
156
    $t->post_ok( '/register_user' => json =>
157
            { data => {}, domain_id => $domain_1->identity_provider_domain_id, interface => 'staff' } )->status_is(401)
158
        ->json_is( '/message', 'unauthorized' );
133
159
134
    $t->post_ok(
160
    $t->post_ok(
135
        '/register_user' => json => {
161
        '/register_user' => json => {
136
            data => $user_data, domain_id => $domain_without_register->identity_provider_domain_id, interface => 'opac'
162
            data => {
163
                firstname    => 'test',
164
                surname      => 'test',
165
                userid       => $userid_2,
166
                branchcode   => $library->branchcode,
167
                categorycode => $category->categorycode
168
            },
169
            domain_id => $domain_2->identity_provider_domain_id,
170
            interface => 'staff'
137
        }
171
        }
138
    )->status_is(401)->json_has( '/message', 'unauthorized' );
172
    )->status_is(200)->json_is( '/userid', $userid_2 );
173
174
    $t->post_ok( '/register_user' => json => { data => {}, domain_id => $domain_1->identity_provider_domain_id } )
175
        ->status_is(400)->json_is( '/message', 'missing parameter: interface' );
176
177
    $t->post_ok( '/register_user' => json =>
178
            { data => {}, domain_id => $domain_1->identity_provider_domain_id, interface => 'invalid' } )
179
        ->status_is(400)->json_is( '/message', 'bad parameter: interface' );
139
180
140
    $schema->storage->txn_rollback;
181
    $schema->storage->txn_rollback;
141
};
182
};
Lines 147-153 subtest 'auth.session helper' => sub { Link Here
147
188
148
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
189
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
149
190
150
    my $t = Test::Mojo->new;
151
    $t->post_ok( '/start_session' => json => { userid => $patron->userid } )->status_is(200)
191
    $t->post_ok( '/start_session' => json => { userid => $patron->userid } )->status_is(200)
152
        ->json_has( '/status', 'ok' );
192
        ->json_has( '/status', 'ok' );
153
193
154
- 

Return to bug 37711