vendor/nen/kennisbank-platform/src/Nen/Controller/GebruikersController.php line 117

Open in your IDE?
  1. <?php namespace Nen\Bundle\KennisbankPlatformBundle\Controller;
  2. use Nen\Bundle\KennisbankPlatformBundle\Authentication\NenConnectClient;
  3. use Nen\Bundle\KennisbankPlatformBundle\Authentication\ResponseException;
  4. use App\Entity\User;
  5. use Nen\Bundle\KennisbankPlatformBundle\Exceptions\InvalidLicenseException;
  6. use Nen\Bundle\KennisbankPlatformBundle\Exceptions\UserUnknownException;
  7. use Nen\Bundle\KennisbankPlatformBundle\Exceptions\ValidationException;
  8. use Nen\Bundle\KennisbankPlatformBundle\Exceptions\WithErrorsException;
  9. use Nen\Bundle\KennisbankPlatformBundle\Form\UserType;
  10. use Nen\Bundle\KennisbankPlatformBundle\Handler\ActivationHandler;
  11. use Nen\Bundle\KennisbankPlatformBundle\Handler\AddUserHandler;
  12. use Nen\Bundle\KennisbankPlatformBundle\Handler\ChangeUsernameHandler;
  13. use Nen\Bundle\KennisbankPlatformBundle\Handler\ForgotPasswordHandler;
  14. use Nen\Bundle\KennisbankPlatformBundle\Handler\LicenseRequestHandler;
  15. use Nen\Bundle\KennisbankPlatformBundle\Handler\LoginHandler;
  16. use Nen\Bundle\KennisbankPlatformBundle\Handler\RegistrationHandler;
  17. use Nen\Bundle\KennisbankPlatformBundle\Handler\ResetPasswordHandler;
  18. use Nen\Bundle\KennisbankPlatformBundle\Repository\CompanyRepository;
  19. use Nen\Bundle\KennisbankPlatformBundle\Support\LicenseHelper;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use function Symfony\Component\Translation\t;
  24. /**
  25.  * Gebruikers Controller
  26.  *
  27.  * @author  G.P. Gautier <ggautier@thirdwave.nl>
  28.  * @version 0.5.0, 2015/12/23
  29.  */
  30. class GebruikersController extends SiteController
  31. {
  32.     /**
  33.      * @var bool
  34.      */
  35.     public static $trialIsNotTrial false;
  36.     // /**
  37.     //  * Inloggen
  38.     //  */
  39.     // public function inloggen()
  40.     // {
  41.     //     $handler  = new LoginHandler($this->app);
  42.     //     $continue = $this->getContinueUrl();
  43.     //     $this->response->data['form'] = [
  44.     //       'continue' => $continue,
  45.     //       'username' => $this->request->request->get('username')
  46.     //     ];
  47.     //     if ($this->request->getMethod() !== 'POST') {
  48.     //         return;
  49.     //     }
  50.     //     try {
  51.     //         $handler->handle($this->request->request->all());
  52.     //     } catch (InvalidLicenseException $e) {
  53.     //         $record = User::with('company')->where('username', $this->request->request->get('username'))->first();
  54.     //         if ((int)$record->company->license_trial === 1) {
  55.     //             $this->response->data['record']  = $record;
  56.     //             $this->response->data['expired'] = true;
  57.     //             $this->app['session']->set('expired', $record->id);
  58.     //         } else {
  59.     //             $this->response->data['form']['errors'] = $e->getErrors();
  60.     //         }
  61.     //         return;
  62.     //     } catch (WithErrorsException $e) {
  63.     //         $this->response->data['form']['errors'] = $e->getErrors();
  64.     //         return;
  65.     //     }
  66.     //     $this->response->redirect($continue);
  67.     // }
  68.     // /**
  69.     //  * Uitloggen
  70.     //  */
  71.     // public function uitloggen()
  72.     // {
  73.     //     $this->app['authentication']->logout();
  74.     //     $this->response->redirect($this->app['router']->getRouteUrl('gebruikers_inloggen'));
  75.     // }
  76. //    /**
  77. //     * @Route("/nen-connect", name="gebruikers_nen_connect")
  78. //     */
  79. //    public function nenConnectSso(NenConnectClient $nenConnectClient)
  80. //    {
  81. //        try {
  82. //            $url = $nenConnectClient->singleSingOn($this->getUser()->getUsername());
  83. //        } catch (ResponseException $e) {
  84. //            mail('bugreports@thirdwave.nl', 'NEN Connect SSO Melding', $e->getMessage());
  85. //
  86. //            $this->app['flash']->add('error', $this->translate('Doorverwijzing naar NEN Connect is op dit moment niet beschikbaar.'));
  87. //
  88. //            return new RedirectResponse($this->app['router']->getRouteUrl('home'));
  89. //        }
  90. //
  91. //        return new RedirectResponse($url);
  92. //    }
  93.     /**
  94.      * @Route("/registreren", name="register")
  95.      *
  96.      * Gebruiker registreren
  97.      */
  98.     public function registreren(Request $requestCompanyRepository $companyRepositoryAddUserHandler $addUserHandler)
  99.     {
  100.         $user = new User();
  101.         $form $this->createForm(UserType::class, $user, ['validation_groups' => ['user'], 'type' => 'register']);
  102.         $form->handleRequest($request);
  103.         if ($form->isSubmitted() && $form->isValid()) {
  104.             if ($form->has('hash')) {
  105.                 $hash $form->get('hash')->getData();
  106.             } else {
  107.                 $hash null;
  108.             }
  109.             $company $companyRepository->findOneBy(['licenseCode' => $form->get('license_code')->getData()]);
  110.             if (empty($company)) {
  111.                 $this->addFlash('error't('Kan geen organisatie vinden voor abonnementscode.'));
  112.                 return $this->render('@KennisbankPlatform/pages/register.html.twig', [
  113.                     'form' => $form->createView()
  114.                 ]);
  115.             }
  116.             if (count($company->getUsers()) >= $company->getLicenseAmount()) {
  117.                 $this->addFlash('error't('Kan geen gebruikers meer toevoegen voor deze organisatie.'));
  118.                 return $this->render('@KennisbankPlatform/pages/register.html.twig', [
  119.                     'form' => $form->createView()
  120.                 ]);
  121.             }
  122.             $user->setCompany($company);
  123.             $addUserHandler->handle($user$hash);
  124.             $this->addFlash('success't('U ontvangt een e-mail om uw gebruiker te activeren via %username%.', ['%username%' => $user->getUsername()]));
  125.             return $this->redirectToRoute('home');
  126.         }
  127.         return $this->render('@KennisbankPlatform/pages/register.html.twig', [
  128.             'form' => $form->createView()
  129.         ]);
  130.     }
  131.     // /**
  132.     //  * @Route("/abonnement", name="gebruikers_licentie")
  133.     //  */
  134.     // public function licentie(Request $request, LicenseHelper $licenseHelper)
  135.     // {
  136.     //     return $this->render('@KennisbankPlatform/gebruikers/licentie.twig', [
  137.     //         'couponcode' => $request->query->get('couponcode'),
  138.     //         'licenses'   => $licenseHelper->getLicenses($this->getParameter('license'))
  139.     //     ]);
  140.     // }
  141.     // /**
  142.     //  *
  143.     //  */
  144.     // public function omzetten(LicenseHelper $licenseHelper)
  145.     // {
  146.     //     if ($this->getUser() && !$this->getUser()->company->canUpgradeLicense()) {
  147.     //         $this->app['flash']->add('error', $this->translate('U kunt dit abonnement niet omzetten.'));
  148.     //         $this->app['response']->redirect($this->app['router']->getRouteUrl('home'));
  149.     //         return;
  150.     //     }
  151.     //     if ($id = $this->app['session']->get('expired')) {
  152.     //         $record                         = User::find($id);
  153.     //         $this->response->data['record'] = $record;
  154.     //     }
  155.     //     $this->response->data['licenses'] = $licenseHelper->getLicenses($this->app['config']['license']);
  156.     // }
  157.     /**
  158.      * Licentie aanvragen
  159.      * @Route("/abonnement/aanvragen", name="gebruikers_aanvragen")
  160.      *
  161.      * @param LicenseRequestHandler $handler
  162.      */
  163.     public function aanvragen(LicenseRequestHandler $handler null)
  164.     {
  165.         if (empty($handler)) {
  166.             $handler = new LicenseRequestHandler($this->app);
  167.         }
  168.         $license $licenseHelper->getLicense(
  169.             array_merge($this->request->request->all(), $this->request->query->all()),
  170.             $this->app['config']['license']
  171.         );
  172.         if (empty($license)) {
  173.             $this->app['flash']->add('error'$this->translate('Kies eerst een geldig abonnement.'));
  174.             $this->app['response']->redirect($this->app['router']->getRouteUrl('gebruikers_licentie'));
  175.             return;
  176.         }
  177.         $couponcode $this->request->get('couponcode');
  178.         if (!empty($couponcode)) {
  179.             $license['couponcode'] = $couponcode;
  180.         }
  181.         $this->response->data['couponcode'] = $couponcode;
  182.         $this->response->data['licenses']   = $licenseHelper->getLicenses($this->app['config']['license']);
  183.         $this->response->data['license']    = $license;
  184.         if (!empty($license['upgrade'])) {
  185.             if ($id $this->app['session']->get('expired')) {
  186.                 $record User::find($id);
  187.             } else {
  188.                 $record $this->getUser();
  189.             }
  190.             if (empty($record)) {
  191.                 $this->app['flash']->add(
  192.                     'error',
  193.                     $this->translate('Kan geen geldige gebruiker vinden voor het omzetten van dit proefabonnement.')
  194.                 );
  195.                 $this->app['response']->redirect($this->app['router']->getRouteUrl('home'));
  196.                 return;
  197.             }
  198.             $handler->setUser($record);
  199.             $handler->setCompany($record->company);
  200.             $handler->setData(
  201.                 [
  202.                 'user'    => $record->toArray(),
  203.                 'company' => $record->company->toArray(),
  204.                 ]
  205.             );
  206.         }
  207.         $handler->setLicense($license);
  208.         if ($this->request->getMethod() !== 'POST') {
  209.             return;
  210.         }
  211.         $handler->setData($this->request->request->all());
  212.         try {
  213.             $handler->handle([]);
  214.         } catch (ValidationException $e) {
  215.             $handler->error($e->getErrors());
  216.             return;
  217.         }
  218.         $user  $handler->getUser();
  219.         $trial = !empty($user->company->license_trial);
  220.         if ($trial) {
  221.             $mails = [
  222.               'activeren'      => 'activeren-licentie-proef',
  223.               'klantenservice' => 'licentie-klantenservice-proef'
  224.             ];
  225.         } else {
  226.             $mails = [
  227.               'activeren'      => 'activeren-licentie',
  228.               'klantenservice' => 'licentie-klantenservice'
  229.             ];
  230.         }
  231.         if (!empty($license['upgrade'])) {
  232.             $this->mail(
  233.                 array_merge(
  234.                     $this->app['config']['email']['default'],
  235.                     $this->app['config']['email']['welkom-licentie'],
  236.                     [
  237.                     'to' => [$user->username => $user->getFullnameAttribute()]
  238.                     ]
  239.                 ),
  240.                 [
  241.                 'user'    => $user->toArray(),
  242.                 'company' => $user->company->toArray()
  243.                 ]
  244.             );
  245.         } else {
  246.             $this->mail(
  247.                 array_merge(
  248.                     $this->app['config']['email']['default'],
  249.                     $this->app['config']['email'][$mails['activeren']],
  250.                     [
  251.                     'to' => [$user->username => $user->getFullnameAttribute()]
  252.                     ]
  253.                 ),
  254.                 [
  255.                 'user'    => $user->toArray(),
  256.                 'company' => $user->company->toArray()
  257.                 ]
  258.             );
  259.         }
  260.         if ((static::$trialIsNotTrial || $mails['klantenservice'] !== 'licentie-klantenservice-proef')) {
  261.             $this->mail(
  262.                 array_merge(
  263.                     $this->app['config']['email']['default'],
  264.                     $this->app['config']['email'][$mails['klantenservice']]
  265.                 ),
  266.                 [
  267.                 'user'    => $user->toArray(),
  268.                 'company' => $user->company->toArray()
  269.                 ]
  270.             );
  271.         }
  272.         if ($this->app['session']->has('expired')) {
  273.             $this->app['authentication']->login($user);
  274.             $this->app['session']->remove('expired');
  275.         }
  276.         if (!empty($license['upgrade'])) {
  277.             $this->app['flash']->add('success'$this->translate('Uw abonnement is succesvol omgezet.'));
  278.             $this->app['response']->redirect($this->app['router']->getRouteUrl('beheer') . '?abonnement=upgrade');
  279.             return;
  280.         }
  281.         $this->app['session']->set('conversion'$trial 'trial' 'regular');
  282.         $this->app['flash']->add('success'$this->translate('U ontvangt een e-mail om uw gebruiker te activeren via ') . $user->username);
  283.         $this->app['response']->redirect($this->app['router']->getRouteUrl('home') . '?abonnement=' . ($trial 'trial' 'regular'));
  284.     }
  285.     // /**
  286.     //  * Gebruiker activeren
  287.     //  */
  288.     // public function activeren()
  289.     // {
  290.     //     $handler = new ActivationHandler($this->app);
  291.     //     $this->response->data['token'] = $this->request->get('token');
  292.     //     $this->response->data['form']  = array_merge(
  293.     //         [
  294.     //         'token' => $this->response->data['token']
  295.     //         ],
  296.     //         $this->request->request->all()
  297.     //     );
  298.     //     try {
  299.     //         $handler->handle($this->response->data['form']);
  300.     //     } catch (ValidationException $e) {
  301.     //         $this->response->data['form']['errors'] = $e->getErrors();
  302.     //         return;
  303.     //     }
  304.     //     if ($handler->isFinished()) {
  305.     //         $user  = $handler->getUser();
  306.     //         $trial = !empty($user->company->license_trial);
  307.     //         $mail  = $trial ? 'welkom-licentie-proef' : 'welkom-licentie';
  308.     //         $this->mail(
  309.     //             array_merge(
  310.     //                 $this->app['config']['email']['default'],
  311.     //                 $this->app['config']['email'][$mail],
  312.     //                 [
  313.     //                 'to' => [$user->username => $user->getFullnameAttribute()]
  314.     //                 ]
  315.     //             ),
  316.     //             [
  317.     //             'user'    => $user->toArray(),
  318.     //             'company' => $user->company->toArray()
  319.     //             ]
  320.     //         );
  321.     //         if (!$user->hasValidLicense()) {
  322.     //             $this->app['flash']->add(
  323.     //                 'warning',
  324.     //                 $this->translate('Uw account is nu actief maar uw abonnement is reeds verlopen.')
  325.     //             );
  326.     //             $this->app['response']->redirect($this->app['router']->getRouteUrl('home'));
  327.     //             return;
  328.     //         }
  329.     //         $this->app['authentication']->login($user);
  330.     //         $this->app['flash']->add('success', $this->translate('Uw account is nu actief.'));
  331.     //         $this->app['response']->redirect($this->app['router']->getRouteUrl('home'));
  332.     //     }
  333.     // }
  334.     // /**
  335.     //  * Gebruikersnaam wijzigen.
  336.     //  */
  337.     // public function gebruikersnaam()
  338.     // {
  339.     //     $handler = new ChangeUsernameHandler($this->app);
  340.     //     $handler->setData($this->request->query->all());
  341.     //     try {
  342.     //         $handler->handle([]);
  343.     //     } catch (ValidationException $e) {
  344.     //         $this->app['flash']->add('error', $this->translate('Ongeldig verzoek.'));
  345.     //         $this->response->redirect($this->app['router']->getRouteUrl('home'));
  346.     //         return;
  347.     //     }
  348.     //     $handler->success(
  349.     //         'U kunt nu inloggen met uw nieuwe gebruikersnaam.',
  350.     //         $this->app['router']->getRouteUrl('home')
  351.     //     );
  352.     // }
  353.     // /**
  354.     //  * Wachtwoord wijzigen.
  355.     //  */
  356.     // public function wachtwoord()
  357.     // {
  358.     //     if ($token = $this->request->get('token')) {
  359.     //         $this->wachtwoordInstellen($token);
  360.     //         return;
  361.     //     }
  362.     //     $handler = new ForgotPasswordHandler($this->app);
  363.     //     $this->response->data['renew'] = $this->request->query->get('renew');
  364.     //     if ($this->request->getMethod() !== 'POST') {
  365.     //         return;
  366.     //     }
  367.     //     $this->response->data['form'] = $this->request->request->all();
  368.     //     $message = 'Indien de gebruikersnaam bekend is, ontvangt u een e-mail met instructies.';
  369.     //     try {
  370.     //         $handler->handle($this->response->data['form']);
  371.     //     } catch (ValidationException $e) {
  372.     //         $this->response->data['form']['errors'] = $e->getErrors();
  373.     //         return;
  374.     //     } catch (UserUnknownException $e) {
  375.     //         $this->app['flash']->add('success', $this->translate($message));
  376.     //         $this->app['response']->redirect($this->app['router']->getRouteUrl('home'));
  377.     //         return;
  378.     //     }
  379.     //     $user = $handler->getUser();
  380.     //     $this->mail(
  381.     //         array_merge(
  382.     //             $this->app['config']['email']['default'],
  383.     //             $this->app['config']['email']['wachtwoord'],
  384.     //             [
  385.     //             'to' => [$user->username => $user->getFullnameAttribute()]
  386.     //             ]
  387.     //         ),
  388.     //         [
  389.     //         'user'    => $user->toArray(),
  390.     //         'company' => $user->company->toArray()
  391.     //         ]
  392.     //     );
  393.     //     $this->app['flash']->add('success', $this->translate($message));
  394.     //     $this->app['response']->redirect($this->app['router']->getRouteUrl('home'));
  395.     // }
  396.     // /**
  397.     //  * Nieuw wachtwoord instellen.
  398.     //  *
  399.     //  * @param string $token
  400.     //  */
  401.     // public function wachtwoordInstellen($token)
  402.     // {
  403.     //     $this->response->data['reset'] = true;
  404.     //     $this->response->data['token'] = $token;
  405.     //     $this->response->data['form']  = $this->request->request->all();
  406.     //     $data = array_merge(
  407.     //         $this->response->data['form'],
  408.     //         ['token' => $token]
  409.     //     );
  410.     //     $handler = new ResetPasswordHandler($this->app);
  411.     //     try {
  412.     //         $handler->handle($data);
  413.     //     } catch (ValidationException $e) {
  414.     //         $this->response->data['form']['errors'] = $e->getErrors();
  415.     //         return;
  416.     //     }
  417.     //     if ($handler->isFinished()) {
  418.     //         $user = $handler->getUser();
  419.     //         if (!$user->hasValidLicense()) {
  420.     //             $this->app['flash']->add(
  421.     //                 'warning',
  422.     //                 $this->translate('Uw wachtwoord is succesvol aangepast maar uw abonnement is reeds verlopen.')
  423.     //             );
  424.     //             $this->app['response']->redirect($this->app['router']->getRouteUrl('home'));
  425.     //             return;
  426.     //         }
  427.     //         $this->app['authentication']->login($handler->getUser());
  428.     //         $this->app['flash']->add('success', $this->translate('Uw wachtwoord is succesvol aangepast.'));
  429.     //         $this->app['response']->redirect($this->app['router']->getRouteUrl('home'));
  430.     //     }
  431.     // }
  432.     // /**
  433.     //  * @return string
  434.     //  */
  435.     // protected function getContinueUrl()
  436.     // {
  437.     //     $continue = $this->request->get('continue');
  438.     //     if (empty($continue)) {
  439.     //         $continue = $this->app['router']->getRouteUrl('werkboek');
  440.     //     }
  441.     //     $continue = parse_url($continue, PHP_URL_PATH);
  442.     //     return $continue;
  443.     // }
  444. }