2017-01-21 04:24:30 +05:30
|
|
|
<?php
|
|
|
|
namespace api\controllers;
|
|
|
|
|
|
|
|
use api\filters\ActiveUserRule;
|
|
|
|
use api\models\profile\TwoFactorAuthForm;
|
|
|
|
use Yii;
|
|
|
|
use yii\filters\AccessControl;
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
|
|
|
|
class TwoFactorAuthController extends Controller {
|
|
|
|
|
|
|
|
public $defaultAction = 'credentials';
|
|
|
|
|
|
|
|
public function behaviors() {
|
|
|
|
return ArrayHelper::merge(parent::behaviors(), [
|
|
|
|
'access' => [
|
|
|
|
'class' => AccessControl::class,
|
|
|
|
'rules' => [
|
|
|
|
[
|
2017-01-23 04:37:29 +05:30
|
|
|
'allow' => true,
|
2017-01-21 04:24:30 +05:30
|
|
|
'class' => ActiveUserRule::class,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-01-23 04:37:29 +05:30
|
|
|
public function verbs() {
|
|
|
|
return [
|
|
|
|
'credentials' => ['GET'],
|
|
|
|
'activate' => ['POST'],
|
|
|
|
'disable' => ['DELETE'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-01-21 04:24:30 +05:30
|
|
|
public function actionCredentials() {
|
|
|
|
$account = Yii::$app->user->identity;
|
|
|
|
$model = new TwoFactorAuthForm($account);
|
|
|
|
|
|
|
|
return $model->getCredentials();
|
|
|
|
}
|
|
|
|
|
2017-01-21 04:58:26 +05:30
|
|
|
public function actionActivate() {
|
|
|
|
$account = Yii::$app->user->identity;
|
|
|
|
$model = new TwoFactorAuthForm($account, ['scenario' => TwoFactorAuthForm::SCENARIO_ACTIVATE]);
|
2017-01-23 04:37:29 +05:30
|
|
|
$model->load(Yii::$app->request->post());
|
2017-01-21 04:58:26 +05:30
|
|
|
if (!$model->activate()) {
|
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $model->getFirstErrors(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionDisable() {
|
|
|
|
$account = Yii::$app->user->identity;
|
|
|
|
$model = new TwoFactorAuthForm($account, ['scenario' => TwoFactorAuthForm::SCENARIO_DISABLE]);
|
2017-01-23 04:37:29 +05:30
|
|
|
$model->load(Yii::$app->request->getBodyParams());
|
2017-01-21 04:58:26 +05:30
|
|
|
if (!$model->disable()) {
|
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $model->getFirstErrors(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-01-21 04:24:30 +05:30
|
|
|
}
|