src/Biz/User/UserProvider.php line 82

Open in your IDE?
  1. <?php
  2. namespace Biz\User;
  3. use AppBundle\Handler\AuthenticationHelper;
  4. use Biz\Role\Util\PermissionBuilder;
  5. use Biz\User\Service\UserService;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  8. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  9. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Symfony\Component\Security\Core\User\UserProviderInterface;
  12. use Topxia\Service\Common\ServiceKernel;
  13. class UserProvider implements UserProviderInterface
  14. {
  15.     private $container;
  16.     public function __construct(ContainerInterface $container)
  17.     {
  18.         $this->container $container;
  19.     }
  20.     public function loadUserByUsername($username)
  21.     {
  22.         $user $this->getUserService()->getUserByLoginField($username);
  23.         if (empty($user)) {
  24.             throw new UserNotFoundException(sprintf('User "%s" not found.'$username));
  25.         } elseif (isset($user['type']) && 'system' == $user['type']) {
  26.             throw new UserNotFoundException(sprintf('User "%s" not found.'$username));
  27.         }
  28.         $request $this->container->get('request_stack')->getMainRequest();
  29.         $forbidden AuthenticationHelper::checkLoginForbidden($user['id'], $request->getClientIp());
  30.         if ('error' == $forbidden['status']) {
  31.             throw new AuthenticationException($forbidden['message']);
  32.         }
  33.         $forbidden AuthenticationHelper::checkMayday($user$request->request->get('isMayday'0));
  34.         if ('error' == $forbidden['status']) {
  35.             throw new AuthenticationException($forbidden['message']);
  36.         }
  37.         $user $this->getUserService()->getUserWithOrgScopes($user['id']);
  38.         $user['currentIp'] = $request->getClientIp();
  39.         $currentUser = new CurrentUser();
  40.         $currentUser->fromArray($user);
  41.         $currentUser->setPermissions(PermissionBuilder::instance()->findPermissionsByRoles($currentUser->getRoles()));
  42.         $currentUser['isSecure'] = $request->isSecure();
  43.         $biz $this->container->get('biz');
  44.         $biz['user'] = $currentUser;
  45.         ServiceKernel::instance()->setCurrentUser($currentUser);
  46.         return $currentUser;
  47.     }
  48.     public function refreshUser(UserInterface $user)
  49.     {
  50.         if (!$user instanceof CurrentUser) {
  51.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.'get_class($user)));
  52.         }
  53.         return $this->loadUserByUsername($user->getUsername());
  54.     }
  55.     public function supportsClass($class)
  56.     {
  57.         return 'Biz\User\CurrentUser' === $class;
  58.     }
  59.     protected function getRoleService()
  60.     {
  61.         return ServiceKernel::instance()->createService('Role:RoleService');
  62.     }
  63.     /**
  64.      * @return UserService
  65.      */
  66.     protected function getUserService()
  67.     {
  68.         return $this->container->get('biz')->service('User:UserService');
  69.     }
  70.     protected function getOrgService()
  71.     {
  72.         return ServiceKernel::instance()->createService('Org:OrgService');
  73.     }
  74. }