src/Controller/User/UserController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller\User;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Security\Core\Security;
  7. // use Symfony\Component\Security\Core\SecurityContextInterface;
  8. use App\Entity\User\User;
  9. use App\Entity\User\Customer;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. class UserController extends AbstractController {
  13.     /**
  14.      * This is an index page of the site
  15.      * 
  16.      * @return Response
  17.      */
  18.     public function indexAction() {
  19.         return $this->render('User/index.html.twig');
  20.     }
  21.     /**
  22.      * This is a login page
  23.      * 
  24.      * @param Request $request
  25.      * @return Response
  26.      */
  27.     public function loginAction(AuthenticationUtils $authenticationUtils): Response {
  28.         // get the login error if there is one
  29.         $error $authenticationUtils->getLastAuthenticationError();
  30.         // last username entered by the user
  31.         $lastUsername $authenticationUtils->getLastUsername();
  32.         // $session = $request->getSession();
  33.         // // get the login error if there is one
  34.         // if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
  35.         //     $error = $request->attributes->get(
  36.         //         Security::AUTHENTICATION_ERROR
  37.         //     );
  38.         // } elseif (null !== $session &&
  39.         //         $session->has(Security::AUTHENTICATION_ERROR)) {
  40.         //     $error = "Email and password do not match";
  41.         //     $session->remove(Security::AUTHENTICATION_ERROR);
  42.         // } else {
  43.         //     $error = '';
  44.         // }
  45.         // last username entered by the user
  46.         // $lastUsername = (null === $session) ? '' :
  47.         //         $session->get(Security::LAST_USERNAME);
  48.         
  49.         return $this->render('User/login.html.twig', array(
  50.             // last username entered by the user
  51.             'last_username' => $lastUsername,
  52.             'error' => $error,
  53.         ));
  54.     }
  55.      
  56.     /**
  57.      * This is the page where invited users will land by the 
  58.      * link sent to their emails and will register themselves
  59.      * 
  60.      * @param type $emailVerifyId
  61.      * @return type
  62.      */
  63.     public function inviteUserAction(ManagerRegistry $doctrine$emailVerifyId){
  64.         $email $status $role '';
  65.         /**
  66.          * @TODO create this page
  67.          */
  68.         // $em = $this->getDoctrine()->getManager();
  69.         $user $doctrine->getRepository(User::class)->findOneBy(array("verificationId" => $emailVerifyId));
  70.         if($user){
  71.             $status $user->getStatus();
  72.             if($user->getStatus() == User::UNVERIFIED){
  73.                 $user->setStatus(User::UNREGISTERED);
  74.                 // $em->flush();
  75.             }
  76.             $email $user->getEmail();
  77.             $role  $user->getRole()->getName();
  78.         }else{
  79.             $status false;
  80.         }
  81.         return $this->render('User/userregistration.html.twig'$param = array(
  82.             'emailVerifyId' => $emailVerifyId,
  83.             'status' => $status,
  84.             "email"  => $email,
  85.             "role"   => $role
  86.         ));
  87.     }
  88.     
  89.     /**
  90.      * Reset password
  91.      * 
  92.      * @param type $resetPasswordId
  93.      * @return Response
  94.      */
  95.     public function resetPasswordAction(ManagerRegistry $doctrine$resetPasswordId){
  96.         // $em = $this->getDoctrine()->getManager();
  97.         $user $doctrine->getRepository(User::class)->findOneBy(array("passwordResetId" => $resetPasswordId));
  98.         if($user){
  99.             $status 1;
  100.         }else{
  101.             $status 0;
  102.         }
  103.         
  104.         return $this->render('User/reset.html.twig', array(
  105.             'resetPasswordId' => $resetPasswordId,
  106.             'status' => $status
  107.         ));
  108.     }
  109.     
  110.     /**
  111.      * Change password
  112.      * 
  113.      * @param Request $request
  114.      * @return Response
  115.      */
  116.     public function changePasswordAction(Request $request){
  117.         return $this->render('User/changepassword.html.twig', array());
  118.     }
  119.     
  120.     /**
  121.      * Show list of users
  122.      * 
  123.      * @param Request $request
  124.      * @return Response
  125.      */
  126.     public function usersAction(ManagerRegistry $doctrineRequest $request){
  127.         $data = array('customers' => $this->_getAllCustomers($doctrine));
  128.         return $this->render('User/users.html.twig'$data);
  129.     }
  130.     
  131.     /**
  132.      * Show list of roles
  133.      * 
  134.      * @param Request $request
  135.      * @return type
  136.      */
  137.     public function rolesAction(Request $request){
  138.         return $this->render('User/roles.html.twig', array());
  139.     }
  140.     
  141.     /**
  142.      * Show MyPreference page
  143.      *  
  144.      * @param Request $request
  145.      * @return type
  146.      */
  147.     public function mypreferencesAction(Request $request){
  148.         
  149.         return $this->render('User/mypreferences.html.twig', array());
  150.     }
  151.     
  152.     /**
  153.      * Show RolePermissions page
  154.      * 
  155.      * @param type $roleId
  156.      * @return Response
  157.      */
  158.     public function rolePermissionsAction($roleId){
  159.         return $this->render('User/rolepermissions.html.twig', array('roleId' => $roleId));
  160.         
  161.     }
  162.     
  163.     /**
  164.      * This is an action to show AccessDenied page
  165.      * 
  166.      * @param Request $request
  167.      * @return Response
  168.      */
  169.     public function noaccessAction(Request $request){
  170.         return $this->render('User/noaccess.html.twig', array());
  171.     }
  172.     
  173.     /**
  174.      * This is an action to choose Customer page
  175.      * 
  176.      * @param Request $request
  177.      * @return Response
  178.      */
  179.     public function choosecustomerAction(Request $request){
  180.         $data = array('customers' => $this->_getAllCustomersForUser($this->getUser()));
  181.         return $this->render('User/choosecustomer.html.twig'$data);
  182.     }
  183.     /**
  184.      * This function will return the array of customers 
  185.      * for a role
  186.      * 
  187.      * @param User $user
  188.      * @return array of customers
  189.      */
  190.     private function _getAllCustomersForUser(User $user) {
  191.         $customer_pointers $user->getAllowedCustomers(true);
  192.         $result = array();
  193.         foreach ($customer_pointers as $customer_pointer) {
  194.             $customer $customer_pointer->getCustomer();
  195.             $result[] = array(
  196.                 'id' => $customer->getId(),
  197.                 'name' => $customer->getName()
  198.             );
  199.         }
  200.         return $result;
  201.     }
  202.     
  203.     /**
  204.      * This method will return all the customers
  205.      * 
  206.      * @return array
  207.      */
  208.     private function _getAllCustomers($doctrine) {
  209.         $repo $doctrine->getRepository(Customer::class);
  210.         $customers $repo->findAll();
  211.         $result = array();
  212.         foreach ($customers as $customer) {
  213.             $result[] = array(
  214.                 'id' => $customer->getId(),
  215.                 'name' => $customer->getName()
  216.             );
  217.         }
  218.         return $result;
  219.     }
  220. }