Skip to content

Instantly share code, notes, and snippets.

@NKPmedia
Last active September 11, 2017 21:53
Show Gist options
  • Save NKPmedia/4a6ee55b6bb96e8af409debd98950678 to your computer and use it in GitHub Desktop.
Save NKPmedia/4a6ee55b6bb96e8af409debd98950678 to your computer and use it in GitHub Desktop.
Unit test problem with addUser
class UserRepository implements UserProviderInterface
{
/**
* Add a user to the database
*
* @param User $user
* @return User
*/
public function addUser(User $user)
{
$pbnlAccount = $this->userToEntities($user);
if(!$this->doesUserExist($user)) {
$pbnlAccount->setUidNumber($this->getNewUidNumber());
$this->ldapEntityManager->persist($pbnlAccount);
$this->ldapEntityManager->flush();
}
else {
throw new UserAlreadyExistException("The user ".$user->getUid()." already exists.");
}
return $this->getUserByUid($user->getUid());
}
/**
* Checks if this user already exists
* For this the function uses the uid and the uidNumber
* @param $user
* @return bool
* @throws UserNotUniqueException if there are more than one users with the same uid or uidNumber
*/
private function doesUserExist(User $user)
{
$ldapUserRepo = $this->ldapEntityManager->getRepository(PbnlAccount::class);
$users = $ldapUserRepo->findByUid($user->getUid());
if(count($users) == 1) return true;
if(count($users) > 1) throw new UserNotUniqueException("The user with the uid ".$user->getUid()." is not unique!");
$users = $ldapUserRepo->findByUidNumber($user->getUidNumber());
if(count($users) == 1) return true;
if(count($users) > 1) throw new UserNotUniqueException("The user with the uid ".$user->getUid()." is not unique!");;
return false;
}
/**
* Creates a PbnlAccount with the data of an User object
*
* @param $user
* @return PbnlAccount
*/
private function userToEntities(User $user)
{
$pbnlAccount = new PbnlAccount();
$pbnlAccount->setL($user->getCity());
$pbnlAccount->setOu($user->getStamm());
$pbnlAccount->setStreet($user->getStreet());
$pbnlAccount->setPostalCode($user->getPostalCode());
$pbnlAccount->setGivenName($user->getGivenName());
$pbnlAccount->setUid($user->getUid());
$pbnlAccount->setCn($user->getFirstName());
$pbnlAccount->setSn($user->getLastName());
$pbnlAccount->setMail($user->getMail());
$pbnlAccount->setTelephoneNumber($user->getHomePhoneNumber());
$pbnlAccount->setMobile($user->getMobilePhoneNumber());
$pbnlAccount->setGidNumber("501");
$pbnlAccount->setHomeDirectory("/home/".$user->getUid());
$pbnlAccount->setUidNumber($user->getUidNumber());
$pbnlAccount->setObjectClass(["inetOrgPerson","posixAccount","pbnlAccount"]);
return $pbnlAccount;
}
/**
* Returns the next uidNumber
* Its the highest uidNumber of the pbnlAccounts + 1
* @return int
*/
private function getNewUidNumber()
{
/** @var $users User[]*/
$users = $this->ldapEntityManager->getRepository(PbnlAccount::class)->findAll();
$highesUidNumber = 0;
foreach ($users as $user) {
if($user->getUidNumber() > $highesUidNumber) {
$highesUidNumber = $user->getUidNumber();
}
}
return $highesUidNumber + 1;
}
/**
* Searches the PbnlAccount (ldap) and returns a User
* Find by GivenName
*
* @param string $givenName
* @return User
*/
public function getUserByUid(String $givenName)
{
/** @var Repository $pbnlAccountRepository */
$pbnlAccountRepository = $this->ldapEntityManager->getRepository(PbnlAccount::class);
/** @var PbnlAccount $ldapPbnlAccount[] */
$ldapPbnlAccount = $pbnlAccountRepository->findOneByUid($givenName);
if (count($ldapPbnlAccount) == 0) {
throw new UsernameNotFoundException(
sprintf('Uid "%s" does not exist.', $givenName)
);
}
return $this->entitiesToUser($ldapPbnlAccount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment