API15

API15:JApplication/login

From Joomla! Documentation

Revision as of 21:51, 22 March 2010 by Doxiki (talk | contribs) (New page: ===Description=== Login authentication function. <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>Edit Descripton<nowiki>]</nowik...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Description

Login authentication function.

[Edit Descripton]

Template:Description:JApplication/login

Syntax

login($credentials, $options=array())
Parameter Name Default Value Description
$credentials Array('username' => string, 'password' => string)
$options array() Array('remember' => boolean)

Returns

boolean True on success.

Defined in

libraries/joomla/application/application.php

Importing

jimport( 'joomla.application.application' );

Source Body

public function login($credentials, $options = array())
{
        // Get the global JAuthentication object.
        jimport('joomla.user.authentication');

        $authenticate = & JAuthentication::getInstance();
        $response         = $authenticate->authenticate($credentials, $options);

        if ($response->status === JAUTHENTICATE_STATUS_SUCCESS)
        {
                // Import the user plugin group.
                JPluginHelper::importPlugin('user');

                // OK, the credentials are authenticated.  Lets fire the onLogin event.
                $results = $this->triggerEvent('onLoginUser', array((array)$response, $options));

                /*
                 * If any of the user plugins did not successfully complete the login routine
                 * then the whole method fails.
                 *
                 * Any errors raised should be done in the plugin as this provides the ability
                 * to provide much more information about why the routine may have failed.
                 */

                if (!in_array(false, $results, true))
                {
                        // Set the remember me cookie if enabled.
                        if (isset($options['remember']) && $options['remember'])
                        {
                                jimport('joomla.utilities.simplecrypt');
                                jimport('joomla.utilities.utility');

                                // Create the encryption key, apply extra hardening using the user agent string.
                                $key = JUtility::getHash(@$_SERVER['HTTP_USER_AGENT']);

                                $crypt = new JSimpleCrypt($key);
                                $rcookie = $crypt->encrypt(serialize($credentials));
                                $lifetime = time() + 365*24*60*60;

                                // Use domain and path set in config for cookie if it exists.
                                $cookie_domain = $this->getCfg('cookie_domain', '');
                                $cookie_path = $this->getCfg('cookie_path', '/');
                                setcookie( JUtility::getHash('JLOGIN_REMEMBER'), $rcookie, $lifetime, $cookie_path, $cookie_domain );
                        }
                        return true;
                }
        }

        // Trigger onLoginFailure Event.
        $this->triggerEvent('onLoginFailure', array((array)$response));


        // If silent is set, just return false.
        if (isset($options['silent']) && $options['silent']) {
                return false;
        }

        // Return the error.
        return JError::raiseWarning('SOME_ERROR_CODE', JText::_('E_LOGIN_AUTHENTICATE'));
}

[Edit See Also] Template:SeeAlso:JApplication/login

Examples

<CodeExamplesForm />