Update log messages to reflect that a user was masquerading
Problem/Motivation
Log messages don't currently reflect that the action was taken by a user masquerading as another user. This makes it impossible to determine which actions were actually performed by the logged user.
Steps to reproduce
- Log in as user 1 in browser 1
- Create basic page as user 1
- Log in as user 2 in browser 2 and masquerade as user 1
- As user 2 masquerading as user 1, edit the basic page created in step 2
- As user 1, edit the basic page created in step 2
- ISSUE: Reviewing the log, you cannot tell which edits from steps 4 and 5 were actually performed by user 2, or as user 1 masquerading as user 2.
Proposed resolution
Prepend "(Masquerading user: @user)" to the end of each log message generated while a user is masquerading.
Remaining tasks
Confirm resolution, review patch and commit.
Original report by deekayen
Since Masquerade keeps track of who the original user was to be able to switch back, and since watchdog() invokes hook_watchdog(), it seems like it should be possible for logged events to record the original user instead of the masqueraded user to maintain accountability.
| Comment | File | Size | Author |
|---|---|---|---|
| #25 | Screenshot 2025-01-27 at 6.00.40 PM.png | 165.67 KB | trackleft2 |
| #25 | Screenshot 2025-01-27 at 6.00.20 PM.png | 201.79 KB | trackleft2 |
| #15 | interdiff-14-15.patch | 1.71 KB | c7bamford |
| #15 | masquerade-log-context-2357187-15.patch | 18 KB | c7bamford |
Issue fork masquerade-2357187
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #1
vulfox commentedAre you talking about the problem that right now if you are masquerading as an user all the actions you make are logged as actions for that user in the database log?
This is a problem that should be fixed! I know that there is a notice in the log when someone starts masquerading but after that no distinction is made.
This behaviour also means that the user masqueraded user is shown as logged in to the site even though he is not.
Comment #2
andypostidea looks good, but I'm sure that we need setting (variable) to enable this, because that could break logging hooks that check messages.
Comment #3
vulfox commentedMaybe some hacky way: add a another entry "user69 is still masquerading as xxx" ..or something?
Comment #4
celticgreen commentedWondering if this one got scoped at all?
Comment #5
jantoine commentedThis was a bit tricky, but I've taken a stab at it. Below is a concise list of the changes in the patch along with reasoning behind each change.
I started out using just the masquerade service, but the new logger.factory.masquerade and updated logger.channel.masquerade classes need to use a masquerade service to determine if the user is currently masquerading or not. Because the masquerade service also relies on the logging service to log, this caused a circular dependency between services. Originally I made the isMasquerading() and getMasqueradingUserId() functions static, but this meant being dependent on the class rather than the service, so I broke it out into two services.
Use the lighter masquerade info service when the masquerade service isn't necessary.
The logger.factory services needs to be decorated (extended) so we can use our own MasqueradeLoggerChannelFactory class.
We need our custom service to be able to call our custom class.
We need a custom MasqueradeLoggerChannel class so we can alter the log message.
The LoggerChannelFactory class hard-codes the use of the LoggerChannel class. In order to be able to use a custom MasqueradeLoggerChannel class, we need a custom factory.
Comment #7
jantoine commentedRebuilt the patch.
Comment #8
jantoine commentedCoding standards.
Comment #9
andypostI dont like decoupling info service cos this will affect contrib like sso to change service name
The same time we can decorate core switcher service to inject session object change
Maybe we should use request data object to get session instead of core's service
Comment #10
jantoine commented@andypost Notice that the original masquerade service class extends the masquerade.info service class, so it still has all of its original functionality! The rest of what you mention decorating the core switcher class and using the request data object instead of the session service seem like related but separate issues that should be completed first?
Comment #11
andypost@jantoine sorry, I misread the patch first time... now it looks like making sense but the main question is how to make this optional...
Maybe better to make a submodule (masquerade_logger) to decorate all logger messages
Also would be great to update issue summary a bit to clarify proposed approach
Comment #12
c7bamford commentedRerolled for commit 2011b21.
Comment #14
jantoine commentedThe patch from #12 wasn't properly rerolled and was removing code that was committed after the patch from #8 was written. Rerolled the patch from #8.
Comment #15
c7bamford commentedRerolled to commit cc5c6357
Comment #16
andypostit could need wrapper to load real user entity, Symfony provides a wrapper for it... but I bet ID is enough
Comment #17
nwom commentedWhat is the difference between this patch and the module here?: https://www.drupal.org/project/masquerade_log
Comment #18
solideogloria commented@NWOM This module is more likely to stay maintained, and the functionality should be in the main module in the first place, IMHO.
Comment #19
rajdeep0826 commentedComment #21
solideogloria commentedI don't know what the purpose of #19 is... That change was already made in #15.
Comment #22
andypostIt was interdiff added as patch instead of txt file, that's why it fails
So #19 makes no sense
Comment #25
trackleft2Since #3502073: Mark Masquerade Log module incompatible with masquerade version 8.x-2.0-rc5 and later. effectively means masquerade_log no-longer works, this issue can likely replace that entire module.
In MR !19 I add a new setting disabled by default named
log_masquerading_user.To update this setting, to true, you can use
drush config:set masquerade.settings log_masquerading_user trueI am also open to moving this functionality into sub-module called
masquerade_loggerif that is desired.Comment #26
andypostLooks the issue could be closed as there's contrib module https://www.drupal.org/project/masquerade_log
Comment #27
solideogloria commentedThe contrib module doesn't work with the most recent version of Masquerade right now.