<?php

/**
 * Switches the current session user to $userId (requires permission!)
 *
 * @param int $userId - Slave User ID
 *
 * @throws QUI\Exception
 */

use QUI\MasterLogin\Manager;
use QUI\MasterLogin\PermissionDeniedException;

QUI::getAjax()->registerFunction(
    'package_quiqqer_masterlogin_ajax_frontend_logInAs',
    function ($userId) {
        Manager::checkUsePermission();

        try {
            Manager::logInAs($userId);
        } catch (PermissionDeniedException $Exception) {
            throw $Exception;
        } catch (Exception $Exception) {
            QUI\System\Log::writeException($Exception);
        }
    },
    ['userId'],
    'Permission::checkUser'
);
