Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 94 |
| UserForm | |
0.00% |
0 / 1 |
|
0.00% |
0 / 7 |
272.00 | |
0.00% |
0 / 94 |
| __construct | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 14 |
|||
| rules | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 21 |
|||
| scenarios | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 7 |
|||
| getId | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
| getIsNewRecord | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
| save | |
0.00% |
0 / 1 |
72.00 | |
0.00% |
0 / 35 |
|||
| sendConfirmationEmail | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 11 |
|||
| <?php | |
| namespace backend\models; | |
| use Yii; | |
| use common\models\User; | |
| use yii\base\Model; | |
| use yii\web\NotFoundHttpException; | |
| /** | |
| * User form | |
| */ | |
| class UserForm extends Model | |
| { | |
| const SCENARIO_CREATE = 'create'; | |
| const SCENARIO_UPDATE = 'update'; | |
| private $id; | |
| public $firstname; | |
| public $lastname; | |
| public $username; | |
| public $email; | |
| public $password; | |
| public $repeatPassword; | |
| public $status; | |
| public function __construct(User $user = null, array $options = []) | |
| { | |
| $this->id = null; | |
| $this->scenario = self::SCENARIO_CREATE; | |
| parent::__construct($options); | |
| if($user != null){ | |
| $this->scenario = self::SCENARIO_UPDATE; | |
| $this->id = $user->id; | |
| $this->username = $user->username; | |
| $this->email = $user->email; | |
| $this->firstname = $user->firstname; | |
| $this->lastname = $user->lastname; | |
| $this->status = $user->status; | |
| } | |
| } | |
| /** | |
| * @inheritdoc | |
| */ | |
| public function rules() | |
| { | |
| return [ | |
| ['firstname', 'filter', 'filter' => 'trim'], | |
| ['firstname', 'required'], | |
| ['lastname', 'filter', 'filter' => 'trim'], | |
| ['lastname', 'required'], | |
| ['username', 'filter', 'filter' => 'trim'], | |
| ['username', 'string', 'min' => 2, 'max' => 255], | |
| ['username', 'unique', 'filter' => ['<>', 'id', $this->id], 'targetClass' => User::classname(), 'message' => Yii::t('app', 'This username has already been taken.')], | |
| ['email', 'filter', 'filter' => 'trim'], | |
| ['email', 'required'], | |
| ['email', 'email'], | |
| ['email', 'unique', 'filter' => ['<>', 'id', $this->id], 'targetClass' => User::classname(), 'message' => Yii::t('app', 'This email address has already been taken.')], | |
| ['password', 'required', 'on' => self::SCENARIO_CREATE], | |
| ['password', 'string', 'min' => 6], | |
| ['repeatPassword', 'required', 'on' => self::SCENARIO_CREATE], | |
| ['repeatPassword', 'compare', 'compareAttribute' => 'password', 'skipOnEmpty' => false, 'message' => Yii::t('app', 'Repeat password should be exacly the same as password.')], | |
| ['status', 'default', 'value' => User::STATUS_ACTIVE], | |
| ['status', 'in', 'range' => [User::STATUS_ACTIVE, User::STATUS_DELETED]], | |
| ]; | |
| } | |
| /** | |
| * @inheritdoc | |
| */ | |
| public function scenarios() | |
| { | |
| $fields = $this->fields(); | |
| return [ | |
| self::SCENARIO_CREATE => $fields, | |
| self::SCENARIO_UPDATE => $fields, | |
| ]; | |
| } | |
| public function getId() | |
| { | |
| return $this->id; | |
| } | |
| /** | |
| * Returns a value indicating whether the current record is new. | |
| * @return boolean whether the record is new and should be inserted when calling [[save()]]. | |
| */ | |
| public function getIsNewRecord() | |
| { | |
| return $this->id == null; | |
| } | |
| /** | |
| * Signs user up. | |
| * | |
| * @return User|null the saved model or null if saving fails | |
| */ | |
| public function save($submitConfirmationToken = false) | |
| { | |
| if ($this->validate()) { | |
| if($this->isNewRecord) { | |
| $user = new User(); | |
| $user->generateAuthKey(); | |
| $user->generateAccessToken(); | |
| $user->setPassword($this->password); | |
| } else { | |
| $user = User::findOne($this->id); | |
| if(empty($user)) { | |
| throw new NotFoundHttpException('The requested page does not exist.'); | |
| } | |
| if(!empty($this->password)) { | |
| $user->setPassword($this->password); | |
| } | |
| } | |
| $user->username = $this->username; | |
| $user->email = $this->email; | |
| $user->firstname = $this->firstname; | |
| $user->lastname = $this->lastname; | |
| $user->status = $this->status; | |
| if($submitConfirmationToken){ | |
| $user->generateEmailConfirmToken(); | |
| } | |
| if($user->save()){ | |
| if($submitConfirmationToken){ | |
| self::sendConfirmationEmail($user); | |
| } | |
| return $user; | |
| } else { | |
| $this->addErrors($user->errors); | |
| } | |
| } | |
| return null; | |
| } | |
| /** | |
| * Sends an email to the specified email address using the information collected by this model. | |
| * | |
| * @param string $email the target email address | |
| * @return boolean whether the email was sent | |
| */ | |
| private static function sendConfirmationEmail($user) | |
| { | |
| $message = Yii::$app->mailer->compose('emailConfirmToken', ['user' => $user]); | |
| $fromEmail = \Yii::$app->params['supportEmail']; | |
| $sourceEmail = $message->getFrom(); | |
| if (!empty($sourceEmail)) $fromEmail = key($sourceEmail); | |
| $message->setFrom([$fromEmail => \Yii::$app->name . ' robot']) | |
| ->setReplyTo([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot']) | |
| ->setTo([$user->email => $user->fullname]) | |
| ->setSubject(Yii::t('app', 'Email confirmation')) | |
| ->send(); | |
| } | |
| } |