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 1146-1151 sub account_locked { Link Here
1146
    return 0;
1149
    return 0;
1147
}
1150
}
1148
1151
1152
=head3 lock
1153
1154
    Koha::Patrons->find($id)->lock({ expire => 1, remove => 1 });
1155
1156
    Lock and optionally expire a patron account.
1157
    Remove holds and article requests if remove flag set.
1158
    In order to distinguish from locking by entering a wrong password, let's
1159
    call this an administrative lockout.
1160
1161
=cut
1162
1163
sub lock {
1164
    my ( $self, $params ) = @_;
1165
    $self->login_attempts( ADMINISTRATIVE_LOCKOUT );
1166
    if( $params->{expire} ) {
1167
        $self->dateexpiry( dt_from_string->subtract(days => 1) );
1168
    }
1169
    $self->store;
1170
    if( $params->{remove} ) {
1171
        $self->holds->delete;
1172
        $self->article_requests->delete;
1173
    }
1174
    return $self;
1175
}
1176
1177
=head3 anonymize
1178
1179
    Koha::Patrons->find($id)->anonymize;
1180
1181
    Anonymize or clear borrower fields. Fields in BorrowerMandatoryField
1182
    are randomized, other personal data is cleared too.
1183
    Patrons with issues are skipped.
1184
1185
=cut
1186
1187
sub anonymize {
1188
    my ( $self ) = @_;
1189
    if( $self->_result->issues->count ) {
1190
        warn "Exiting anonymize: patron ".$self->borrowernumber." still has issues";
1191
        return;
1192
    }
1193
    my $mandatory = { map { (lc $_, 1); }
1194
        split /\s*\|\s*/, C4::Context->preference('BorrowerMandatoryField') };
1195
    $mandatory->{userid} = 1; # needed since sub store does not clear field
1196
    my @columns = $self->_result->result_source->columns;
1197
    @columns = grep { !/borrowernumber|branchcode|categorycode|^date|password|flags|updated_on|lastseen|lang|login_attempts|flgAnonymized/ } @columns;
1198
    push @columns, 'dateofbirth'; # add this date back in
1199
    foreach my $col (@columns) {
1200
        if( $mandatory->{lc $col} ) {
1201
            my $str = $self->_anonymize_column($col);
1202
            $self->$col($str);
1203
        } else {
1204
            $self->$col(undef);
1205
        }
1206
    }
1207
    $self->flgAnonymized(1)->store;
1208
}
1209
1210
sub _anonymize_column {
1211
    my ( $self, $col ) = @_;
1212
    my $type = $self->_result->result_source->column_info($col)->{data_type};
1213
    if( $type =~ /char|text/ ) {
1214
        return Koha::Token->new->generate({ pattern => '\w{10}' });
1215
    } elsif( $type =~ /integer|int$|float|dec|double/ ) {
1216
        return 0;
1217
    } elsif( $type =~ /date|time/ ) {
1218
        return dt_from_string;
1219
    }
1220
}
1221
1149
=head3 can_see_patron_infos
1222
=head3 can_see_patron_infos
1150
1223
1151
my $can_see = $patron->can_see_patron_infos( $patron );
1224
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