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

(-)a/Koha/Patron.pm (-23 / +29 lines)
Lines 3597-3622 sub is_anonymous { Link Here
3597
3597
3598
my $flags = $patron->permissions
3598
my $flags = $patron->permissions
3599
3599
3600
Returns a structure such as:
3600
Returns a hashref of the patron's permissions. This method has the same
3601
return structure as C4::Auth::getuserflags for consistency.
3602
3603
Example return structure:
3601
{
3604
{
3602
  "permissions": 1,
3605
  "superlibrarian": 0,
3606
  "permissions": 0,
3603
  "borrowers": 1,
3607
  "borrowers": 1,
3604
  "circulate": {
3608
  "circulate": {
3605
    "manage_curbside_pickups": 1,
3609
    "manage_curbside_pickups": 1,
3606
    "overdues_report": 1,
3610
    "overdues_report": 1,
3607
    "override_renewals": 1,
3611
    "override_renewals": 1,
3608
    "manage_restrictions": 1,
3609
    "manage_checkout_notes": 1,
3610
    "manage_bookings": 1
3611
  },
3612
  },
3612
  "catalogue": 1,
3613
  "catalogue": 1,
3613
  "reserveforothers": {
3614
  "reserveforothers": 0,
3614
    "place_holds": 1,
3615
  "tools": 0,
3615
    "modify_holds_priority": 1
3616
  ...
3616
  }
3617
}
3617
}
3618
where a 1 indicates full permissions and a hash indicates
3618
3619
partial permissions with the given permissions as hash keys
3619
All permission flags are included in the returned hashref:
3620
- Flags set to 1 indicate the user has full permissions for that module
3621
- Flags set to 0 indicate the user does not have that permission
3622
- Flags set to a hashref indicate partial/granular permissions, where the
3623
  hash keys are the specific subpermissions granted
3620
3624
3621
=cut
3625
=cut
3622
3626
Lines 3628-3654 sub permissions { Link Here
3628
    my @userflags     = $schema->resultset('Userflag')->search( {}, { order_by => 'bit' } )->all;
3632
    my @userflags     = $schema->resultset('Userflag')->search( {}, { order_by => 'bit' } )->all;
3629
    my %userflags_map = map { $_->bit => $_->flag } @userflags;
3633
    my %userflags_map = map { $_->bit => $_->flag } @userflags;
3630
3634
3631
    my $flags        = $self->flags // 0;
3635
    my $flags     = $self->flags // 0;
3632
    my $active_flags = {};
3636
    my $userflags = {};
3633
3637
3634
    # Check which flags are active based on bit values
3638
    # Initialize all flags to 0, then set granted ones to 1
3639
    # This matches the return structure of C4::Auth::getuserflags
3635
    foreach my $bit ( keys %userflags_map ) {
3640
    foreach my $bit ( keys %userflags_map ) {
3641
        my $flag = $userflags_map{$bit};
3636
        if ( $flags & ( 1 << $bit ) ) {
3642
        if ( $flags & ( 1 << $bit ) ) {
3637
            my $flag = $userflags_map{$bit};
3643
            $userflags->{$flag} = 1;
3638
            $active_flags->{$flag} = 1;
3644
        } else {
3645
            $userflags->{$flag} = 0;
3639
        }
3646
        }
3640
    }
3647
    }
3641
3648
3642
    # Get granular subpermissions using existing helper
3649
    # Get granular subpermissions and merge with top-level permissions
3643
    my $user_perms = get_user_subpermissions( $self->userid );
3650
    my $user_subperms = get_user_subpermissions( $self->userid );
3644
3651
3645
    for my $module ( keys %$user_perms ) {
3652
    foreach my $module ( keys %$user_subperms ) {
3646
        for my $code ( keys %{ $user_perms->{$module} } ) {
3653
        next if $userflags->{$module} == 1;    # user already has permission for everything in this module
3647
            $active_flags->{$module}->{$code} = 1;
3654
        $userflags->{$module} = $user_subperms->{$module};
3648
        }
3649
    }
3655
    }
3650
3656
3651
    return $active_flags;
3657
    return $userflags;
3652
}
3658
}
3653
3659
3654
=head2 Internal methods
3660
=head2 Internal methods
(-)a/t/db_dependent/Koha/Patron.t (-8 / +11 lines)
Lines 808-814 subtest 'is_superlibrarian() tests' => sub { Link Here
808
808
809
subtest 'permissions() tests' => sub {
809
subtest 'permissions() tests' => sub {
810
810
811
    plan tests => 8;
811
    plan tests => 12;
812
812
813
    $schema->storage->txn_begin;
813
    $schema->storage->txn_begin;
814
814
Lines 824-832 subtest 'permissions() tests' => sub { Link Here
824
824
825
    my $permissions = $patron->permissions;
825
    my $permissions = $patron->permissions;
826
826
827
    is( ref($permissions),         'HASH', "permissions() returns a hashref" );
827
    is( ref($permissions), 'HASH', "permissions() returns a hashref" );
828
    is( scalar keys %$permissions, 1,      "Patron has one module permission" );
828
    ok( scalar keys %$permissions > 0, "permissions() returns all flags, not just granted ones" );
829
    is( $permissions->{catalogue}, 1,      "Patron has catalogue permission" );
829
    is( $permissions->{catalogue},      1, "Patron has catalogue permission" );
830
    is( $permissions->{superlibrarian}, 0, "Patron does not have superlibrarian (demonstrates all flags present)" );
830
831
831
    # Test patron with no permissions
832
    # Test patron with no permissions
832
    my $patron_no_perms = $builder->build_object(
833
    my $patron_no_perms = $builder->build_object(
Lines 836-842 subtest 'permissions() tests' => sub { Link Here
836
        }
837
        }
837
    );
838
    );
838
    my $no_perms = $patron_no_perms->permissions;
839
    my $no_perms = $patron_no_perms->permissions;
839
    is( scalar keys %$no_perms, 0, "Patron with no flags has no permissions" );
840
    ok( scalar keys %$no_perms > 0, "Patron with no flags still has hashref with all flags" );
841
    is( $no_perms->{catalogue},      0, "Patron does not have catalogue permission" );
842
    is( $no_perms->{superlibrarian}, 0, "Patron does not have superlibrarian permission" );
840
843
841
    # Test patron with multiple flags (catalogue=4 + circulate=2 = 6)
844
    # Test patron with multiple flags (catalogue=4 + circulate=2 = 6)
842
    my $patron_multi = $builder->build_object(
845
    my $patron_multi = $builder->build_object(
Lines 846-853 subtest 'permissions() tests' => sub { Link Here
846
        }
849
        }
847
    );
850
    );
848
    my $multi_perms = $patron_multi->permissions;
851
    my $multi_perms = $patron_multi->permissions;
849
    is( $multi_perms->{catalogue}, 1, "Multi-flag patron has catalogue permission" );
852
    is( $multi_perms->{catalogue},      1, "Multi-flag patron has catalogue permission" );
850
    is( $multi_perms->{circulate}, 1, "Multi-flag patron has circulate permission" );
853
    is( $multi_perms->{circulate},      1, "Multi-flag patron has circulate permission" );
854
    is( $multi_perms->{superlibrarian}, 0, "Multi-flag patron does not have superlibrarian" );
851
855
852
    # Test patron with granular subpermissions
856
    # Test patron with granular subpermissions
853
    my $patron_subperm = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 0 } } );
857
    my $patron_subperm = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 0 } } );
854
- 

Return to bug 20956