Search code examples
phpxmljoomla

email as username to login joomla 3.4


I can't set an email to perform a login in my site. I create a plugin authentication to override the existing one where I change the SQL query to select id and password:

$query = $db->getQuery(true)
    ->select('id, password')
    ->from('#__users')
    ->where('**email**=' . $db->quote($credentials['username']));

I also tried to modify login.xml as below:

 <field name="username"
        type="email"
        class="validate-email"
        filter="email"
        label="COM_USERS_LOGIN_EMAIL_LABEL"
        size="25"
        required="true"
        validate="email"
        />

When I try to login I get:

You can't access to the private section of the site

Can someone help me?


Solution

  • I solved, and i want to share my solution: you don't need to modify joomla.php of authentication plugin.

    You have to modify controller user.php in /components/com_users/controllers as below:

    $data['email']  = $input->$method->get('email', '', 'EMAIL');
    $db    = JFactory::getDbo();
    $query = $db->getQuery(true)
        ->select('username')
        ->from('#__users')
        ->where('email=' . $db->quote($data['email']));
    $db->setQuery($query);
    $utente = $db->loadResult();
    $data['username'] = $utente;
    

    In this way you select the correct username using the email passed in the form.

    Now you can modify the form of login (/components/com_users/models/form/login.xml changing all occurences of "username" with "email" (see below):

        <field
                name="email"
                type="email"
                class="validate-email"
                filter="email"
                label="E-mail"
                size="25"
                required="true"
                validate="email"
        />
    

    then u have to modify jmessage in case of login failed in your language files.

    That's all!

    (for post-login redirection, adding the Itemid in the url it backs to work....)

    In these way I edited core files and i don't like. is there anyway to make a good override of com_users?