title: Joomla plugin for login
body: <p><strong>UPDATE:</strong></p>
  
  <p>@Elin:</p>
  
  <p>Thanks for the link! And sorry for the awful formating of my comments, I just don't make line-breaks working.<br>
  But what I try is not really to change a user. I just wanted to create it.
  However, I changed my plans of what my plugin should do. Perhaps you have an idea how to make my plan working:<br>
  I explored the following:<br>
  If I have an user with username 1 with password 1 in Joomla DB and the same user with another password (this is important because otherwise the user would directly authenticate against Joomla DB) in my external DB, a login in frontend with the password from the external DB works without any problems.<br>
  A login to backend works ONLY with the password (and so with) the user in Joomlas DB, even if all details are the same (even the groups).  </p>
  
  <p>How can I log the user who was authenticated against my external DB into backend?!  </p>
  
  <p>One more time many thanks in advance!</p>
  
  <p><strong>_____</strong> End of update <strong>____</strong></p>
  
  <p>I have a (hopefully) little problem that drives me crazy:</p>
  
  <p>I juste wrote a plugin for Joomla 3.1. This plugin allows an authentication against an external webserver script (with onUserAuthenticate()). Works properly.</p>
  
  <p>Than I thought it was better to make backend access possible. So I wrote a second plugin to make that possible.
  This plugin searches the authorized user in Joomlas DB and adds him if not found to <em>#__users</em> and <em>#__user_usergroup_map</em>. To here it works properly as well.</p>
  
  <p>Now my question:</p>
  
  <p>Frontend login works, backend doesn't (only a blank white page is displayed).
  <strong>Do I have to insert the user in any other tables as well?</strong> Or is anything missing? Did I forget something?!</p>
  
  <p>My code for the login plugin:</p>
  
  <pre><code>&lt;?php
  /**
   * @package     Joomla.Plugin
   * @subpackage  Authentication.External
   *
   * @copyright   Copyright (C) 2014 Stefan Herzog. All rights reserved.
   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   */
  
  defined('_JEXEC') or die;
  
  /**
   * External Authentication Plugin
   *
   * @package     Joomla.Plugin
   * @subpackage  Authentication.external
   * @since       3.1
   */
  
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  class PlgUserExternal extends JPlugin
  {
      /**
       * Load the language file on instantiation. Note this is only available in Joomla 3.1 and higher.
       * If you want to support 3.0 series you must override the constructor
       *
       * @var    boolean
       * @since  3.1
       */
      protected $autoloadLanguage = true;
      /**
           * This method should handle any authentication and report back to the subject
           *
           * @param   array   $credentials  Array holding the user credentials
           * @param   array   $options      Array of extra options
           * @param   object  &amp;$response    Authentication response object
           * @param   object  $response     Authentication response object
           * @param   array   $result       Authenticataion response array    
           * @return  boolean
           *
           * @since   1.5
           */
          public function onUserLogin($user, $options = array())
          {
  
              // Set default values
              $allow_backend_access = FALSE;  
  
  
              if(isset($user['admin']) AND $user['admin'] === 1)
              {
                  $allow_backend_access = TRUE;     // works! This parameter is delievered with $response from onUserAuthentication()
              }
  
              // Get a db connection.
              $db = JFactory::getDbo();
  
              // Create a new query object.
              $query = $db-&gt;getQuery(true);
  
              // Select all records from the user profile table where key begins with "custom.".
              // Order it by the ordering field.
              $query-&gt;select($db-&gt;quoteName(array('id')));
              $query-&gt;from($db-&gt;quoteName('#__users'));
              $query-&gt;where($db-&gt;quoteName('username') . ' = '. $db-&gt;quote($user['username']));
  
              // Reset the query using our newly populated query object.
              $db-&gt;setQuery($query);
  
              // Load the results as a list of stdClass objects (see later for more options on retrieving data).
              $results = $db-&gt;loadAssoc();    
  
              if(count($results) == 0)
              {
                  $checkUser = 0;
              }
              elseif(count($results) &gt;= 1)
              {
                  $checkUser = 1;
                  $user_id = $results['id'];
              }
  
              if($checkUser === 0)
              {
  
                  // Create a new query object.
                  $query = $db-&gt;getQuery(true);
  
                  // Insert columns.
                  $columns = array('name', 'username','email','password');
  
                  // Insert values.
                  $values = array($db-&gt;quote($user['fullname']), $db-&gt;quote($user['username']),  $db-&gt;quote($user['email']), $db-&gt;quote($user['password']));
  
                  // Prepare the insert query.
                  $query
                      -&gt;insert($db-&gt;quoteName('#__users'))
                      -&gt;columns($db-&gt;quoteName($columns))
                      -&gt;values(implode(',', $values));
  
                  // Set the query using our newly populated query object and execute it.
                  $db-&gt;setQuery($query);
                  $db-&gt;query();
                  $user_id = $db-&gt;insertid();
  
                  if($user_id &gt;&gt; 0)
                  {
                      /************* Insert user into user_usergroup_map table *************/
  
                      foreach($user['groups'] AS $group_id)
                      {
                          $columns    = NULL;
                          $values     = NULL;
                          $query = $db-&gt;getQuery(true);
  
                          // Insert columns.
                          $columns = array('user_id', 'group_id');
  
                          // Insert values.
                          $values = array($db-&gt;quote($user_id), $db-&gt;quote($group_id));
  
                          // Prepare the insert query.
                          $query
                              -&gt;insert($db-&gt;quoteName('#__user_usergroup_map'))
                              -&gt;columns($db-&gt;quoteName($columns))
                              -&gt;values(implode(',', $values));
  
                          // Set the query using our newly populated query object and execute it.
                          $db-&gt;setQuery($query);
                          $db-&gt;query();
                      }
                  //$instance = $this-&gt;_getUser($user, $options);
                  }
              }
  
              // $instance = JFactory::getUser($user_id); // works
          }
      }?&gt;
  </code></pre>
  
  <p>Thank you for every help!</p>
  
  <p>Best regards
  Stefan</p>