Guest User

Untitled

a guest
Aug 14th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. это экшн
  2. ------------
  3. public function actionLogin()
  4. { $model=new LoginForm;
  5. if (isset ($_POST['LoginForm']))
  6. {
  7.  
  8. $model->attributes=$_POST['LoginForm'];
  9. if($model->validate() && $model->login()) //echo '';
  10. $this->redirect(Yii::app()->user->returnUrl);
  11.  
  12. }
  13. $this->render('login',array('model'=>$model));
  14.  
  15.  
  16. }
  17. -------------
  18. это модель LoginFrom
  19. ------------------
  20.  
  21. <?php
  22.  
  23. /**
  24. * LoginForm class.
  25. * LoginForm is the data structure for keeping
  26. * user login form data. It is used by the 'login' action of 'SiteController'.
  27. */
  28. class LoginForm extends CFormModel
  29. {
  30. public $user_login;
  31. public $user_password;
  32. public $rememberMe;
  33.  
  34. private $_identity;
  35.  
  36. /**
  37. * Declares the validation rules.
  38. * The rules state that username and password are required,
  39. * and password needs to be authenticated.
  40. */
  41. public function rules()
  42. {
  43. return array(
  44. // username and password are required
  45. array('user_login, user_password', 'required'),
  46. // rememberMe needs to be a boolean
  47. array('rememberMe', 'boolean'),
  48. // password needs to be authenticated
  49. array('user_password', 'authenticate'),
  50. );
  51. }
  52.  
  53. /**
  54. * Declares attribute labels.
  55. */
  56. public function attributeLabels()
  57. {
  58. return array(
  59. 'rememberMe'=>'Remember me next time',
  60. );
  61. }
  62.  
  63. /**
  64. * Authenticates the password.
  65. * This is the 'authenticate' validator as declared in rules().
  66. */
  67. public function authenticate($attribute,$params)
  68. {
  69. if(!$this->hasErrors())
  70. {
  71. $this->_identity=new UserIdentity($this->user_login,$this->user_password);
  72. if(!$this->_identity->authenticate())
  73. $this->addError('user_password','Incorrect username or password.');
  74. }
  75. }
  76.  
  77. /**
  78. * Logs in the user using the given username and password in the model.
  79. * @return boolean whether login is successful
  80. */
  81. public function login()
  82. {
  83. if($this->_identity===null)
  84. {
  85. $this->_identity=new UserIdentity($this->user_login,$this->user_password);
  86. $this->_identity->authenticate();
  87. }
  88. if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
  89. { $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
  90. Yii::app()->user->login($this->_identity,$duration);
  91. if (Yii::app()->user->isGuest) echo '123';
  92. return true;
  93. }
  94.  
  95. else
  96. return false;
  97. }
  98. }
  99. ----------------
  100. это компонент авторизации немного переодпределнный под пою авторизацию
  101. ------------------
  102. <?php
  103.  
  104. /**
  105. * UserIdentity represents the data needed to identity a user.
  106. * It contains the authentication method that checks if the provided
  107. * data can identity the user.
  108. */
  109. class UserIdentity extends CUserIdentity
  110. {
  111. private $user_id;
  112. public $user_login;
  113. public $user_password;
  114. public function authenticate()
  115. {
  116. $this->user_login = $this->username;
  117. $this->user_password = $this->password;
  118.  
  119. $record=User::model()->findByAttributes(array('user_login'=>$this->username));
  120. if($record===null)
  121. $this->errorCode=self::ERROR_USERNAME_INVALID;
  122.  
  123. else if($record->user_password!==md5($this->password))
  124. $this->errorCode=self::ERROR_PASSWORD_INVALID;
  125. else
  126. $this->errorCode=self::ERROR_NONE;
  127.  
  128. return !$this->errorCode;
  129. }
  130.  
  131. public function getId()
  132. {
  133. return $this->user_id;
  134. }
  135. }
  136. ---------------
Add Comment
Please, Sign In to add comment