Advertisement
Guest User

Untitled

a guest
Mar 18th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. <?php
  2.  
  3. namespace appmodels;
  4.  
  5. use yii;
  6. use yiidbActiveRecord;
  7. use yiiwebIdentityInterface;
  8.  
  9. class User extends ActiveRecord implements IdentityInterface
  10. {
  11. public $id;
  12. public $username;
  13. public $email;
  14. public $password;
  15. public $phone_number;
  16. public $authkey;
  17.  
  18.  
  19. public static function tableName(){
  20. return 'user';
  21. }
  22.  
  23. public function rules(){
  24. return [
  25. [['id','authkey'],'safe'],
  26. [['username', 'password','email'], 'required'],
  27. [['email'], 'email'],
  28. [['phone_number'],'string', 'max' => 13],
  29. [['username'], 'string', 'max' => 16],
  30. ];
  31. }
  32.  
  33. /**
  34. * @inheritdoc
  35. */
  36. public static function findIdentity($id)
  37. {
  38. return static::findOne($id);
  39. }
  40.  
  41. //Не нужен
  42. public static function findIdentityByAccessToken($token, $type = null)
  43. {
  44. throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  45. }
  46. /**
  47. * Finds user by username
  48. *
  49. * @param string $username
  50. * @return static|null
  51. */
  52. public static function findByUsername($username)
  53. {
  54. return static::find()->where('username = :username',[':username' => $username])->one();
  55. }
  56.  
  57. /**
  58. * @inheritdoc
  59. */
  60. public function getId()
  61. {
  62. return $this->id;
  63. }
  64.  
  65. /**
  66. * @inheritdoc
  67. */
  68. public function getAuthKey()
  69. {
  70. return $this->authKey;
  71. }
  72.  
  73. /**
  74. * @inheritdoc
  75. */
  76. public function validateAuthKey($authKey)
  77. {
  78. return $this->authKey === $authKey;
  79. }
  80.  
  81. /**
  82. * Validates password
  83. *
  84. * @param string $password password to validate
  85. * @return bool if password provided is valid for current user
  86. */
  87. public function validatePassword($password)
  88. {
  89. $hash = $this->password;
  90. return Yii::$app->getSecurity()->validatePassword($password,$hash);
  91. }
  92.  
  93. public function beforeSave($insert){
  94. if (parent::beforeSave($insert)){
  95. if($this->isNewRecord){
  96. $this->authkey = Yii::$app->getSecurity()->generateRandomString();
  97. }
  98. return true;
  99. }
  100. return false;
  101. }
  102. }
  103.  
  104. <?php
  105.  
  106. namespace appmodels;
  107.  
  108. use Yii;
  109. use yiibaseModel;
  110. use appmodelsUser;
  111.  
  112.  
  113. class SignupForm extends Model
  114. {
  115. public $username;
  116. public $email;
  117. public $password;
  118. public $confirmPassword;
  119. public $phone_number;
  120.  
  121.  
  122. public function rules()
  123. {
  124. return [
  125. [['username','email', 'password','confirmPassword'], 'required'],
  126. ['email', 'email'],
  127. ['phone_number', 'string', 'max' => 13],
  128. ['password', 'string', 'min' => 6],
  129. ['confirmPassword', 'compare', 'compareAttribute' => 'password']
  130. ];
  131. }
  132.  
  133. public function signup()
  134. {
  135. $new_user = new User();
  136. $new_user->username = $this->username;
  137. $new_user->email = $this->email;
  138. $new_user->phone_number = $this->phone_number;
  139. $new_user->password = Yii::$app->getSecurity()->generatePasswordHash($this->password);
  140. Yii::trace($new_user->attributes;
  141. Yii::trace($new_user->getDirtyAttributes());
  142. Yii::trace($new_user->getOldAttributes());
  143. $status = $new_user->save();
  144. Yii::trace($status);
  145. return $status;
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement