Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
102 lines (82 sloc) 2.61 KB
<?php
namespace Destruktiv\UserBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class EjabberdCommand extends ContainerAwareCommand
{
private $stdin;
private $stdout;
protected function configure()
{
$this
->setName("destruktiv:user:ejabberd")
->setDescription("Authenticate ejabberd users")
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
while (true) {
$request = $this->read();
$response = false;
switch ($request[0]) {
case "isuser":
$response = $this->exists($request[1]);
break;
case "auth":
$response = $this->authenticate($request[1], $request[3]);
break;
default:
case "setpass":
case "tryregister":
case "removeuser":
case "removeuser3":
// Do nothing
break;
}
$this->write($response);
};
}
/**
* Read the first three bytes to determine the length of the request
* @return array The request
*/
private function read()
{
$length = unpack("n", fgets(STDIN, 3))[1];
if ($length <= 0) {
return;
}
$request = fgets(STDIN, $length + 1);
return explode(":", $request);
}
/**
* @param $response boolean Falsey value to indicate success or error
*/
private function write($response)
{
$response = pack("nn", 2, $response ? 1 : 0);
fwrite(STDOUT, $response);
}
private function exists($username)
{
$userManager = $this->getContainer()->get("fos_user.user_manager");
$user = $userManager->findUserByUsername($username);
if (!$user) {
return false;
}
return true;
}
private function authenticate($username, $password)
{
$userManager = $this->getContainer()->get("fos_user.user_manager");
$user = $userManager->findUserByUsername($username);
if (!$user) {
return false;
}
$encoder = $this->getContainer()->get("security.encoder_factory")->getEncoder($user);
return $encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt());
}
}