Mostrando entradas con la etiqueta Autentificación. Mostrar todas las entradas
Mostrando entradas con la etiqueta Autentificación. Mostrar todas las entradas

viernes, 5 de julio de 2013

Autenticación con pass md5 en yii

Despues de generar  el medelo de la tabla usuario


class UserIdentity extends CUserIdentity
{
    /**
     * Authenticates a user.
     * The example implementation makes sure if the username and password
     * are both 'demo'.
     * In practical applications, this should be changed to authenticate
     * against some persistent user identity storage (e.g. database).
     * @return boolean whether authentication succeeds.
     */
public function authenticate()
    {


$user = Usuario::model()->findByAttributes(array('login'=>$this->username));
if ($user === null) {
// No user found!
$this->errorCode=self::ERROR_USERNAME_INVALID;
} else if ($user->password !==md5($this->password)) {
// Invalid password!
$this->errorCode=self::ERROR_PASSWORD_INVALID;
} else { // Okay!
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;



    }
}


Función del sitecontrolador que manda llamar la vista login.php


    public function actionIndex()
    {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'
    /*    if(Yii::app()->user->isGuest){
                $this->layout='//layouts/login';
        $this->render('index');
            } else {
        //$this->layout='/layouts/login';
        $this->render('index');*/
 $this->layout='//layouts/login';
        $model=new LoginForm;

        $this->layout='/layouts/login';

        // if it is ajax validation request
        if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if(isset($_POST['LoginForm']))
        {
            $model->attributes=$_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if($model->validate() && $model->login())
                 $this->redirect(array('/aspirantes/index'));
        }
        // display the login form
        $this->render('login',array('model'=>$model));
   
    }

Si la validación es correcta manda llamar al controlador aspirantes/index