forked from brussens/yii2-maintenance-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaintenanceMode.php
151 lines (131 loc) · 3.36 KB
/
MaintenanceMode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* Maintenance mode component for Yii framework 2.x.x version.
*
* Class MaintenanceMode
*
* @package brussens\maintenance
* @version 0.2.0
* @author BrusSENS (Brusenskiy Dmitry) <brussens@nativeweb.ru>
* @link https://github.com/brussens/yii2-maintenance-mode
*/
namespace brussens\maintenance;
use yii\base\InvalidConfigException;
use yii\base\Component;
use Yii;
class MaintenanceMode extends Component
{
/**
*
* Mode status
*
* @var bool
*/
public $enabled = true;
/**
* Route to action
*
* @var string
*/
public $route = 'maintenance/index';
/**
* Show message
*
* @var null
*/
public $message;
/**
* Allowed user names
*
* @var array
*/
public $users;
/**
* Allowed roles
* @var array
*/
public $roles;
/**
* Allowed IP addresses
*
* @var array
*/
public $ips;
/**
* Allowed urls
*
* @var array
*/
public $urls;
/**
* Path to layout file
*
* @var string
*/
public $layoutPath = '@vendor/brussens/yii2-maintenance-mode/views/layouts/main';
/**
* Path to view file
*
* @var string
*/
public $viewPath = '@vendor/brussens/yii2-maintenance-mode/views/maintenance/index';
/**
* Username attribute name
*
* @var string
*/
public $usernameAttribute = 'username';
/**
* Disable items.
* @var
*/
protected $_disable;
/**
* init method
*/
public function init()
{
if ($this->enabled) {
if($this->users) {
if(is_array($this->users)) {
$this->_disable = in_array(Yii::$app->user->identity->{$this->usernameAttribute}, $this->users);
}
else {
throw new InvalidConfigException('Parameter "users" should be an array.');
}
}
if($this->roles) {
if(is_array($this->roles)) {
foreach ($this->roles as $role) {
$this->_disable = $this->_disable || Yii::$app->user->can($role);
}
}
else {
throw new InvalidConfigException('Parameter "roles" should be an array.');
}
}
if($this->urls) {
if(is_array($this->urls)) {
$this->_disable = $this->_disable || in_array(Yii::$app->request->getPathInfo(), $this->urls);
}
else {
throw new InvalidConfigException('Parameter "urls" should be an array.');
}
}
if($this->ips) {
if(is_array($this->ips)) {
$this->_disable = $this->_disable || in_array(Yii::$app->request->userIP, $this->ips);
}
else {
throw new InvalidConfigException('Parameter "ips" should be an array.');
}
}
if (!$this->_disable) {
if ($this->route === 'maintenance/index') {
Yii::$app->controllerMap['maintenance'] = 'brussens\maintenance\controllers\MaintenanceController';
}
Yii::$app->catchAll = [$this->route];
}
}
}
}