Example #1
0
 public function testSendAllowsEmptyFrom()
 {
     $this->mailer->setFromAddress(null);
     $this->mailer->setFromName(null);
     $this->mailer->sendConfirmationMessage(new User('*****@*****.**'));
     $this->mailer->setFromAddress('');
     $this->mailer->setFromName('');
     $this->mailer->sendConfirmationMessage(new User('*****@*****.**'));
 }
 /**
  * Registers services on the given app.
  *
  * This method should only be used to configure services and parameters.
  * It should not get services.
  *
  * @param Application $app An Application instance
  */
 public function register(Application $app)
 {
     // Default options.
     $app['user.options.default'] = array('templates' => array('layout' => '@user/layout.twig', 'register' => '@user/register.twig', 'register-confirmation-sent' => '@user/register-confirmation-sent.twig', 'login' => '@user/login.twig', 'login-confirmation-needed' => '@user/login-confirmation-needed.twig', 'forgot-password' => '@user/forgot-password.twig', 'reset-password' => '@user/reset-password.twig', 'view' => '@user/view.twig', 'edit' => '@user/edit.twig', 'list' => '@user/list.twig'), 'mailer' => array('enabled' => true, 'fromEmail' => array('address' => 'do-not-reply@' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : gethostname()), 'name' => null)), 'emailConfirmation' => array('required' => false, 'template' => '@user/email/confirm-email.twig'), 'passwordReset' => array('template' => '@user/email/reset-password.twig', 'tokenTTL' => 86400), 'userClass' => 'SimpleUser\\User', 'isUsernameRequired' => false, 'editCustomFields' => array(), 'userTableName' => 'users', 'userCustomFieldsTableName' => 'user_custom_fields');
     // Initialize $app['user.options'].
     $app['user.options.init'] = $app->protect(function () use($app) {
         $options = $app['user.options.default'];
         if (isset($app['user.options'])) {
             // Merge default and configured options
             $options = array_replace_recursive($options, $app['user.options']);
             // Migrate deprecated options for backward compatibility
             if (isset($app['user.options']['layoutTemplate']) && !isset($app['user.options']['templates']['layout'])) {
                 $options['templates']['layout'] = $app['user.options']['layoutTemplate'];
             }
             if (isset($app['user.options']['loginTemplate']) && !isset($app['user.options']['templates']['login'])) {
                 $options['templates']['login'] = $app['user.options']['loginTemplate'];
             }
             if (isset($app['user.options']['registerTemplate']) && !isset($app['user.options']['templates']['register'])) {
                 $options['templates']['register'] = $app['user.options']['registerTemplate'];
             }
             if (isset($app['user.options']['viewTemplate']) && !isset($app['user.options']['templates']['view'])) {
                 $options['templates']['view'] = $app['user.options']['viewTemplate'];
             }
             if (isset($app['user.options']['editTemplate']) && !isset($app['user.options']['templates']['edit'])) {
                 $options['templates']['edit'] = $app['user.options']['editTemplate'];
             }
             if (isset($app['user.options']['listTemplate']) && !isset($app['user.options']['templates']['list'])) {
                 $options['templates']['list'] = $app['user.options']['listTemplate'];
             }
         }
         $app['user.options'] = $options;
     });
     // Token generator.
     $app['user.tokenGenerator'] = $app->share(function ($app) {
         return new TokenGenerator($app['logger']);
     });
     // User manager.
     $app['user.manager'] = $app->share(function ($app) {
         $app['user.options.init']();
         $userManager = new UserManager($app->entityManager, $app);
         $userManager->setUserClass($app['user.options']['userClass']);
         $userManager->setUsernameRequired($app['user.options']['isUsernameRequired']);
         $userManager->setUserTableName($app['user.options']['userTableName']);
         $userManager->setUserCustomFieldsTableName($app['user.options']['userCustomFieldsTableName']);
         return $userManager;
     });
     // Current user.
     $app['user'] = $app->share(function ($app) {
         return $app['user.manager']->getCurrentUser();
     });
     // User controller service.
     $app['user.controller'] = $app->share(function ($app) {
         $app['user.options.init']();
         $controller = new UserController($app['user.manager']);
         $controller->setUsernameRequired($app['user.options']['isUsernameRequired']);
         $controller->setEmailConfirmationRequired($app['user.options']['emailConfirmation']['required']);
         $controller->setTemplates($app['user.options']['templates']);
         $controller->setEditCustomFields($app['user.options']['editCustomFields']);
         return $controller;
     });
     // User mailer.
     $app['user.mailer'] = $app->share(function ($app) {
         $app['user.options.init']();
         $missingDeps = array();
         if (!isset($app['mailer'])) {
             $missingDeps[] = 'SwiftMailerServiceProvider';
         }
         if (!isset($app['url_generator'])) {
             $missingDeps[] = 'UrlGeneratorServiceProvider';
         }
         if (!isset($app['twig'])) {
             $missingDeps[] = 'TwigServiceProvider';
         }
         if (!empty($missingDeps)) {
             throw new \RuntimeException('To access the SimpleUser mailer you must enable the following missing dependencies: ' . implode(', ', $missingDeps));
         }
         $mailer = new Mailer($app['mailer'], $app['url_generator'], $app['twig']);
         $mailer->setFromAddress($app['user.options']['mailer']['fromEmail']['address'] ?: null);
         $mailer->setFromName($app['user.options']['mailer']['fromEmail']['name'] ?: null);
         $mailer->setConfirmationTemplate($app['user.options']['emailConfirmation']['template']);
         $mailer->setResetTemplate($app['user.options']['passwordReset']['template']);
         $mailer->setResetTokenTtl($app['user.options']['passwordReset']['tokenTTL']);
         if (!$app['user.options']['mailer']['enabled']) {
             $mailer->setNoSend(true);
         }
         return $mailer;
     });
     // Add a custom security voter to support testing user attributes.
     $app['security.voters'] = $app->extend('security.voters', function ($voters) use($app) {
         foreach ($voters as $voter) {
             if ($voter instanceof RoleHierarchyVoter) {
                 $roleHierarchyVoter = $voter;
                 break;
             }
         }
         $voters[] = new EditUserVoter($roleHierarchyVoter);
         return $voters;
     });
     // Helper function to get the last authentication exception thrown for the given request.
     // It does the same thing as $app['security.last_error'](),
     // except it returns the whole exception instead of just $exception->getMessage()
     $app['user.last_auth_exception'] = $app->protect(function (Request $request) {
         if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
             return $request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR);
         }
         $session = $request->getSession();
         if ($session && $session->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
             $exception = $session->get(SecurityContextInterface::AUTHENTICATION_ERROR);
             $session->remove(SecurityContextInterface::AUTHENTICATION_ERROR);
             return $exception;
         }
     });
 }