public function register(Application $app)
 {
     $config = $this->config;
     $app['user'] = null;
     $app['devture_user.roles'] = $config['roles'];
     $app['devture_user.db'] = $app->share(function ($app) use($config) {
         return $app[$config['database_service_id']];
     });
     if ($config['database_type'] === 'relational') {
         $app['devture_user.repository'] = $app->share(function ($app) {
             return new Repository\Relational\UserRepository($app['devture_user.db']);
         });
     } else {
         if ($config['database_type'] === 'mongodb') {
             $app['devture_user.repository'] = $app->share(function ($app) {
                 return new Repository\MongoDB\UserRepository($app['devture_user.db']);
             });
         } else {
             throw new \InvalidArgumentException('Unrecognized database type: ' . $config['database_type']);
         }
     }
     $app['devture_user.password_encoder'] = $app->share(function ($app) use($config) {
         return new Helper\PasswordEncoder($config['blowfish_cost']);
     });
     $app['devture_user.auth_helper'] = $app->share(function ($app) use($config) {
         return new Helper\AuthHelper($app['devture_user.repository'], $app['devture_user.password_encoder'], $config['password_token_salt']);
     });
     $app['devture_user.login_manager'] = $app->share(function ($app) use($config) {
         return new Helper\LoginManager($app['devture_user.auth_helper'], $config['cookie_signing_secret'], $config['cookie_path']);
     });
     $app['devture_user.access_control'] = $app->share(function ($app) {
         return new AccessControl\AccessControl($app);
     });
     $app['devture_user.validator'] = function ($app) {
         return new Validator\UserValidator($app['devture_user.repository'], $app['devture_user.roles']);
     };
     $app['devture_user.form_binder'] = function ($app) {
         $binder = new Form\FormBinder($app['devture_user.validator'], $app['devture_user.password_encoder']);
         $binder->setCsrfProtection($app['devture_framework.csrf_token_manager'], 'user');
         return $binder;
     };
     $app['devture_user.listener.user_from_request_initializer'] = $app->protect(function (Request $request) use($app) {
         $app['user'] = $app['devture_user.login_manager']->createUserFromRequest($request);
     });
     $app['devture_user.listener.csrf_token_manager_salter'] = $app->protect(function (Request $request) use($app) {
         if ($app['user'] instanceof Model\User) {
             $app['devture_framework.csrf_token_manager']->setSalt($app['user']->getUsername());
         }
     });
     $app['devture_user.listener.conditional_session_extender'] = $app->protect(function (Request $request, Response $response) use($app) {
         if ($app['user'] instanceof Model\User) {
             $app['devture_user.login_manager']->extendSessionIfNeeded($app['user'], $request, $response);
         }
     });
     $app['devture_user.twig.user_extension'] = $app->share(function ($app) {
         return new Twig\UserExtension($app['devture_user.access_control'], $app);
     });
     $this->registerConsoleServices($app);
     $this->registerControllerServices($app);
 }
예제 #2
0
 /**
  * @param User $entity
  * @param Request $request
  * @param array $options
  */
 protected function doBindRequest($entity, Request $request, array $options = array())
 {
     parent::doBindRequest($entity, $request, $options);
     //To avoid having to override the user validator as well, we'll do group validation during binding.
     $entity->clearGroups();
     foreach ((array) $request->request->get('groups') as $groupName) {
         if (NameValidator::isValid($groupName)) {
             $entity->addGroup($groupName);
         } else {
             $this->getViolations()->add('groups', 'The group name %name% is not valid.', array('%name%' => $groupName));
         }
     }
 }