Last active
May 12, 2017 13:17
-
-
Save almirb/8d872520a18ab371686259f6e77af8bb to your computer and use it in GitHub Desktop.
Getting Yii2 debug bar to show on user role instead of IP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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