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

(-)a/Koha/Patron.pm (-23 / +29 lines)
Lines 3572-3597 sub is_anonymous { Link Here
3572
3572
3573
my $flags = $patron->permissions
3573
my $flags = $patron->permissions
3574
3574
3575
Returns a structure such as:
3575
Returns a hashref of the patron's permissions. This method has the same
3576
return structure as C4::Auth::getuserflags for consistency.
3577
3578
Example return structure:
3576
{
3579
{
3577
  "permissions": 1,
3580
  "superlibrarian": 0,
3581
  "permissions": 0,
3578
  "borrowers": 1,
3582
  "borrowers": 1,
3579
  "circulate": {
3583
  "circulate": {
3580
    "manage_curbside_pickups": 1,
3584
    "manage_curbside_pickups": 1,
3581
    "overdues_report": 1,
3585
    "overdues_report": 1,
3582
    "override_renewals": 1,
3586
    "override_renewals": 1,
3583
    "manage_restrictions": 1,
3584
    "manage_checkout_notes": 1,
3585
    "manage_bookings": 1
3586
  },
3587
  },
3587
  "catalogue": 1,
3588
  "catalogue": 1,
3588
  "reserveforothers": {
3589
  "reserveforothers": 0,
3589
    "place_holds": 1,
3590
  "tools": 0,
3590
    "modify_holds_priority": 1
3591
  ...
3591
  }
3592
}
3592
}
3593
where a 1 indicates full permissions and a hash indicates
3593
3594
partial permissions with the given permissions as hash keys
3594
All permission flags are included in the returned hashref:
3595
- Flags set to 1 indicate the user has full permissions for that module
3596
- Flags set to 0 indicate the user does not have that permission
3597
- Flags set to a hashref indicate partial/granular permissions, where the
3598
  hash keys are the specific subpermissions granted
3595
3599
3596
=cut
3600
=cut
3597
3601
Lines 3603-3629 sub permissions { Link Here
3603
    my @userflags     = $schema->resultset('Userflag')->search( {}, { order_by => 'bit' } )->all;
3607
    my @userflags     = $schema->resultset('Userflag')->search( {}, { order_by => 'bit' } )->all;
3604
    my %userflags_map = map { $_->bit => $_->flag } @userflags;
3608
    my %userflags_map = map { $_->bit => $_->flag } @userflags;
3605
3609
3606
    my $flags        = $self->flags // 0;
3610
    my $flags     = $self->flags // 0;
3607
    my $active_flags = {};
3611
    my $userflags = {};
3608
3612
3609
    # Check which flags are active based on bit values
3613
    # Initialize all flags to 0, then set granted ones to 1
3614
    # This matches the return structure of C4::Auth::getuserflags
3610
    foreach my $bit ( keys %userflags_map ) {
3615
    foreach my $bit ( keys %userflags_map ) {
3616
        my $flag = $userflags_map{$bit};
3611
        if ( $flags & ( 1 << $bit ) ) {
3617
        if ( $flags & ( 1 << $bit ) ) {
3612
            my $flag = $userflags_map{$bit};
3618
            $userflags->{$flag} = 1;
3613
            $active_flags->{$flag} = 1;
3619
        } else {
3620
            $userflags->{$flag} = 0;
3614
        }
3621
        }
3615
    }
3622
    }
3616
3623
3617
    # Get granular subpermissions using existing helper
3624
    # Get granular subpermissions and merge with top-level permissions
3618
    my $user_perms = get_user_subpermissions( $self->userid );
3625
    my $user_subperms = get_user_subpermissions( $self->userid );
3619
3626
3620
    for my $module ( keys %$user_perms ) {
3627
    foreach my $module ( keys %$user_subperms ) {
3621
        for my $code ( keys %{ $user_perms->{$module} } ) {
3628
        next if $userflags->{$module} == 1;    # user already has permission for everything in this module
3622
            $active_flags->{$module}->{$code} = 1;
3629
        $userflags->{$module} = $user_subperms->{$module};
3623
        }
3624
    }
3630
    }
3625
3631
3626
    return $active_flags;
3632
    return $userflags;
3627
}
3633
}
3628
3634
3629
=head2 Internal methods
3635
=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