You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
6.0KB

  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\base\NotSupportedException;
  5. use yii\behaviors\TimestampBehavior;
  6. use yii\db\ActiveRecord;
  7. use yii\db\CDbCriteria;
  8. use yii\web\IdentityInterface;
  9. /**
  10. * User model
  11. *
  12. * @property integer $id
  13. * @property string $username
  14. * @property string $password_hash
  15. * @property string $password_reset_token
  16. * @property string $email
  17. * @property string $auth_key
  18. * @property integer $status
  19. * @property integer $created_at
  20. * @property integer $updated_at
  21. * @property string $password write-only password
  22. * @property boolean $confiance
  23. */
  24. class User extends ActiveRecord implements IdentityInterface
  25. {
  26. const STATUS_DELETED = 0;
  27. const STATUS_ACTIVE = 10;
  28. const STATUS_ADMIN = 13;
  29. /**
  30. * @inheritdoc
  31. */
  32. public static function tableName()
  33. {
  34. return '{{%user}}';
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function behaviors()
  40. {
  41. return [
  42. TimestampBehavior::className(),
  43. ];
  44. }
  45. /**
  46. * @inheritdoc
  47. */
  48. public function rules()
  49. {
  50. return [
  51. ['confiance','default','value'=>0],
  52. [['no_mail','mail_prod_lundi','mail_prod_mardi','mail_prod_mercredi','mail_prod_jeudi','mail_prod_vendredi','mail_prod_samedi','mail_prod_dimanche'],'boolean'],
  53. [['nom','prenom','telephone','adresse'], 'string'],
  54. [['nom','prenom','email'],'required','message'=> 'Ce champs ne peut être vide'],
  55. ['email','email','message'=> 'Cette adresse email n\'est pas valide'],
  56. ['email','verifyEmail'],
  57. ['status', 'default', 'value' => self::STATUS_ACTIVE],
  58. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED, self::STATUS_ADMIN]],
  59. ];
  60. }
  61. public function verifyEmail($attribute,$params) {
  62. $user = User::find()->where("email LIKE :email AND id != :id")->params(array(':email'=>'%'.$this->email.'%', ':id'=>$this->id))->one() ;
  63. if($user)
  64. $this->addError($attribute, 'Cette adresse email est déjà utilisée par un autre utilisateur ');
  65. }
  66. /**
  67. * @inheritdoc
  68. */
  69. public static function findIdentity($id)
  70. {
  71. return static::findOne(['id' => $id/*, 'status' => self::STATUS_ACTIVE*/]);
  72. }
  73. /**
  74. * @inheritdoc
  75. */
  76. public static function findIdentityByAccessToken($token, $type = null)
  77. {
  78. throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  79. }
  80. /**
  81. * Finds user by username
  82. *
  83. * @param string $username
  84. * @return static|null
  85. */
  86. public static function findByUsername($username)
  87. {
  88. return static::findOne(['username' => $username/*, 'status' => self::STATUS_ACTIVE*/]);
  89. }
  90. public static function findByEmail($email)
  91. {
  92. return static::findOne(['email' => $email /*, 'status' => self::STATUS_ACTIVE*/]);
  93. }
  94. /**
  95. * Finds user by password reset token
  96. *
  97. * @param string $token password reset token
  98. * @return static|null
  99. */
  100. public static function findByPasswordResetToken($token)
  101. {
  102. if (!static::isPasswordResetTokenValid($token)) {
  103. return null;
  104. }
  105. return static::findOne([
  106. 'password_reset_token' => $token,
  107. 'status' => self::STATUS_ACTIVE,
  108. ]);
  109. }
  110. /**
  111. * Finds out if password reset token is valid
  112. *
  113. * @param string $token password reset token
  114. * @return boolean
  115. */
  116. public static function isPasswordResetTokenValid($token)
  117. {
  118. if (empty($token)) {
  119. return false;
  120. }
  121. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  122. $parts = explode('_', $token);
  123. $timestamp = (int) end($parts);
  124. return $timestamp + $expire >= time();
  125. }
  126. /**
  127. * @inheritdoc
  128. */
  129. public function getId()
  130. {
  131. return $this->getPrimaryKey();
  132. }
  133. /**
  134. * @inheritdoc
  135. */
  136. public function getAuthKey()
  137. {
  138. return $this->auth_key;
  139. }
  140. /**
  141. * @inheritdoc
  142. */
  143. public function validateAuthKey($authKey)
  144. {
  145. return $this->getAuthKey() === $authKey;
  146. }
  147. /**
  148. * Validates password
  149. *
  150. * @param string $password password to validate
  151. * @return boolean if password provided is valid for current user
  152. */
  153. public function validatePassword($password)
  154. {
  155. return Yii::$app->security->validatePassword($password, $this->password_hash);
  156. }
  157. /**
  158. * Generates password hash from password and sets it to the model
  159. *
  160. * @param string $password
  161. */
  162. public function setPassword($password)
  163. {
  164. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  165. }
  166. /**
  167. * Generates "remember me" authentication key
  168. */
  169. public function generateAuthKey()
  170. {
  171. $this->auth_key = Yii::$app->security->generateRandomString();
  172. }
  173. /**
  174. * Generates new password reset token
  175. */
  176. public function generatePasswordResetToken()
  177. {
  178. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  179. }
  180. /**
  181. * Removes password reset token
  182. */
  183. public function removePasswordResetToken()
  184. {
  185. $this->password_reset_token = null;
  186. }
  187. public function attributeLabels()
  188. {
  189. return [
  190. 'id' => 'ID',
  191. 'username' => 'Identifiant',
  192. 'password' => 'Mot de passe',
  193. 'rememberMe' => 'Se souvenir de moi',
  194. 'confiance' => 'De confiance',
  195. 'no_mail' => 'Ne pas recevoir d\'email de la part du Chat des Noisettes',
  196. 'mail_prod_lundi' => 'Lundi',
  197. 'mail_prod_mardi' => 'Mardi',
  198. 'mail_prod_mercredi' => 'Mercredi',
  199. 'mail_prod_jeudi' => 'Jeudi',
  200. 'mail_prod_vendredi' => 'Vendredi',
  201. 'mail_prod_samedi' => 'Samedi',
  202. 'mail_prod_dimanche' => 'Dimanche',
  203. ];
  204. }
  205. public function isBoulanger()
  206. {
  207. return $this->is_boulanger ;
  208. }
  209. }