Need Some Help

Hi there,

I have a question regaridng an authentication mechanism i just implemented with the help of some tutorials, but i’m not understanding the logic of it.

I have this table,


CREATE TABLE IF NOT EXISTS `User` (

  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,

  `email` varchar(80) NOT NULL,

  `pass` char(40) NOT NULL,

  `role` enum('ddd','ccc','bbb','aaa') NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;



UserIdentity:


class UserIdentity extends CUserIdentity

{


	 // Need to store the user's ID:

	 private $_id;




	

	public function authenticate()

	{

		

		//'email' é o nome do formulario e $this->username o campo da classe IUserIdentity

		$user = User::model()->findByAttributes(array('email'=>$this->username));


		if ($user===null) { // No user found!

			$this->errorCode=self::ERROR_USERNAME_INVALID;

		} else if ($user->pass !== $this->password) { // Invalid password!

			$this->errorCode=self::ERROR_PASSWORD_INVALID;

		} else { // Okay!

		    $this->errorCode=self::ERROR_NONE;

		    // Store the role in a session:

		    //$this->setState('role', $user->role);

			$this->_id = $user->id;

		}

		return $this->errorCode;

	}

	

	public function getId()

	{

	 return $this->_id;

	}


	

}

LoginForm




<?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 $email;

	public $pass;

	/**

	 * Declares the validation rules.

	 * The rules state that username and password are required,

	 * and password needs to be authenticated.

	 */

public function rules()

{

	return array(

		//array('email, pass','role', 'required'),

		array('email, pass', 'required'),

		array('email', 'email'),

		array('pass', 'authenticate'),

		//array('role', 'my_validation_rule'),


	);

}





	/**

	 * Declares attribute labels.

	 */

	public function attributeLabels()

	{

		return array(

			'email'=>'Endereço de email',

		);

	}




	/**

	 * Authenticates the password.

	 * This is the 'authenticate' validator as declared in rules().

	 */


public function authenticate($attribute,$params)

	{

		if(!$this->hasErrors())  // we only want to authenticate when no input errors

		{

			$identity=new UserIdentity($this->email,$this->pass);

			$identity->authenticate();

			switch($identity->errorCode)

			{

				case UserIdentity::ERROR_NONE:

					Yii::app()->user->login($identity);

					break;

				case UserIdentity::ERROR_USERNAME_INVALID:

					$this->addError('email','Email address is incorrect.');

					break;

				default: // UserIdentity::ERROR_PASSWORD_INVALID

					$this->addError('pass','Password is incorrect.');

					break;

			}

		}

	}


	

	public function login()

        {

                if($identity===null)

                {

                        $identity=new UserIdentity($this->email,$this->pass);

                        $identity->authenticate();

                }

                if($identity->errorCode===UserIdentity::ERROR_NONE)

                {

                     

                        Yii::app()->user->login($identity);

                        return true;

                }

                else

                        return false;   }

	

}



My question is:

Why do i have methods from LoginForm doing excataly the same thing or similar things…

I understand that UserIdentity check if the identity is correct, and LoginForm perform the authentication. And Login?

thanks!

The authenticate() methods may look similar but LoginForm being the model calls the one in UserIdentity, which can perform basic password, LDAP, or some other method of authentication. The model would remain the same regardless.