<?php

/**
 * This example file implements a custom controller method to allow integrators
 * to customize the server response after logout, as in a single logout scenario. As
 * written, this would be uploaded to the /dav/cp/customer/development/controllers
 * directory, and then accessed in the browser or via AJAX call to the following URL:
 * https://<myinterface>/cc/slo/logout
 *
 * If customization is not required, SSO administrators may use the standard OpenLogin
 * controller's logout method at https://<myinterface>/ci/openlogin/logout.
 *
 * The sample code in this document or accessed through this document is not certified
 * or supported by Oracle. It is intended for educational or testing purposes only.
 * Use of this sample code implies acceptance of the License Agreement located at
 * http://www.oracle.com/technetwork/licenses/standard-license-152015.html
 *
 * Original release: September 26, 2023
 */

namespace Custom\Controllers;

use RightNow\Utils\Config,
    RightNow\Utils\Framework,
    RightNow\Utils\Url;

class SLO extends \RightNow\Controllers\Base
{
    function logout () {
        Url::redirectToHttpsIfNecessary();
        /**
         * Call the contact model's doLogout method to perform the logout.
         * That method returns success if the contact wasn't logged in.
         * Check Framework::isLoggedIn() if you need to send a different response in that case.
         */
        $result = $this->model('Contact')->doLogout(Url::getOriginalUrl())->result;
        if ($result['success'] === 1) {
            // Do something on successful logout here, for example construct a LogoutResponse
            // and POST it to the IDP. Note that your choice of SP metadata is arbitrary;
            // it must simply match the configuration in your IDP.
        } else {
            // Logout failed.
            header("HTTP/1.1 418 I'm a teapot");
            exit(Config::getMessage(ERR_WERENT_EXPECTING_HAPPENED_SORRY_MSG));
        }

        /**
         * For testing/validation only: In dev mode, redirect to account overview page.
         * If logout worked, we will be immediately redirected to the login page at
         * CP_LOGIN_URL (or, if PTA_ENABLED is true, PTA_EXTERNAL_LOGIN_URL)
         */
        if (IS_DEVELOPMENT) Framework::setLocationHeader('/app/account/overview');
    }
}