Skip to content

Instantly share code, notes, and snippets.

@almirb
Last active May 12, 2017 13:17
Show Gist options
  • Save almirb/8d872520a18ab371686259f6e77af8bb to your computer and use it in GitHub Desktop.
Save almirb/8d872520a18ab371686259f6e77af8bb to your computer and use it in GitHub Desktop.
Getting Yii2 debug bar to show on user role instead of IP
<?php
namespace app\modules\sistema\components;
use Yii;
use yii\debug\Module;
/**
* Description of DebugModule
*
* @author Almir Bolduan
* Based on post from SAMMAYE'S BLOG
* https://sammaye.wordpress.com/2014/11/23/getting-yii2-debug-to-show-on-user-role-instead-of-ip/
*/
class DebugModule extends Module
{
private $_basePath;
protected function checkAccess()
{
$ip = Yii::$app->getRequest()->getUserIP();
foreach ($this->allowedIPs as $filter) {
if ($filter === '*' || $filter === $ip || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos))) {
return true;
}
}
foreach ($this->allowedHosts as $hostname) {
$filter = gethostbyname($hostname);
if ($filter === $ip) {
return true;
}
}
$user = Yii::$app->getUser();
if ($user->identity && $user->can('role')) {
return true;
}
return false;
}
/**
* 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment