PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/models/User.php

https://bitbucket.org/maaguilar90/asopreol
PHP | 210 lines | 149 code | 19 blank | 42 comment | 8 complexity | e925e12a4aa754349669f2c2ec6c97a4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\NotSupportedException;
  5. use yii\db\ActiveRecord;
  6. use yii\base\Security;
  7. use yii\web\IdentityInterface;
  8. use yii\behaviors\TimestampBehavior;
  9. use yii\db\Expression;
  10. use yii\filters\AccessControl;
  11. use kartik\password\StrengthValidator;
  12. use yii\models\Token;
  13. /**
  14. * This is the model class for table "user".
  15. *
  16. * @property string $identity
  17. * @property string $type
  18. * @property string $address_home
  19. * @property string $phone_home
  20. * @property string $cellphone
  21. * @property string $cellphone_company
  22. * @property string $username
  23. * @property string $password
  24. * @property string $country_origin
  25. * @property string $province_residence
  26. * @property string $canton_residence
  27. * @property string $zone_residence
  28. * @property string $chief_representative
  29. * @property string $secondary_representative
  30. * @property string $status
  31. * @property integer $office
  32. * @property string $names
  33. * @property string $lastnames
  34. * @property string $economic_sector
  35. * @property integer $link
  36. * @property string $internal_rating
  37. * @property string $city_residence
  38. */
  39. class User extends ActiveRecord implements IdentityInterface
  40. {
  41. /**
  42. * @inheritdoc
  43. */
  44. public static function tableName()
  45. {
  46. return 'user';
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. public function rules()
  52. {
  53. return [
  54. [['identity', 'address_home', 'phone_home', 'cellphone', 'username', 'province_residence', 'canton_residence', 'zone_residence', 'names', 'lastnames', 'city_residence'], 'required'],
  55. [['office', 'link'], 'integer'],
  56. [['identity'], 'string', 'max' => 13],
  57. [['type', 'cellphone_company', 'status'], 'string', 'max' => 1],
  58. [['address_home'], 'string', 'max' => 80],
  59. [['phone_home'], 'string', 'max' => 20],
  60. [['cellphone', 'province_residence', 'canton_residence', 'zone_residence', 'chief_representative', 'secondary_representative', 'economic_sector', 'internal_rating'], 'string', 'max' => 10],
  61. [['username'], 'string', 'max' => 150],
  62. [['password'], 'string', 'max' => 255],
  63. [['country_origin'], 'string', 'max' => 40],
  64. ['type_client', 'in', 'range' => ['CLIENT','ADMIN']],
  65. ['status_client', 'in', 'range' => ['ACTIVE','INACTIVE']],
  66. [['names', 'lastnames', 'city_residence'], 'string', 'max' => 100],
  67. ];
  68. }
  69. /**
  70. * @inheritdoc
  71. */
  72. public function attributeLabels()
  73. {
  74. return [
  75. 'identity' => 'Cédula',
  76. 'type' => 'Tipo',
  77. 'address_home' => 'Dirección Casa',
  78. 'phone_home' => 'Teléfono Casa',
  79. 'cellphone' => 'Celular',
  80. 'cellphone_company' => 'Celular Empresa',
  81. 'username' => 'Email',
  82. 'password' => 'Password',
  83. 'country_origin' => 'País de Origen',
  84. 'province_residence' => 'Provincia de Residencia',
  85. 'canton_residence' => 'Cantón de Residencia',
  86. 'zone_residence' => 'Zona de Residencia (Parroquia)',
  87. 'chief_representative' => 'Representante Legal',
  88. 'secondary_representative' => 'Representante Secundario',
  89. 'status' => 'Estatus',
  90. 'office' => 'Oficina',
  91. 'names' => 'Nombres',
  92. 'lastnames' => 'Apellidos',
  93. 'economic_sector' => 'Sector Económico',
  94. 'link' => 'Link',
  95. 'internal_rating' => 'Internal Rating',
  96. 'city_residence' => 'Ciudad de Residencia',
  97. ];
  98. }
  99. public static function findIdentity($id)
  100. {
  101. return static::findOne($id);
  102. }
  103. public static function findIdentityByAccessToken($token, $type = null)
  104. {
  105. throw new NotSupportedException('"findIdentityByAccessToken" No está implementado.');
  106. }
  107. public function beforeSave($insert) {
  108. if (parent::beforeSave($insert)) {
  109. if ($this->isNewRecord) {
  110. $this->auth_key = \Yii::$app->security->generateRandomString();
  111. if(isset($this->password))
  112. $this->password = $this->hashPassword($this->password);
  113. }
  114. }
  115. return parent::beforeSave($insert);
  116. }
  117. public static function isUserClient($username)
  118. {
  119. if (static::findOne(['username' => $username, 'type_client' => 'CLIENT'])){
  120. return true;
  121. } else {
  122. return false;
  123. }
  124. }
  125. public static function isUserAdmin($username)
  126. {
  127. if (static::findOne(['username' => $username, 'type_client' => 'ADMIN'])){
  128. return true;
  129. } else {
  130. return false;
  131. }
  132. }
  133. public static function findByUsername($username)
  134. {
  135. return static::findOne(['username' => $username]);
  136. }
  137. public static function findByPasswordResetToken($token)
  138. {
  139. $expire = (1 * 4 * 60 * 60);
  140. $parts = explode('_', $token);
  141. $timestamp = (int) end($parts);
  142. if ($timestamp + $expire < time()) {
  143. return null;
  144. }
  145. return static::findOne([
  146. 'password_reset_token' => $token
  147. ]);
  148. }
  149. public function getId()
  150. {
  151. return $this->identity;
  152. }
  153. public function getUsername()
  154. {
  155. return $this->username;
  156. }
  157. public function getAuthKey()
  158. {
  159. return $this->auth_key;
  160. }
  161. public function validateAuthKey($authKey)
  162. {
  163. return $this->auth_key === $authKey;
  164. }
  165. public function validatePassword($password)
  166. {
  167. return $this->password === md5($password);
  168. }
  169. public function hashPassword($password){
  170. //return hash('sha256',$password);
  171. return md5($password);
  172. }
  173. public function generateAuthKey()
  174. {
  175. $this->auth_key = Yii::$app->getSecurity()->generateRandomKey();
  176. }
  177. /**
  178. * Generates new password reset token
  179. */
  180. public function generatePasswordResetToken()
  181. {
  182. $this->password_reset_token = \Yii::$app->getSecurity()->generateRandomString() . '_' . time();
  183. }
  184. /**
  185. * Removes password reset token
  186. */
  187. public function removePasswordResetToken()
  188. {
  189. $this->password_reset_token = null;
  190. }
  191. }