1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace enigmatix\confirmation; |
4
|
|
|
|
5
|
|
|
use yii\base\ErrorException; |
6
|
|
|
use yii\behaviors\BlameableBehavior; |
7
|
|
|
use yii\helpers\ArrayHelper; |
8
|
|
|
use yii\behaviors\TimestampBehavior; |
9
|
|
|
use Yii; |
10
|
|
|
use yii\helpers\Url; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This is the model class for table "{{%confirmation_request}}". |
15
|
|
|
* |
16
|
|
|
* @property integer $id |
17
|
|
|
* @property string $model |
18
|
|
|
* @property integer $object_id |
19
|
|
|
* @property string $object |
20
|
|
|
* @property string $release_token |
21
|
|
|
* @property integer $created_at |
22
|
|
|
* @property integer $updated_at |
23
|
|
|
* @property string $values |
24
|
|
|
* @property integer $created_by |
25
|
|
|
* @property integer $updated_by |
26
|
|
|
* |
27
|
|
|
* @property User $createdBy |
28
|
|
|
* @property User $updatedBy |
29
|
|
|
*/ |
30
|
|
|
class ConfirmationRequest extends \yii\db\ActiveRecord |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
protected $delivery = 'display'; |
34
|
|
|
|
35
|
|
|
protected $secondFactor = 'email'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
|
|
public static function tableName() |
41
|
|
|
{ |
42
|
|
|
return '{{%confirmation_request}}'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
public function rules() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
[['object_id'], 'integer'], |
52
|
|
|
[['object', 'values'], 'string'], |
53
|
|
|
[['model', 'release_token'], 'string', 'max' => 255], |
54
|
|
|
[['release_token'], 'default', 'value' => function ($model, $attribute){ return $this->generateReleaseToken();}], |
55
|
|
|
[['created_by'], 'exist', 'skipOnError' => true, 'targetClass' => $this->getUserClassName(), 'targetAttribute' => ['created_by' => 'id']], |
56
|
|
|
[['updated_by'], 'exist', 'skipOnError' => true, 'targetClass' => $this->getUserClassName(), 'targetAttribute' => ['updated_by' => 'id']], |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
|
|
public function attributeLabels() |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
'id' => Yii::t('app', 'ID'), |
67
|
|
|
'model' => Yii::t('app', 'Model'), |
68
|
|
|
'object_id' => Yii::t('app', 'Object'), |
69
|
|
|
'object' => Yii::t('app', 'Object'), |
70
|
|
|
'release_token' => Yii::t('app', 'Release Token'), |
71
|
|
|
'created_at' => Yii::t('app', 'Created At'), |
72
|
|
|
'updated_at' => Yii::t('app', 'Updated At'), |
73
|
|
|
'values' => Yii::t('app', 'Values'), |
74
|
|
|
'created_by' => Yii::t('app', 'Created By'), |
75
|
|
|
'updated_by' => Yii::t('app', 'Updated By'), |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritdoc |
82
|
|
|
*/ |
83
|
|
|
public function relations() |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
|
|
'CreatedBy' => 'one', |
87
|
|
|
'UpdatedBy' => 'one', |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function behaviors() { |
92
|
|
|
|
93
|
|
|
return ArrayHelper::merge(parent::behaviors(), |
94
|
|
|
[ |
95
|
|
|
TimestampBehavior::className(), |
96
|
|
|
BlameableBehavior::className(), |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getViewLink(){ |
101
|
|
|
return Url::to(['@web/confirmation-requests', 'release_token' => $this->release_token],true); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Generates new password reset token |
106
|
|
|
*/ |
107
|
|
|
public function generateReleaseToken() |
108
|
|
|
{ |
109
|
|
|
return Yii::$app->security->generateRandomString() . '_' . time(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return \yii\db\ActiveQuery |
115
|
|
|
*/ |
116
|
|
|
public function getCreatedBy() |
117
|
|
|
{ |
118
|
|
|
return $this->hasOne($this->getUserClassName(), ['id' => 'created_by']); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return \yii\db\ActiveQuery |
123
|
|
|
*/ |
124
|
|
|
public function getUpdatedBy() |
125
|
|
|
{ |
126
|
|
|
return $this->hasOne($this->getUserClassName(), ['id' => 'updated_by']); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string ActiveRecord user class, as per application implementation |
131
|
|
|
*/ |
132
|
|
|
protected function getUserClassName(){ |
133
|
|
|
return Yii::$app->user->identityClass; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function release(){ |
|
|
|
|
137
|
|
|
|
138
|
|
|
$model = $this->constructObject(); |
|
|
|
|
139
|
|
|
$changedValues = $model->getChangedValues(); |
|
|
|
|
140
|
|
|
$current = clone $model; |
|
|
|
|
141
|
|
|
$current->refresh(); |
142
|
|
|
|
143
|
|
|
foreach ($changedValues as $field => $value){ |
144
|
|
|
$oldValue = $model->oldAttributes[$field]; |
145
|
|
|
if($current->$field !== $oldValue && $current->$field !== $value){ |
146
|
|
|
throw new ErrorException( |
147
|
|
|
sprintf('Unable to release change, protected field %s has been updated since this request.' |
148
|
|
|
. ' Expected to find %s or %s, found %s', $field, $value, $oldValue, $current->$field)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$model->releaseToken = $this->release_token; |
154
|
|
|
return $model->save(); |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return \enigmatix\core\Model |
161
|
|
|
*/ |
162
|
|
|
public function constructObject(){ |
163
|
|
|
return unserialize($this->object); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.