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

  1. Log in as user 1 in browser 1
  2. Create basic page as user 1
  3. Log in as user 2 in browser 2 and masquerade as user 1
  4. As user 2 masquerading as user 1, edit the basic page created in step 2
  5. As user 1, edit the basic page created in step 2
  6. 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.

Issue fork masquerade-2357187

Command icon 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

vulfox’s picture

Are 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.

andypost’s picture

idea looks good, but I'm sure that we need setting (variable) to enable this, because that could break logging hooks that check messages.

vulfox’s picture

Maybe some hacky way: add a another entry "user69 is still masquerading as xxx" ..or something?

celticgreen’s picture

Wondering if this one got scoped at all?

jantoine’s picture

Title: Hook watchdog to log original user in events » Update log messages to reflect that a user was masquerading
Version: 7.x-1.x-dev » 8.x-2.x-dev
Component: Miscellaneous » Code
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new18.12 KB

This 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.

  • Split read portions of the masquerade service into a masquerade.info service.

    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.

  • Replaced use of the masquerade service with the masquerade.info service everywhere where information was only being read.

    Use the lighter masquerade info service when the masquerade service isn't necessary.

  • Added a logger.factory.masquerade service to decorate the logger.factory service and switch out the LoggerChannel class with our MasqueradeLoggerChannel class when appropriate.

    The logger.factory services needs to be decorated (extended) so we can use our own MasqueradeLoggerChannelFactory class.

  • Changed the logger.channel.masquerade service to not extend the logger.channel_base service and instead use a class that extends the LoggerChannel class used by the logger.channel_base service.

    We need our custom service to be able to call our custom class.

  • Added the MasqueradeLoggerChannel class that extends the LoggerChannel class and overrides the log() method appending masquerading information to the message before calling the parent log() method.

    We need a custom MasqueradeLoggerChannel class so we can alter the log message.

  • Added the MasqueradeLoggerChannelFactory class that extends the LoggerChannelFactory class and overrides the get() method returning a MasqueradeLoggerChannel instance instead of a LoggerChannel instance if the channel is not 'masquerade' and the user is currently masquerading.

    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.

Status: Needs review » Needs work

The last submitted patch, 5: masquerade-log-context-2357187-5.patch, failed testing. View results

jantoine’s picture

Status: Needs work » Needs review
StatusFileSize
new18.12 KB

Rebuilt the patch.

jantoine’s picture

StatusFileSize
new18.05 KB

Coding standards.

andypost’s picture

Issue tags: +Needs tests

I 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

jantoine’s picture

@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?

andypost’s picture

@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

c7bamford’s picture

StatusFileSize
new18.3 KB

Rerolled for commit 2011b21.

Status: Needs review » Needs work

The last submitted patch, 12: masquerade-log-context-2357187-12.patch, failed testing. View results

jantoine’s picture

Status: Needs work » Needs review
StatusFileSize
new17.98 KB

The 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.

c7bamford’s picture

StatusFileSize
new18 KB
new1.71 KB

Rerolled to commit cc5c6357

andypost’s picture

+++ b/src/MasqueradeInfo.php
@@ -0,0 +1,84 @@
+   * Gets the masquerading user.
...
+   * @return \Drupal\user\UserInterface
+   *   The masquerading user entity.
...
+  public function getMasqueradingUserId() {
...
+    // Return previous user account.
+    return $this->session->get('masquerading');

it could need wrapper to load real user entity, Symfony provides a wrapper for it... but I bet ID is enough

nwom’s picture

What is the difference between this patch and the module here?: https://www.drupal.org/project/masquerade_log

solideogloria’s picture

@NWOM This module is more likely to stay maintained, and the functionality should be in the main module in the first place, IMHO.

rajdeep0826’s picture

StatusFileSize
new487 bytes

Status: Needs review » Needs work

The last submitted patch, 19: 2357187-update-log-message.patch, failed testing. View results

solideogloria’s picture

Status: Needs work » Needs review

I don't know what the purpose of #19 is... That change was already made in #15.

andypost’s picture

It was interdiff added as patch instead of txt file, that's why it fails
So #19 makes no sense

trackleft2 made their first commit to this issue’s fork.

trackleft2’s picture

Since #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 true

I am also open to moving this functionality into sub-module called masquerade_logger if that is desired.

no
no

andypost’s picture

Looks the issue could be closed as there's contrib module https://www.drupal.org/project/masquerade_log

solideogloria’s picture

The contrib module doesn't work with the most recent version of Masquerade right now.