title: How to login with id or email
body: <p>I created  Login functions in  yii but i need to develop it to work with email or User ID in same time .
  In below you will check the login by email code and it's work in perfect mode  , Any help to develop it ?</p>
  
  <p>components/UserIdentity.php</p>
  
  <pre><code>&lt;?php
  
  /** Ahmad Samilo
   * 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
  {
  
       // Need to store the user's ID:
       private $_id;
       public $password;
       public $status;
       public $session;
  
      /**
       * 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 = Users::model()-&gt;findByAttributes(array('email'=&gt;$this-&gt;username));
  
  
              /// set number of faild login
               $session=new CHttpSession;
               $session-&gt;open();
  
             Yii::app()-&gt;session['number']= Yii::app()-&gt;session['number']+1;
             $session=Yii::app()-&gt;session['number'];
                /// end 
  
  
  
  
          if ($user===null) { // No user found!
              $this-&gt;errorCode=self::ERROR_USERNAME_INVALID;
          } else if ($user-&gt;password !== SHA1($this-&gt;password) ) { // Invalid password!
              $this-&gt;errorCode=self::ERROR_PASSWORD_INVALID;
  
  
  
  
  
  
  
          } else { // Okay!
  
  
  
               $this-&gt;errorCode=self::ERROR_NONE;
              // Store the role in a session:
               $this-&gt;setState('status', $user-&gt;active);
  
               $this-&gt;setState('id', $user-&gt;user_id);
              $this-&gt;_id = $user-&gt;user_id;
          }
          return !$this-&gt;errorCode;
      }
  
      public function getId()
      {
       return $this-&gt;_id;
      }
  
  
  }
  </code></pre>
  
  <p>view/login.php   </p>
  
  <pre><code>&lt;?php
  /* @var $this SiteController */
  /* @var $model LoginForm */
  /* @var $form CActiveForm  */
  
  $this-&gt;pageTitle=Yii::app()-&gt;name . ' - Login';
  $this-&gt;breadcrumbs=array(
      'Login',
  );
  ?&gt;
  
  &lt;h1&gt;Login&lt;/h1&gt;
  
  &lt;p&gt;Please fill out the following form with your login credentials:&lt;/p&gt;
  
  &lt;div class="form"&gt;
  &lt;?php $form=$this-&gt;beginWidget('CActiveForm', array(
      'id'=&gt;'login-form',
      'enableClientValidation'=&gt;true,
      'clientOptions'=&gt;array(
          'validateOnSubmit'=&gt;false,
      ),
  )); ?&gt;
  
      &lt;p class="note"&gt;Fields with &lt;span class="required"&gt;*&lt;/span&gt; are required.&lt;/p&gt;
  
  &lt;?php
  
  
  
  foreach(Yii::app()-&gt;user-&gt;getFlashes() as $key=&gt;$message){
  
      echo "&lt;div class='info'&gt;";
       echo '&lt;div class="flash-' . $key . '"&gt;' . $message . "&lt;/div&gt;\n";
  
       echo "&lt;/div&gt;";
  }
  
  
  ?&gt;
  
  
  &lt;?php
  
  
  //// Get Session 
  $session=new CHttpSession();
  $session-&gt;open();
  $count= Yii::app()-&gt;session['number'];
  
  
  /// End Get Session 
  
  //// Send Email to user notify him about that with help guide 
             function send_mail($to,$from,$subject,$msg){
          // message
          $message = '
          &lt;html&gt;
          &lt;head&gt;
            &lt;title&gt;Activation &lt;/title&gt;
          &lt;/head&gt;
          &lt;body dir="rtl"&gt;
          &lt;p align="right"&gt;'
          . $msg .
          '&lt;/p&gt;
          &lt;br&gt;
  
          &lt;/p&gt;
          &lt;/body&gt;
          &lt;/html&gt;
          ';
  
          // To send HTML mail, the Content-type header must be set
          $headers  = 'MIME-Version: 1.0' . "\n";
          $headers .= 'Content-type: text/html; charset=windows-1256' . "\n";
  
          // Additional heade rs
          $headers .= "From: ".$from . "\n";
          //$headers .= 'Bcc: info@illaftrain.co.uk' . "\n";
  
          // Mail it
          return mail($to, '=?windows-1256?B?'.base64_encode($subject).'?=', $message, $headers);
      }
  
  //////
  
  $email=$model-&gt;email;
  
  
  $subject="تنبيه تكرار تسجيل الدخول الخاطئ ";
  
  $url=Yii::app()-&gt;request-&gt;serverName;
  $message=" 
  
  text here 
  
  ";
  
   send_mail($email,'noreply@domain.net',$subject,$message);
  
  
  //// End mail Function 
  
  
  
  
  
  //// End Send mail 
  
  
  ?&gt;
      &lt;div class="row"&gt;
          &lt;?php echo $form-&gt;labelEx($model,'Email'); ?&gt;
          &lt;?php echo $form-&gt;textField($model,'email'); ?&gt;
          &lt;?php echo $form-&gt;error($model,'email'); ?&gt;
      &lt;/div&gt;
  
      &lt;div class="row"&gt;
          &lt;?php echo $form-&gt;labelEx($model,'password'); ?&gt;
          &lt;?php echo $form-&gt;passwordField($model,'password'); ?&gt;
          &lt;?php echo $form-&gt;error($model,'password'); ?&gt;
  
  
  
      &lt;/div&gt;
  
  
      &lt;div class="row buttons"&gt;
          &lt;?php echo CHtml::submitButton('Login'); ?&gt;
      &lt;/div&gt;
  
  
      &lt;?php if($count&gt;3) :?&gt;
      &lt;?php if(CCaptcha::checkRequirements()): ?&gt;
          &lt;?php echo $form-&gt;labelEx($model,'verifyCode'); ?&gt;
          &lt;div&gt;
          &lt;?php $this-&gt;widget('CCaptcha'); ?&gt;
          &lt;?php echo $form-&gt;textField($model,'verifyCode'); ?&gt;
          &lt;/div&gt;
      &lt;div class="row"&gt;
          &lt;div class="hint"&gt;Please enter the letters as they are shown in the image above.
          &lt;br/&gt;Letters are not case-sensitive.&lt;/div&gt;
          &lt;?php echo $form-&gt;error($model,'verifyCode'); ?&gt;
      &lt;/div&gt;
      &lt;?php endif; ?&gt;
      &lt;?php endif; ?&gt;
  
  
  
  
  
  
  
  
  
  &lt;?php $this-&gt;endWidget(); ?&gt;
  </code></pre>
  
  <p>Models/LoginForm.php</p>
  
  <pre><code>&lt;?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 $password;
      public $login;
      public $verifyCode;
  
  
      /**
       * 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, password', 'required'),
          array('email', 'email'),
          array('password', 'authenticate'),
              // verifyCode needs to be entered correctly
              array('verifyCode', 'captcha', 'allowEmpty'=&gt;!CCaptcha::checkRequirements()),
  
  );
  
  }
  
      /**
       * Declares attribute labels.
       */
      public function attributeLabels()
      {
          return array(
              'email'=&gt;'Email Address',
              'user_id'=&gt;'User ID',
          );
      }
  
      /**
       * Authenticates the password.
       * This is the 'authenticate' validator as declared in rules().
       */
      public function authenticate($attribute,$params)
      {
          if(!$this-&gt;hasErrors())  // we only want to authenticate when no input errors
          {
              $identity=new UserIdentity($this-&gt;email,$this-&gt;password);
              $identity-&gt;authenticate();
              switch($identity-&gt;errorCode)
              {
                  case UserIdentity::ERROR_NONE:
                      Yii::app()-&gt;user-&gt;login($identity);
  
                      break;
                  case UserIdentity::ERROR_USERNAME_INVALID:
                      $this-&gt;addError('email','Email address is incorrect.');
                      break;
                  default: // UserIdentity::ERROR_PASSWORD_INVALID
                      $this-&gt;addError('password','Password is incorrect.');
                      break;
              }
          }
      }
  
  
  }
  </code></pre>