You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 line
5.8KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\gii;
  8. use Yii;
  9. use yii\base\BootstrapInterface;
  10. use yii\web\ForbiddenHttpException;
  11. /**
  12. * This is the main module class for the Gii module.
  13. *
  14. * To use Gii, include it as a module in the application configuration like the following:
  15. *
  16. * ~~~
  17. * return [
  18. * 'bootstrap' => ['gii'],
  19. * 'modules' => [
  20. * 'gii' => ['class' => 'yii\gii\Module'],
  21. * ],
  22. * ]
  23. * ~~~
  24. *
  25. * Because Gii generates new code files on the server, you should only use it on your own
  26. * development machine. To prevent other people from using this module, by default, Gii
  27. * can only be accessed by localhost. You may configure its [[allowedIPs]] property if
  28. * you want to make it accessible on other machines.
  29. *
  30. * With the above configuration, you will be able to access GiiModule in your browser using
  31. * the URL `http://localhost/path/to/index.php?r=gii`
  32. *
  33. * If your application enables [[\yii\web\UrlManager::enablePrettyUrl|pretty URLs]],
  34. * you can then access Gii via URL: `http://localhost/path/to/index.php/gii`
  35. *
  36. * @author Qiang Xue <qiang.xue@gmail.com>
  37. * @since 2.0
  38. */
  39. class Module extends \yii\base\Module implements BootstrapInterface
  40. {
  41. /**
  42. * @inheritdoc
  43. */
  44. public $controllerNamespace = 'yii\gii\controllers';
  45. /**
  46. * @var array the list of IPs that are allowed to access this module.
  47. * Each array element represents a single IP filter which can be either an IP address
  48. * or an address with wildcard (e.g. 192.168.0.*) to represent a network segment.
  49. * The default value is `['127.0.0.1', '::1']`, which means the module can only be accessed
  50. * by localhost.
  51. */
  52. public $allowedIPs = ['127.0.0.1', '::1'];
  53. /**
  54. * @var array|Generator[] a list of generator configurations or instances. The array keys
  55. * are the generator IDs (e.g. "crud"), and the array elements are the corresponding generator
  56. * configurations or the instances.
  57. *
  58. * After the module is initialized, this property will become an array of generator instances
  59. * which are created based on the configurations previously taken by this property.
  60. *
  61. * Newly assigned generators will be merged with the [[coreGenerators()|core ones]], and the former
  62. * takes precedence in case when they have the same generator ID.
  63. */
  64. public $generators = [];
  65. /**
  66. * @var integer the permission to be set for newly generated code files.
  67. * This value will be used by PHP chmod function.
  68. * Defaults to 0666, meaning the file is read-writable by all users.
  69. */
  70. public $newFileMode = 0666;
  71. /**
  72. * @var integer the permission to be set for newly generated directories.
  73. * This value will be used by PHP chmod function.
  74. * Defaults to 0777, meaning the directory can be read, written and executed by all users.
  75. */
  76. public $newDirMode = 0777;
  77. /**
  78. * @inheritdoc
  79. */
  80. public function bootstrap($app)
  81. {
  82. if ($app instanceof \yii\web\Application) {
  83. $app->getUrlManager()->addRules([
  84. $this->id => $this->id . '/default/index',
  85. $this->id . '/<id:\w+>' => $this->id . '/default/view',
  86. $this->id . '/<controller:[\w\-]+>/<action:[\w\-]+>' => $this->id . '/<controller>/<action>',
  87. ], false);
  88. } elseif ($app instanceof \yii\console\Application) {
  89. $app->controllerMap[$this->id] = [
  90. 'class' => 'yii\gii\console\GenerateController',
  91. 'generators' => array_merge($this->coreGenerators(), $this->generators),
  92. 'module' => $this,
  93. ];
  94. }
  95. }
  96. /**
  97. * @inheritdoc
  98. */
  99. public function beforeAction($action)
  100. {
  101. if (!parent::beforeAction($action)) {
  102. return false;
  103. }
  104. if (Yii::$app instanceof \yii\web\Application && !$this->checkAccess()) {
  105. throw new ForbiddenHttpException('You are not allowed to access this page.');
  106. }
  107. foreach (array_merge($this->coreGenerators(), $this->generators) as $id => $config) {
  108. $this->generators[$id] = Yii::createObject($config);
  109. }
  110. $this->resetGlobalSettings();
  111. return true;
  112. }
  113. /**
  114. * Resets potentially incompatible global settings done in app config.
  115. */
  116. protected function resetGlobalSettings()
  117. {
  118. if (Yii::$app instanceof \yii\web\Application) {
  119. Yii::$app->assetManager->bundles = [];
  120. }
  121. }
  122. /**
  123. * @return boolean whether the module can be accessed by the current user
  124. */
  125. protected function checkAccess()
  126. {
  127. $ip = Yii::$app->getRequest()->getUserIP();
  128. foreach ($this->allowedIPs as $filter) {
  129. if ($filter === '*' || $filter === $ip || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos))) {
  130. return true;
  131. }
  132. }
  133. Yii::warning('Access to Gii is denied due to IP address restriction. The requested IP is ' . $ip, __METHOD__);
  134. return false;
  135. }
  136. /**
  137. * Returns the list of the core code generator configurations.
  138. * @return array the list of the core code generator configurations.
  139. */
  140. protected function coreGenerators()
  141. {
  142. return [
  143. 'model' => ['class' => 'yii\gii\generators\model\Generator'],
  144. 'crud' => ['class' => 'yii\gii\generators\crud\Generator'],
  145. 'controller' => ['class' => 'yii\gii\generators\controller\Generator'],
  146. 'form' => ['class' => 'yii\gii\generators\form\Generator'],
  147. 'module' => ['class' => 'yii\gii\generators\module\Generator'],
  148. 'extension' => ['class' => 'yii\gii\generators\extension\Generator'],
  149. ];
  150. }
  151. }