diff --git a/mirzaev/skillparts/system/config/web.php.example b/mirzaev/skillparts/system/config/web.php.example index 9934e07..3943420 100644 --- a/mirzaev/skillparts/system/config/web.php.example +++ b/mirzaev/skillparts/system/config/web.php.example @@ -5,7 +5,7 @@ $config = [ 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], 'aliases' => [ - '@vendor' => dirname(__DIR__) . '../../../../vendor', + '@vendor' => dirname(__DIR__) . '/../../../vendor', '@bower' => '@vendor/bower-asset', '@npm' => '@vendor/npm-asset', '@explosivebit' => '@vendor/explosivebit', diff --git a/mirzaev/skillparts/system/controllers/SiteController.php b/mirzaev/skillparts/system/controllers/SiteController.php index bd9eeec..1ff2c5d 100644 --- a/mirzaev/skillparts/system/controllers/SiteController.php +++ b/mirzaev/skillparts/system/controllers/SiteController.php @@ -7,10 +7,8 @@ use yii\filters\AccessControl; use yii\web\Controller; use yii\web\Response; use yii\filters\VerbFilter; -use app\models\AuthenticationForm; -use app\models\RegistrationForm; +use app\models\AccountForm; use app\models\ContactForm; -use app\models\Account; class SiteController extends Controller { @@ -106,13 +104,15 @@ class SiteController extends Controller */ public function actionAuthentication() { - $model = new AuthenticationForm(Yii::$app->request->post()['AuthenticationForm'] ?? Yii::$app->request->get()['AuthenticationForm']); + $model = new AccountForm(Yii::$app->request->post()['AccountForm'] ?? Yii::$app->request->get()['AccountForm'] ?? null); if (Yii::$app->request->isAjax) { // AJAX-POST-запрос + Yii::$app->response->format = Response::FORMAT_JSON; + if (!Yii::$app->user->isGuest) { - // Аккаунт уже авторизован + // Аккаунт уже аутентифицирован Yii::$app->response->statusCode = 403; return [ 'form' => $this->renderPartial('index'), @@ -120,26 +120,27 @@ class SiteController extends Controller ]; } - Yii::$app->response->format = Response::FORMAT_JSON; + if ($model->authentication()) { + // Данные прошли проверку - if (isset($model->mail, $model->pswd)) { - if ($model->load(Yii::$app->request->post()) && $model->authentication()) { - return [ - 'menu' => << - - - Выход ($model->mail) -
- HTML, - 'form' => $this->renderPartial('index'), - '_csrf' => Yii::$app->request->getCsrfToken() - ]; - } - } else { return [ - 'menu' => 'Вход', - 'form' => $this->renderPartial('authentication', compact('model')), + 'menu' => << + + + Выход ($model->mail) + + HTML, + 'form' => $this->renderPartial('index'), + '_csrf' => Yii::$app->request->getCsrfToken() + ]; + } else { + // Данные не прошли проверку + + Yii::$app->response->statusCode = 400; + + return [ + 'form' => $this->renderPartial('account', compact('model')), '_csrf' => Yii::$app->request->getCsrfToken() ]; } @@ -149,7 +150,7 @@ class SiteController extends Controller // GET-запрос и прочие if (!Yii::$app->user->isGuest) { - // Аккаунт уже авторизован + // Аккаунт уже аутентифицирован Yii::$app->response->redirect('/'); } } @@ -172,7 +173,6 @@ class SiteController extends Controller return [ 'menu' => 'Вход', - 'form' => $this->renderPartial('index'), '_csrf' => Yii::$app->request->getCsrfToken() ]; } @@ -187,13 +187,16 @@ class SiteController extends Controller */ public function actionRegistration() { - $model = new RegistrationForm(Yii::$app->request->post()['RegistrationForm'] ?? Yii::$app->request->get()['RegistrationForm']); + $model = new AccountForm(Yii::$app->request->post()['AccountForm'] ?? Yii::$app->request->get()['AccountForm'] ?? null); + $model->type = 0; if (Yii::$app->request->isAjax) { // AJAX-POST-запрос + Yii::$app->response->format = Response::FORMAT_JSON; + if (!Yii::$app->user->isGuest) { - // Аккаунт уже авторизован + // Аккаунт уже аутентифицирован Yii::$app->response->statusCode = 302; return [ 'form' => $this->renderPartial('index'), @@ -201,46 +204,27 @@ class SiteController extends Controller ]; } - Yii::$app->response->format = Response::FORMAT_JSON; - - if (isset($model->mail, $model->pswd)) { - // Аккаунт передал необходимые параметры - - // Инициализация нового аккаунта - $account = new Account(); - $account->mail = $model->mail; - $account->pswd = Yii::$app->security->generatePasswordHash($model->pswd); - - if ($model->load(Yii::$app->request->post()) && $model->validate() && $account->save()) { - // Данные прошли проверку и аккаунт был создан - - return [ - 'menu' => << - - - Выход ($account->mail) - - HTML, - 'form' => $this->renderPartial('index'), - '_csrf' => Yii::$app->request->getCsrfToken() - ]; - } else { - // Данные не прошли проверку - - Yii::$app->response->statusCode = 400; - - return [ - 'form' => $this->renderPartial('registration', compact('model')), - '_csrf' => Yii::$app->request->getCsrfToken() - ]; - } - } else { - // Аккаунт не передал необходимые параметры + if ($model->registration()) { + // Данные прошли проверку и аккаунт был создан return [ - 'menu' => 'Вход', - 'form' => $this->renderPartial('registration', compact('model')), + 'menu' => << + + + Выход ($model->mail) + + HTML, + 'form' => $this->renderPartial('index'), + '_csrf' => Yii::$app->request->getCsrfToken() + ]; + } else { + // Данные не прошли проверку + + Yii::$app->response->statusCode = 400; + + return [ + 'form' => $this->renderPartial('account', compact('model')), '_csrf' => Yii::$app->request->getCsrfToken() ]; } @@ -251,7 +235,7 @@ class SiteController extends Controller // GET-запрос и прочие if (!Yii::$app->user->isGuest) { - // Аккаунт уже авторизован + // Аккаунт уже аутентифицирован Yii::$app->response->redirect('/'); } } @@ -259,6 +243,18 @@ class SiteController extends Controller return $this->render('registration', compact('model')); } + /** + * Displays профиль + * + * @return Response|string + */ + public function actionProfile() + { + $model = new Account(); + + return $this->render('profile', compact('model')); + } + /** * Displays contact page. * diff --git a/mirzaev/skillparts/system/models/Account.php b/mirzaev/skillparts/system/models/Account.php index 576325b..9126749 100644 --- a/mirzaev/skillparts/system/models/Account.php +++ b/mirzaev/skillparts/system/models/Account.php @@ -57,7 +57,7 @@ class Account extends ActiveRecord implements IdentityInterface */ public static function findIdentity($mail) { - return static::findOne(['mail' => $mail]); + return static::findByMail($mail); } /** @@ -82,6 +82,23 @@ class Account extends ActiveRecord implements IdentityInterface return static::findOne(['mail' => $mail]); } + /** + * Validates mail + * + * @param string $pswd password to validate + * @return bool if password provided is valid for current user + */ + public function validateMail($mail) + { + if (static::findByMail($mail)) { + // Почта найдена в базе данных + + return false; + } + + return true; + } + /** * Validates password * diff --git a/mirzaev/skillparts/system/models/AccountForm.php b/mirzaev/skillparts/system/models/AccountForm.php new file mode 100644 index 0000000..132e014 --- /dev/null +++ b/mirzaev/skillparts/system/models/AccountForm.php @@ -0,0 +1,151 @@ + 'Заполните поле'], + // Функция "Запомнить меня" + ['auto', 'boolean'], + // Проверка почты + ['mail', 'validateMail', 'message'=>'Неправильная почта'], + // Проверка пароля + ['pswd', 'validatePassword', 'message'=>'Неправильный пароль'], + ]; + } + + public function attributeLabels() + { + return [ + 'mail' => 'Почта', + 'pswd' => 'Пароль', + 'auto' => 'Запомнить' + ]; + } + + /** + * @param string $attribute the attribute currently being validated + * @param array $params the additional name-value pairs given in the rule + */ + public function validateMail($attribute, $params) + { + if (!$this->hasErrors() && $this->type === 0) { + // Проблем нет, обрабатывается событие регистрации + + $account = $this->getAccount(); + + if (!$account || !$account->validateMail($this->mail)) { + // Проверка не пройдена + $this->addError($attribute, 'Почта уже привязана к другому аккаунту'); + } + } + } + + /** + * Validates the password. + * This method serves as the inline validation for password. + * + * @param string $attribute the attribute currently being validated + * @param array $params the additional name-value pairs given in the rule + */ + public function validatePassword($attribute, $params) + { + if (!$this->hasErrors() && $this->type === 1) { + // Проблем нет, обрабатывается событие аутентификации + + $account = $this->getAccount(); + + if (!$account || !$account->validatePassword($this->pswd)) { + // Проверка не пройдена + + $this->addError($attribute, 'Проверьте входные данные'); + } + } + } + + /** + * Logs in a account using the provided accountname and password. + * @return bool whether the account is logged in successfully + */ + public function authentication() + { + if (isset($this->mail, $this->pswd) && $this->validate()) { + // Проверка пройдена + + // Аутентификация + return Yii::$app->user->login($this->getAccount(), $this->auto ? 3600*24*30 : 0); + } + + return false; + } + + /** + * @return bool + */ + public function registration() + { + // Инициализация нового аккаунта + $this->account = new Account(); + + if (isset($this->mail, $this->pswd) && $this->validate()) { + // Проверка пройдена + + // Запись параметров + $this->account->mail = $this->mail; + $this->account->pswd = Yii::$app->security->generatePasswordHash($this->pswd); + + // Регистрация + return $this->account->save(); + } + + return false; + } + + /** + * Finds account by [[accountname]] + * + * @return Account|null + */ + public function getAccount() + { + if ($this->account === false) { + $this->account = Account::findByMail($this->mail); + } + + return $this->account; + } +} diff --git a/mirzaev/skillparts/system/models/AuthenticationForm.php b/mirzaev/skillparts/system/models/AuthenticationForm.php deleted file mode 100644 index 24b2425..0000000 --- a/mirzaev/skillparts/system/models/AuthenticationForm.php +++ /dev/null @@ -1,91 +0,0 @@ - 'Почта', - 'pswd' => 'Пароль', - 'auto' => 'Запомнить' - ]; - } - - /** - * Validates the password. - * This method serves as the inline validation for password. - * - * @param string $attribute the attribute currently being validated - * @param array $params the additional name-value pairs given in the rule - */ - public function validatePassword($attribute, $params) - { - if (!$this->hasErrors()) { - $account = $this->getAccount(); - - if (!$account || !$account->validatePassword($this->pswd)) { - $this->addError($attribute, 'Не удалось идентифицировать'); - } - } - } - - /** - * Logs in a account using the provided accountname and password. - * @return bool whether the account is logged in successfully - */ - public function authentication() - { - if ($this->validate()) { - return Yii::$app->user->login($this->getAccount(), $this->auto ? 3600*24*30 : 0); - } - return false; - } - - /** - * Finds account by [[accountname]] - * - * @return Account|null - */ - public function getAccount() - { - if ($this->account === false) { - $this->account = Account::findByMail($this->mail); - } - - return $this->account; - } -} diff --git a/mirzaev/skillparts/system/models/RegistrationForm.php b/mirzaev/skillparts/system/models/RegistrationForm.php deleted file mode 100644 index 23364b0..0000000 --- a/mirzaev/skillparts/system/models/RegistrationForm.php +++ /dev/null @@ -1,28 +0,0 @@ - 'Заполните поле'], - ['mail', 'unique', 'targetClass' => Account::class, 'message' => 'Почта уже привязана к другому аккаунту'], - ]; - } - - public function attributeLabels() - { - return [ - 'mail' => 'Почта', - 'pswd' => 'Пароль', - ]; - } -} diff --git a/mirzaev/skillparts/system/views/site/account.php b/mirzaev/skillparts/system/views/site/account.php new file mode 100644 index 0000000..6f03fe6 --- /dev/null +++ b/mirzaev/skillparts/system/views/site/account.php @@ -0,0 +1,34 @@ + + +Please fill out the following fields to login:
- - 'form_authentication', - 'layout' => 'horizontal', - 'fieldConfig' => [ - 'template' => "{label}\napp\models\User::$users
.
-
+ Note that if you turn on the Yii debugger, you should be able
+ to view the mail message on the mail panel of the debugger.
+ mailer->useFileTransport): ?>
+ Because the application is in development mode, the email is not sent but saved as
+ a file under = Yii::getAlias(Yii::$app->mailer->fileTransportPath) ?>
.
+ Please configure the useFileTransport
property of the mail
+ application component to be false to enable email sending.
+
+
+ If you have business inquiries or other questions, please fill out the following form to contact us. + Thank you. +
+ +