Back to CMSApplication class

Method logout

public bool
logout
(mixed $userid = null, mixed $options = array())
Logout authentication function.
Parameters
  • int $userid The user to load - Can be an integer or string - If string, it is converted to ID automatically
  • array $options Array('clientid' => array of client id's)
Returns
  • bool True on success
Since
  • 3.2

Method logout - Source code

/**
 * Logout authentication function.
 *
 * Passed the current user information to the onUserLogout event and reverts the current
 * session record back to 'anonymous' parameters.
 * If any of the authentication plugins did not successfully complete
 * the logout routine then the whole method fails. Any errors raised
 * should be done in the plugin as this provides the ability to give
 * much more information about why the routine may have failed.
 *
 * @param   integer  $userid   The user to load - Can be an integer or string - If string, it is converted to ID automatically
 * @param   array    $options  Array('clientid' => array of client id's)
 *
 * @return  boolean  True on success
 *
 * @since   3.2
 */
public function logout($userid = null, $options = array())
{
    // Get a user object from the Application.
    $user = Factory::getUser($userid);
    // Build the credentials array.
    $parameters['username'] = $user->get('username');
    $parameters['id'] = $user->get('id');
    // Set clientid in the options array if it hasn't been set already and shared sessions are not enabled.
    if (!$this->get('shared_session', '0') && !isset($options['clientid'])) {
        $options['clientid'] = $this->getClientId();
    }
    // Import the user plugin group.
    PluginHelper::importPlugin('user');
    // OK, the credentials are built. Lets fire the onLogout event.
    $results = $this->triggerEvent('onUserLogout', array($parameters, $options));
    // Check if any of the plugins failed. If none did, success.
    if (!\in_array(false, $results, true)) {
        $options['username'] = $user->get('username');
        $this->triggerEvent('onUserAfterLogout', array($options));
        return true;
    }
    // Trigger onUserLogoutFailure Event.
    $this->triggerEvent('onUserLogoutFailure', array($parameters));
    return false;
}