PHP, Yii Framework

Getting Yii2 debug to show on user role instead of IP

I recently had to get Yii2 debug to show based on whether I was an admin user or not due to not connecting to the site using static IPs.

Having the debug in production run for the admin user is really useful for fixing those problems which are not easily, or possibly, replicated down to development.

So I overrode the Module class for debug but found some problems with view paths. I ended up with:


<?php

namespace common\components;

use Yii;
use yii\debug\Module as DebugModule;
use common\models\User;

class Module extends DebugModule
{
	private $_basePath;
	
	protected function checkAccess()
	{
		$user = Yii::$app->getUser();

		if(
			$user->identity && 
			$user->can('admin')
		){
			return true;
		}
		return parent::checkAccess();
	}
	
	/**
	 * Returns the root directory of the module.
	 * It defaults to the directory containing the module class file.
	 * @return string the root directory of the module.
	 */
	public function getBasePath()
	{
		if ($this->_basePath === null) {
			$class = new \ReflectionClass(new yii\debug\Module('debug'));
			$this->_basePath = dirname($class->getFileName());
		}
	
		return $this->_basePath;
	}
}

Advertisements

2 thoughts on “Getting Yii2 debug to show on user role instead of IP

  1. tried it and had an error. I removed the line ” return parent::checkAccess(); ” instead and got a flawless response. Thank you for this!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s