Created
April 23, 2013 07:05
-
-
Save dbu/5441411 to your computer and use it in GitHub Desktop.
confluence soap user provider
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 Acme\UserBundle\Security\User; | |
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; | |
use Symfony\Component\Security\Core\User\UserProviderInterface; | |
use Symfony\Component\Security\Core\User\UserInterface as SecurityUserInterface; | |
use Symfony\Component\Validator\Validator; | |
class ConfluenceUserProvider implements UserProviderInterface | |
{ | |
protected $userProvider; | |
protected $validator; | |
protected $wsdlUrl; | |
/** | |
* Create the provider the url to the soap end point will look something like this: | |
* http://confluence.host.tld:8081/rpc/soap-axis/confluenceservice-v1?wsdl | |
* | |
* @param UserProviderInterface $userProvider | |
* @param Validator $validator the symfony validator service | |
* @param string $wsdlUrl location of your confluence install | |
*/ | |
public function __construct(UserProviderInterface $userProvider, $validator, $wsdlUrl) | |
{ | |
$this->userProvider = $userProvider; | |
$this->validator = $validator; | |
$this->wsdlUrl = $wsdlUrl; | |
} | |
/** | |
* Loads a user by username (and confluenceToken) | |
* | |
* It is strongly discouraged to call this method manually as it bypasses | |
* all ACL checks. | |
* | |
* @param string $username | |
* @param string $confluenceToken | |
* @return UserInterface | |
*/ | |
public function loadUser($username, $confluenceToken) | |
{ | |
try { | |
return $user = $this->userProvider->loadUserByUsername($username); | |
} catch (UsernameNotFoundException $e) { | |
$user = $this->userProvider->createUser(); | |
$user->setEnabled(true); | |
$user->setPassword(''); | |
$user->setAlgorithm(''); | |
$user->setUsername($username); | |
$user->addRole('ROLE_LIIPER'); | |
$userInfo = $this->getConfluenceData($username, $confluenceToken); | |
$user->setAdditionalData($userInfo); | |
$validation = $this->validator->validate($user); | |
if (count($validation)) { | |
throw new UsernameNotFoundException('Confluence user could not be stored'); | |
} | |
$this->userProvider->updateUser($user); | |
} | |
if (empty($user)) { | |
throw new UsernameNotFoundException('Confluence user not found'); | |
} | |
return $user; | |
} | |
public function loadUserByUsername($username) | |
{ | |
return $this->userProvider->loadUserByUsername($username); | |
} | |
public function refreshUser(SecurityUserInterface $user) | |
{ | |
return $this->userProvider->refreshUser($user); | |
} | |
public function supportsClass($class) | |
{ | |
return $this->userProvider->supportsClass($class); | |
} | |
protected function getConfluenceData($username, $confluenceToken) | |
{ | |
$client = new \SoapClient($this->wsdlUrl); | |
try { | |
return $client->getUser($confluenceToken, $username); | |
} catch (\Exception $e) { | |
throw new UsernameNotFoundException('Could not get additional user info'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment