Bugzilla – Attachment 186388 Details for
Bug 20956
BorrowersLog is not logging permission changes
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 20956: BorrowersLog is not logging permission changes
Bug-20956-BorrowersLog-is-not-logging-permission-c.patch (text/plain), 5.62 KB, created by
Hammat wele
on 2025-09-12 19:31:13 UTC
(
hide
)
Description:
Bug 20956: BorrowersLog is not logging permission changes
Filename:
MIME Type:
Creator:
Hammat wele
Created:
2025-09-12 19:31:13 UTC
Size:
5.62 KB
patch
obsolete
>From 69a2b908b802e20fe74a125bddea558020d713f5 Mon Sep 17 00:00:00 2001 >From: Hammat Wele <hammat.wele@inlibro.com> >Date: Fri, 12 Sep 2025 19:14:59 +0000 >Subject: [PATCH] Bug 20956: BorrowersLog is not logging permission changes > >Changes to permissions are not logged in the Koha action_logs, making it impossible to trace changes to staff permissions. This patch ensures that all changes to permissions are logged. > >Test plan: > > 1. Go to a staff interface > 2. Search for a patron and change their permissions (note their borrowernumber) > 3. Go to tools/Log viewer > 4. Filter by Module = Patrons, Object = <borrowernumber>. > 5. Click on submit > ==> No permission changes are logged. > 6. Apply the patch > 7. Repeat step 1, 2, 3, 4, 5 > ==> Permission changes are now logged > 8. Check that the diff column contains the change: > select diff from action_logs where object= <borrowernumber> order by action_id desc limit 1; > >Signed-off-by: David Nind <david@davidnind.com> >--- > C4/Log.pm | 2 ++ > members/member-flags.pl | 45 ++++++++++++++++++++++++++++++++++++++--- > t/db_dependent/Log.t | 21 ++++++++++++++++++- > 3 files changed, 64 insertions(+), 4 deletions(-) > >diff --git a/C4/Log.pm b/C4/Log.pm >index b15774e6c94..2e109a2e687 100644 >--- a/C4/Log.pm >+++ b/C4/Log.pm >@@ -140,6 +140,8 @@ sub logaction { > $diff //= encode_json( diff( $original, $updated, noU => 1 ) ) > if $original && ref $updated eq 'HASH'; > >+ $infos = encode_json($infos) if ( ref $infos eq 'HASH' ); >+ > Koha::ActionLog->new( > { > timestamp => \'NOW()', >diff --git a/members/member-flags.pl b/members/member-flags.pl >index ccc6092e6bc..42f04bf9828 100755 >--- a/members/member-flags.pl >+++ b/members/member-flags.pl >@@ -10,6 +10,7 @@ use CGI qw ( -utf8 ); > use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers ); > use C4::Auth qw( get_template_and_user get_all_subpermissions get_user_subpermissions ); > use C4::Context; >+use C4::Log qw( logaction ); > > use Koha::Patron::Categories; > use Koha::Patrons; >@@ -69,16 +70,19 @@ if ( $op eq 'cud-newflags' ) { > > # construct flags > my $module_flags = 0; >- my $sth = $dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit"); >+ my $sth = $dbh->prepare("SELECT bit,flag,flagdesc FROM userflags ORDER BY bit"); > $sth->execute(); >- while ( my ( $bit, $flag ) = $sth->fetchrow_array ) { >+ my %userflags; >+ while ( my ( $bit, $flag, $flagdesc ) = $sth->fetchrow_array ) { > if ( exists $all_module_perms{$flag} ) { > $module_flags += 2**$bit; > } >+ $userflags{$bit} = [ $flag, $flagdesc ]; > } > > $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?"); >- my $old_flags = $patron->flags // 0; >+ my $old_flags = $patron->flags // 0; >+ my %permissions_before = get_permissions( \%userflags, $old_flags ); > if ( ( $old_flags == 1 || $module_flags == 1 ) > && $old_flags != $module_flags ) > { >@@ -105,6 +109,8 @@ if ( $op eq 'cud-newflags' ) { > } > } > >+ my %permissions_after = get_permissions( \%userflags, $module_flags ); >+ logaction( 'MEMBERS', 'MODIFY', $member, \%permissions_after, undef, \%permissions_before ); > print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member"); > } else { > >@@ -200,3 +206,36 @@ if ( $op eq 'cud-newflags' ) { > output_html_with_http_headers $input, $cookie, $template->output; > > } >+ >+sub get_permissions { >+ my ( $userflags, $old_flags ) = @_; >+ >+ my %active_flag = decode_flags( $userflags, $old_flags ); >+ my $user_perms = get_user_subpermissions( $bor->{'userid'} ); >+ my %permissions; >+ my $dbh = C4::Context->dbh(); >+ my $sth = $dbh->prepare("SELECT code, description FROM permissions"); >+ $sth->execute(); >+ >+ while ( my ( $code, $desc ) = $sth->fetchrow_array ) { >+ $permissions{$code} = $desc; >+ } >+ for my $module ( keys %$user_perms ) { >+ for my $code ( keys %{ $user_perms->{$module} } ) { >+ $active_flag{$code} = $permissions{$code}; >+ } >+ } >+ return %active_flag; >+} >+ >+sub decode_flags { >+ my ( $userflags, $flags ) = @_; >+ my %active_flags; >+ foreach my $bit ( keys %$userflags ) { >+ if ( $flags & ( 2**$bit ) ) { >+ my ( $flag, $desc ) = @{ $userflags->{$bit} }; >+ $active_flags{$flag} = $desc; >+ } >+ } >+ return %active_flags; >+} >diff --git a/t/db_dependent/Log.t b/t/db_dependent/Log.t >index 90bb355be79..ab7537045e8 100755 >--- a/t/db_dependent/Log.t >+++ b/t/db_dependent/Log.t >@@ -16,8 +16,9 @@ > > use Modern::Perl; > use Data::Dumper qw( Dumper ); >+ > use Test::NoWarnings; >-use Test::More tests => 8; >+use Test::More tests => 9; > > use C4::Context; > use C4::Log qw( logaction cronlogaction ); >@@ -221,6 +222,24 @@ subtest 'Test storing diff of objects' => sub { > is( $diff->{D}->{barcode}->{N}, '_MY_TEST_BARCODE_', 'Diff of changes logged successfully' ); > }; > >+subtest 'Test storing diff of hashrefs' => sub { >+ plan tests => 1; >+ my $original_data = { >+ firstname => 'John', >+ surname => 'Doe' >+ }; >+ my $edited_data = { >+ firstname => 'Johnny', >+ surname => 'Doe' >+ }; >+ >+ logaction( 'MY_MODULE', 'TEST', 12345, $edited_data, 'staff', $original_data ); >+ >+ my $logs = Koha::ActionLogs->search( { module => 'MY_MODULE', action => 'TEST' } )->next; >+ my $diff = decode_json( $logs->diff ); >+ is( $diff->{D}->{firstname}->{N}, 'Johnny', 'Diff of changes logged successfully' ); >+}; >+ > subtest 'Test storing original version of an item' => sub { > plan tests => 1; > >-- >2.34.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 20956
:
182955
|
185483
|
185491
| 186388