-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfirmationBehavior.php
executable file
·303 lines (242 loc) · 8.65 KB
/
ConfirmationBehavior.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
* Created by PhpStorm.
* User: joels
* Date: 5/3/17
* Time: 6:12 PM
*/
namespace enigmatix\confirmation;
use yii\base\Behavior;
use yii\base\Event;
use yii\base\InvalidCallException;
use yii\db\BaseActiveRecord;
use Yii;
use yii\helpers\ArrayHelper;
/**
* Class ConfirmationBehavior
*
* @package enigmatix\confirmation
*/
class ConfirmationBehavior extends Behavior
{
/**
* @var bool whether to skip typecasting of `null` values.
* If enabled attribute value which equals to `null` will not be type-casted (e.g. `null` remains `null`),
* otherwise it will be converted according to the type configured at [[attributeTypes]].
*/
public $skipOnNull = true;
/**
* @var array a list of the attributes to be protected, provided when constructing the behavior and attaching,
* or in the model's behaviors method.
*/
public $protectedAttributes = [];
/**
* @var string If a release token has been supplied, provided the corresponding release object is valid, the change
* will be executed.
*/
public $releaseToken;
/**
* @var array A list of roles that can bypass the protection and make the change without triggering a confirmation
* request.
*/
public $allow = [];
/**
* @var string namespace of the object that stores and executes the ConfirmationRequest
*/
public $confirmationRequestClass = 'enigmatix\\confirmation\\ConfirmationRequest';
/**
* @var string delivery method for the Confirmation Request. Currently only email is supported.
*/
public $secondFactor = 'email';
/**
* @var string the name of the variable to use to traverse to the user table from the secured model.
*/
public $createdByAttribute = 'createdBy';
/**
* @var string
*/
public $confirmationViewPath = '@vendor/enigmatix/yii2-confirmation/mail/_confirmationEmail';
/**
* @var string The name of the table attribute that tracks when a record is updated. This value is ignored when
* determining of a record has changed values in it.
*/
public $timestampAttribute = 'updated_at';
/**
* @inheritdoc
*/
public function events()
{
return [BaseActiveRecord::EVENT_BEFORE_UPDATE => 'beforeSave'];
}
/**
* @param $event Event;
*/
public function beforeSave($event) {
$this->protectAttributes();
}
/**
* Business logic around triggering the confirmation request.
*/
protected function protectAttributes()
{
$user = Yii::$app->user;
$changedValues = $this->getChangedValues();
foreach ($changedValues as $attribute => $value) {
if ($this->skipOnNull && $value === null || $attribute == $this->timestampAttribute) {
continue;
}
if (!$this->isAuthorised($user, $attribute, $value)) {
$this->createConfirmationRequest();
$this->resetAttribute($attribute);
}
}
}
/**
* Checks whether a user is allowed to make the change without triggering a confirmation request.
* @param \yii\web\User $user
* @param string $attribute
* @param string $value
*
* @return bool
*/
protected function isAuthorised($user, $attribute, $value) {
//Check for pre-defined administration roles
if ($this->userIsAuthorised($user)) {
return true;
}
//Check for valid release token , eg that the token exists and is for the same record as this
if ($this->releaseToken != null) {
$confirmation = ConfirmationRequest::findOne(['release_token' => $this->releaseToken]);
if ($confirmation == null) {
return false;
}
$model = $confirmation->constructObject();
return $this->owner->getPrimaryKey(true) == $model->getPrimaryKey(true);
}
//Check to see if any protected attributes have been altered
foreach ($this->protectedAttributes as $attribute) {
if ($this->hasChanged($attribute))
return false;
}
return true;
}
/**
* Iterates over roles to determine if the user is authorised to complete the action.
* @param \yii\web\User $user
*
* @return bool
*/
protected function userIsAuthorised($user) {
foreach ($this->allow as $role) {
if ($user->can($role))
return true;
}
return false;
}
/**
* Business logic handling the creation of the Confirmation Request, and sending the second factor message.
*/
protected function createConfirmationRequest() {
$model = $this->owner;
$changedValues = $this->getChangedValues();
/* @var ConfirmationRequest $request */
$request = new $this->confirmationRequestClass([
'model' => $model->className(),
'object' => serialize($model),
'values' => serialize($changedValues),
]);
$request->save();
$this->sendSecondFactorMessage($request);
}
/**
* Determines whether an attribute has been changed in the object.
* @param string $attribute
*
* @return bool
*/
protected function hasChanged($attribute) {
return $this->owner->oldAttributes[$attribute] != $this->owner->{$attribute};
}
/**
* Fetches all values which have changed, expect for the timestamp attribute.
* @return array
*/
public function getChangedValues() {
$changedAttributes = [];
foreach ($this->owner->attributes() as $attribute) {
if ($this->hasChanged($attribute))
$changedAttributes[$attribute] = $this->owner->$attribute;
}
unset($changedAttributes[$this->timestampAttribute]);
return $changedAttributes;
}
/**
* Sets an attribute back to it's original value when it was fetched.
* @param string $attribute
*/
protected function resetAttribute($attribute) {
$this->owner->$attribute = $this->owner->oldAttributes[$attribute];
}
/**
* Adds a flash message to the interface stating the change has been held over pending confirmation.
* @param ConfirmationRequest $model
*/
public function createFeedbackMessage($model) {
$this->displayMessage($model);
}
/**
* Business logic around displaying an appropriate feedback message to the user regbarding the change.
* @param ConfirmationRequest $model
*/
protected function displayMessage($model) {
Yii::$app->session->setFlash('warning', 'Your update is pending confirmation. Please check your email for a confirmation link.');
}
/**
* Business logic around transmitting the second factor message.
* @param ConfirmationRequest $model
*/
public function sendSecondFactorMessage($model) {
switch ($this->secondFactor) {
case 'email':
Yii::$app->mailer
->compose($this->confirmationViewPath, ['model' => $model])
->setTo([$this->getEmail($model)])
->send();
$this->createFeedbackMessage($model);
break;
default:
break;
}
}
/**
* Attempts to retrieve an address from several places within the request. Firstly, attempts to find an email in the
* changed values, and then looks within the current object for an email, and lastly attempts to traverse to the User
* who created the object if an identifier has been recorded.
*
* @param ConfirmationRequest $model
*
* @return string
* @throws InvalidCallException
*/
protected function getEmail($model) {
$values = unserialize($model->values);
$email = ArrayHelper::getValue($values, 'email');
$object = $model->constructObject();
if ($email == null) {
$email = ArrayHelper::getValue($values, 'email_address');
}
if ($email == null) {
$email = ArrayHelper::getValue($object, 'email');
}
if ($email == null) {
$email = ArrayHelper::getValue($object, 'email_address');
}
if ($email == null) {
$email = ArrayHelper::getValue($object, $this->createdByAttribute . '.email');
}
if ($email == null) {
throw new InvalidCallException('Unable to locate email address via record, changed values, or user account');
}
return $email;
}
}