-
Notifications
You must be signed in to change notification settings - Fork 7
/
EnterCode.php
180 lines (161 loc) · 4.21 KB
/
EnterCode.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace mdm\filter;
use Yii;
use yii\base\Module;
use yii\base\InvalidConfigException;
/**
* EnterCode
* Attach to Module or Application
*
* ~~~
* 'as filter' => [
* 'class' => 'mdm\filter\EnterCode',
* 'timeout' => 600, // default 300
* 'validationCallback' => function ($code, $actionId) {
* return $code === 'bismillah';
* },
* 'only' => [
* 'default/view', // actions
* ]
* ]
* ~~~
*
* @property \yii\base\Module $owner
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class EnterCode extends \yii\base\ActionFilter
{
/**
* @var string
*/
public $verificationRoute = 'verify';
/**
* @var Closure function use to validate
*
* ```php
* function($code, $actionId){
* // check
* return true;
* }
* ```
*/
public $validationCallback;
/**
* @var integer validation time in second
*/
public $timeout = 300;
/**
* @var string key to validate session variable.
* If null mean no validation
*/
public $validationKey;
/**
* @var boolean
*/
public $renewSession = true;
/**
* @var boolean
*/
public $validateForAll = false;
/**
* @var string message when code entered is invalid.
*/
public $message;
/**
* @var string file to be rendered
*/
public $viewFile = '@mdm/filter/views/verify.php';
/**
* @inheritdoc
*/
public function init()
{
if ($this->validationCallback === null) {
throw new InvalidConfigException('$validationCallback must be set');
}
if ($this->message === null) {
$this->message = "Code invalid";
}
$this->except[] = $this->verificationRoute . '/verify';
}
/**
* @inheritdoc
*/
public function attach($owner)
{
if ($owner instanceof Module) {
$owner->controllerMap[$this->verificationRoute] = [
'class' => __NAMESPACE__ . '\VerifyController',
'viewFile' => $this->viewFile,
'filter' => $this,
];
parent::attach($owner);
} else {
throw new InvalidConfigException(static::className() . '::$owner must instanceof yii\base\Module');
}
}
/**
* @inheritdoc
*/
public function beforeAction($action)
{
$route = $action->uniqueId;
$verify = Yii::$app->session->get($this->buildKey($this->validateForAll ? '' : $route));
if (!empty($this->validationKey)) {
$verify = is_string($verify) ? Yii::$app->security->validateData($verify, $this->validationKey) : false;
}
if ($verify < time() - $this->timeout) {
return $this->verifyRequired($route);
}
if ($this->renewSession) {
$this->setValid($route);
}
return true;
}
/**
* Redirect to verify controller
* @param string $route
* @return \yii\web\Response
*/
protected function verifyRequired($route)
{
$request = Yii::$app->getRequest();
Yii::$app->session->set($this->buildKey('_url'), [$route, $request->getUrl()]);
$id = $this->owner->uniqueId . '/' . $this->verificationRoute;
return Yii::$app->getResponse()->redirect([$id]);
}
/**
* Build session key
* @param type $key
* @return type
*/
public function buildKey($key)
{
return md5(serialize([__CLASS__, $this->owner->uniqueId, $key]));
}
/**
* Check entered code for action
* @param string $code
* @param string $route
* @return boolean
*/
public function isValid($code, $route)
{
return call_user_func($this->validationCallback, $code, $this->validateForAll ? '' : $route);
}
/**
* Set session
* @param string $route
*/
public function setValid($route)
{
if (!empty($this->validationKey)) {
$verify = Yii::$app->security->hashData(time(), $this->validationKey);
} else {
$verify = time();
}
Yii::$app->session->set($this->buildKey($this->validateForAll ? '' : $route), $verify);
}
}