Skip to content
CallbackHandler.php 2.15 KiB
Newer Older
<?php

/* ----------------------------------------------------------------------------
 *               INAF - National Institute for Astrophysics
 *               IRA  - Radioastronomical Institute - Bologna
 *               OATS - Astronomical Observatory - Trieste
 * ----------------------------------------------------------------------------
 *
 * Copyright (C) 2016 Istituto Nazionale di Astrofisica
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License Version 3 as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

namespace RAP;

class CallbackHandler {

    /**
     * returns null if the callback URL is not listed in configuration file.
     */
    public static function getCallbackTitle($callbackURL) {

        if ($callbackURL === null) {
            return "Account Management";
        }

        global $CALLBACKS;

        foreach ($CALLBACKS as $callback) {
            if ($callback['url'] === $callbackURL) {
                return $callback['title'];
            }
        }

        throw new \Exception("Unauthorized callback URL");
    }

    public static function manageLoginRedirect($user) {

        global $BASE_PATH, $session, $log;
        if ($session->getCallbackURL() !== null) {
            // External login using token
            $token = Util::createNewToken();
            DAO::get()->createLoginToken($token, $user->id);
            header('Location: ' . $session->getCallbackURL() . '?token=' . $token);
        } else {
            // Login in session
            $session->user = $user;
            $session->save();
            // Return to index
            header('Location: ' . $BASE_PATH);
        }
    }

}