Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

236 lines
6.1KB

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