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

(-)a/Koha/Patron.pm (+41 lines)
Lines 1158-1163 sub set_password { Link Here
1158
    return $self;
1158
    return $self;
1159
}
1159
}
1160
1160
1161
=head3 has_2fa_enabled
1162
1163
my $has_2fa = $patron->has_2fa_enabled;
1164
1165
Returns 1 if the patron has two-factor authentication enabled, 0 otherwise.
1166
1167
=cut
1168
1169
sub has_2fa_enabled {
1170
    my ($self) = @_;
1171
1172
    return ( defined $self->secret && $self->secret ne '' ) ? 1 : 0;
1173
}
1174
1175
=head3 reset_2fa
1176
1177
$patron->reset_2fa;
1178
1179
Resets the patron's two-factor authentication settings by clearing the secret
1180
and setting the auth_method back to 'password'.
1181
1182
Returns the patron object for method chaining.
1183
1184
=cut
1185
1186
sub reset_2fa {
1187
    my ($self) = @_;
1188
1189
    $self->set(
1190
        {
1191
            secret      => undef,
1192
            auth_method => 'password',
1193
        }
1194
    )->store;
1195
1196
    logaction( "MEMBERS", "RESET 2FA", $self->borrowernumber, "" )
1197
        if C4::Context->preference("BorrowersLog");
1198
1199
    return $self;
1200
}
1201
1161
=head3 renew_account
1202
=head3 renew_account
1162
1203
1163
my $new_expiry_date = $patron->renew_account
1204
my $new_expiry_date = $patron->renew_account
(-)a/t/db_dependent/Koha/Patron.t (-2 / +56 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 42;
22
use Test::More tests => 44;
23
use Test::NoWarnings;
23
use Test::NoWarnings;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::Warn;
25
use Test::Warn;
Lines 3309-3311 subtest 'is_anonymous' => sub { Link Here
3309
    $schema->storage->txn_rollback;
3309
    $schema->storage->txn_rollback;
3310
3310
3311
};
3311
};
3312
- 
3312
3313
subtest 'has_2fa_enabled() tests' => sub {
3314
    plan tests => 3;
3315
3316
    $schema->storage->txn_begin;
3317
3318
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
3319
3320
    # Test patron without 2FA
3321
    is( $patron->has_2fa_enabled, 0, 'has_2fa_enabled returns 0 when no secret is set' );
3322
3323
    # Test patron with empty secret
3324
    $patron->secret('')->store;
3325
    is( $patron->has_2fa_enabled, 0, 'has_2fa_enabled returns 0 when secret is empty string' );
3326
3327
    # Test patron with 2FA enabled
3328
    $patron->secret('some_secret_value')->store;
3329
    is( $patron->has_2fa_enabled, 1, 'has_2fa_enabled returns 1 when secret is set' );
3330
3331
    $schema->storage->txn_rollback;
3332
};
3333
3334
subtest 'reset_2fa() tests' => sub {
3335
    plan tests => 6;
3336
3337
    $schema->storage->txn_begin;
3338
3339
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
3340
3341
    # Set up patron with 2FA enabled
3342
    $patron->set(
3343
        {
3344
            secret      => 'test_secret_123',
3345
            auth_method => 'two-factor',
3346
        }
3347
    )->store;
3348
3349
    # Verify 2FA is enabled
3350
    is( $patron->has_2fa_enabled, 1,            '2FA is enabled before reset' );
3351
    is( $patron->auth_method,     'two-factor', 'auth_method is two-factor before reset' );
3352
3353
    # Test reset_2fa method
3354
    my $result = $patron->reset_2fa;
3355
3356
    # Verify method returns patron object for chaining
3357
    isa_ok( $result, 'Koha::Patron', 'reset_2fa returns Koha::Patron object' );
3358
    is( $result->borrowernumber, $patron->borrowernumber, 'returned object is the same patron' );
3359
3360
    # Verify 2FA has been reset
3361
    $patron->discard_changes;
3362
    is( $patron->has_2fa_enabled, 0,          '2FA is disabled after reset' );
3363
    is( $patron->auth_method,     'password', 'auth_method is password after reset' );
3364
3365
    $schema->storage->txn_rollback;
3366
};

Return to bug 40545