1, 'email' => 2, 'full' => 3 ); protected $_checkTypeArray = array('phone', 'email', 'full'); protected $_codeTypeArray = array('phone', 'email'); protected $_successMessages = array( 'logout' => 'User has been logged out', 'update' => 'User data has been updated', 'checkCode' => 'Success check code', 'sendCode' => 'Confirmation code has been sent' ); protected $_errorMessages = array( 'badRequest' => 'Bad request', 'emptyData' => 'User does not exist', 'guestMode' => 'This feature not available for guest', 'update' => 'User data has not been updated', 'checkType' => 'Wrong check type', 'checkCode' => 'Wrong check code', 'emptyPhoneCode' => 'Empty phone code', 'emptyEmailCode' => 'Empty email code', 'sendCode' => 'Confirmation code has not been sent', 'cantLogin' => 'Can\'t login user', ); public function behaviors() { return array( 'access' => array( 'class' => AccessControlBehavior::className(), 'rules' => array( array( 'allow' => true, 'actions' => array('login', 'info', 'switch-language') ), array( 'allow' => true, 'actions' => array('switch-language', 'info', 'fullinfo', 'logout', 'update', 'checkcode', 'sendcode'), 'roles'=>array('@') ), // deny all array( 'allow' => false ) ) ) ); } public function actionSwitchLanguage() { $this->baseVerifyAjaxRequest(); $data = $this->getInputJson(); if (empty($data) || empty($data['languageId']) || !array_key_exists($data['languageId'], LanguageHelper::$db2jsMap)) { $this->_status->setError(Yii::t('api', $this->_errorMessages['badRequest'])); } else { $languageId = $data['languageId']; LanguageHelper::setLanguage($languageId); $this->_status->setSuccess(Yii::t('api', $this->_successMessages['update'])); if (!yii::$app->user->isGuest) { $user = Users::find(yii::$app->user->identity->id); $user->scenario = 'update'; $user['preferredLanguage'] = $languageId; if (!$user->update(false)) { $this->_status->setErrorReport(Yii::t('api', $this->_errorMessages['update']), $user->errors); } } } return $this->answer(); } public function actionInfo() { $this->baseVerifyAjaxRequest(); if (\Yii::$app->user->isGuest) { $this->_status->setError(\Yii::t('api', $this->_errorMessages['emptyData'])); } else { $this->_status->data = Users::getUserData(); } return $this->answer(); } public function actionFullinfo() { $this->baseVerifyAjaxRequest(); if (\Yii::$app->user->isGuest) { $this->_status->setError(\Yii::t('api', $this->_errorMessages['emptyData'])); } else { $this->_status->data = Users::getUserData(); } return $this->answer(); } public function actionLogout() { \Yii::$app->user->logout(); $this->_status->setSuccess(\Yii::t('api', $this->_successMessages['logout'])); return $this->answer(); } public function actionLogin() { $model = new LoginForm(); $model->load(array($model->formName() => $this->getInputJson())); if ($model->load(array($model->formName() => $this->getInputJson())) && $model->login()) { $this->_status->data = Users::getUserData(); } else { $this->_status->setErrorReport(\Yii::t('api',$this->_errorMessages['cantLogin']), $model->errors); } return $this->answer(); } public function actionUpdate() { $this->baseVerifyAjaxRequest(); $data = $this->getInputJson(); if (isset($data['birthDate'])){ $data['birthDate'] = date('Y-m-d', strtotime($data['birthDate'])); } if ($data['phone']) { $data['login'] = $data['phone']; } /** @var Users $model */ $model = Users::find(yii::$app->user->identity->id); if ($model === null) { $this->_status->setError(\Yii::t('api', $this->_errorMessages['guestMode'])); } else { $model->setScenario('update'); $model->password = null; if ($model->load(array($model->formName() => $data)) && $model->save()) { if ($model->hasErrors()) { $this->_status->setErrorReport(\Yii::t('api', $this->_errorMessages['update']), $model->errors); } else { if (isset($model->preferredLanguage) && $model->preferredLanguage !== LanguageHelper::getLanguage()) { LanguageHelper::setLanguage($model->preferredLanguage); } $this->_status->setSuccess(\Yii::t('api', $this->_successMessages['update'])); $this->_status->data = Users::getUserData(); } } else { $this->_status->setErrorReport(\Yii::t('api', $this->_errorMessages['update']), $model->errors); } } return $this->answer(); } public function actionCheckcode() { $this->baseVerifyAjaxRequest(); /** @var Users $model */ $model = Users::find(yii::$app->user->identity->id); if ($model === null) { $this->_status->setError(Yii::t('api', $this->_errorMessages['guestMode'])); } else { $data = $this->getInputJson(); if (!isset($data['checkType']) || !in_array($data['checkType'], $this->_checkTypeArray)) { $this->_status->setError(\Yii::t('api', $this->_errorMessages['checkType'])); } else { $error = array(); switch ($data['checkType']) { case 'phone': if (!isset($data['phoneCode'])){ $error[] = $this->_errorMessages['emptyPhoneCode']; $checkResult = false; } else { $checkResult = Users::checkCode($data['phoneCode'], null) == $this->_checkTypeSuccessCode[$data['checkType']]; } break; case 'email': if (!isset($data['emailCode'])){ $error[] = $this->_errorMessages['emptyPhoneCode']; $checkResult = false; } else { $checkResult = Users::checkCode(null, $data['emailCode']) == $this->_checkTypeSuccessCode[$data['checkType']]; } break; case 'full': default: if (!isset($data['phoneCode'])){ $error[] = $this->_errorMessages['emptyPhoneCode']; } if (!isset($data['emailCode'])){ $error[] = $this->_errorMessages['emptyPhoneCode']; } if (empty($error)) { $checkResult = Users::checkCode($data['phoneCode'], $data['emailCode']) == $this->_checkTypeSuccessCode[$data['checkType']]; } else { $checkResult = false; } break; } if ($checkResult) { $this->_status->setSuccess(Yii::t('api', $this->_successMessages['checkCode'])); $model->resetCheckCodes($data['checkType']); } else { $this->_status->setError(Yii::t('api', $this->_errorMessages['checkCode'])); if (!empty($error)) { if (!isset($this->_status->data)) $this->_status->data['error'] = $error; } } } } return $this->answer(); } public function actionSendcode() { $this->baseVerifyAjaxRequest(); /** @var Users $model */ $model = Users::find(yii::$app->user->identity->id); if ($model === null) { $this->_status->setError(Yii::t('api', $this->_errorMessages['guestMode'])); } else { $data = $this->getInputJson(); if (!isset($data['codeType']) || !in_array($data['codeType'], $this->_codeTypeArray)) { $this->_status->setError(\Yii::t('api', $this->_errorMessages['checkType'])); } else { $sendResult = $model->sendCode(yii::$app->user->getId(), $data['codeType']); $this->_status->status = $sendResult; $this->_status->message = $sendResult ? Yii::t('api', $this->_successMessages['sendCode']) : Yii::t('api', $this->_errorMessages['sendCode']); } } return $this->answer(); } }