1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace luya\web; |
4
|
|
|
|
5
|
|
|
use luya\helpers\StringHelper; |
6
|
|
|
use Yii; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Request Component. |
10
|
|
|
* |
11
|
|
|
* Extending the {{yii\web\Request}} class by predefine values and add ability to verify whether the request is in admin context or not. |
12
|
|
|
* |
13
|
|
|
* @property boolean $isAdmin Whether the request is admin or not. |
14
|
|
|
* |
15
|
|
|
* @author Basil Suter <[email protected]> |
16
|
|
|
* @since 1.0.0 |
17
|
|
|
*/ |
18
|
|
|
class Request extends \yii\web\Request |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var boolean Force web request to enable unit tests with simulated web requests |
22
|
|
|
*/ |
23
|
|
|
public $forceWebRequest = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string The validation cookie for cookies, should be overwritten in your configuration. |
27
|
|
|
* |
28
|
|
|
* The cookie validation key is generated randomly or by any new release but should be overriten in your config. |
29
|
|
|
* |
30
|
|
|
* http://randomkeygen.com using a 504-bit WPA Key |
31
|
|
|
*/ |
32
|
|
|
public $cookieValidationKey = '(`1gq(|TI2Zxx7zZH<Zk052a9a$@l2EtD9wT`lkTO@7uy{cPaJt4y70mxh4q(3'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array A list of default available parsers. |
36
|
|
|
*/ |
37
|
|
|
public $parsers = [ |
38
|
|
|
'application/json' => 'yii\web\JsonParser', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
private $_isAdmin; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Setter method to force isAdmin request. |
45
|
|
|
* |
46
|
|
|
* @param boolean $state Whether its an admin request or not |
47
|
|
|
*/ |
48
|
|
|
public function setIsAdmin($state) |
49
|
|
|
{ |
50
|
|
|
$this->_isAdmin = $state; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Getter method resolves the current url request and check if admin context. |
55
|
|
|
* |
56
|
|
|
* This is mostly used in order to bootstrap more modules and application logic in admin context. |
57
|
|
|
* |
58
|
|
|
* @return boolean If the current request is in admin context return value is true, otherwise false. |
59
|
|
|
*/ |
60
|
|
|
public function getIsAdmin() |
61
|
|
|
{ |
62
|
|
|
if ($this->_isAdmin === null) { |
63
|
|
|
if ($this->getIsConsoleRequest() && !$this->forceWebRequest && !Yii::$app->hasModule('admin')) { |
64
|
|
|
$this->_isAdmin = false; |
65
|
|
|
} else { |
66
|
|
|
// if there is only an application with admin module and set as default route |
67
|
|
|
// this might by the admin module even when pathInfo is empty |
68
|
|
|
if (Yii::$app->defaultRoute == 'admin' && empty($this->pathInfo)) { |
69
|
|
|
$this->_isAdmin = true; |
70
|
|
|
} else { |
71
|
|
|
$resolver = Yii::$app->composition->getResolvedPathInfo($this); |
72
|
|
|
$parts = explode('/', $resolver->resolvedPath); |
73
|
|
|
$first = reset($parts); |
74
|
|
|
|
75
|
|
|
// Check for a full route path where the module ends with admin like `newsadmin` and this module is loaded in the list of modules. |
76
|
|
|
// @see https://github.com/luyadev/luya/pull/2027 |
77
|
|
|
if (count($parts) > 0 && StringHelper::endsWith($first, 'admin') && Yii::$app->hasModule($first)) |
78
|
|
|
$this->_isAdmin = true; |
79
|
|
|
elseif (strtolower(trim($first)) == 'admin') { |
80
|
|
|
$this->_isAdmin = true; |
81
|
|
|
} else { |
82
|
|
|
$this->_isAdmin = false; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->_isAdmin; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the user client language. |
93
|
|
|
* |
94
|
|
|
* @param string $defaultValue Return if not set. |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getClientLanguage($defaultValue) |
98
|
|
|
{ |
99
|
|
|
return isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) : $defaultValue; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|