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

(-)a/Koha/Patron.pm (-8 / +9 lines)
Lines 3598-3619 partial permissions with the given permissions as hash keys Link Here
3598
sub permissions {
3598
sub permissions {
3599
    my ($self) = @_;
3599
    my ($self) = @_;
3600
3600
3601
    my $dbh = C4::Context->dbh;
3601
    # Get userflags using ORM
3602
    my $sth = $dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
3602
    my $schema        = Koha::Database->schema;
3603
    $sth->execute();
3603
    my @userflags     = $schema->resultset('Userflag')->search( {}, { order_by => 'bit' } )->all;
3604
    my $userflags = { map { $_->[0] => $_->[1] } @{ $sth->fetchall_arrayref() } };
3604
    my %userflags_map = map { $_->bit => $_->flag } @userflags;
3605
3606
    my $flags = $self->flags // 0;
3607
3605
3606
    my $flags        = $self->flags // 0;
3608
    my $active_flags = {};
3607
    my $active_flags = {};
3609
3608
3610
    foreach my $bit ( keys %$userflags ) {
3609
    # Check which flags are active based on bit values
3610
    foreach my $bit ( keys %userflags_map ) {
3611
        if ( $flags & ( 1 << $bit ) ) {
3611
        if ( $flags & ( 1 << $bit ) ) {
3612
            my $flag = $userflags->{$bit};
3612
            my $flag = $userflags_map{$bit};
3613
            $active_flags->{$flag} = 1;
3613
            $active_flags->{$flag} = 1;
3614
        }
3614
        }
3615
    }
3615
    }
3616
3616
3617
    # Get granular subpermissions using existing helper
3617
    my $user_perms = get_user_subpermissions( $self->userid );
3618
    my $user_perms = get_user_subpermissions( $self->userid );
3618
3619
3619
    for my $module ( keys %$user_perms ) {
3620
    for my $module ( keys %$user_perms ) {
(-)a/t/db_dependent/Koha/Patron.t (-8 / +41 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 45;
22
use Test::More tests => 46;
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 806-829 subtest 'is_superlibrarian() tests' => sub { Link Here
806
    $schema->storage->txn_rollback;
806
    $schema->storage->txn_rollback;
807
};
807
};
808
808
809
subtest 'is_superlibrarian() tests' => sub {
809
subtest 'permissions() tests' => sub {
810
810
811
    plan tests => 2;
811
    plan tests => 8;
812
812
813
    $schema->storage->txn_begin;
813
    $schema->storage->txn_begin;
814
814
815
    my $dbh = C4::Context->dbh;
816
817
    # Test patron with single flag permission (catalogue = bit 2, value 4)
815
    my $patron = $builder->build_object(
818
    my $patron = $builder->build_object(
816
        {
819
        {
817
            class => 'Koha::Patrons',
820
            class => 'Koha::Patrons',
818
819
            value => { flags => 4 }
821
            value => { flags => 4 }
820
        }
822
        }
821
    );
823
    );
822
824
823
    my %permissions = $patron->permissions;
825
    my $permissions = $patron->permissions;
824
826
825
    is( scalar keys %permissions, 1, "Patron has one module permission" );
827
    is( ref($permissions),         'HASH', "permissions() returns a hashref" );
826
    is( $permissions{catalogue},  1, "Patron has catalogue permission" );
828
    is( scalar keys %$permissions, 1,      "Patron has one module permission" );
829
    is( $permissions->{catalogue}, 1,      "Patron has catalogue permission" );
830
831
    # Test patron with no permissions
832
    my $patron_no_perms = $builder->build_object(
833
        {
834
            class => 'Koha::Patrons',
835
            value => { flags => 0 }
836
        }
837
    );
838
    my $no_perms = $patron_no_perms->permissions;
839
    is( scalar keys %$no_perms, 0, "Patron with no flags has no permissions" );
840
841
    # Test patron with multiple flags (catalogue=4 + circulate=2 = 6)
842
    my $patron_multi = $builder->build_object(
843
        {
844
            class => 'Koha::Patrons',
845
            value => { flags => 6 }
846
        }
847
    );
848
    my $multi_perms = $patron_multi->permissions;
849
    is( $multi_perms->{catalogue}, 1, "Multi-flag patron has catalogue permission" );
850
    is( $multi_perms->{circulate}, 1, "Multi-flag patron has circulate permission" );
851
852
    # Test patron with granular subpermissions
853
    my $patron_subperm = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 0 } } );
854
    $dbh->do(
855
        "INSERT INTO user_permissions (borrowernumber, module_bit, code) VALUES (?, ?, ?)",
856
        undef, $patron_subperm->borrowernumber, 1, 'override_renewals'
857
    );
858
    my $subperms = $patron_subperm->get_from_storage->permissions;
859
    is( ref( $subperms->{circulate} ),               'HASH', "Granular permissions stored as hashref" );
860
    is( $subperms->{circulate}->{override_renewals}, 1,      "Subpermission correctly detected" );
827
861
828
    $schema->storage->txn_rollback;
862
    $schema->storage->txn_rollback;
829
};
863
};
830
- 

Return to bug 20956