示例#1
0
 public function listAction(Application $app, Request $request)
 {
     $order_by = $request->get('order_by') ?: 'name';
     $order_dir = $request->get('order_dir') == 'DESC' ? 'DESC' : 'ASC';
     $limit = (int) ($request->get('limit') ?: 50);
     $page = (int) ($request->get('page') ?: 1);
     $offset = ($page - 1) * $limit;
     $criteria = array();
     if (!$app['security']->isGranted('ROLE_ADMIN')) {
         $criteria['isEnabled'] = true;
     }
     $users = $this->userManager->findBy($criteria, array('limit' => array($offset, $limit), 'order_by' => array($order_by, $order_dir)));
     $numResults = $this->userManager->findCount($criteria);
     $paginator = new Paginator($numResults, $limit, $page, $app['url_generator']->generate('user.list') . '?page=(:num)&limit=' . $limit . '&order_by=' . $order_by . '&order_dir=' . $order_dir);
     foreach ($users as $user) {
         $user->imageUrl = $this->getGravatarUrl($user->getEmail(), 40);
     }
     return $app['twig']->render($this->getTemplate('list'), array('layout_template' => $this->getTemplate('layout'), 'users' => $users, 'paginator' => $paginator));
 }
示例#2
0
 /**
  * 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', 'userColumns' => array('id' => 'id', 'email' => 'email', 'password' => 'password', 'salt' => 'salt', 'roles' => 'roles', 'name' => 'name', 'time_created' => 'time_created', 'username' => 'username', 'isEnabled' => 'isEnabled', 'confirmationToken' => 'confirmationToken', 'timePasswordResetRequested' => 'timePasswordResetRequested', 'user_id' => 'user_id', 'attribute' => 'attribute', 'value' => 'value'), 'userRoles' => array('ROLE_USER', 'ROLE_ADMIN'));
     // 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'];
             }
             // No duplicates allowed
             $options['userRoles'] = array_unique($options['userRoles']);
         }
         $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['db'], $app);
         $userManager->setUsernameRequired($app['user.options']['isUsernameRequired']);
         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->setUserRoles($app['user.options']['userRoles']);
         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'], $app['config']['baseUrl']);
         $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;
         }
     });
 }