2016-02-24 04:04:01 +05:30
|
|
|
<?php
|
|
|
|
namespace api\controllers;
|
|
|
|
|
2016-08-07 05:38:48 +05:30
|
|
|
use api\filters\ActiveUserRule;
|
2016-08-06 21:52:09 +05:30
|
|
|
use api\models\profile\AcceptRulesForm;
|
2016-05-16 13:51:12 +05:30
|
|
|
use api\models\profile\ChangeEmail\ConfirmNewEmailForm;
|
|
|
|
use api\models\profile\ChangeEmail\InitStateForm;
|
|
|
|
use api\models\profile\ChangeEmail\NewEmailForm;
|
2016-05-19 03:40:05 +05:30
|
|
|
use api\models\profile\ChangeLanguageForm;
|
2016-05-14 05:17:17 +05:30
|
|
|
use api\models\profile\ChangePasswordForm;
|
|
|
|
use api\models\profile\ChangeUsernameForm;
|
2016-07-17 23:16:04 +05:30
|
|
|
use common\helpers\Error as E;
|
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-08-06 21:52:09 +05:30
|
|
|
'actions' => ['current', 'accept-rules'],
|
2016-02-24 04:04:01 +05:30
|
|
|
'allow' => true,
|
|
|
|
'roles' => ['@'],
|
|
|
|
],
|
2016-04-24 00:14:10 +05:30
|
|
|
[
|
2016-08-07 05:38:48 +05:30
|
|
|
'class' => ActiveUserRule::class,
|
2016-05-16 13:51:12 +05:30
|
|
|
'actions' => [
|
|
|
|
'change-password',
|
|
|
|
'change-username',
|
|
|
|
'change-email-initialize',
|
|
|
|
'change-email-submit-new-email',
|
|
|
|
'change-email-confirm-new-email',
|
2016-05-19 03:40:05 +05:30
|
|
|
'change-lang',
|
2016-05-16 13:51:12 +05:30
|
|
|
],
|
2016-04-24 00:14:10 +05:30
|
|
|
],
|
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-05-16 13:51:12 +05:30
|
|
|
'change-email-initialize' => ['POST'],
|
|
|
|
'change-email-submit-new-email' => ['POST'],
|
|
|
|
'change-email-confirm-new-email' => ['POST'],
|
2016-05-19 03:40:05 +05:30
|
|
|
'change-lang' => ['POST'],
|
2016-08-06 21:52:09 +05:30
|
|
|
'accept-rules' => ['POST'],
|
2016-02-24 04:04:01 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionCurrent() {
|
|
|
|
$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-08-06 21:22:03 +05:30
|
|
|
'shouldAcceptRules' => !$account->isAgreedWithActualRules(),
|
2016-02-24 04:04:01 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-02-27 03:52:09 +05:30
|
|
|
public function actionChangePassword() {
|
|
|
|
$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-05-16 13:51:12 +05:30
|
|
|
public function actionChangeEmailInitialize() {
|
|
|
|
$account = Yii::$app->user->identity;
|
|
|
|
$model = new InitStateForm($account);
|
2016-05-23 00:34:52 +05:30
|
|
|
$model->load(Yii::$app->request->post());
|
2016-05-16 13:51:12 +05:30
|
|
|
if (!$model->sendCurrentEmailConfirmation()) {
|
2016-07-17 23:16:04 +05:30
|
|
|
$data = [
|
2016-05-16 13:51:12 +05:30
|
|
|
'success' => false,
|
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
|
|
|
];
|
2016-07-17 23:16:04 +05:30
|
|
|
|
|
|
|
if (ArrayHelper::getValue($data['errors'], 'email') === E::RECENTLY_SENT_MESSAGE) {
|
|
|
|
$emailActivation = $model->getEmailActivation();
|
|
|
|
$data['data'] = [
|
|
|
|
'canRepeatIn' => $emailActivation->canRepeatIn(),
|
|
|
|
'repeatFrequency' => $emailActivation->repeatTimeout,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
2016-05-16 13:51:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionChangeEmailSubmitNewEmail() {
|
|
|
|
$account = Yii::$app->user->identity;
|
|
|
|
$model = new NewEmailForm($account);
|
|
|
|
$model->load(Yii::$app->request->post());
|
|
|
|
if (!$model->sendNewEmailConfirmation()) {
|
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionChangeEmailConfirmNewEmail() {
|
|
|
|
$account = Yii::$app->user->identity;
|
|
|
|
$model = new ConfirmNewEmailForm($account);
|
|
|
|
$model->load(Yii::$app->request->post());
|
|
|
|
if (!$model->changeEmail()) {
|
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
'data' => [
|
|
|
|
'email' => $account->email,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-05-19 03:40:05 +05:30
|
|
|
public function actionChangeLang() {
|
|
|
|
$account = Yii::$app->user->identity;
|
|
|
|
$model = new ChangeLanguageForm($account);
|
|
|
|
$model->load(Yii::$app->request->post());
|
|
|
|
if (!$model->applyLanguage()) {
|
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-08-06 21:52:09 +05:30
|
|
|
public function actionAcceptRules() {
|
|
|
|
$account = Yii::$app->user->identity;
|
|
|
|
$model = new AcceptRulesForm($account);
|
|
|
|
$model->load(Yii::$app->request->post());
|
|
|
|
if (!$model->agreeWithLatestRules()) {
|
|
|
|
return [
|
|
|
|
'success' => false,
|
|
|
|
'errors' => $this->normalizeModelErrors($model->getErrors()),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => true,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-02-24 04:04:01 +05:30
|
|
|
}
|