Module.php 6.5 KB

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