您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

331 行
9.3KB

  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'],'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,self::STATUS_BOULANGER ]],
  60. [['date_derniere_connexion'],'safe'],
  61. ];
  62. }
  63. public function verifyEmail($attribute,$params) {
  64. $user = User::find()->where("email LIKE :email AND id != :id")->params(array(':email'=>'%'.$this->email.'%', ':id'=>$this->id))->one() ;
  65. if($user)
  66. $this->addError($attribute, 'Cette adresse email est déjà utilisée par un autre utilisateur ');
  67. }
  68. public function getUserEtablissement() {
  69. return $this->hasMany(UserEtablissement::className(), ['id_user'=>'id']) ;
  70. }
  71. /**
  72. * @inheritdoc
  73. */
  74. public static function findIdentity($id)
  75. {
  76. return static::findOne(['id' => $id/*, 'status' => self::STATUS_ACTIVE*/]);
  77. }
  78. /**
  79. * @inheritdoc
  80. */
  81. public static function findIdentityByAccessToken($token, $type = null)
  82. {
  83. throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  84. }
  85. /**
  86. * Finds user by username
  87. *
  88. * @param string $username
  89. * @return static|null
  90. */
  91. public static function findByUsername($username)
  92. {
  93. return static::findOne(['username' => $username/*, 'status' => self::STATUS_ACTIVE*/]);
  94. }
  95. public static function findByEmail($email)
  96. {
  97. return static::findOne(['email' => $email /*, 'status' => self::STATUS_ACTIVE*/]);
  98. }
  99. /**
  100. * Finds user by password reset token
  101. *
  102. * @param string $token password reset token
  103. * @return static|null
  104. */
  105. public static function findByPasswordResetToken($token)
  106. {
  107. if (!static::isPasswordResetTokenValid($token)) {
  108. return null;
  109. }
  110. return static::findOne([
  111. 'password_reset_token' => $token,
  112. ]);
  113. }
  114. public static function findBy($params = [])
  115. {
  116. if(!isset($params['id_etablissement']))
  117. $params['id_etablissement'] = Yii::$app->user->identity->id_etablissement ;
  118. $query = (new \yii\db\Query())
  119. ->select(['user.id AS user_id', 'user.prenom','user.nom','user.telephone','user.email','user.created_at','user.date_derniere_connexion'])
  120. ->from('user, user_etablissement')
  121. ->where('user.id = user_etablissement.id_user')
  122. ->andWhere('user_etablissement.actif = 1')
  123. ->andWhere('user_etablissement.id_etablissement = '.$params['id_etablissement']) ;
  124. if(isset($params['nom']))
  125. $query->andFilterWhere(['like', 'nom', $params['nom']]);
  126. if(isset($params['prenom']))
  127. $query->andFilterWhere(['like', 'prenom', $params['prenom']]) ;
  128. if(isset($params['email']))
  129. $query->andFilterWhere(['like', 'email', $params['email']]) ;
  130. if(isset($params['telephone']))
  131. $query->andFilterWhere(['like', 'telephone', $params['telephone']]) ;
  132. return $query ;
  133. }
  134. /**
  135. * Finds out if password reset token is valid
  136. *
  137. * @param string $token password reset token
  138. * @return boolean
  139. */
  140. public static function isPasswordResetTokenValid($token)
  141. {
  142. if (empty($token)) {
  143. return false;
  144. }
  145. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  146. $parts = explode('_', $token);
  147. $timestamp = (int) end($parts);
  148. return $timestamp + $expire >= time();
  149. }
  150. /**
  151. * @inheritdoc
  152. */
  153. public function getId()
  154. {
  155. return $this->getPrimaryKey();
  156. }
  157. /**
  158. * @inheritdoc
  159. */
  160. public function getAuthKey()
  161. {
  162. return $this->auth_key;
  163. }
  164. /**
  165. * @inheritdoc
  166. */
  167. public function validateAuthKey($authKey)
  168. {
  169. return $this->getAuthKey() === $authKey;
  170. }
  171. /**
  172. * Validates password
  173. *
  174. * @param string $password password to validate
  175. * @return boolean if password provided is valid for current user
  176. */
  177. public function validatePassword($password)
  178. {
  179. return Yii::$app->security->validatePassword($password, $this->password_hash);
  180. }
  181. /**
  182. * Generates password hash from password and sets it to the model
  183. *
  184. * @param string $password
  185. */
  186. public function setPassword($password)
  187. {
  188. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  189. }
  190. /**
  191. * Generates "remember me" authentication key
  192. */
  193. public function generateAuthKey()
  194. {
  195. $this->auth_key = Yii::$app->security->generateRandomString();
  196. }
  197. /**
  198. * Generates new password reset token
  199. */
  200. public function generatePasswordResetToken()
  201. {
  202. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  203. }
  204. /**
  205. * Removes password reset token
  206. */
  207. public function removePasswordResetToken()
  208. {
  209. $this->password_reset_token = null;
  210. }
  211. public function attributeLabels()
  212. {
  213. return [
  214. 'id' => 'ID',
  215. 'username' => 'Identifiant',
  216. 'password' => 'Mot de passe',
  217. 'rememberMe' => 'Se souvenir de moi',
  218. 'confiance' => 'De confiance',
  219. 'no_mail' => 'Ne pas recevoir d\'email de la part du Chat des Noisettes',
  220. 'mail_prod_lundi' => 'Lundi',
  221. 'mail_prod_mardi' => 'Mardi',
  222. 'mail_prod_mercredi' => 'Mercredi',
  223. 'mail_prod_jeudi' => 'Jeudi',
  224. 'mail_prod_vendredi' => 'Vendredi',
  225. 'mail_prod_samedi' => 'Samedi',
  226. 'mail_prod_dimanche' => 'Dimanche',
  227. ];
  228. }
  229. public function isBoulanger()
  230. {
  231. return ($this->status == User::STATUS_ADMIN || $this->status == User::STATUS_BOULANGER) && $this->id_etablissement ;
  232. }
  233. public function getNomMagasin()
  234. {
  235. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  236. return $etablissement->nom ;
  237. }
  238. public function getEtablissementsFavoris()
  239. {
  240. $etabs = (new \yii\db\Query())
  241. ->select('*')
  242. ->from(['user_etablissement', 'etablissement'])
  243. ->where('user_etablissement.id_etablissement = etablissement.id')
  244. ->andWhere(['user_etablissement.id_user' => $this->id])
  245. ->andWhere(['user_etablissement.actif' => 1])
  246. ->all();
  247. return $etabs ;
  248. }
  249. public function etatPaiementEtablissement()
  250. {
  251. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  252. if($etablissement)
  253. {
  254. return $etablissement->etatPaiement() ;
  255. }
  256. }
  257. public function periodeEssai()
  258. {
  259. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  260. if($etablissement)
  261. {
  262. $date_limite = strtotime($etablissement->date_creation) + 30*24*60*60 ;
  263. $date = time() ;
  264. if($date < $date_limite)
  265. {
  266. $date = $date_limite - $date ;
  267. return (int) ($date / (24*60*60)) ;
  268. }
  269. else {
  270. return 0 ;
  271. }
  272. }
  273. }
  274. public function getCredit($id_etablissement)
  275. {
  276. $user_etablissement = UserEtablissement::find()
  277. ->where([
  278. 'id_user' => $this->id,
  279. 'id_etablissement' => $id_etablissement
  280. ])
  281. ->one() ;
  282. if($user_etablissement)
  283. {
  284. return $user_etablissement->credit ;
  285. }
  286. return 0 ;
  287. }
  288. }