|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
-
-
- namespace yii\gii;
-
- use Yii;
- use yii\base\BootstrapInterface;
- use yii\web\ForbiddenHttpException;
-
-
- class Module extends \yii\base\Module implements BootstrapInterface
- {
-
-
- public $controllerNamespace = 'yii\gii\controllers';
-
-
- public $allowedIPs = ['127.0.0.1', '::1'];
-
-
- public $generators = [];
-
-
- public $newFileMode = 0666;
-
-
- public $newDirMode = 0777;
-
-
-
-
- public function bootstrap($app)
- {
- if ($app instanceof \yii\web\Application) {
- $app->getUrlManager()->addRules([
- ['class' => 'yii\web\UrlRule', 'pattern' => $this->id, 'route' => $this->id . '/default/index'],
- ['class' => 'yii\web\UrlRule', 'pattern' => $this->id . '/<id:\w+>', 'route' => $this->id . '/default/view'],
- ['class' => 'yii\web\UrlRule', 'pattern' => $this->id . '/<controller:[\w\-]+>/<action:[\w\-]+>', 'route' => $this->id . '/<controller>/<action>'],
- ], false);
- } elseif ($app instanceof \yii\console\Application) {
- $app->controllerMap[$this->id] = [
- 'class' => 'yii\gii\console\GenerateController',
- 'generators' => array_merge($this->coreGenerators(), $this->generators),
- 'module' => $this,
- ];
- }
- }
-
-
-
- public function beforeAction($action)
- {
- if (!parent::beforeAction($action)) {
- return false;
- }
-
- if (Yii::$app instanceof \yii\web\Application && !$this->checkAccess()) {
- throw new ForbiddenHttpException('You are not allowed to access this page.');
- }
-
- foreach (array_merge($this->coreGenerators(), $this->generators) as $id => $config) {
- if (is_object($config)) {
- $this->generators[$id] = $config;
- } else {
- $this->generators[$id] = Yii::createObject($config);
- }
- }
-
- $this->resetGlobalSettings();
-
- return true;
- }
-
-
-
- protected function resetGlobalSettings()
- {
- if (Yii::$app instanceof \yii\web\Application) {
- Yii::$app->assetManager->bundles = [];
- }
- }
-
-
-
- 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;
- }
- }
- Yii::warning('Access to Gii is denied due to IP address restriction. The requested IP is ' . $ip, __METHOD__);
-
- return false;
- }
-
-
-
- protected function coreGenerators()
- {
- return [
- 'model' => ['class' => 'yii\gii\generators\model\Generator'],
- 'crud' => ['class' => 'yii\gii\generators\crud\Generator'],
- 'controller' => ['class' => 'yii\gii\generators\controller\Generator'],
- 'form' => ['class' => 'yii\gii\generators\form\Generator'],
- 'module' => ['class' => 'yii\gii\generators\module\Generator'],
- 'extension' => ['class' => 'yii\gii\generators\extension\Generator'],
- ];
- }
- }
|