Provide a backtrace if user 1 hits 'access denied'
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #610568. -->
Reported by: [salvis](https://www.drupal.org/user/82964)
>>>
<p>I just had user 1 getting an 'access denied' error. This shouldn't happen by definition, but finding out where it came from was a bit of a pain, so I wrote the following function:</p>
<div class="codeblock">
<pre><span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #FF8000">/**<br> * Implementation of hook_watchdog().<br> * <br> * If an 'access denied' error is logged for user 1, then something is<br> * seriously wrong: provide a backtrace so the user can see where the<br> * error is generated.<br> */<br></span><span style="color: #007700">function </span><span style="color: #0000BB">devel_watchdog</span><span style="color: #007700">(</span><span style="color: #0000BB">$log_entry</span><span style="color: #007700">) {<br> if (</span><span style="color: #0000BB">$log_entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">] == </span><span style="color: #DD0000">'access denied' </span><span style="color: #007700">&& </span><span style="color: #0000BB">$log_entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'user'</span><span style="color: #007700">]-></span><span style="color: #0000BB">uid </span><span style="color: #007700">== </span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$trace </span><span style="color: #007700">= </span><span style="color: #0000BB">debug_backtrace</span><span style="color: #007700">();<br> </span><span style="color: #FF8000">// Pop the stack up to the drupal_access_denied() call.<br> </span><span style="color: #007700">while (isset(</span><span style="color: #0000BB">$trace</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]) && (empty(</span><span style="color: #0000BB">$trace</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #DD0000">'function'</span><span style="color: #007700">]) || </span><span style="color: #0000BB">$trace</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #DD0000">'function'</span><span style="color: #007700">] != </span><span style="color: #DD0000">'drupal_access_denied'</span><span style="color: #007700">)) {<br> </span><span style="color: #0000BB">array_shift</span><span style="color: #007700">(</span><span style="color: #0000BB">$trace</span><span style="color: #007700">);<br> }<br> </span><span style="color: #0000BB">dsm</span><span style="color: #007700">(</span><span style="color: #0000BB">$trace</span><span style="color: #007700">, </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'ACCESS DENIED error generated for user 1, backtrace'</span><span style="color: #007700">));<br> }<br>}<br></span><span style="color: #0000BB">?></span></span></pre></div>
<p>Could this be generally useful enough to go into Devel? I don't think it would need a setting to enable/disable, it could be active all of the time.</p>
<p>(Would be nice if we could commit <span class="drupalorg-gitlab-issue-link drupalorg-gitlab-link-wrapper"><a href="https://git.drupalcode.org/project/devel/-/work_items/498028" class="drupalorg-gitlab-link">https://git.drupalcode.org/project/devel/-/work_items/498028</a></span> so that the introductory line ($name in dsm()) is actually displayed...)</p>
issue
GitLab AI Context
Project: project/devel
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/devel/-/raw/5.x/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/devel
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD