first commit
This commit is contained in:
61
frontend/models/ContactForm.php
Executable file
61
frontend/models/ContactForm.php
Executable file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\models;
|
||||
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
|
||||
/**
|
||||
* ContactForm is the model behind the contact form.
|
||||
*/
|
||||
class ContactForm extends Model
|
||||
{
|
||||
public $name;
|
||||
public $email;
|
||||
public $subject;
|
||||
public $body;
|
||||
public $verifyCode;
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
// name, email, subject and body are required
|
||||
[['name', 'email', 'subject', 'body'], 'required'],
|
||||
// email has to be a valid email address
|
||||
['email', 'email'],
|
||||
// verifyCode needs to be entered correctly
|
||||
['verifyCode', 'captcha'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'verifyCode' => 'Verification Code',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an email to the specified email address using the information collected by this model.
|
||||
*
|
||||
* @param string $email the target email address
|
||||
* @return bool whether the email was sent
|
||||
*/
|
||||
public function sendEmail($email)
|
||||
{
|
||||
return Yii::$app->mailer->compose()
|
||||
->setTo($email)
|
||||
->setFrom([Yii::$app->params['senderEmail'] => Yii::$app->params['senderName']])
|
||||
->setReplyTo([$this->email => $this->name])
|
||||
->setSubject($this->subject)
|
||||
->setTextBody($this->body)
|
||||
->send();
|
||||
}
|
||||
}
|
40
frontend/models/DataForm.php
Executable file
40
frontend/models/DataForm.php
Executable file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\models;
|
||||
|
||||
use yii\base\Model;
|
||||
|
||||
/**
|
||||
* ContactForm is the model behind the contact form.
|
||||
*/
|
||||
class DataForm extends Model
|
||||
{
|
||||
public $fio;
|
||||
public $email;
|
||||
public $phone;
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['fio', 'email', 'phone'], 'required'],
|
||||
['email', 'email'],
|
||||
[['fio', 'phone'], 'string'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'email' => 'Электронная почта',
|
||||
'fio' => 'ФИО',
|
||||
'phone' => 'Номер телефона',
|
||||
];
|
||||
}
|
||||
}
|
69
frontend/models/PasswordResetRequestForm.php
Executable file
69
frontend/models/PasswordResetRequestForm.php
Executable file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\models;
|
||||
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
use common\models\User;
|
||||
|
||||
/**
|
||||
* Password reset request form
|
||||
*/
|
||||
class PasswordResetRequestForm extends Model
|
||||
{
|
||||
public $email;
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
['email', 'trim'],
|
||||
['email', 'required'],
|
||||
['email', 'email'],
|
||||
['email', 'exist',
|
||||
'targetClass' => '\common\models\User',
|
||||
'filter' => ['status' => User::STATUS_ACTIVE],
|
||||
'message' => 'There is no user with this email address.'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an email with a link, for resetting the password.
|
||||
*
|
||||
* @return bool whether the email was send
|
||||
*/
|
||||
public function sendEmail()
|
||||
{
|
||||
/* @var $user User */
|
||||
$user = User::findOne([
|
||||
'status' => User::STATUS_ACTIVE,
|
||||
'email' => $this->email,
|
||||
]);
|
||||
|
||||
if (!$user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!User::isPasswordResetTokenValid($user->password_reset_token)) {
|
||||
$user->generatePasswordResetToken();
|
||||
if (!$user->save()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return Yii::$app
|
||||
->mailer
|
||||
->compose(
|
||||
['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'],
|
||||
['user' => $user]
|
||||
)
|
||||
->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot'])
|
||||
->setTo($this->email)
|
||||
->setSubject('Password reset for ' . Yii::$app->name)
|
||||
->send();
|
||||
}
|
||||
}
|
61
frontend/models/ResendVerificationEmailForm.php
Executable file
61
frontend/models/ResendVerificationEmailForm.php
Executable file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\models;
|
||||
|
||||
use Yii;
|
||||
use common\models\User;
|
||||
use yii\base\Model;
|
||||
|
||||
class ResendVerificationEmailForm extends Model
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $email;
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
['email', 'trim'],
|
||||
['email', 'required'],
|
||||
['email', 'email'],
|
||||
['email', 'exist',
|
||||
'targetClass' => '\common\models\User',
|
||||
'filter' => ['status' => User::STATUS_INACTIVE],
|
||||
'message' => 'There is no user with this email address.'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends confirmation email to user
|
||||
*
|
||||
* @return bool whether the email was sent
|
||||
*/
|
||||
public function sendEmail()
|
||||
{
|
||||
$user = User::findOne([
|
||||
'email' => $this->email,
|
||||
'status' => User::STATUS_INACTIVE
|
||||
]);
|
||||
|
||||
if ($user === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Yii::$app
|
||||
->mailer
|
||||
->compose(
|
||||
['html' => 'emailVerify-html', 'text' => 'emailVerify-text'],
|
||||
['user' => $user]
|
||||
)
|
||||
->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot'])
|
||||
->setTo($this->email)
|
||||
->setSubject('Account registration at ' . Yii::$app->name)
|
||||
->send();
|
||||
}
|
||||
}
|
67
frontend/models/ResetPasswordForm.php
Executable file
67
frontend/models/ResetPasswordForm.php
Executable file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\models;
|
||||
|
||||
use yii\base\InvalidArgumentException;
|
||||
use yii\base\Model;
|
||||
use Yii;
|
||||
use common\models\User;
|
||||
|
||||
/**
|
||||
* Password reset form
|
||||
*/
|
||||
class ResetPasswordForm extends Model
|
||||
{
|
||||
public $password;
|
||||
|
||||
/**
|
||||
* @var \common\models\User
|
||||
*/
|
||||
private $_user;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a form model given a token.
|
||||
*
|
||||
* @param string $token
|
||||
* @param array $config name-value pairs that will be used to initialize the object properties
|
||||
* @throws InvalidArgumentException if token is empty or not valid
|
||||
*/
|
||||
public function __construct($token, $config = [])
|
||||
{
|
||||
if (empty($token) || !is_string($token)) {
|
||||
throw new InvalidArgumentException('Password reset token cannot be blank.');
|
||||
}
|
||||
$this->_user = User::findByPasswordResetToken($token);
|
||||
if (!$this->_user) {
|
||||
throw new InvalidArgumentException('Wrong password reset token.');
|
||||
}
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
['password', 'required'],
|
||||
['password', 'string', 'min' => Yii::$app->params['user.passwordMinLength']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets password.
|
||||
*
|
||||
* @return bool if password was reset.
|
||||
*/
|
||||
public function resetPassword()
|
||||
{
|
||||
$user = $this->_user;
|
||||
$user->setPassword($this->password);
|
||||
$user->removePasswordResetToken();
|
||||
$user->generateAuthKey();
|
||||
|
||||
return $user->save(false);
|
||||
}
|
||||
}
|
80
frontend/models/SignupForm.php
Executable file
80
frontend/models/SignupForm.php
Executable file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\models;
|
||||
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
use common\models\User;
|
||||
|
||||
/**
|
||||
* Signup form
|
||||
*/
|
||||
class SignupForm extends Model
|
||||
{
|
||||
public $username;
|
||||
public $email;
|
||||
public $password;
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
['username', 'trim'],
|
||||
['username', 'required'],
|
||||
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
|
||||
['username', 'string', 'min' => 2, 'max' => 255],
|
||||
|
||||
['email', 'trim'],
|
||||
['email', 'required'],
|
||||
['email', 'email'],
|
||||
['email', 'string', 'max' => 255],
|
||||
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],
|
||||
|
||||
['password', 'required'],
|
||||
['password', 'string', 'min' => Yii::$app->params['user.passwordMinLength']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs user up.
|
||||
*
|
||||
* @return bool whether the creating new account was successful and email was sent
|
||||
*/
|
||||
public function signup()
|
||||
{
|
||||
if (!$this->validate()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$user = new User();
|
||||
$user->username = $this->username;
|
||||
$user->email = $this->email;
|
||||
$user->setPassword($this->password);
|
||||
$user->generateAuthKey();
|
||||
$user->generateEmailVerificationToken();
|
||||
|
||||
return $user->save() && $this->sendEmail($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends confirmation email to user
|
||||
* @param User $user user model to with email should be send
|
||||
* @return bool whether the email was sent
|
||||
*/
|
||||
protected function sendEmail($user)
|
||||
{
|
||||
return Yii::$app
|
||||
->mailer
|
||||
->compose(
|
||||
['html' => 'emailVerify-html', 'text' => 'emailVerify-text'],
|
||||
['user' => $user]
|
||||
)
|
||||
->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot'])
|
||||
->setTo($this->email)
|
||||
->setSubject('Account registration at ' . Yii::$app->name)
|
||||
->send();
|
||||
}
|
||||
}
|
52
frontend/models/VerifyEmailForm.php
Executable file
52
frontend/models/VerifyEmailForm.php
Executable file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\models;
|
||||
|
||||
use common\models\User;
|
||||
use yii\base\InvalidArgumentException;
|
||||
use yii\base\Model;
|
||||
|
||||
class VerifyEmailForm extends Model
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $token;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
private $_user;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a form model with given token.
|
||||
*
|
||||
* @param string $token
|
||||
* @param array $config name-value pairs that will be used to initialize the object properties
|
||||
* @throws InvalidArgumentException if token is empty or not valid
|
||||
*/
|
||||
public function __construct($token, array $config = [])
|
||||
{
|
||||
if (empty($token) || !is_string($token)) {
|
||||
throw new InvalidArgumentException('Verify email token cannot be blank.');
|
||||
}
|
||||
$this->_user = User::findByVerificationToken($token);
|
||||
if (!$this->_user) {
|
||||
throw new InvalidArgumentException('Wrong verify email token.');
|
||||
}
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify email
|
||||
*
|
||||
* @return User|null the saved model or null if saving fails
|
||||
*/
|
||||
public function verifyEmail()
|
||||
{
|
||||
$user = $this->_user;
|
||||
$user->status = User::STATUS_ACTIVE;
|
||||
return $user->save(false) ? $user : null;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user