Module.php 5.1 KB

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