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

(-)a/Koha/Patron.pm (+41 lines)
Lines 1139-1144 sub set_password { Link Here
1139
    return $self;
1139
    return $self;
1140
}
1140
}
1141
1141
1142
=head3 has_2fa_enabled
1143
1144
my $has_2fa = $patron->has_2fa_enabled;
1145
1146
Returns 1 if the patron has two-factor authentication enabled, 0 otherwise.
1147
1148
=cut
1149
1150
sub has_2fa_enabled {
1151
    my ($self) = @_;
1152
1153
    return ( defined $self->secret && $self->secret ne '' ) ? 1 : 0;
1154
}
1155
1156
=head3 reset_2fa
1157
1158
$patron->reset_2fa;
1159
1160
Resets the patron's two-factor authentication settings by clearing the secret
1161
and setting the auth_method back to 'password'.
1162
1163
Returns the patron object for method chaining.
1164
1165
=cut
1166
1167
sub reset_2fa {
1168
    my ($self) = @_;
1169
1170
    $self->set(
1171
        {
1172
            secret      => undef,
1173
            auth_method => 'password',
1174
        }
1175
    )->store;
1176
1177
    logaction( "MEMBERS", "RESET 2FA", $self->borrowernumber, "" )
1178
        if C4::Context->preference("BorrowersLog");
1179
1180
    return $self;
1181
}
1182
1142
=head3 renew_account
1183
=head3 renew_account
1143
1184
1144
my $new_expiry_date = $patron->renew_account
1185
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 3118-3120 subtest 'is_anonymous' => sub { Link Here
3118
    $schema->storage->txn_rollback;
3118
    $schema->storage->txn_rollback;
3119
3119
3120
};
3120
};
3121
- 
3121
3122
subtest 'has_2fa_enabled() tests' => sub {
3123
    plan tests => 3;
3124
3125
    $schema->storage->txn_begin;
3126
3127
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
3128
3129
    # Test patron without 2FA
3130
    is( $patron->has_2fa_enabled, 0, 'has_2fa_enabled returns 0 when no secret is set' );
3131
3132
    # Test patron with empty secret
3133
    $patron->secret('')->store;
3134
    is( $patron->has_2fa_enabled, 0, 'has_2fa_enabled returns 0 when secret is empty string' );
3135
3136
    # Test patron with 2FA enabled
3137
    $patron->secret('some_secret_value')->store;
3138
    is( $patron->has_2fa_enabled, 1, 'has_2fa_enabled returns 1 when secret is set' );
3139
3140
    $schema->storage->txn_rollback;
3141
};
3142
3143
subtest 'reset_2fa() tests' => sub {
3144
    plan tests => 6;
3145
3146
    $schema->storage->txn_begin;
3147
3148
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
3149
3150
    # Set up patron with 2FA enabled
3151
    $patron->set(
3152
        {
3153
            secret      => 'test_secret_123',
3154
            auth_method => 'two-factor',
3155
        }
3156
    )->store;
3157
3158
    # Verify 2FA is enabled
3159
    is( $patron->has_2fa_enabled, 1,            '2FA is enabled before reset' );
3160
    is( $patron->auth_method,     'two-factor', 'auth_method is two-factor before reset' );
3161
3162
    # Test reset_2fa method
3163
    my $result = $patron->reset_2fa;
3164
3165
    # Verify method returns patron object for chaining
3166
    isa_ok( $result, 'Koha::Patron', 'reset_2fa returns Koha::Patron object' );
3167
    is( $result->borrowernumber, $patron->borrowernumber, 'returned object is the same patron' );
3168
3169
    # Verify 2FA has been reset
3170
    $patron->discard_changes;
3171
    is( $patron->has_2fa_enabled, 0,          '2FA is disabled after reset' );
3172
    is( $patron->auth_method,     'password', 'auth_method is password after reset' );
3173
3174
    $schema->storage->txn_rollback;
3175
};

Return to bug 40545