'Ce champs ne peut être vide'], ['email', 'email', 'message' => 'Cette adresse email n\'est pas valide'], ['email', 'verifyEmail'], ['status', 'default', 'value' => self::STATUS_ACTIVE], ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED, self::STATUS_ADMIN, self::STATUS_PRODUCER]], ['password_old', 'verifyPasswordOld'], ['password_new', 'verifyPasswordNew'], ['password_new_confirm', 'verifyPasswordNewConfirm'], [['date_last_connection', 'password_old', 'password_new', 'password_new_confirm', 'password_hash'], 'safe'], ]; } public function attributeLabels() { return [ 'id' => 'ID', 'username' => 'Identifiant', 'password' => 'Mot de passe', 'rememberMe' => 'Se souvenir de moi', 'no_mail' => 'Ne pas recevoir d\'email de la part du Chat des Noisettes', 'mail_distribution_monday' => 'Lundi', 'mail_distribution_tuesday' => 'Mardi', 'mail_distribution_wednesday' => 'Mercredi', 'mail_distribution_thursday' => 'Jeudi', 'mail_distribution_friday' => 'Vendredi', 'mail_distribution_saturday' => 'Samedi', 'mail_distribution_sunday' => 'Dimanche', 'password_old' => 'Ancien mot de passe', 'password_new' => 'Nouveau mot de passe', 'password_new_confirm' => 'Confirmation du nouveau mot de passe', ]; } /** * Retourne les options de base nécessaires à la fonction de recherche. * * @return array */ public static function defaultOptionsSearch() { return [ 'with' => [], 'join_with' => ['userProducer'], 'orderby' => 'user.name ASC, user.lastname ASC', 'attribute_id_producer' => '' ] ; } /** * Vérifie le mot de passe envoyé par l'utilisateur. * * @param string $attribute * @param array $params */ public function verifyPasswordOld($attribute, $params) { if (strlen($this->password_old)) { if (!$this->validatePassword($this->password_old)) { $this->addError($attribute, 'Mot de passe invalide.'); } } if (!strlen($this->password_old) && (strlen($this->password_new) || strlen($this->password_new_confirm))) { $this->addError($attribute, 'Ce champs ne peut être vide'); } if (!strlen($this->password_new) && (strlen($this->password_old) || strlen($this->password_new_confirm))) { $this->addError('password_new', 'Ce champs ne peut être vide'); } if (!strlen($this->password_new_confirm) && (strlen($this->password_old) || strlen($this->password_new))) { $this->addError('password_new_confirm', 'Ce champs ne peut être vide'); } } /** * Vérifie le mot de passe de l'utilisateur. * * @param string $attribute * @param array $params */ public function verifyPasswordNew($attribute, $params) { if (strlen($this->password_new) < 6) { $this->addError($attribute, 'Votre mot de passe doit comporter au moins 6 caractères.'); } } /** * Vérifie la confirmation de mot de passe de l'utilisateur. * * @param string $attribute * @param array $params */ public function verifyPasswordNewConfirm($attribute, $params) { if ($this->password_new != $this->password_new_confirm) { $this->addError($attribute, 'Les deux mots de passe doivent être identiques'); } } /** * Vérifie l'email de l'utilisateur. * * @param string $attribute * @param array $params */ public function verifyEmail($attribute, $params) { $user = User::find()->where("email LIKE :email AND id != :id")->params(array(':email' => '%' . $this->email . '%', ':id' => $this->id))->one(); if ($user) { $this->addError($attribute, 'Cette adresse email est déjà utilisée par un autre utilisateur '); } } /* * Relations */ public function getUserProducer() { return $this->hasMany(UserProducer::className(), ['id_user' => 'id']); } /** * @inheritdoc */ public static function findIdentity($id) { return static::findOne(['id' => $id]); } /** * @inheritdoc */ public static function findIdentityByAccessToken($token, $type = null) { throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); } /** * Finds user by username * * @param string $username * @return static|null */ public static function findByUsername($username) { return static::findOne(['username' => $username]); } /** * Recherche un utilisateur via son adresse email. * * @param string $email * @return User */ public static function findByEmail($email) { return static::findOne(['email' => $email]); } /** * Finds user by password reset token * * @param string $token password reset token * @return static|null */ public static function findByPasswordResetToken($token) { if (!static::isPasswordResetTokenValid($token)) { return null; } return static::findOne([ 'password_reset_token' => $token, ]); } /** * Recherche des utilisateurs suivant les paramètres : id_etablissement, * inactifs, id_point_vente, nom, prenom, email, telephone. * * @param array $params * @return Query */ public static function findBy($params = []) { if (!isset($params['id_producer'])) { $params['id_producer'] = Producer::getId() ; } $query = (new Query()) ->select(['user.id AS user_id', 'user.name', 'user.lastname', 'user.phone', 'user.email', 'user.created_at', 'user.date_last_connection', 'user_producer.*']) ->from('user'); $active = (isset($params['inactive']) && $params['inactive']) ? 0 : 1 ; $query->innerJoin('user_producer','user.id = user_producer.id_user AND user_producer.active = '.$active.' AND user_producer.id_producer = :id_producer', [':id_producer' => $params['id_producer']]) ; if(isset($params['id_point_sale']) && $params['id_point_sale']) { $point_sale = PointSale::findOne(['id' => $params['id_point_sale']]) ; if($point_sale->restricted_access) { $query->innerJoin('user_point_sale','user.id = user_point_sale.id_user AND user_point_sale.id_point_sale = :id_point_sale',[':id_point_sale' => $params['id_point_sale']]) ; } else { $query->innerJoin( 'order', 'user.id = order.id_user AND order.id_point_sale = :id_point_sale', [':id_point_sale' => $params['id_point_sale']] )->groupBy('user.id') ; } } if(isset($params['inactive']) && $params['inactive']) { $query->innerJoin( 'order', 'user.id = order.id_user' ) ->groupBy('user.id'); } if (isset($params['name'])) { $query->andFilterWhere(['like', 'name', $params['name']]); } if (isset($params['lastname'])) { $query->andFilterWhere(['like', 'lastname', $params['lastname']]); } if (isset($params['email'])) { $query->andFilterWhere(['like', 'email', $params['email']]); } if (isset($params['phone'])) { $query->andFilterWhere(['like', 'phone', $params['phone']]); } return $query; } /** * Finds out if password reset token is valid * * @param string $token password reset token * @return boolean */ public static function isPasswordResetTokenValid($token) { if (empty($token)) { return false; } $expire = Yii::$app->params['user.passwordResetTokenExpire']; $parts = explode('_', $token); $timestamp = (int) end($parts); return $timestamp + $expire >= time(); } /** * @inheritdoc */ public function getId() { return $this->getPrimaryKey(); } /** * @inheritdoc */ public function getAuthKey() { return $this->auth_key; } /** * @inheritdoc */ public function validateAuthKey($authKey) { return $this->getAuthKey() === $authKey; } /** * Validates password * * @param string $password password to validate * @return boolean if password provided is valid for current user */ public function validatePassword($password) { return Yii::$app->security->validatePassword($password, $this->password_hash); } /** * Generates password hash from password and sets it to the model * * @param string $password */ public function setPassword($password) { $this->password_hash = Yii::$app->security->generatePasswordHash($password); } /** * Generates "remember me" authentication key */ public function generateAuthKey() { $this->auth_key = Yii::$app->security->generateRandomString(); } /** * Generates new password reset token */ public function generatePasswordResetToken() { $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time(); } /** * Removes password reset token */ public function removePasswordResetToken() { $this->password_reset_token = null; } /** * Retourne si l'utilisateur est un boulanger ou non. * * @return boolean */ public function isProducer() { return ($this->status == User::STATUS_ADMIN || $this->status == User::STATUS_PRODUCER) && $this->id_producer; } /** * Retourne le nom du producteur. * * @return string */ public function getNameProducer() { $producer = Producer::findOne($this->id_producer); return $producer->name; } /** * Retourne les établissements liés à l'utilisateur. * * @return array */ public function getBookmarkedProducers() { $producers = (new \yii\db\Query()) ->select('*') ->from(['user_producer', 'producer']) ->where('user_producer.id_producer = producer.id') ->andWhere(['user_producer.id_user' => $this->id]) ->andWhere(['user_producer.active' => 1]) ->all(); return $producers; } /** * Retourne le crédit de l'utilisateur pour un producteur donné. * * @param integer $id_etablissement * @return float */ public function getCredit($idProducer) { $userProducer = UserProducer::searchOne([ 'id_user' => $this->id ]) ; if ($userProducer) { return $userProducer->credit; } return 0; } /** * Met à jour la date de dernière connexion de l'utilisateur. */ public function updateLastConnection() { $this->date_last_connection = date('Y-m-d H:i:s'); $this->save(); } /** * Envoie un email de bienvenue à l'utilisateur lors de son inscription * via le backend du site. * * @param string $password */ public function sendMailWelcome($password) { if (strlen($this->email)) { $producer = Producer::findOne(Producer::getId()); Yii::$app->mailer->compose(); $mail = Yii::$app->mailer->compose( ['html' => 'createUserAdmin-html', 'text' => 'createUserAdmin-text'], ['user' => $this, 'producer' => $producer, 'password' => $password] ) ->setTo($this->email) ->setFrom(['contact@laboiteapain.net' => 'La boîte à pain']) ->setSubject('[La boîte à pain] Inscription') ->send(); } } /** * Retourne l'ID de l'utilisateur courant connecté. * * @return mixed */ public static function getCurrentId() { if(!Yii::$app->user->isGuest) { return Yii::$app->user->identity->id ; } return false ; } /** * Retourne le status de l'utilisateur courant connecté. * * @return integer|boolean */ public static function getCurrentStatus() { if(!Yii::$app->user->isGuest) { return Yii::$app->user->identity->status ; } return false ; } public static function hasAccessBackend() { return User::getCurrentStatus() == USER::STATUS_ADMIN || User::getCurrentStatus() == USER::STATUS_PRODUCER; } }