1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yrc\api\actions; |
4
|
|
|
|
5
|
|
|
use app\forms\ResetPassword; |
6
|
|
|
use yrc\rest\Action as RestAction; |
7
|
|
|
|
8
|
|
|
use yii\web\HttpException; |
9
|
|
|
use Yii; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @class ResetPasswordAction |
13
|
|
|
* Handles token refresh |
14
|
|
|
*/ |
15
|
|
|
class ResetPasswordAction extends RestAction |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Allows a user to reset their password |
19
|
|
|
* @return mixed |
20
|
|
|
*/ |
21
|
|
|
public static function post($params) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
static $form; |
24
|
|
|
$token = Yii::$app->request->get('reset_token', false); |
25
|
|
|
|
26
|
|
|
// Determine the correct scenario to use based upon the reset token |
27
|
|
|
if ($token === false) { |
28
|
|
|
$form = new ResetPassword(['scenario' => ResetPassword::SCENARIO_INIT]); |
29
|
|
|
} else { |
30
|
|
|
$form = new ResetPassword(['scenario' => ResetPassword::SCENARIO_RESET]); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Load the form |
34
|
|
|
if (self::load($form, Yii::$app->request->post())) { |
35
|
|
|
$form->reset_token = Yii::$app->request->get('reset_token', null); |
36
|
|
|
|
37
|
|
|
// If the user is authenticated, populate the model |
38
|
|
|
if (!Yii::$app->user->isGuest) { |
39
|
|
|
$user = Yii::$app->yrc->userClass::findOne(['id' => Yii::$app->user->id]); |
40
|
|
|
$form->setUser($user); |
41
|
|
|
} else { |
42
|
|
|
$form->email = Yii::$app->request->post('email', null); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Validate the form and make sure all of the attributes are set, then perform the reset task depending upon the scenario |
46
|
|
|
if ($form->validate()) { |
47
|
|
|
return $form->reset(); |
48
|
|
|
} else { |
49
|
|
|
throw new HttpException(400, \json_encode($form->getErrors())); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($form->getScenario() === ResetPassword::SCENARIO_INIT) { |
|
|
|
|
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private static function load(&$form, $attributes) |
61
|
|
|
{ |
62
|
|
|
foreach ($attributes as $k => $v) { |
63
|
|
|
if (property_exists($form, $k)) { |
64
|
|
|
$form->$k = $v; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $form; |
69
|
|
|
} |
70
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.