2016-02-24 04:04:01 +05:30
|
|
|
<?php
|
|
|
|
namespace api\controllers;
|
|
|
|
|
2016-05-14 05:17:17 +05:30
|
|
|
use api\models\profile\ChangePasswordForm;
|
|
|
|
use api\models\profile\ChangeUsernameForm;
|
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-04-24 00:14:10 +05:30
|
|
|
'actions' => ['current'],
|
2016-02-24 04:04:01 +05:30
|
|
|
'allow' => true,
|
|
|
|
'roles' => ['@'],
|
|
|
|
],
|
2016-04-24 00:14:10 +05:30
|
|
|
[
|
|
|
|
'actions' => ['change-password', 'change-username'],
|
|
|
|
'allow' => true,
|
|
|
|
'roles' => ['@'],
|
|
|
|
'matchCallback' => function() {
|
|
|
|
/** @var Account $account */
|
|
|
|
$account = Yii::$app->user->identity;
|
|
|
|
return $account->status > Account::STATUS_REGISTERED;
|
|
|
|
},
|
|
|
|
],
|
2016-02-24 04:04:01 +05:30
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function verbs() {
|
|
|
|
return [
|
|
|
|
'current' => ['GET'],
|
2016-02-27 03:52:09 +05:30
|
|
|
'change-password' => ['POST'],
|
2016-04-24 00:14:10 +05:30
|
|
|
'change-username' => ['POST'],
|
2016-02-24 04:04:01 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionCurrent() {
|
|
|
|
/** @var Account $account */
|
|
|
|
$account = Yii::$app->user->identity;
|
|
|
|
|
|
|
|
return [
|
|
|
|
'id' => $account->id,
|
2016-03-12 03:25:46 +05:30
|
|
|
'uuid' => $account->uuid,
|
2016-02-24 04:04:01 +05:30
|
|
|
'username' => $account->username,
|
|
|
|
'email' => $account->email,
|
2016-05-13 14:33:00 +05:30
|
|
|
'lang' => $account->lang,
|
2016-02-24 04:04:01 +05:30
|
|
|
'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-03-12 03:32:52 +05:30
|
|
|
'passwordChangedAt' => $account->password_changed_at,
|
2016-04-24 00:14:10 +05:30
|
|
|
'hasMojangUsernameCollision' => $account->hasMojangUsernameCollision(),
|
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-03-20 04:55:26 +05:30
|
|
|
public function actionChangeUsername() {
|
|
|
|
$model = new ChangeUsernameForm();
|
|
|
|
$model->load(Yii::$app->request->post());
|
|
|
|
if (!$model->change()) {
|
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-02-24 04:04:01 +05:30
|
|
|
}
|