Created
August 6, 2018 12:56
-
-
Save datio/23b78c963a48498fecf2d4d3d15163d0 to your computer and use it in GitHub Desktop.
XF2 SSO: XenForo 2 Single Sign On session cookie set up
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Datio\WeightixCli\Cli\Command\Misc; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
class UserLogin extends Command | |
{ | |
protected function configure() | |
{ | |
$this | |
->setName('misc-user:login') | |
->setDescription('Create an authenticated session for a user') | |
->addOption( | |
'user-id', | |
null, | |
InputOption::VALUE_REQUIRED, | |
'User id of user' | |
); | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$userId = $input->getOption('user-id'); | |
$errors = false; | |
$missingArgs = []; | |
if (!$userId) { | |
array_push($missingArgs, 'user-id'); | |
$errors = true; | |
} | |
if ($errors) { | |
$output->writeln(sprintf("<error>Required argument(s): %s.</error>", implode(', ', $missingArgs))); | |
return 1; | |
} | |
/** @var \XF\Entity\User $user */ | |
$user = \XF::app()->finder('XF:User')->where('user_id', $userId)->with(['Profile'])->fetchOne(); | |
if (!$user) { | |
$output->writeln("<error>User could not be found.</error>"); | |
return 1; | |
} | |
$class = \XF::app()->extendClass('XF\Session\Session'); | |
/** @var \XF\Session\Session $session */ | |
$session = new $class(\XF::app()->container('session.public.storage'), [ | |
'cookie' => 'session' | |
]); | |
$session->start(\XF::app()->request()); | |
$session->changeUser($user); | |
$cookieVal = $session->getSessionId(); | |
$session->save(); | |
$output->writeln(sprintf("xf_session: %s", $cookieVal)); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment