/**
  * Register new Diamante User and grant API access for it.
  * Sends confirmation email. While registration is not confirmed API access is not active
  * @param Command\RegisterCommand $command
  * @return void
  */
 public function register(Command\RegisterCommand $command)
 {
     $diamanteUser = $this->diamanteUserFactory->create($command->email, $command->firstName, $command->lastName);
     $apiUser = $this->apiUserFactory->create($command->email, $command->password);
     if ($this->diamanteUserRepository->findUserByEmail($command->email)) {
         throw new \RuntimeException('An account with this email address already exists');
     }
     $diamanteUser->setApiUser($apiUser);
     $this->diamanteUserRepository->store($diamanteUser);
     $this->registrationMailer->sendConfirmationEmail($diamanteUser->getEmail(), $apiUser->getHash());
 }
 public function testResetPassword()
 {
     $emailAddress = '*****@*****.**';
     $diamanteUser = new DiamanteUser($emailAddress, null, 'firstName', 'lastName');
     $apiUser = new ApiUser($emailAddress, null);
     $this->diamanteUserRepository->expects($this->once())->method('findUserByEmail')->with($this->equalTo($emailAddress))->will($this->returnValue($diamanteUser));
     $this->apiUserRepository->expects($this->once())->method('findUserByEmail')->with($this->equalTo($emailAddress))->will($this->returnValue($apiUser));
     $this->apiUserRepository->expects($this->once())->method('store')->with($apiUser);
     $this->resetPasswordMailer->expects($this->once())->method('sendResetEmail')->with($emailAddress, $apiUser->getHash());
     $command = new ResetPasswordCommand();
     $command->email = $emailAddress;
     $this->resetPasswordService->resetPassword($command);
 }
 public function testRegister()
 {
     $apiUser = $this->createApiUser();
     $diamanteUser = $this->createDiamanteUser();
     $this->diamanteUserFactory->expects($this->once())->method('create')->with($diamanteUser->getEmail(), $diamanteUser->getFirstName(), $diamanteUser->getLastName())->will($this->returnValue($diamanteUser));
     $this->apiUserFactory->expects($this->once())->method('create')->with($apiUser->getEmail(), $apiUser->getPassword())->will($this->returnValue($apiUser));
     $this->diamanteUserRepository->expects($this->once())->method('store')->with($diamanteUser);
     $this->registrationMailer->expects($this->once())->method('sendConfirmationEmail')->with($diamanteUser->getEmail(), $apiUser->getHash());
     $command = new RegisterCommand();
     $command->email = $diamanteUser->getEmail();
     $command->password = $apiUser->getPassword();
     $command->firstName = $diamanteUser->getFirstName();
     $command->lastName = $diamanteUser->getLastName();
     $this->service->register($command);
 }
 /**
  * @param DiamanteUser $user
  */
 protected function restoreUser(DiamanteUser $user)
 {
     $user->setDeleted(false);
     $user->updateTimestamp();
     $this->diamanteUserRepository->store($user);
     $this->resetPassword(new User($user->getId(), User::TYPE_DIAMANTE));
 }
 /**
  * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException
  * @expectedExceptionMessage Attempt of unauthorized access
  */
 public function testGetCurrentUserWithNoDiamanteUser()
 {
     $apiUser = $this->createApiUser();
     $this->authorizationService->expects($this->once())->method('getLoggedUser')->will($this->returnValue($apiUser));
     $this->diamanteUserRepository->expects($this->once())->method('findUserByEmail')->with($apiUser->getEmail())->will($this->returnValue(null));
     $this->service->getCurrentUser();
 }
 /**
  * Gets search results, that includes found items and any additional information.
  *
  * @param string $query
  * @param int $page
  * @param int $perPage
  * @param bool $searchById
  * @return array
  * @TODO: Refactor
  */
 public function search($query, $page, $perPage, $searchById = false)
 {
     $items = array();
     if ($searchById) {
         $idParts = explode(User::DELIMITER, $query);
         if (count($idParts) === 2) {
             $query = $idParts[1];
             if ($idParts[0] == User::TYPE_DIAMANTE) {
                 $isDiamanteUserSearch = true;
             }
         }
     }
     if ($searchById && isset($isDiamanteUserSearch)) {
         $diamanteUsers = [$this->diamanteUserRepository->get($query)];
     } else {
         $diamanteUsers = $this->diamanteUserRepository->searchByInput($query, $this->properties);
     }
     if (!isset($isDiamanteUserSearch)) {
         $oroUsers = $this->oroUserSearchHandler->search($query, $page, $perPage, $searchById);
     }
     if (!empty($diamanteUsers)) {
         $convertedDiamanteUsers = $this->convertUsers($diamanteUsers, User::TYPE_DIAMANTE);
         $items = array_merge($items, $convertedDiamanteUsers);
     }
     if (!empty($oroUsers['results'])) {
         $convertedOroUsers = $this->convertUsers($oroUsers['results'], User::TYPE_ORO);
         $items = array_merge($items, $convertedOroUsers);
     }
     return array('results' => $items, 'more' => false);
 }
 public function testNotifyWithEmptyAuthor()
 {
     $ticketUniqueId = UniqueId::generate();
     $reporter = new UserAdapter(1, UserAdapter::TYPE_DIAMANTE);
     $assignee = new User();
     $assignee->setId(2);
     $assignee->setEmail('*****@*****.**');
     $author = null;
     $branch = new Branch('KEY', 'Name', 'Description');
     $ticket = new Ticket($ticketUniqueId, new TicketSequenceNumber(1), 'Subject', 'Description', $branch, $reporter, $assignee, new Source(Source::WEB), new Priority(Priority::PRIORITY_MEDIUM), new Status(Status::NEW_ONE));
     $notification = new TicketNotification((string) $ticketUniqueId, $author, 'Header', 'Subject', new \ArrayIterator(array('key' => 'value')), array('file.ext'));
     $message = new \Swift_Message();
     $this->watchersService->expects($this->once())->method('getWatchers')->will($this->returnValue([new WatcherList($ticket, 'diamante_1')]));
     $this->diamanteUserRepository->expects($this->exactly(2))->method('get')->with(1)->will($this->returnValue($this->diamanteUser));
     $this->configManager->expects($this->once())->method('get')->will($this->returnValue('*****@*****.**'));
     $this->nameFormatter->expects($this->once())->method('format')->with($this->diamanteUser)->will($this->returnValue('First Last'));
     $this->mailer->expects($this->once())->method('createMessage')->will($this->returnValue($message));
     $this->ticketRepository->expects($this->once())->method('getByUniqueId')->with($ticketUniqueId)->will($this->returnValue($ticket));
     $this->templateResolver->expects($this->any())->method('resolve')->will($this->returnValueMap(array(array($notification, TemplateResolver::TYPE_TXT, 'txt.template.html'), array($notification, TemplateResolver::TYPE_HTML, 'html.template.html'))));
     $optionsConstraint = $this->logicalAnd($this->arrayHasKey('changes'), $this->arrayHasKey('attachments'), $this->arrayHasKey('user'), $this->arrayHasKey('header'), $this->contains($notification->getChangeList()), $this->contains($notification->getAttachments()), $this->contains('First Last'), $this->contains($notification->getHeaderText()));
     $this->twig->expects($this->at(0))->method('render')->with('txt.template.html', $optionsConstraint)->will($this->returnValue('Rendered TXT template'));
     $this->twig->expects($this->at(1))->method('render')->with('html.template.html', $optionsConstraint)->will($this->returnValue('Rendered HTML template'));
     $this->mailer->expects($this->once())->method('send')->with($this->logicalAnd($this->isInstanceOf('\\Swift_Message'), $this->callback(function (\Swift_Message $other) use($notification) {
         $to = $other->getTo();
         return false !== strpos($other->getSubject(), $notification->getSubject()) && false !== strpos($other->getSubject(), 'KEY-1') && false !== strpos($other->getBody(), 'Rendered TXT template') && array_key_exists('*****@*****.**', $to) && $other->getHeaders()->has('References') && false !== strpos($other->getHeaders()->get('References'), '*****@*****.**') && false !== strpos($other->getHeaders()->get('References'), '*****@*****.**');
     })));
     $this->messageReferenceRepository->expects($this->once())->method('findAllByTicket')->with($ticket)->will($this->returnValue(array(new MessageReference('*****@*****.**', $ticket), new MessageReference('*****@*****.**', $ticket))));
     $this->messageReferenceRepository->expects($this->once())->method('store')->with($this->logicalAnd($this->isInstanceOf('\\Diamante\\DeskBundle\\Model\\Ticket\\EmailProcessing\\MessageReference')));
     $notifier = new EmailNotifier($this->container, $this->twig, $this->mailer, $this->templateResolver, $this->ticketRepository, $this->messageReferenceRepository, $this->userService, $this->nameFormatter, $this->diamanteUserRepository, $this->configManager, $this->oroUserManager, $this->watchersService, $this->senderHost);
     $notifier->notify($notification);
 }
 /**
  * @param Message $message
  * @param Ticket $ticket
  */
 private function processWatchers(Message $message, $ticket)
 {
     if (!$ticket) {
         return;
     }
     /** @var Message\MessageRecipient $recipient */
     foreach ($message->getRecipients() as $recipient) {
         $email = $recipient->getEmail();
         if ($email == $this->configManager->get(self::EMAIL_NOTIFIER_CONFIG_PATH)) {
             continue;
         }
         $diamanteUser = $this->diamanteUserRepository->findUserByEmail($email);
         $oroUser = $this->oroUserManager->findUserByEmail($email);
         if ($oroUser) {
             $user = new User($oroUser->getId(), User::TYPE_ORO);
         } elseif ($diamanteUser) {
             $user = new User($diamanteUser->getId(), User::TYPE_DIAMANTE);
         } else {
             $diamanteUser = $this->diamanteUserFactory->create($email, $recipient->getFirstName(), $recipient->getLastName());
             $this->diamanteUserRepository->store($diamanteUser);
             $user = new User($diamanteUser->getId(), User::TYPE_DIAMANTE);
         }
         $this->watchersService->addWatcher($ticket, $user);
     }
 }
 /**
  * @param ApiUser $apiUser
  * @return DiamanteUser
  */
 private function loadDiamanteUser(ApiUser $apiUser)
 {
     $diamanteUser = $this->diamanteUserRepository->findUserByEmail($apiUser->getEmail());
     if (is_null($diamanteUser)) {
         throw new \RuntimeException('User loading failed, user not found.');
     }
     return $diamanteUser;
 }
 /**
  * @param Command\RegisterCommand $command
  * @param DiamanteUser $user
  */
 protected function restoreUser(Command\RegisterCommand $command, DiamanteUser $user)
 {
     $user->setEmail($command->email);
     $user->setFirstName($command->firstName);
     $user->setLastName($command->lastName);
     $user->setDeleted(false);
     $user->getApiUser()->setPassword($command->password);
     $this->diamanteUserRepository->store($user);
 }
 public function testProcessWhenMessageWithReference()
 {
     $message = new Message(self::DUMMY_UNIQUE_ID, self::DUMMY_MESSAGE_ID, self::DUMMY_SUBJECT, self::DUMMY_CONTENT, $this->getDummyFrom(), self::DUMMY_MESSAGE_TO, self::DUMMY_REFERENCE);
     $diamanteUser = $this->getDiamanteUser();
     $this->diamanteUserRepository->expects($this->once())->method('findUserByEmail')->with($this->equalTo(self::DUMMY_MESSAGE_FROM))->will($this->returnValue($diamanteUser));
     $reporter = $this->getReporter($diamanteUser->getId());
     $this->messageReferenceService->expects($this->once())->method('createCommentForTicket')->with($this->equalTo($message->getContent()), $reporter, $message->getReference());
     $this->ticketStrategy->process($message);
 }
 /**
  * @param ResetPasswordCommand $command
  * @return void
  * @throws \RuntimeException if given emailAddres is not equal to generated one for user
  */
 public function resetPassword(ResetPasswordCommand $command)
 {
     /**
      * @var DiamanteUser $diamanteUser
      */
     $diamanteUser = $this->diamanteUserRepository->findUserByEmail($command->email);
     if (is_null($diamanteUser)) {
         throw new \RuntimeException('No accounts with that email found.');
     }
     /**
      * @var ApiUser $apiUser
      */
     $apiUser = $this->apiUserRepository->findUserByEmail($command->email);
     if (is_null($apiUser)) {
         $apiUser = $this->apiUserFactory->create($command->email, sha1(microtime(true), true));
     }
     $apiUser->generateHash();
     $this->apiUserRepository->store($apiUser);
     $this->resetPasswordMailer->sendResetEmail($diamanteUser->getEmail(), $apiUser->getHash());
 }
 /**
  * @param Notification $notification
  * @param Ticket $ticket
  *
  * @return string
  */
 private function getFormattedUserName(Notification $notification, Ticket $ticket)
 {
     $author = $notification->getAuthor();
     if (is_null($author)) {
         $reporterId = $ticket->getReporter()->getId();
         $user = $this->diamanteUserRepository->get($reporterId);
     } else {
         $user = $this->getUserDependingOnType($author);
     }
     $name = $this->nameFormatter->format($user);
     if (empty($name)) {
         $format = $this->nameFormatter->getNameFormat();
         $name = str_replace(array('%first_name%', '%last_name%', '%prefix%', '%middle_name%', '%suffix%'), array($user->getFirstName(), $user->getLastName(), '', '', ''), $format);
     }
     $name = preg_replace('/\\s+/', ' ', $name);
     return trim($name);
 }
 /**
  * @test
  */
 public function testSearchWithNotEmptyQuery()
 {
     $query = 'Name';
     $expectedDiamanteUsers = 1;
     $expectedOroUsers = 3;
     $totalExpectedResult = $expectedDiamanteUsers + $expectedOroUsers;
     $this->userService->expects($this->once())->method('getGravatarLink')->with($this->equalTo('*****@*****.**', DiamanteUserSearchHandler::AVATAR_SIZE));
     $this->diamanteUserRepository->expects($this->once())->method('searchByInput')->with($this->equalTo($query), $this->equalTo($this->getProperties()))->will($this->returnValue($this->getDiamanteUsersCollection($expectedDiamanteUsers, $query)));
     $this->userSearchHandler->expects($this->once())->method('search')->with($this->equalTo($query), 1, 10)->will($this->returnValue(array('results' => $this->getOroUsersCollection($expectedOroUsers, $query), 'more' => false)));
     $result = $this->diamanteUserSearchHandler->search($query, 1, 10);
     $this->assertInternalType('array', $result);
     $this->assertTrue(array_key_exists('results', $result));
     $this->assertEquals($totalExpectedResult, count($result['results']));
     foreach ($result['results'] as $item) {
         $this->assertStringEndsWith($query, $item['firstName']);
     }
 }
 /**
  * @test
  * @expectedException \RuntimeException
  * @expectedExceptionMessage User loading failed. User not found
  */
 public function testThrowsExceptionIfUserNotFound()
 {
     $userValueObject = new User(1, User::TYPE_DIAMANTE);
     $this->diamanteUserRepository->expects($this->once())->method('get')->with($this->equalTo($userValueObject->getId()))->will($this->returnValue(null));
     $this->diamanteUserService->getByUser($userValueObject);
 }