/common/models/LoginInstitution.php

https://gitlab.com/abigabaw/dptt · PHP · 202 lines · 99 code · 22 blank · 81 comment · 2 complexity · 364e7015057dbc2d3df4cd1ae58b0437 MD5 · raw file

  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\web\IdentityInterface;
  8. /**
  9. * This is the model class for table "login_innovator".
  10. *
  11. * @property integer $id
  12. * @property string $name
  13. * @property string $email
  14. * @property integer $innovator
  15. * @property string $auth_key
  16. * @property integer $status
  17. * @property string $password
  18. * @property string $password_reset_token
  19. * @property string $created_at
  20. * @property string $updated_at
  21. *
  22. * @property Innovator $innovator0
  23. */
  24. class LoginInstitution extends ActiveRecord implements IdentityInterface
  25. {
  26. const STATUS_DELETED = 0;
  27. const STATUS_ACTIVE = 10;
  28. /**
  29. * @inheritdoc
  30. */
  31. public static function tableName()
  32. {
  33. return 'login_institution';
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function behaviors()
  39. {
  40. return [
  41. TimestampBehavior::className(),
  42. ];
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function rules()
  48. {
  49. return [
  50. ['status', 'default', 'value' => self::STATUS_ACTIVE],
  51. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
  52. ];
  53. }
  54. /**
  55. * @inheritdoc
  56. */
  57. public static function findIdentity($id)
  58. {
  59. return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public static function findIdentityByAccessToken($token, $type = null)
  65. {
  66. throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  67. }
  68. /**
  69. * Finds user by username
  70. *
  71. * @param string $username
  72. * @return static|null
  73. */
  74. public static function findByEmail($email)
  75. {
  76. return static::findOne(['email' => $email, 'status' => self::STATUS_ACTIVE]);
  77. }
  78. /**
  79. * Finds user by password reset token
  80. *
  81. * @param string $token password reset token
  82. * @return static|null
  83. */
  84. public static function findByPasswordResetToken($token)
  85. {
  86. if (!static::isPasswordResetTokenValid($token)) {
  87. return null;
  88. }
  89. return static::findOne([
  90. 'password_reset_token' => $token,
  91. 'status' => self::STATUS_ACTIVE,
  92. ]);
  93. }
  94. /**
  95. * Finds out if password reset token is valid
  96. *
  97. * @param string $token password reset token
  98. * @return boolean
  99. */
  100. public static function isPasswordResetTokenValid($token)
  101. {
  102. if (empty($token)) {
  103. return false;
  104. }
  105. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  106. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  107. return $timestamp + $expire >= time();
  108. }
  109. /**
  110. * @inheritdoc
  111. */
  112. public function getId()
  113. {
  114. return $this->getPrimaryKey();
  115. }
  116. /**
  117. * @inheritdoc
  118. */
  119. public function getAuthKey()
  120. {
  121. return $this->auth_key;
  122. }
  123. /**
  124. * @inheritdoc
  125. */
  126. public function validateAuthKey($authKey)
  127. {
  128. return $this->getAuthKey() === $authKey;
  129. }
  130. /**
  131. * Validates password
  132. *
  133. * @param string $password password to validate
  134. * @return boolean if password provided is valid for current user
  135. */
  136. public function validatePassword($password)
  137. {
  138. return Yii::$app->security->validatePassword($password, $this->password_hash);
  139. }
  140. /**
  141. * Generates password hash from password and sets it to the model
  142. *
  143. * @param string $password
  144. */
  145. public function setPassword($password)
  146. {
  147. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  148. }
  149. /**
  150. * Generates "remember me" authentication key
  151. */
  152. public function generateAuthKey()
  153. {
  154. $this->auth_key = Yii::$app->security->generateRandomString();
  155. }
  156. /**
  157. * Generates new password reset token
  158. */
  159. public function generatePasswordResetToken()
  160. {
  161. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  162. }
  163. /**
  164. * Removes password reset token
  165. */
  166. public function removePasswordResetToken()
  167. {
  168. $this->password_reset_token = null;
  169. }
  170. /**
  171. * @return \yii\db\ActiveQuery
  172. */
  173. public function getInstitution0()
  174. {
  175. return $this->hasOne(Institution::className(), ['id' => 'Institution']);
  176. }
  177. public function getUsername(){
  178. return $this->email;
  179. }
  180. }