示例#1
0
 public function execute(Input $input, Output $output)
 {
     $username = $this->getUsername($input);
     $pwd = $this->getPwd($input);
     $salt = $this->getSalt($input);
     $email = $this->getEmail($input);
     $password = Signature::instance([$username, $pwd], UserInterface::SALT)->toMd5();
     $container = $this->getContainer();
     $event = new BaseEvent();
     $event->setContainer($container);
     try {
         $repository = $event->getParameters('admin-bundle.repository');
         $connection = $event->getParameters('admin-bundle.connection');
         $managerRepository = $event->getConnection($connection)->getRepository($repository);
     } catch (\Exception $e) {
         throw new ServerInternalErrorException('Admin bundle is unconfiguration. Parameters "redirect_url", "repository", "connection"');
     }
     if (!$managerRepository instanceof UserInterface) {
         throw new ServerInternalErrorException(sprintf('Repository implements extends "[%s]"', UserInterface::class));
     }
     $result = $managerRepository->getConnection()->createQuery('select count(1) as total from ' . $managerRepository->getTable() . ' where ' . $managerRepository->getUsernameField() . ' = \'' . $username . '\'')->getQuery()->getOne('total');
     if ($result > 0) {
         $output->writeln(sprintf('User "%s" is exists.', $user[$managerRepository->getUsernameField()]), Output::STYLE_BG_INFO);
         $output->writeln($managerRepository->getUsernameField() . ': ' . $user[$managerRepository->getUsernameField()], Output::STYLE_SUCCESS);
         $output->writeln($managerRepository->getPasswordField() . ': ' . $user[$managerRepository->getPasswordField()], Output::STYLE_SUCCESS);
         $output->writeln($managerRepository->getEmailField() . ': ' . $user[$managerRepository->getEmailField()], Output::STYLE_SUCCESS);
         return 0;
     }
     $data = [$managerRepository->getUsernameField() => $username, $managerRepository->getEmailField() => $email, $managerRepository->getPasswordField() => $password];
     foreach ($data as $key => $value) {
         if (empty($key)) {
             unset($data[$key]);
         }
     }
     if (false !== $managerRepository->insert($data)) {
         $output->writeln($managerRepository->getUsernameField() . ': ' . $username, Output::STYLE_SUCCESS);
         $output->writeln($managerRepository->getPasswordField() . ': ' . $pwd, Output::STYLE_SUCCESS);
         $output->writeln($managerRepository->getEmailField() . ': ' . $email, Output::STYLE_SUCCESS);
         return 0;
     }
     $output->writeln('make fiald. error. ' . json_encode($managerRepository->getErrors()));
     return 1;
 }
示例#2
0
 /**
  * @param Request $request
  * @return array|JsonResponse|\FastD\Http\RedirectResponse
  * @throws ServerInternalErrorException
  */
 public function signInAction(Request $request)
 {
     if (($user = $this->verifyAccountAndPassword($request)) instanceof Response) {
         return $user;
     }
     $referer = $request->header->hasGet('REFERER', null);
     if (!$request->isXmlHttpRequest() && null === $referer) {
         throw new \RuntimeException('Access denied.');
     }
     try {
         $redirectUrl = $this->generateUrl($this->getParameters('admin-bundle.login_url'));
     } catch (\Exception $e) {
         if (!$request->request->has('login_url')) {
             throw new ServerInternalErrorException('redirect_url unconfiguration.');
         }
         $redirectUrl = $request->request->get('login_url');
     }
     $repository = $this->getParameters('admin-bundle.repository');
     $connection = $this->getParameters('admin-bundle.connection');
     $managerRepository = $this->getConnection($connection)->getRepository($repository);
     unset($repository, $connection);
     if (!$managerRepository instanceof UserInterface) {
         throw new ServerInternalErrorException(sprintf('Repository implements extends ["%s"]', UserInterface::class));
     }
     $manager = $managerRepository->find(['OR' => [$managerRepository->getUsernameField() => $user['username'], $managerRepository->getEmailField() => $user['password']]]);
     if (empty($manager)) {
         if ($request->isXmlHttpRequest()) {
             return new JsonResponse(['code' => 10086, 'msg' => 'Access denied.'], Response::HTTP_FORBIDDEN);
         }
         return $this->redirect($referer);
     }
     $sign = Signature::instance($user, $managerRepository::SALT)->toMd5();
     if ($sign !== $manager[$managerRepository->getPasswordField()]) {
         if ($request->isXmlHttpRequest()) {
             return new JsonResponse(['code' => 10087, 'msg' => 'Manger authorization fail.'], Response::HTTP_FORBIDDEN);
         }
         return $this->redirect($redirectUrl);
     }
     unset($manager[$managerRepository->getPasswordField()]);
     $request->setSession('manager', $manager);
     if ($request->isXmlHttpRequest()) {
         return new JsonResponse(['redirect_url' => $redirectUrl]);
     }
     return $this->redirect($redirectUrl);
 }