title: CakePHP Auth Component Using 2 Tables
body: <p>CakePHP Version 1.2.5</p>
  
  <p>I would like a single user to have multiple email addresses.<br>
  I would like a single user to have a single password.<br>
  I would like users to log in using any of their multiple email addresses and their single password.</p>
  
  <p>I have created a users table with an id and a password field.<br>
  I have created a user_email_addresses table with an id field a user_id field and an email_address field.</p>
  
  <p>Question:<br>
  How do I modify the auth component minimally to look for the "username" in this case, "email_address", in the user_email_addresses table and the "password" in the users table?</p>
  
  <p>Seems as though modifying the identify method in the auth component might do it. But I think modifying the auth component directly is a bad idea - any ideas on how to extend and still possibly modify the identify method? <a href="http://cakebaker.42dh.com/2009/09/08/extending-cakephps-core-components/" rel="noreferrer">http://cakebaker.42dh.com/2009/09/08/extending-cakephps-core-components/</a> or possibly nominate a different authenticate object?</p>
  
  <p>Starting line 774:</p>
  
  <pre><code>    function identify($user = null, $conditions = null) {
      if ($conditions === false) {
          $conditions = null;
      } elseif (is_array($conditions)) {
          $conditions = array_merge((array)$this-&gt;userScope, $conditions);
      } else {
          $conditions = $this-&gt;userScope;
      }
      if (empty($user)) {
          $user = $this-&gt;user();
          if (empty($user)) {
              return null;
          }
      } elseif (is_object($user) &amp;&amp; is_a($user, 'Model')) {
          if (!$user-&gt;exists()) {
              return null;
          }
          $user = $user-&gt;read();
          $user = $user[$this-&gt;userModel];
      } elseif (is_array($user) &amp;&amp; isset($user[$this-&gt;userModel])) {
          $user = $user[$this-&gt;userModel];
      }
  
      if (is_array($user) &amp;&amp; (isset($user[$this-&gt;fields['username']]) || isset($user[$this-&gt;userModel . '.' . $this-&gt;fields['username']]))) {
  
          if (isset($user[$this-&gt;fields['username']]) &amp;&amp; !empty($user[$this-&gt;fields['username']])  &amp;&amp; !empty($user[$this-&gt;fields['password']])) {
              if (trim($user[$this-&gt;fields['username']]) == '=' || trim($user[$this-&gt;fields['password']]) == '=') {
                  return false;
              }
              $find = array(
                  $this-&gt;userModel.'.'.$this-&gt;fields['username'] =&gt; $user[$this-&gt;fields['username']],
                  $this-&gt;userModel.'.'.$this-&gt;fields['password'] =&gt; $user[$this-&gt;fields['password']]
              );
          } elseif (isset($user[$this-&gt;userModel . '.' . $this-&gt;fields['username']]) &amp;&amp; !empty($user[$this-&gt;userModel . '.' . $this-&gt;fields['username']])) {
              if (trim($user[$this-&gt;userModel . '.' . $this-&gt;fields['username']]) == '=' || trim($user[$this-&gt;userModel . '.' . $this-&gt;fields['password']]) == '=') {
                  return false;
              }
              $find = array(
                  $this-&gt;userModel.'.'.$this-&gt;fields['username'] =&gt; $user[$this-&gt;userModel . '.' . $this-&gt;fields['username']],
                  $this-&gt;userModel.'.'.$this-&gt;fields['password'] =&gt; $user[$this-&gt;userModel . '.' . $this-&gt;fields['password']]
              );
          } else {
              return false;
          }
          $model =&amp; $this-&gt;getModel();
          $data = $model-&gt;find(array_merge($find, $conditions), null, null, 0);
          if (empty($data) || empty($data[$this-&gt;userModel])) {
              return null;
          }
      } elseif (!empty($user) &amp;&amp; is_string($user)) {
          $model =&amp; $this-&gt;getModel();
          $data = $model-&gt;find(array_merge(array($model-&gt;escapeField() =&gt; $user), $conditions));
  
          if (empty($data) || empty($data[$this-&gt;userModel])) {
              return null;
          }
      }
  
      if (!empty($data)) {
          if (!empty($data[$this-&gt;userModel][$this-&gt;fields['password']])) {
              unset($data[$this-&gt;userModel][$this-&gt;fields['password']]);
          }
          return $data[$this-&gt;userModel];
      }
      return null;
  }
  </code></pre>