Module.php 4.65 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\gii;

use Yii;
use yii\web\HttpException;

/**
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 * This is the main module class for the Gii module.
 *
 * To use Gii, include it as a module in the application configuration like the following:
 *
 * ~~~
 * return array(
 *     ......
 *     'modules' => array(
 *         'gii' => array(
 *             'class' => 'yii\gii\Module',
 *         ),
 *     ),
 * )
 * ~~~
 *
 * Because Gii generates new code files on the server, you should only use it on your own
 * development machine. To prevent other people from using this module, by default, Gii
 * can only be accessed by localhost. You may configure its [[allowedIPs]] property if
 * you want to make it accessible on other machines.
 *
 * With the above configuration, you will be able to access GiiModule in your browser using
 * the URL `http://localhost/path/to/index.php?r=gii`
 *
Qiang Xue committed
37 38 39
 * If your application enables [[UrlManager::enablePrettyUrl|pretty URLs]] and you have defined
 * custom URL rules or enabled [[UrlManager::enableStrictParsing], you may need to add
 * the following URL rules at the beginning of your URL rule set in your application configuration
40 41 42 43 44 45 46 47 48 49 50 51 52
 * in order to access Gii:
 *
 * ~~~
 * 'rules'=>array(
 *     'gii' => 'gii',
 *     'gii/<controller>' => 'gii/<controller>',
 *     'gii/<controller>/<action>' => 'gii/<controller>/<action>',
 *     ...
 * ),
 * ~~~
 *
 * You can then access Gii via URL: `http://localhost/path/to/index.php/gii`
 *
Qiang Xue committed
53 54 55 56 57
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Module extends \yii\base\Module
{
58 59 60
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
61 62 63 64 65 66 67 68 69 70
	public $controllerNamespace = 'yii\gii\controllers';
	/**
	 * @var array the list of IPs that are allowed to access this module.
	 * Each array element represents a single IP filter which can be either an IP address
	 * or an address with wildcard (e.g. 192.168.0.*) to represent a network segment.
	 * The default value is `array('127.0.0.1', '::1')`, which means the module can only be accessed
	 * by localhost.
	 */
	public $allowedIPs = array('127.0.0.1', '::1');
	/**
71 72 73 74 75 76 77 78 79
	 * @var array|Generator[] a list of generator configurations or instances. The array keys
	 * are the generator IDs (e.g. "crud"), and the array elements are the corresponding generator
	 * configurations or the instances.
	 *
	 * After the module is initialized, this property will become an array of generator instances
	 * which are created based on the configurations previously taken by this property.
	 *
	 * Newly assigned generators will be merged with the [[coreGenerators()|core ones]], and the former
	 * takes precedence in case when they have the same generator ID.
Qiang Xue committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	 */
	public $generators = array();
	/**
	 * @var integer the permission to be set for newly generated code files.
	 * This value will be used by PHP chmod function.
	 * Defaults to 0666, meaning the file is read-writable by all users.
	 */
	public $newFileMode = 0666;
	/**
	 * @var integer the permission to be set for newly generated directories.
	 * This value will be used by PHP chmod function.
	 * Defaults to 0777, meaning the directory can be read, written and executed by all users.
	 */
	public $newDirMode = 0777;


	/**
97
	 * @inheritdoc
Qiang Xue committed
98 99 100 101 102 103 104 105 106
	 */
	public function init()
	{
		parent::init();
		foreach (array_merge($this->coreGenerators(), $this->generators) as $id => $config) {
			$this->generators[$id] = Yii::createObject($config);
		}
	}

107 108 109
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
110 111 112 113 114 115 116 117 118
	public function beforeAction($action)
	{
		if ($this->checkAccess()) {
			return parent::beforeAction($action);
		} else {
			throw new HttpException(403, 'You are not allowed to access this page.');
		}
	}

119 120 121
	/**
	 * @return boolean whether the module can be accessed by the current user
	 */
Qiang Xue committed
122 123 124 125 126 127 128 129 130 131 132
	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;
			}
		}
		return false;
	}

133 134 135 136
	/**
	 * Returns the list of the core code generator configurations.
	 * @return array the list of the core code generator configurations.
	 */
Qiang Xue committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	protected function coreGenerators()
	{
		return array(
			'model' => array(
				'class' => 'yii\gii\generators\model\Generator',
			),
			'crud' => array(
				'class' => 'yii\gii\generators\crud\Generator',
			),
			'controller' => array(
				'class' => 'yii\gii\generators\controller\Generator',
			),
			'form' => array(
				'class' => 'yii\gii\generators\form\Generator',
			),
			'module' => array(
				'class' => 'yii\gii\generators\module\Generator',
			),
		);
	}
}