No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

170 líneas
6.1KB

  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. ['class' => 'yii\web\UrlRule', 'pattern' => $this->id, 'route' => $this->id . '/default/index'],
  85. ['class' => 'yii\web\UrlRule', 'pattern' => $this->id . '/<id:\w+>', 'route' => $this->id . '/default/view'],
  86. ['class' => 'yii\web\UrlRule', 'pattern' => $this->id . '/<controller:[\w\-]+>/<action:[\w\-]+>', 'route' => $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. if (is_object($config)) {
  109. $this->generators[$id] = $config;
  110. } else {
  111. $this->generators[$id] = Yii::createObject($config);
  112. }
  113. }
  114. $this->resetGlobalSettings();
  115. return true;
  116. }
  117. /**
  118. * Resets potentially incompatible global settings done in app config.
  119. */
  120. protected function resetGlobalSettings()
  121. {
  122. if (Yii::$app instanceof \yii\web\Application) {
  123. Yii::$app->assetManager->bundles = [];
  124. }
  125. }
  126. /**
  127. * @return boolean whether the module can be accessed by the current user
  128. */
  129. protected function checkAccess()
  130. {
  131. $ip = Yii::$app->getRequest()->getUserIP();
  132. foreach ($this->allowedIPs as $filter) {
  133. if ($filter === '*' || $filter === $ip || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos))) {
  134. return true;
  135. }
  136. }
  137. Yii::warning('Access to Gii is denied due to IP address restriction. The requested IP is ' . $ip, __METHOD__);
  138. return false;
  139. }
  140. /**
  141. * Returns the list of the core code generator configurations.
  142. * @return array the list of the core code generator configurations.
  143. */
  144. protected function coreGenerators()
  145. {
  146. return [
  147. 'model' => ['class' => 'yii\gii\generators\model\Generator'],
  148. 'crud' => ['class' => 'yii\gii\generators\crud\Generator'],
  149. 'controller' => ['class' => 'yii\gii\generators\controller\Generator'],
  150. 'form' => ['class' => 'yii\gii\generators\form\Generator'],
  151. 'module' => ['class' => 'yii\gii\generators\module\Generator'],
  152. 'extension' => ['class' => 'yii\gii\generators\extension\Generator'],
  153. ];
  154. }
  155. }