Jump to content

Enable debug mode for only superusers or a given username


lpa
 Share

Recommended Posts

Why couldn't I use this in a template:

if ($user->name == 'admin') {
        $config->debug = true;
}
 
or 
 
if ($user->isSuperuser()) {
        $config->debug = true;
}
 
 
 
  • Like 1
Link to comment
Share on other sites

I could enable debugging on production site for superusers only. If I change config.php $config->debug = true, then it is global for the whole site. 

Link to comment
Share on other sites

It's not possible or doesn't do anything in a template because PW needs the config to be loaded at the very start, and at the time a template is rendered it's maybe already too late. Also it won't enable it in the backend, which you probably want. 

Why not directly in the config.php.? Well, you also can't use $user in the config.php itself, since it's too early and $user isn't yet there when loaded by PW index.php.

So the best option (if I'm not missing something) would be a autoload module and put that code in the init function.

public function init() {
    if($this->user->hasRole('superuser')){
        $this->config->debug = maybe;
    } else {
        $this->config->debug = false;
    }
}

If you put that in HelloWorld.module's init it will work fine and load at the beginning when the API becomes available.

Edit: somewhere along these lines if not completely mistaken.

  • Like 2
Link to comment
Share on other sites

All you can do in the site/config.php is something like you can get from PHP:

$config->debug = false;
if(isset($_GET['debug'])) $config->debug = true;

So you could add /?debug to a url and have debug mode on.

Or you could add this to the top in admin.php template in site/templates/ folder

if($user->name == 'admin') $config->debug = true;
And have debug mode only for admin only in admin.
  • Like 2
Link to comment
Share on other sites

For security, I'd avoid implementing solutions that would let anyone enable debug mode. So GET vars aren't ideal unless you can get enough "security by obscurity" with the GET variable (perhaps by name or value). My IP address doesn't change often, so I usually enable debug mode directly from my /site/config.php with a line like this:

$config->debug = $_SERVER['REMOTE_ADDR'] === '123.123.123.123'; 
  • Like 1
Link to comment
Share on other sites

  • 3 years later...

I know this is ancient, but I wanted to note that if this worked in the past, it no longer works. I think I recently even saw reference to placing $config->debug = true in ready.php. 

None of these options work properly. If you want conditional debug mode, you need to use the debugif option: https://github.com/processwire/processwire/blob/35df716082b779de0e53a3fcf7996403c49c9f8a/wire/config.php#L56-L71

Keep in mind that setting it to true in a module or ready.php etc might make it look like it's working because the "Debug Mode Tools" link and icon will appear in the admin, but it will not output any errors/notices/warnings to the browser. Also, in the debug panel you'll notice that the "Database Queries", "Timers", and "Autoload" sections are not populated because they require debug mode to be on

Also, of course, with Tracy enabled you don't really need debug mode on (or if you do have it on), it will take care of capturing any errors, and hiding them from users, anyway. The only thing you will be missing with it off, are those "Database Queries", "Timers", and "Autoload" sections.*

Anyway, hope that helps to avoid any confusion for those who come here in the future.

* This really wasn't meant to be a Tracy advert, but thought it was relevant because it solves the reason why you would want debug mode on only for superusers or the like.

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...