Skip to content

[5.4] Fix null user check in ActionLogPlugin to prevent PHP warnings#46599

Merged
richard67 merged 3 commits intojoomla:5.4-devfrom
Shauryan0207:fix-45843-actionlog-null-check
Feb 5, 2026
Merged

[5.4] Fix null user check in ActionLogPlugin to prevent PHP warnings#46599
richard67 merged 3 commits intojoomla:5.4-devfrom
Shauryan0207:fix-45843-actionlog-null-check

Conversation

@Shauryan0207
Copy link
Copy Markdown
Contributor

@Shauryan0207 Shauryan0207 commented Dec 20, 2025

Summary of Changes

Added a check for the $user object in ActionLogPlugin to prevent PHP warnings when getIdentity() returns null.

Testing Instructions

Since reproducing a scenario where getIdentity() returns null via the UI is difficult please use the following script to verify the fix:
Create a test file test_actionlog_null.php

<?php
// Simulate scenario where getIdentity() returns null (guest user or logout scenario)
$user = null;

echo "TEST 1: Before fix (shows PHP warning)\n";
echo "Attempting to access \$user->id when \$user is null:\n";
try {
    $id = $user->id; // Old code behavior
    echo "User ID: " . $id . "\n";
} catch (\Error $e) {
    echo "❌ PHP Warning: " . $e->getMessage() . "\n\n";
}

echo "TEST 2: After fix (no warning)\n";
echo "Using null-safe operator:\n";
$id = $user?->id; // Fixed code behavior
$username = $user?->username ?? '';
echo "User ID: " . ($id ?? 'null') . "\n";
echo "Username: " . ($username ?: 'empty') . "\n";
echo "✅ No warnings - fix works correctly\n";

php test_actionlog_null.php

Expected output:
TEST 1 shows a PHP error when accessing properties on null
TEST 2 shows no warnings and handles null safely

Actual result BEFORE applying this Pull Request

PHP warnings when ActionLog runs without authenticated user.

Expected result AFTER applying this Pull Request

No PHP warnings; gracefully handles null user.

Link to documentations

  • No documentation changes for docs.joomla.org needed
  • No documentation changes for manual.joomla.org needed

@richard67
Copy link
Copy Markdown
Member

Testing Instructions

Code review - verify null-safe operators (?-> and ??) are used for user property access.

@Shauryan0207 I'd prefer to have instructions for a real test, not just code review.

Providing reproducible test instructions would also show that you have tested the changes from your PR yourself before submitting it, which is a mandatory requirement for contributing here.

Could you provide instructions? Thanks in advance.

@exlemor
Copy link
Copy Markdown

exlemor commented Dec 21, 2025

@Shauryan0207 - I am trying to test your PR and I read the Testing Instructions of "PHP warnings when ActionLog runs without authenticated user" but I must admit not understanding what you meant as I can't think of anything that triggers an ActionLog without a user being authenticated (but that could just be ME not thinking through all of the cases)...

I could imagine maybe this happening if a Session timeout happened during an action, but that's not so easy to trigger.

Looking forward to specific use case that I can test for you.

@Shauryan0207
Copy link
Copy Markdown
Contributor Author

Hey @richard67
Create a test file test_actionlog_null.php

<?php
// Simulate scenario where getIdentity() returns null (guest user or logout scenario)
$user = null;

echo "TEST 1: Before fix (shows PHP warning)\n";
echo "Attempting to access \$user->id when \$user is null:\n";
try {
    $id = $user->id; // Old code behavior
    echo "User ID: " . $id . "\n";
} catch (\Error $e) {
    echo "❌ PHP Warning: " . $e->getMessage() . "\n\n";
}

echo "TEST 2: After fix (no warning)\n";
echo "Using null-safe operator:\n";
$id = $user?->id; // Fixed code behavior
$username = $user?->username ?? '';
echo "User ID: " . ($id ?? 'null') . "\n";
echo "Username: " . ($username ?: 'empty') . "\n";
echo "✅ No warnings - fix works correctly\n";

php test_actionlog_null.php

Expected output:
TEST 1 shows a PHP error when accessing properties on null
TEST 2 shows no warnings and handles null safely

@Shauryan0207
Copy link
Copy Markdown
Contributor Author

Hey @exlemor ,Thanks for reviewing this while it’s rare, this can happen during things like command-line scripts, scheduled tasks, system maintenance, or guest API requests, where actions are logged even though no user is logged in.

@Shauryan0207 Shauryan0207 force-pushed the fix-45843-actionlog-null-check branch 2 times, most recently from a7b1fd7 to 998963d Compare December 23, 2025 09:04
@Shauryan0207 Shauryan0207 requested a review from Fedik December 23, 2025 09:06
@Fedik Fedik added the bug label Dec 23, 2025
@Fedik
Copy link
Copy Markdown
Member

Fedik commented Dec 23, 2025

I have tested this item ✅ successfully on 998963d

Review


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/46599.

@richard67
Copy link
Copy Markdown
Member

@Shauryan0207 Could you adjust the testing instructions in the description (initial post) of this PR to the latest code changes? Thanks in advance.

@Shauryan0207
Copy link
Copy Markdown
Contributor Author

@Shauryan0207 Could you adjust the testing instructions in the description (initial post) of this PR to the latest code changes? Thanks in advance.

@richard67 Done ! Thanks

@exlemor
Copy link
Copy Markdown

exlemor commented Dec 24, 2025

@Shauryan0207 I'm probably doing something incorrectly but I'm not matching the BEFORE condition of a PHP error.

I created the php file: test_actionlog_null.php in the root of the sub-folder where Joomla 5.4 is installed, /_j540/
I connected to an SSH terminal and ran from the /_j540/ root, php test_actionlog_null.php and received this result:

TEST 1: Before fix (shows PHP warning)
Attempting to access $user->id when $user is null:
User ID: 
TEST 2: After fix (no warning)
Using null-safe operator:
User ID: null
Username: empty
✅ No warnings - fix works correctly

What did I screw up? ;(

@ceford
Copy link
Copy Markdown
Contributor

ceford commented Jan 5, 2026

I looked at this PR and I am not happy with it. The quoted test would just show the desired behaviour. It does not test Joomla. Various CLI commands such as php cli/joomla.php site:up don't trigger the plugin. It seems to me it may be better to see the PHP warning so that the site owner can see that something unusual is going on. Can you provide any sort of screenshot showing the warning?


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/46599.

@muhme
Copy link
Copy Markdown
Contributor

muhme commented Feb 5, 2026

I have tested this item ✅ successfully on 998963d

Tested with JBT on current 5.4-dev

  • Enabled Xdebug and in file administrator/components/com_actionlogs/src/Plugin/ActionLogPlugin.php
    • Set break point in line 70 and logged in from site
    • After line 71 $user = $app->getIdentity(); set $user = null
    • After line 73 foreach ($messages as $index => $message) { set $message = array(1)
    • Seen PHP warning on frontend:
      Warning: Attempt to read property "id" on null in /var/www/html/administrator/components/com_actionlogs/src/Plugin/ActionLogPlugin.php on line 75
      
      Warning: Attempt to read property "username" on null in /var/www/html/administrator/components/com_actionlogs/src/Plugin/ActionLogPlugin.php on line 79
      
  • Applied PR with Patch Tester
    • with brakpoint and overwriting variables:
      • No warning anymore on frontend
      • Actions log entry is User {username} logged in to {app}
    • Without breakpoint login/logout actions are logged

This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/46599.

@muhme
Copy link
Copy Markdown
Contributor

muhme commented Feb 5, 2026

I looked at this PR and I am not happy with it.

@ceford The code review shows that the object $user is no longer used if it does not exist. As a PHP novice, this seems safe to me.

@muhme muhme removed the bug label Feb 5, 2026
@muhme
Copy link
Copy Markdown
Contributor

muhme commented Feb 5, 2026

RTC


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/46599.

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Feb 5, 2026
@muhme muhme added the bug label Feb 5, 2026
@muhme muhme added this to the Joomla! 5.4.3 milestone Feb 5, 2026
@joomdonation
Copy link
Copy Markdown
Contributor

@muhme Haven't looked at the details, but some questions raise:

  • Do we really need to want to log the action without knowing the user who is performing the action? Is that kind of log useful ?
  • And with the change, when the user is not available, I guess the data such as userid is not available, so it won't be replaced?

@muhme muhme added RMDQ ReleaseManagerDecisionQueue and removed RTC This Pull Request is Ready To Commit labels Feb 5, 2026
@muhme
Copy link
Copy Markdown
Contributor

muhme commented Feb 5, 2026

@muhme Haven't looked at the details, but some questions raise:

Do we really need to want to log the action without knowing the user who is performing the action? Is that kind of log useful ?

And with the change, when the user is not available, I guess the data such as userid is not available, so it won't 

@joomdonation These are valid questions, but I cannot answer them. Perhaps you or someone else can answer them? thx

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Feb 5, 2026
@Fedik
Copy link
Copy Markdown
Member

Fedik commented Feb 5, 2026

The fix is valid despite the questions.

About the questions.

The CLI app does not have an User.
Extension may trigger some Content import from CLI or anything else that works with content.
We can skip logs without User of course, but I would keep it because it still helpful to track changes in the system.

@muhme
Copy link
Copy Markdown
Contributor

muhme commented Feb 5, 2026

Thank you @Fedik for stepping in and answering the questions.

@muhme muhme removed the RMDQ ReleaseManagerDecisionQueue label Feb 5, 2026
@richard67 richard67 merged commit eba02fb into joomla:5.4-dev Feb 5, 2026
69 checks passed
@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label Feb 5, 2026
@richard67
Copy link
Copy Markdown
Member

Thanks @Shauryan0207 for this PR, and thanks @Fedik and @muhme for testing, and again @Fedik for advice and support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants