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 1142-1147 sub account_locked { Link Here
1142
    return 0;
1145
    return 0;
1143
}
1146
}
1144
1147
1148
=head3 lock
1149
1150
    Koha::Patrons->find($id)->lock({ expire => 1, remove => 1 });
1151
1152
    Lock and optionally expire a patron account.
1153
    Remove holds and article requests if remove flag set.
1154
    In order to distinguish from locking by entering a wrong password, let's
1155
    call this an administrative lockout.
1156
1157
=cut
1158
1159
sub lock {
1160
    my ( $self, $params ) = @_;
1161
    $self->login_attempts( ADMINISTRATIVE_LOCKOUT );
1162
    if( $params->{expire} ) {
1163
        $self->dateexpiry( dt_from_string->subtract(days => 1) );
1164
    }
1165
    $self->store;
1166
    if( $params->{remove} ) {
1167
        $self->holds->delete;
1168
        $self->article_requests->delete;
1169
    }
1170
    return $self;
1171
}
1172
1173
=head3 anonymize
1174
1175
    Koha::Patrons->find($id)->anonymize;
1176
1177
    Anonymize or clear borrower fields. Fields in BorrowerMandatoryField
1178
    are randomized, other personal data is cleared too.
1179
    Patrons with issues are skipped.
1180
1181
=cut
1182
1183
sub anonymize {
1184
    my ( $self ) = @_;
1185
    if( $self->_result->issues->count ) {
1186
        warn "Exiting anonymize: patron ".$self->borrowernumber." still has issues";
1187
        return;
1188
    }
1189
    my $mandatory = { map { (lc $_, 1); }
1190
        split /\s*\|\s*/, C4::Context->preference('BorrowerMandatoryField') };
1191
    $mandatory->{userid} = 1; # needed since sub store does not clear field
1192
    my @columns = $self->_result->result_source->columns;
1193
    @columns = grep { !/borrowernumber|branchcode|categorycode|^date|password|flags|updated_on|lastseen|lang|login_attempts|flgAnonymized/ } @columns;
1194
    push @columns, 'dateofbirth'; # add this date back in
1195
    foreach my $col (@columns) {
1196
        if( $mandatory->{lc $col} ) {
1197
            my $str = $self->_anonymize_column($col);
1198
            $self->$col($str);
1199
        } else {
1200
            $self->$col(undef);
1201
        }
1202
    }
1203
    $self->flgAnonymized(1)->store;
1204
}
1205
1206
sub _anonymize_column {
1207
    my ( $self, $col ) = @_;
1208
    my $type = $self->_result->result_source->column_info($col)->{data_type};
1209
    if( $type =~ /char|text/ ) {
1210
        return Koha::Token->new->generate({ pattern => '\w{10}' });
1211
    } elsif( $type =~ /integer|int$|float|dec|double/ ) {
1212
        return 0;
1213
    } elsif( $type =~ /date|time/ ) {
1214
        return dt_from_string;
1215
    }
1216
}
1217
1145
=head3 can_see_patron_infos
1218
=head3 can_see_patron_infos
1146
1219
1147
my $can_see = $patron->can_see_patron_infos( $patron );
1220
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 1454-1459 subtest 'Log cardnumber change' => sub { Link Here
1454
    is( scalar @logs, 2, 'With BorrowerLogs, Change in cardnumber should be logged, as well as general alert of patron mod.' );
1454
    is( scalar @logs, 2, 'With BorrowerLogs, Change in cardnumber should be logged, as well as general alert of patron mod.' );
1455
};
1455
};
1456
1456
1457
subtest 'search_unsubscribed' => sub {
1458
    plan tests => 4;
1459
1460
    t::lib::Mocks::mock_preference( 'FailedLoginAttempts', 3 );
1461
    t::lib::Mocks::mock_preference( 'UnsubscribeReflectionDelay', '' );
1462
    is( Koha::Patrons->search_unsubscribed->count, 0, 'Empty delay should return empty set' );
1463
1464
    my $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
1465
    my $patron2 = $builder->build_object({ class => 'Koha::Patrons' });
1466
1467
    t::lib::Mocks::mock_preference( 'UnsubscribeReflectionDelay', 0 );
1468
    Koha::Patron::Consents->delete; # for correct counts
1469
    Koha::Patron::Consent->new({ borrowernumber => $patron1->borrowernumber, type => 'GDPR_PROCESSING',  refused_on => dt_from_string })->store;
1470
    is( Koha::Patrons->search_unsubscribed->count, 1, 'Find patron1' );
1471
1472
    # Add another refusal but shift the period
1473
    t::lib::Mocks::mock_preference( 'UnsubscribeReflectionDelay', 2 );
1474
    Koha::Patron::Consent->new({ borrowernumber => $patron2->borrowernumber, type => 'GDPR_PROCESSING',  refused_on => dt_from_string->subtract(days=>2) })->store;
1475
    is( Koha::Patrons->search_unsubscribed->count, 1, 'Find patron2 only' );
1476
1477
    # Try another (special) attempts setting
1478
    t::lib::Mocks::mock_preference( 'FailedLoginAttempts', 0 );
1479
    # Lockout is now disabled
1480
    # Patron2 still matches: refused earlier, not locked
1481
    is( Koha::Patrons->search_unsubscribed->count, 1, 'Lockout disabled' );
1482
};
1483
1484
subtest 'search_anonymize_candidates' => sub {
1485
    plan tests => 5;
1486
    my $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
1487
    my $patron2 = $builder->build_object({ class => 'Koha::Patrons' });
1488
    $patron1->flgAnonymized(0);
1489
    $patron1->dateexpiry( dt_from_string->add(days => 1) )->store;
1490
    $patron2->flgAnonymized(undef);
1491
    $patron2->dateexpiry( dt_from_string->add(days => 1) )->store;
1492
1493
    t::lib::Mocks::mock_preference( 'PatronAnonymizeDelay', q{} );
1494
    is( Koha::Patrons->search_anonymize_candidates->count, 0, 'Empty set' );
1495
1496
    t::lib::Mocks::mock_preference( 'PatronAnonymizeDelay', 0 );
1497
    my $cnt = Koha::Patrons->search_anonymize_candidates->count;
1498
    $patron1->dateexpiry( dt_from_string->subtract(days => 1) )->store;
1499
    $patron2->dateexpiry( dt_from_string->subtract(days => 3) )->store;
1500
    is( Koha::Patrons->search_anonymize_candidates->count, $cnt+2, 'Delay 0' );
1501
1502
    t::lib::Mocks::mock_preference( 'PatronAnonymizeDelay', 2 );
1503
    $patron1->dateexpiry( dt_from_string->add(days => 1) )->store;
1504
    $patron2->dateexpiry( dt_from_string->add(days => 1) )->store;
1505
    $cnt = Koha::Patrons->search_anonymize_candidates->count;
1506
    $patron1->dateexpiry( dt_from_string->subtract(days => 1) )->store;
1507
    $patron2->dateexpiry( dt_from_string->subtract(days => 3) )->store;
1508
    is( Koha::Patrons->search_anonymize_candidates->count, $cnt+1, 'Delay 2' );
1509
1510
    t::lib::Mocks::mock_preference( 'PatronAnonymizeDelay', 4 );
1511
    $patron1->dateexpiry( dt_from_string->add(days => 1) )->store;
1512
    $patron2->dateexpiry( dt_from_string->add(days => 1) )->store;
1513
    $cnt = Koha::Patrons->search_anonymize_candidates->count;
1514
    $patron1->dateexpiry( dt_from_string->subtract(days => 1) )->store;
1515
    $patron2->dateexpiry( dt_from_string->subtract(days => 3) )->store;
1516
    is( Koha::Patrons->search_anonymize_candidates->count, $cnt, 'Delay 4' );
1517
1518
    t::lib::Mocks::mock_preference( 'FailedLoginAttempts', 3 );
1519
    $patron1->dateexpiry( dt_from_string->subtract(days => 5) )->store;
1520
    $patron1->login_attempts(0)->store;
1521
    $patron2->dateexpiry( dt_from_string->subtract(days => 5) )->store;
1522
    $patron2->login_attempts(0)->store;
1523
    $cnt = Koha::Patrons->search_anonymize_candidates({locked => 1})->count;
1524
    $patron1->login_attempts(3)->store;
1525
    is( Koha::Patrons->search_anonymize_candidates({locked => 1})->count,
1526
        $cnt+1, 'Locked flag' );
1527
};
1528
1529
subtest 'search_anonymized' => sub {
1530
    plan tests => 3;
1531
    my $patron1 = $builder->build_object( { class => 'Koha::Patrons' } );
1532
1533
    t::lib::Mocks::mock_preference( 'PatronRemovalDelay', q{} );
1534
    is( Koha::Patrons->search_anonymized->count, 0, 'Empty set' );
1535
1536
    t::lib::Mocks::mock_preference( 'PatronRemovalDelay', 1 );
1537
    $patron1->dateexpiry( dt_from_string );
1538
    $patron1->flgAnonymized(0)->store;
1539
    my $cnt = Koha::Patrons->search_anonymized->count;
1540
    $patron1->flgAnonymized(1)->store;
1541
    is( Koha::Patrons->search_anonymized->count, $cnt, 'Number unchanged' );
1542
    $patron1->dateexpiry( dt_from_string->subtract(days => 1) )->store;
1543
    is( Koha::Patrons->search_anonymized->count, $cnt+1, 'Found patron1' );
1544
};
1545
1546
subtest 'lock' => sub {
1547
    plan tests => 8;
1548
1549
    my $patron1 = $builder->build_object( { class => 'Koha::Patrons' } );
1550
    my $patron2 = $builder->build_object( { class => 'Koha::Patrons' } );
1551
    my $hold = $builder->build_object({
1552
        class => 'Koha::Holds',
1553
        value => { borrowernumber => $patron1->borrowernumber },
1554
    });
1555
1556
    t::lib::Mocks::mock_preference( 'FailedLoginAttempts', 3 );
1557
    my $expiry = dt_from_string->add(days => 1);
1558
    $patron1->dateexpiry( $expiry );
1559
    $patron1->lock;
1560
    is( $patron1->login_attempts, Koha::Patron::ADMINISTRATIVE_LOCKOUT, 'Check login_attempts' );
1561
    is( $patron1->dateexpiry, $expiry, 'Not expired yet' );
1562
    is( $patron1->holds->count, 1, 'No holds removed' );
1563
1564
    $patron1->lock({ expire => 1, remove => 1});
1565
    isnt( $patron1->dateexpiry, $expiry, 'Expiry date adjusted' );
1566
    is( $patron1->holds->count, 0, 'Holds removed' );
1567
1568
    # Disable lockout feature
1569
    t::lib::Mocks::mock_preference( 'FailedLoginAttempts', q{} );
1570
    $patron1->login_attempts(0);
1571
    $patron1->dateexpiry( $expiry );
1572
    $patron1->store;
1573
    $patron1->lock;
1574
    is( $patron1->login_attempts, Koha::Patron::ADMINISTRATIVE_LOCKOUT, 'Check login_attempts' );
1575
1576
    # Trivial wrapper test (Koha::Patrons->lock)
1577
    $patron1->login_attempts(0)->store;
1578
    Koha::Patrons->search({ borrowernumber => [ $patron1->borrowernumber, $patron2->borrowernumber ] })->lock;
1579
    $patron1->discard_changes; # refresh
1580
    $patron2->discard_changes;
1581
    is( $patron1->login_attempts, Koha::Patron::ADMINISTRATIVE_LOCKOUT, 'Check login_attempts patron 1' );
1582
    is( $patron2->login_attempts, Koha::Patron::ADMINISTRATIVE_LOCKOUT, 'Check login_attempts patron 2' );
1583
};
1584
1585
subtest 'anonymize' => sub {
1586
    plan tests => 9;
1587
1588
    my $patron1 = $builder->build_object( { class => 'Koha::Patrons' } );
1589
    my $patron2 = $builder->build_object( { class => 'Koha::Patrons' } );
1590
1591
    # First try patron with issues
1592
    my $issue = $builder->build_object({ class => 'Koha::Checkouts', value => { borrowernumber => $patron2->borrowernumber } });
1593
    warning_like { $patron2->anonymize } qr/still has issues/, 'Skip patron with issues';
1594
    $issue->delete;
1595
1596
    t::lib::Mocks::mock_preference( 'BorrowerMandatoryField', 'surname|email|cardnumber' );
1597
    my $surname = $patron1->surname; # expect change, no clear
1598
    my $branchcode = $patron1->branchcode; # expect skip
1599
    $patron1->anonymize;
1600
    is($patron1->flgAnonymized, 1, 'Check flag' );
1601
1602
    is( $patron1->dateofbirth, undef, 'Birth date cleared' );
1603
    is( $patron1->firstname, undef, 'First name cleared' );
1604
    isnt( $patron1->surname, $surname, 'Surname changed' );
1605
    ok( $patron1->surname =~ /^\w{10}$/, 'Mandatory surname randomized' );
1606
    is( $patron1->branchcode, $branchcode, 'Branch code skipped' );
1607
1608
    # Test wrapper in Koha::Patrons
1609
    $patron1->surname($surname)->store; # restore
1610
    my $rs = Koha::Patrons->search({ borrowernumber => [ $patron1->borrowernumber, $patron2->borrowernumber ] })->anonymize;
1611
    $patron1->discard_changes; # refresh
1612
    isnt( $patron1->surname, $surname, 'Surname patron1 changed again' );
1613
    $patron2->discard_changes; # refresh
1614
    is( $patron2->firstname, undef, 'First name patron2 cleared' );
1615
};
1616
1457
$schema->storage->txn_rollback;
1617
$schema->storage->txn_rollback;
1458
1618
1459
subtest 'Test Koha::Patrons::merge' => sub {
1619
subtest 'Test Koha::Patrons::merge' => sub {
1460
- 

Return to bug 21336