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

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

Return to bug 37711