Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
Add generic LdapUser class that supports custom properties via __call.
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann committed May 30, 2015
1 parent 5dd6178 commit 314d51f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 85 deletions.
94 changes: 94 additions & 0 deletions src/Security/Core/User/LdapUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/*
* This file is part of the LdapAuthentication service provider.
*
* (c) Martin Rademacher <mano@radebatz.net>
*
* 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);
}

}
2 changes: 1 addition & 1 deletion tests/LdapAuthTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
84 changes: 0 additions & 84 deletions tests/Security/Core/User/CustomUser.php

This file was deleted.

0 comments on commit 314d51f

Please sign in to comment.