2016-02-24 04:04:01 +05:30
|
|
|
<?php
|
|
|
|
namespace api\controllers;
|
|
|
|
|
2016-02-27 03:52:09 +05:30
|
|
|
use api\models\ChangePasswordForm;
|
2016-02-24 04:04:01 +05:30
|
|
|
use common\models\Account;
|
|
|
|
use Yii;
|
|
|
|
use yii\filters\AccessControl;
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
|
2016-02-26 04:15:21 +05:30
|
|
|
class AccountsController extends Controller {
|
2016-02-24 04:04:01 +05:30
|
|
|
|
|
|
|
public function behaviors() {
|
|
|
|
return ArrayHelper::merge(parent::behaviors(), [
|
|
|
|
'access' => [
|
|
|
|
'class' => AccessControl::class,
|
|
|
|
'rules' => [
|
|
|
|
[
|
2016-02-27 03:52:09 +05:30
|
|
|
'actions' => ['current', 'change-password'],
|
2016-02-24 04:04:01 +05:30
|
|
|
'allow' => true,
|
|
|
|
'roles' => ['@'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function verbs() {
|
|
|
|
return [
|
|
|
|
'current' => ['GET'],
|
2016-02-27 03:52:09 +05:30
|
|
|
'change-password' => ['POST'],
|
2016-02-24 04:04:01 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionCurrent() {
|
|
|
|
/** @var Account $account */
|
|
|
|
$account = Yii::$app->user->identity;
|
|
|
|
|
|
|
|
return [
|
|
|
|
'id' => $account->id,
|
|
|
|
'username' => $account->username,
|
|
|
|
'email' => $account->email,
|
|
|
|
'shouldChangePassword' => $account->password_hash_strategy === Account::PASS_HASH_STRATEGY_OLD_ELY,
|
2016-02-26 11:55:47 +05:30
|
|
|
'isActive' => $account->status === Account::STATUS_ACTIVE,
|
2016-02-24 04:04:01 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-02-27 03:52:09 +05:30
|
|
|
public function actionChangePassword() {
|
|
|
|
/** @var Account $account */
|
|
|
|
$account = Yii::$app->user->identity;
|
|
|
|
$model = new ChangePasswordForm($account);
|
|
|
|
$model->load(Yii::$app->request->post());
|
|
|
|
if (!$model->changePassword()) {
|
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-02-24 04:04:01 +05:30
|
|
|
}
|