From 314d51fa1c0fa0d0b0d7dd419e35855c7831fade Mon Sep 17 00:00:00 2001 From: DerManoMann Date: Sat, 30 May 2015 21:31:59 +1200 Subject: [PATCH] Add generic LdapUser class that supports custom properties via __call. --- src/Security/Core/User/LdapUser.php | 94 +++++++++++++++++++++++++ tests/LdapAuthTestCase.php | 2 +- tests/Security/Core/User/CustomUser.php | 84 ---------------------- 3 files changed, 95 insertions(+), 85 deletions(-) create mode 100644 src/Security/Core/User/LdapUser.php delete mode 100644 tests/Security/Core/User/CustomUser.php diff --git a/src/Security/Core/User/LdapUser.php b/src/Security/Core/User/LdapUser.php new file mode 100644 index 0000000..03e4e37 --- /dev/null +++ b/src/Security/Core/User/LdapUser.php @@ -0,0 +1,94 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Radebatz\Silex\LdapAuth\Security\Core\User; + +use Symfony\Component\Security\Core\User\UserInterface; + +/** + * Ldap user class that supports custom properties via magic methods. + */ +class LdapUser implements UserInterface +{ + protected $username; + protected $password; + protected $firstName; + protected $roles; + protected $properties; + + public function __construct($username, $password, array $roles = array()) + { + $this->username = $username; + $this->password = $password; + $this->roles = $roles; + $this->properties = array(); + } + + /** + * {@inheritDoc} + */ + public function getRoles() + { + return $this->roles; + } + + /** + * {@inheritDoc} + */ + public function getPassword() + { + return $this->password; + } + + /** + * {@inheritDoc} + */ + public function getSalt() + { + return; + } + + /** + * {@inheritDoc} + */ + public function getUsername() + { + return $this->username; + } + + /** + * {@inheritDoc} + */ + public function eraseCredentials() + { + } + + /** + * Handle get/set property. + */ + public function __call($method, array $args) + { + $prefix = substr($method, 0, 3); + if ('set' == $prefix && 1 == count($args)) { + $property = lcfirst(substr($method, 3)); + $this->properties[$property] = $args[0]; + + return; + } elseif ('get' == $prefix) { + $property = lcfirst(substr($method, 3)); + + return array_key_exists($property, $this->properties) ? $this->properties[$property] : null; + } + + throw new \RuntimeException('Invalid method: '.$method); + } + +} diff --git a/tests/LdapAuthTestCase.php b/tests/LdapAuthTestCase.php index ab63349..ad0daef 100644 --- a/tests/LdapAuthTestCase.php +++ b/tests/LdapAuthTestCase.php @@ -97,7 +97,7 @@ protected function getOptions() 'CN=Development,OU=Groups,DC=radebatz,DC=net' => 'ROLE_USER', 'CN=Admins,OU=Groups,DC=radebatz,DC=net' => 'ROLE_ADMIN', ), - 'class' => 'Radebatz\Silex\LdapAuth\Tests\Security\Core\User\CustomUser', + 'class' => 'Radebatz\Silex\LdapAuth\Security\Core\User\LdapUser', // just the name :) 'filter' => '%s', 'baseDn' => 'DC=radebatz,DC=net', diff --git a/tests/Security/Core/User/CustomUser.php b/tests/Security/Core/User/CustomUser.php deleted file mode 100644 index 0e96608..0000000 --- a/tests/Security/Core/User/CustomUser.php +++ /dev/null @@ -1,84 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Radebatz\Silex\LdapAuth\Tests\Security\Core\User; - -use Symfony\Component\Security\Core\User\AdvancedUserInterface; - -/** - * Custom user class that supports first name. - */ -class CustomUser implements AdvancedUserInterface -{ - protected $username; - protected $firstName; - protected $roles; - - public function __construct($username, $password, array $roles = array(), $enabled = true, $userNonExpired = true, $credentialsNonExpired = true, $userNonLocked = true) - { - $this->username = $username; - $this->roles = $roles; - } - - public function getRoles() - { - return $this->roles; - } - - public function getPassword() - { - return; - } - - public function getSalt() - { - return; - } - - public function getUsername() - { - return $this->username; - } - - public function eraseCredentials() - { - } - - public function isAccountNonExpired() - { - return true; - } - - public function isAccountNonLocked() - { - return true; - } - - public function isCredentialsNonExpired() - { - return true; - } - - public function isEnabled() - { - return true; - } - - public function setFirstName($firstName) - { - $this->firstName = $firstName; - } - - public function getFirstName() - { - return $this->firstName; - } -}