From 322042a71d401342d4720ffa0ddb2155628e7692 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 19 Apr 2013 09:58:02 +0200 Subject: [PATCH] Bug 10861: Add a check on cardnumber length Content-Type: text/plain; charset=utf-8 Some libraries would like to add a check on the cardnumber length. This patch adds the ability to restrict the cardnumber to a specific length (strictly equal to XX, or length > XX or min < length < max). This restriction is checked on inserting/updating a patron or on importing patrons. This patch adds: - 1 new syspref CardnumberLength. 2 formats: a number or a range (xx,yy). - 1 new unit test file t/Members/checkcardnumber.t for the C4::Members::checkcardnumber routine. Test plan: 1/ Fill the pref CardnumberLength with '5,8' 2/ Create a new patron with an invalid cardnumber (123456789) 3/ Check that you cannot save 4/ With Firebug, replace the pattern attribute value (for the cardnumber input) with ".{5,10}" 5/ You are allowed to save but an error occurred. 6/ Try the same steps for update. 7/ Go to the import borrowers tool. 8/ Play with the import borrowers tool. We must test add/update patrons and the "record matching" field (cardnumber or a uniq patron attribute) Signed-off-by: Marcel de Rooy Tested adding, updating; importing and ran unit test. Preliminary QA comments on Bugzilla --- C4/Members.pm | 43 +++++++++---- installer/data/mysql/updatedatabase.pl | 10 +++ .../prog/en/modules/admin/preferences/patrons.pref | 4 + .../prog/en/modules/members/memberentrygen.tt | 34 ++++++---- .../prog/en/modules/tools/import_borrowers.tt | 6 ++ members/memberentry.pl | 20 ++++++- t/Members/checkcardnumber.t | 66 ++++++++++++++++++++ tools/import_borrowers.pl | 13 ++++- 8 files changed, 166 insertions(+), 30 deletions(-) create mode 100644 t/Members/checkcardnumber.t diff --git a/C4/Members.pm b/C4/Members.pm index b04cc64..cbcb78f 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -1329,25 +1329,40 @@ sub checkuniquemember { } sub checkcardnumber { - my ($cardnumber,$borrowernumber) = @_; + my ( $cardnumber, $borrowernumber ) = @_; + # If cardnumber is null, we assume they're allowed. - return 0 if !defined($cardnumber); + return 0 unless defined $cardnumber; + my $dbh = C4::Context->dbh; my $query = "SELECT * FROM borrowers WHERE cardnumber=?"; $query .= " AND borrowernumber <> ?" if ($borrowernumber); - my $sth = $dbh->prepare($query); - if ($borrowernumber) { - $sth->execute($cardnumber,$borrowernumber); - } else { - $sth->execute($cardnumber); - } - if (my $data= $sth->fetchrow_hashref()){ - return 1; - } - else { - return 0; + my $sth = $dbh->prepare($query); + $sth->execute( + $cardnumber, + ( $borrowernumber ? $borrowernumber : () ) + ); + + return 1 if $sth->fetchrow_hashref; + + if ( my $length = C4::Context->preference('CardnumberLength') ) { + # Is integer and length match + if ( + $length =~ m|^\d+$| + and length $cardnumber == $length + ) { + return 0 + } + # Else assuming it is a range + else { + my $qr = qr|^\d{$length}$|; + return 0 + if $cardnumber =~ $qr; + } + return 1 } -} + return 0; +} =head2 getzipnamecity (OUEST-PROVENCE) diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index 19893b4..1eb2c23 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -7824,6 +7824,16 @@ if ( CheckVersion($DBversion) ) { SetVersion($DBversion); } +$DBversion = "3.15.00.XXX"; +if ( CheckVersion($DBversion) ) { + $dbh->do(" + INSERT INTO systempreferences (variable,value,options,explanation,type) + VALUES('CardnumberLength', '', '', 'Set a length for card numbers.', 'Free'); + "); + print "Upgrade to $DBversion done (Bug 10861: Add CardnumberLength syspref)\n"; + SetVersion ($DBversion); +} + =head1 FUNCTIONS diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref index 74d14af..a0c9dbf 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref @@ -139,3 +139,7 @@ Patrons: yes: Do no: "Don't" - enable the ability to upload and attach arbitrary files to a borrower record. + - + - Card numbers for patrons must be strictly equal to + - pref: CardnumberLength + - characters. The length can be a simple number or a range separated by a coma (xx,yy). diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt index 4aa88cf..10e3ef1 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt @@ -178,9 +178,9 @@ [% IF ( ERROR_login_exist ) %]
  • Username/password already exists.
  • [% END %] - [% IF ( ERROR_cardnumber ) %] -
  • Cardnumber already in use.
  • - [% END %] + [% IF ( ERROR_cardnumber ) %] +
  • Cardnumber already in use or not in a good format.
  • + [% END %] [% IF ( ERROR_age_limitations ) %]
  • Patron's age is incorrect for their category. Ages allowed are [% age_low %]-[% age_high %].
  • @@ -878,17 +878,23 @@
    Library management
      - [% UNLESS nocardnumber %] -
    1. [% IF ( mandatorycardnumber ) %] -
    2. - [% END %] - [% UNLESS nobranchcode %] + [% UNLESS nocardnumber %] +
    3. + [% IF ( mandatorycardnumber ) %] +
    4. + [% END %] + [% UNLESS nobranchcode %]