Bug 17494: Prevent duplicate tokens from getting stored

Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Mason James <mtj@kohaaloha.com>
This commit is contained in:
Kyle M Hall 2016-10-31 11:15:02 +00:00 committed by Mason James
parent 5f160f675b
commit d76cd8a639
4 changed files with 55 additions and 3 deletions

View File

@ -23,6 +23,9 @@ use Carp;
use Koha::Database;
use Koha::Patron::Modifications;
use Koha::Exceptions::Patron::Modification;
use base qw(Koha::Object);
=head1 NAME
@ -31,7 +34,23 @@ Koha::AudioAlert - Koha Audio Alert object class
=head1 API
=head2 Class Methods
=head2 store
=cut
sub store {
my ($self) = @_;
if ( $self->verification_token ) {
if ( Koha::Patron::Modifications->search( { verification_token => $self->verification_token } )->count() ) {
Koha::Exceptions::Koha::Patron::Modification::DuplicateVerificationToken->throw;
}
}
return $self->SUPER::store();
}
=head2 approve
=head3 store

View File

@ -0,0 +1,12 @@
package Koha::Exceptions::Patron::Modification;
use Modern::Perl;
use Exception::Class (
'Koha::Exceptions::Koha::Patron::Modification::DuplicateVerificationToken' => {
isa => 'Koha::Exceptions::Object',
description => "The verification token given already exists",
},
);
1;

View File

@ -124,6 +124,11 @@ if ( $action eq 'create' ) {
$template->param( 'email' => $borrower{'email'} );
my $verification_token = md5_hex( time().{}.rand().{}.$$ );
while ( Koha::Patron::Modifications->search( { verification_token => $verification_token } )->count() ) {
$verification_token = md5_hex( time().{}.rand().{}.$$ );
}
$borrower{'password'} = random_string("..........");
Koha::Patron::Modifications->new(

View File

@ -1,10 +1,12 @@
#!/usr/bin/perl
use Modern::Perl;
use Test::More tests => 14;
use Test::More tests => 15;
use Try::Tiny;
use t::lib::TestBuilder;
use C4::Context;
use t::lib::TestBuilder;
use C4::Members;
use Koha::Patron::Modifications;
@ -19,6 +21,20 @@ $dbh->do("DELETE FROM borrower_modifications");
Koha::Patron::Modifications->new( verification_token => '1234567890' )
->AddModifications( { surname => 'Hall', firstname => 'Kyle' } );
## Ensure duplicate verification tokens cannot be added to the database
try {
Koha::Patron::Modification->new(
{
verification_token => '1234567890',
surname => 'Hall',
firstname => 'Daria'
}
)->store();
} catch {
ok( $_->isa('Koha::Exceptions::Koha::Patron::Modification::DuplicateVerificationToken'),
'Attempting to add a duplicate verification token to the database should raise a Koha::Exceptions::Koha::Patron::Modification::DuplicateVerificationToken exception' );
};
## Get the new pending modification
my $borrower = Koha::Patron::Modifications->GetModifications(
{ verification_token => '1234567890' } );