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

(-)a/Koha/Patron.pm (+73 lines)
Lines 44-52 use Koha::Virtualshelves; Link Here
44
use Koha::Club::Enrollments;
44
use Koha::Club::Enrollments;
45
use Koha::Account;
45
use Koha::Account;
46
use Koha::Subscription::Routinglists;
46
use Koha::Subscription::Routinglists;
47
use Koha::Token;
47
48
48
use base qw(Koha::Object);
49
use base qw(Koha::Object);
49
50
51
use constant ADMINISTRATIVE_LOCKOUT => -1;
52
50
our $RESULTSET_PATRON_ID_MAPPING = {
53
our $RESULTSET_PATRON_ID_MAPPING = {
51
    Accountline          => 'borrowernumber',
54
    Accountline          => 'borrowernumber',
52
    Aqbasketuser         => 'borrowernumber',
55
    Aqbasketuser         => 'borrowernumber',
Lines 1139-1144 sub account_locked { Link Here
1139
    return 0;
1142
    return 0;
1140
}
1143
}
1141
1144
1145
=head3 lock
1146
1147
    Koha::Patrons->find($id)->lock({ expire => 1, remove => 1 });
1148
1149
    Lock and optionally expire a patron account.
1150
    Remove holds and article requests if remove flag set.
1151
    In order to distinguish from locking by entering a wrong password, let's
1152
    call this an administrative lockout.
1153
1154
=cut
1155
1156
sub lock {
1157
    my ( $self, $params ) = @_;
1158
    $self->login_attempts( ADMINISTRATIVE_LOCKOUT );
1159
    if( $params->{expire} ) {
1160
        $self->dateexpiry( dt_from_string->subtract(days => 1) );
1161
    }
1162
    $self->store;
1163
    if( $params->{remove} ) {
1164
        $self->holds->delete;
1165
        $self->article_requests->delete;
1166
    }
1167
    return $self;
1168
}
1169
1170
=head3 anonymize
1171
1172
    Koha::Patrons->find($id)->anonymize;
1173
1174
    Anonymize or clear borrower fields. Fields in BorrowerMandatoryField
1175
    are randomized, other personal data is cleared too.
1176
    Patrons with issues are skipped.
1177
1178
=cut
1179
1180
sub anonymize {
1181
    my ( $self ) = @_;
1182
    if( $self->_result->issues->count ) {
1183
        warn "Exiting anonymize: patron ".$self->borrowernumber." still has issues";
1184
        return;
1185
    }
1186
    my $mandatory = { map { (lc $_, 1); }
1187
        split /\s*\|\s*/, C4::Context->preference('BorrowerMandatoryField') };
1188
    $mandatory->{userid} = 1; # needed since sub store does not clear field
1189
    my @columns = $self->_result->result_source->columns;
1190
    @columns = grep { !/borrowernumber|branchcode|categorycode|^date|password|flags|updated_on|lastseen|lang|login_attempts|flgAnonymized/ } @columns;
1191
    push @columns, 'dateofbirth'; # add this date back in
1192
    foreach my $col (@columns) {
1193
        if( $mandatory->{lc $col} ) {
1194
            my $str = $self->_anonymize_column($col);
1195
            $self->$col($str);
1196
        } else {
1197
            $self->$col(undef);
1198
        }
1199
    }
1200
    $self->flgAnonymized(1)->store;
1201
}
1202
1203
sub _anonymize_column {
1204
    my ( $self, $col ) = @_;
1205
    my $type = $self->_result->result_source->column_info($col)->{data_type};
1206
    if( $type =~ /char|text/ ) {
1207
        return Koha::Token->new->generate({ pattern => '\w{10}' });
1208
    } elsif( $type =~ /integer|int$|float|dec|double/ ) {
1209
        return 0;
1210
    } elsif( $type =~ /date|time/ ) {
1211
        return dt_from_string;
1212
    }
1213
}
1214
1142
=head3 can_see_patron_infos
1215
=head3 can_see_patron_infos
1143
1216
1144
my $can_see = $patron->can_see_patron_infos( $patron );
1217
my $can_see = $patron->can_see_patron_infos( $patron );
(-)a/Koha/Patrons.pm (+122 lines)
Lines 236-241 sub delete { Link Here
236
    return $patrons_deleted;
236
    return $patrons_deleted;
237
}
237
}
238
238
239
=head3 search_unsubscribed
240
241
    Koha::Patrons->search_unsubscribed;
242
243
    Returns a set of Koha patron objects for patrons that recently
244
    unsubscribed and are not locked (candidates for locking).
245
    Depends on UnsubscribeReflectionDelay.
246
247
=cut
248
249
sub search_unsubscribed {
250
    my ( $class ) = @_;
251
252
    my $delay = C4::Context->preference('UnsubscribeReflectionDelay');
253
    if( !defined($delay) || $delay eq q{} ) {
254
        # return empty set
255
        return $class->search({ borrowernumber => undef });
256
    }
257
    my $parser = Koha::Database->new->schema->storage->datetime_parser;
258
    my $dt = dt_from_string()->subtract( days => $delay );
259
    my $str = $parser->format_datetime($dt);
260
    my $fails = C4::Context->preference('FailedLoginAttempts') || 0;
261
    my $cond = [ undef, 0, 1..$fails-1 ]; # NULL, 0, 1..fails-1 (if fails>0)
262
    return $class->search(
263
        {
264
            'patron_consents.refused_on' => { '<=' => $str },
265
            'login_attempts' => $cond,
266
        },
267
        { join => 'patron_consents' },
268
    );
269
}
270
271
=head3 search_anonymize_candidates
272
273
    Koha::Patrons->search_anonymize_candidates({ locked => 1 });
274
275
    Returns a set of Koha patron objects for patrons whose account is expired
276
    and locked (if parameter set). These are candidates for anonymizing.
277
    Depends on PatronAnonymizeDelay.
278
279
=cut
280
281
sub search_anonymize_candidates {
282
    my ( $class, $params ) = @_;
283
284
    my $delay = C4::Context->preference('PatronAnonymizeDelay');
285
    if( !defined($delay) || $delay eq q{} ) {
286
        # return empty set
287
        return $class->search({ borrowernumber => undef });
288
    }
289
    my $cond = {};
290
    my $parser = Koha::Database->new->schema->storage->datetime_parser;
291
    my $dt = dt_from_string()->subtract( days => $delay );
292
    my $str = $parser->format_datetime($dt);
293
    $cond->{dateexpiry} = { '<=' => $str };
294
    $cond->{flgAnonymized} = [ undef, 0 ]; # not yet done
295
    if( $params->{locked} ) {
296
        my $fails = C4::Context->preference('FailedLoginAttempts');
297
        $cond->{login_attempts} = [ -and => { '!=' => undef }, { -not_in => [0, 1..$fails-1 ] } ]; # -not_in does not like undef
298
    }
299
    return $class->search( $cond );
300
}
301
302
=head3 search_anonymized
303
304
    Koha::Patrons->search_anonymized;
305
306
    Returns a set of Koha patron objects for patron accounts that have been
307
    anonymized before and could be removed.
308
    Depends on PatronRemovalDelay.
309
310
=cut
311
312
sub search_anonymized {
313
    my ( $class ) = @_;
314
315
    my $delay = C4::Context->preference('PatronRemovalDelay');
316
    if( !defined($delay) || $delay eq q{} ) {
317
        # return empty set
318
        return $class->search({ borrowernumber => undef });
319
    }
320
    my $cond = {};
321
    my $parser = Koha::Database->new->schema->storage->datetime_parser;
322
    my $dt = dt_from_string()->subtract( days => $delay );
323
    my $str = $parser->format_datetime($dt);
324
    $cond->{dateexpiry} = { '<=' => $str };
325
    $cond->{flgAnonymized} = 1;
326
    return $class->search( $cond );
327
}
328
329
=head3 lock
330
331
    Koha::Patrons->search({ some filters })->lock({ expire => 1, remove => 1 })
332
333
    Lock the passed set of patron objects. Optionally expire and remove holds.
334
    Wrapper around Koha::Patron->lock.
335
336
=cut
337
338
sub lock {
339
    my ( $self, $params ) = @_;
340
    while( my $patron = $self->next ) {
341
        $patron->lock($params);
342
    }
343
}
344
345
=head3 anonymize
346
347
    Koha::Patrons->search({ some filters })->anonymize;
348
349
    Anonymize passed set of patron objects.
350
    Wrapper around Koha::Patron->anonymize.
351
352
=cut
353
354
sub anonymize {
355
    my ( $self ) = @_;
356
    while( my $patron = $self->next ) {
357
        $patron->anonymize;
358
    }
359
}
360
239
=head3 _type
361
=head3 _type
240
362
241
=cut
363
=cut
(-)a/t/db_dependent/Koha/Patrons.t (-2 / +161 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 34;
22
use Test::More tests => 39;
23
use Test::Warn;
23
use Test::Warn;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
Lines 1453-1458 subtest 'Log cardnumber change' => sub { Link Here
1453
    is( scalar @logs, 2, 'With BorrowerLogs, Change in cardnumber should be logged, as well as general alert of patron mod.' );
1453
    is( scalar @logs, 2, 'With BorrowerLogs, Change in cardnumber should be logged, as well as general alert of patron mod.' );
1454
};
1454
};
1455
1455
1456
subtest 'search_unsubscribed' => sub {
1457
    plan tests => 4;
1458
1459
    t::lib::Mocks::mock_preference( 'FailedLoginAttempts', 3 );
1460
    t::lib::Mocks::mock_preference( 'UnsubscribeReflectionDelay', '' );
1461
    is( Koha::Patrons->search_unsubscribed->count, 0, 'Empty delay should return empty set' );
1462
1463
    my $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
1464
    my $patron2 = $builder->build_object({ class => 'Koha::Patrons' });
1465
1466
    t::lib::Mocks::mock_preference( 'UnsubscribeReflectionDelay', 0 );
1467
    Koha::Patron::Consents->delete; # for correct counts
1468
    Koha::Patron::Consent->new({ borrowernumber => $patron1->borrowernumber, type => 'GDPR_PROCESSING',  refused_on => dt_from_string })->store;
1469
    is( Koha::Patrons->search_unsubscribed->count, 1, 'Find patron1' );
1470
1471
    # Add another refusal but shift the period
1472
    t::lib::Mocks::mock_preference( 'UnsubscribeReflectionDelay', 2 );
1473
    Koha::Patron::Consent->new({ borrowernumber => $patron2->borrowernumber, type => 'GDPR_PROCESSING',  refused_on => dt_from_string->subtract(days=>2) })->store;
1474
    is( Koha::Patrons->search_unsubscribed->count, 1, 'Find patron2 only' );
1475
1476
    # Try another (special) attempts setting
1477
    t::lib::Mocks::mock_preference( 'FailedLoginAttempts', 0 );
1478
    # Lockout is now disabled
1479
    # Patron2 still matches: refused earlier, not locked
1480
    is( Koha::Patrons->search_unsubscribed->count, 1, 'Lockout disabled' );
1481
};
1482
1483
subtest 'search_anonymize_candidates' => sub {
1484
    plan tests => 5;
1485
    my $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
1486
    my $patron2 = $builder->build_object({ class => 'Koha::Patrons' });
1487
    $patron1->flgAnonymized(0);
1488
    $patron1->dateexpiry( dt_from_string->add(days => 1) )->store;
1489
    $patron2->flgAnonymized(undef);
1490
    $patron2->dateexpiry( dt_from_string->add(days => 1) )->store;
1491
1492
    t::lib::Mocks::mock_preference( 'PatronAnonymizeDelay', q{} );
1493
    is( Koha::Patrons->search_anonymize_candidates->count, 0, 'Empty set' );
1494
1495
    t::lib::Mocks::mock_preference( 'PatronAnonymizeDelay', 0 );
1496
    my $cnt = Koha::Patrons->search_anonymize_candidates->count;
1497
    $patron1->dateexpiry( dt_from_string->subtract(days => 1) )->store;
1498
    $patron2->dateexpiry( dt_from_string->subtract(days => 3) )->store;
1499
    is( Koha::Patrons->search_anonymize_candidates->count, $cnt+2, 'Delay 0' );
1500
1501
    t::lib::Mocks::mock_preference( 'PatronAnonymizeDelay', 2 );
1502
    $patron1->dateexpiry( dt_from_string->add(days => 1) )->store;
1503
    $patron2->dateexpiry( dt_from_string->add(days => 1) )->store;
1504
    $cnt = Koha::Patrons->search_anonymize_candidates->count;
1505
    $patron1->dateexpiry( dt_from_string->subtract(days => 1) )->store;
1506
    $patron2->dateexpiry( dt_from_string->subtract(days => 3) )->store;
1507
    is( Koha::Patrons->search_anonymize_candidates->count, $cnt+1, 'Delay 2' );
1508
1509
    t::lib::Mocks::mock_preference( 'PatronAnonymizeDelay', 4 );
1510
    $patron1->dateexpiry( dt_from_string->add(days => 1) )->store;
1511
    $patron2->dateexpiry( dt_from_string->add(days => 1) )->store;
1512
    $cnt = Koha::Patrons->search_anonymize_candidates->count;
1513
    $patron1->dateexpiry( dt_from_string->subtract(days => 1) )->store;
1514
    $patron2->dateexpiry( dt_from_string->subtract(days => 3) )->store;
1515
    is( Koha::Patrons->search_anonymize_candidates->count, $cnt, 'Delay 4' );
1516
1517
    t::lib::Mocks::mock_preference( 'FailedLoginAttempts', 3 );
1518
    $patron1->dateexpiry( dt_from_string->subtract(days => 5) )->store;
1519
    $patron1->login_attempts(0)->store;
1520
    $patron2->dateexpiry( dt_from_string->subtract(days => 5) )->store;
1521
    $patron2->login_attempts(0)->store;
1522
    $cnt = Koha::Patrons->search_anonymize_candidates({locked => 1})->count;
1523
    $patron1->login_attempts(3)->store;
1524
    is( Koha::Patrons->search_anonymize_candidates({locked => 1})->count,
1525
        $cnt+1, 'Locked flag' );
1526
};
1527
1528
subtest 'search_anonymized' => sub {
1529
    plan tests => 3;
1530
    my $patron1 = $builder->build_object( { class => 'Koha::Patrons' } );
1531
1532
    t::lib::Mocks::mock_preference( 'PatronRemovalDelay', q{} );
1533
    is( Koha::Patrons->search_anonymized->count, 0, 'Empty set' );
1534
1535
    t::lib::Mocks::mock_preference( 'PatronRemovalDelay', 1 );
1536
    $patron1->dateexpiry( dt_from_string );
1537
    $patron1->flgAnonymized(0)->store;
1538
    my $cnt = Koha::Patrons->search_anonymized->count;
1539
    $patron1->flgAnonymized(1)->store;
1540
    is( Koha::Patrons->search_anonymized->count, $cnt, 'Number unchanged' );
1541
    $patron1->dateexpiry( dt_from_string->subtract(days => 1) )->store;
1542
    is( Koha::Patrons->search_anonymized->count, $cnt+1, 'Found patron1' );
1543
};
1544
1545
subtest 'lock' => sub {
1546
    plan tests => 8;
1547
1548
    my $patron1 = $builder->build_object( { class => 'Koha::Patrons' } );
1549
    my $patron2 = $builder->build_object( { class => 'Koha::Patrons' } );
1550
    my $hold = $builder->build_object({
1551
        class => 'Koha::Holds',
1552
        value => { borrowernumber => $patron1->borrowernumber },
1553
    });
1554
1555
    t::lib::Mocks::mock_preference( 'FailedLoginAttempts', 3 );
1556
    my $expiry = dt_from_string->add(days => 1);
1557
    $patron1->dateexpiry( $expiry );
1558
    $patron1->lock;
1559
    is( $patron1->login_attempts, Koha::Patron::ADMINISTRATIVE_LOCKOUT, 'Check login_attempts' );
1560
    is( $patron1->dateexpiry, $expiry, 'Not expired yet' );
1561
    is( $patron1->holds->count, 1, 'No holds removed' );
1562
1563
    $patron1->lock({ expire => 1, remove => 1});
1564
    isnt( $patron1->dateexpiry, $expiry, 'Expiry date adjusted' );
1565
    is( $patron1->holds->count, 0, 'Holds removed' );
1566
1567
    # Disable lockout feature
1568
    t::lib::Mocks::mock_preference( 'FailedLoginAttempts', q{} );
1569
    $patron1->login_attempts(0);
1570
    $patron1->dateexpiry( $expiry );
1571
    $patron1->store;
1572
    $patron1->lock;
1573
    is( $patron1->login_attempts, Koha::Patron::ADMINISTRATIVE_LOCKOUT, 'Check login_attempts' );
1574
1575
    # Trivial wrapper test (Koha::Patrons->lock)
1576
    $patron1->login_attempts(0)->store;
1577
    Koha::Patrons->search({ borrowernumber => [ $patron1->borrowernumber, $patron2->borrowernumber ] })->lock;
1578
    $patron1->discard_changes; # refresh
1579
    $patron2->discard_changes;
1580
    is( $patron1->login_attempts, Koha::Patron::ADMINISTRATIVE_LOCKOUT, 'Check login_attempts patron 1' );
1581
    is( $patron2->login_attempts, Koha::Patron::ADMINISTRATIVE_LOCKOUT, 'Check login_attempts patron 2' );
1582
};
1583
1584
subtest 'anonymize' => sub {
1585
    plan tests => 9;
1586
1587
    my $patron1 = $builder->build_object( { class => 'Koha::Patrons' } );
1588
    my $patron2 = $builder->build_object( { class => 'Koha::Patrons' } );
1589
1590
    # First try patron with issues
1591
    my $issue = $builder->build_object({ class => 'Koha::Checkouts', value => { borrowernumber => $patron2->borrowernumber } });
1592
    warning_like { $patron2->anonymize } qr/still has issues/, 'Skip patron with issues';
1593
    $issue->delete;
1594
1595
    t::lib::Mocks::mock_preference( 'BorrowerMandatoryField', 'surname|email|cardnumber' );
1596
    my $surname = $patron1->surname; # expect change, no clear
1597
    my $branchcode = $patron1->branchcode; # expect skip
1598
    $patron1->anonymize;
1599
    is($patron1->flgAnonymized, 1, 'Check flag' );
1600
1601
    is( $patron1->dateofbirth, undef, 'Birth date cleared' );
1602
    is( $patron1->firstname, undef, 'First name cleared' );
1603
    isnt( $patron1->surname, $surname, 'Surname changed' );
1604
    ok( $patron1->surname =~ /^\w{10}$/, 'Mandatory surname randomized' );
1605
    is( $patron1->branchcode, $branchcode, 'Branch code skipped' );
1606
1607
    # Test wrapper in Koha::Patrons
1608
    $patron1->surname($surname)->store; # restore
1609
    my $rs = Koha::Patrons->search({ borrowernumber => [ $patron1->borrowernumber, $patron2->borrowernumber ] })->anonymize;
1610
    $patron1->discard_changes; # refresh
1611
    isnt( $patron1->surname, $surname, 'Surname patron1 changed again' );
1612
    $patron2->discard_changes; # refresh
1613
    is( $patron2->firstname, undef, 'First name patron2 cleared' );
1614
};
1615
1456
$schema->storage->txn_rollback;
1616
$schema->storage->txn_rollback;
1457
1617
1458
subtest 'Test Koha::Patrons::merge' => sub {
1618
subtest 'Test Koha::Patrons::merge' => sub {
1459
- 

Return to bug 21336