Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- это экшн
- ------------
- public function actionLogin()
- { $model=new LoginForm;
- if (isset ($_POST['LoginForm']))
- {
- $model->attributes=$_POST['LoginForm'];
- if($model->validate() && $model->login()) //echo '';
- $this->redirect(Yii::app()->user->returnUrl);
- }
- $this->render('login',array('model'=>$model));
- }
- -------------
- это модель LoginFrom
- ------------------
- <?php
- /**
- * LoginForm class.
- * LoginForm is the data structure for keeping
- * user login form data. It is used by the 'login' action of 'SiteController'.
- */
- class LoginForm extends CFormModel
- {
- public $user_login;
- public $user_password;
- public $rememberMe;
- private $_identity;
- /**
- * Declares the validation rules.
- * The rules state that username and password are required,
- * and password needs to be authenticated.
- */
- public function rules()
- {
- return array(
- // username and password are required
- array('user_login, user_password', 'required'),
- // rememberMe needs to be a boolean
- array('rememberMe', 'boolean'),
- // password needs to be authenticated
- array('user_password', 'authenticate'),
- );
- }
- /**
- * Declares attribute labels.
- */
- public function attributeLabels()
- {
- return array(
- 'rememberMe'=>'Remember me next time',
- );
- }
- /**
- * Authenticates the password.
- * This is the 'authenticate' validator as declared in rules().
- */
- public function authenticate($attribute,$params)
- {
- if(!$this->hasErrors())
- {
- $this->_identity=new UserIdentity($this->user_login,$this->user_password);
- if(!$this->_identity->authenticate())
- $this->addError('user_password','Incorrect username or password.');
- }
- }
- /**
- * Logs in the user using the given username and password in the model.
- * @return boolean whether login is successful
- */
- public function login()
- {
- if($this->_identity===null)
- {
- $this->_identity=new UserIdentity($this->user_login,$this->user_password);
- $this->_identity->authenticate();
- }
- if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
- { $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
- Yii::app()->user->login($this->_identity,$duration);
- if (Yii::app()->user->isGuest) echo '123';
- return true;
- }
- else
- return false;
- }
- }
- ----------------
- это компонент авторизации немного переодпределнный под пою авторизацию
- ------------------
- <?php
- /**
- * UserIdentity represents the data needed to identity a user.
- * It contains the authentication method that checks if the provided
- * data can identity the user.
- */
- class UserIdentity extends CUserIdentity
- {
- private $user_id;
- public $user_login;
- public $user_password;
- public function authenticate()
- {
- $this->user_login = $this->username;
- $this->user_password = $this->password;
- $record=User::model()->findByAttributes(array('user_login'=>$this->username));
- if($record===null)
- $this->errorCode=self::ERROR_USERNAME_INVALID;
- else if($record->user_password!==md5($this->password))
- $this->errorCode=self::ERROR_PASSWORD_INVALID;
- else
- $this->errorCode=self::ERROR_NONE;
- return !$this->errorCode;
- }
- public function getId()
- {
- return $this->user_id;
- }
- }
- ---------------
Add Comment
Please, Sign In to add comment