src/Service/AuthService.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use App\Entity\UserEntity;
  7. Class AuthService {
  8.     private $em;
  9.     private $container;
  10.     public function __construct(EntityManagerInterface $emContainerInterface $container) {
  11.         $this->em $em;
  12.         $this->container $container;
  13.     }
  14.     // Original PHP code by Chirp Internet: www.chirp.com.au
  15.     public function better_crypt($input$rounds 7)
  16.     {
  17.         $salt "";
  18.         $salt_chars array_merge(range('A','Z'), range('a','z'), range(0,9));
  19.         for($i=0$i 22$i++) {
  20.             $salt .= $salt_chars[array_rand($salt_chars)];
  21.         }
  22.         return crypt($inputsprintf('$2a$%02d$'$rounds) . $salt);
  23.     }
  24.     /**
  25.      * Checks if the user is logged in or not
  26.      */
  27.     public function isLoggedIn($requestUri true) {
  28.         $session $this->container->get('session');
  29.         if($session->has('userData')) {
  30.             return true;
  31.         } else {
  32.             if($requestUri) {
  33.                 $req_uri $_SERVER['REQUEST_URI'];
  34.                 if($req_uri !== $this->container->get('router')->generate('home_login') &&
  35.                     $req_uri !== $this->container->get('router')->generate('home_logout') &&
  36.                     $req_uri !== $this->container->get('router')->generate('home_forgot_password') &&
  37.                     //$req_uri !== $this->container->get('router')->generate('auth_forgot_password_confirmation') &&
  38.                     strpos($req_uri'ajax') === false$session->set('req_uri'$req_uri);
  39.             }
  40.             return false;
  41.         }
  42.     }
  43.     /**
  44.      * Checks if the user has the ess
  45.      */
  46.     public function isUserHasAccesses($accessDescriptions$hasErrorMsg=true$matchCtr=false) {
  47.         $session $this->container->get('session');
  48.         //$session->save(); // This will automatically call session_write_close() to prevent session lock timeout error
  49.         $userData $session->get('userData');
  50.         if($userData['type'] === 'Super Admin') {
  51.             return true;
  52.         } else {
  53.             if($matchCtr) {
  54.                 $accessCtr 0;
  55.                 foreach($accessDescriptions as $accessDescription) if(in_array($accessDescription$userData['accesses'])) $accessCtr++;
  56.                 $hasAccess count($accessDescriptions) === $accessCtr;
  57.                 if(!$hasAccess) {
  58.                     if($hasErrorMsg) {
  59.                         $session->getFlashBag()->set('error_messages'"You don't have the right to access the page. Please contact the administrator.");
  60.                     }
  61.                     return false;
  62.                 } else {
  63.                     return true;
  64.                 }
  65.             } else {
  66.                 foreach($accessDescriptions as $accessDescription) if(in_array($accessDescription$userData['accesses'])) return true;
  67.                 if($hasErrorMsg$session->getFlashBag()->set('error_messages'"You don't have the right to access the page. Please contact the administrator.");
  68.                 return false;
  69.             }
  70.         }
  71.     }
  72.     /**
  73.      * Redirects to login page
  74.      */
  75.     public function redirectToLogin() {
  76.         return new RedirectResponse($this->container->get('router')->generate('home_login'), 302);
  77.     }
  78.     /**
  79.      * Redirects to registration page
  80.      */
  81.     public function redirectToRegistration() {
  82.         return new RedirectResponse($this->container->get('router')->generate('frontend_registration'), 302);
  83.     }
  84.     /**
  85.      * Redirects to home page
  86.      */
  87.     public function redirectToHome() {
  88.         return new RedirectResponse($this->container->get('router')->generate('admin'), 302);
  89.     }
  90.     /**
  91.      * Get user
  92.      */
  93.     public function getUser() {
  94.         $userData $this->container->get('session')->get('userData');
  95.         return $this->em->getRepository(UserEntity::class)->find($userData['id']);
  96.     }
  97.     /**
  98.      * Get user types
  99.      */
  100.     public function getUserTypes() {
  101.         return array(
  102.             'Admin',
  103.             'Staff',
  104.             'Agent',
  105.             'Public',
  106.             'Guest',
  107.         );
  108.     }
  109.     public function getAccesses() {
  110.         return array(
  111.             array('label' => 'Admin''description' => 'Admin''children' => array(
  112.                 array('label' => 'Manage Users''description' => 'Admin Users''children' => array(
  113.                     array('label' => 'User''description' => 'Admin User''children' => array(
  114.                         array('label' => 'New''description' => 'Admin User New'),
  115.                         array('label' => 'Update''description' => 'Admin User Update'),
  116.                         array('label' => 'Delete''description' => 'Admin User Delete'),
  117.                     )),
  118.                 )),
  119.                 array('label' => 'Manage Property Listing''description' => 'Admin Property Listing''children' => array(
  120.                     array('label' => 'Export to Excel''description' => 'Admin Property Listing Export To Excel'),
  121.                     array('label' => 'New''description' => 'Admin Property Listing New'),
  122.                     array('label' => 'Update''description' => 'Admin Property Listing Update'),
  123.                     array('label' => 'Delete''description' => 'Admin Property Listing Delete'),
  124.                     array('label' => 'Remarks''description' => 'Admin Property Listing Remark'),
  125.                 )),
  126.                 array('label' => 'Manage Deleted Property Listing''description' => 'Admin Deleted Property Listing'),
  127.                 array('label' => 'Manage Search''description' => 'Admin Search'),
  128.                 array('label' => 'Manage Inquiry Record''description' => 'Admin Inquiry Record'),
  129.                 array('label' => 'Inquiry''description' => 'Admin Inquiry'),
  130.                 array('label' => 'Print''description' => 'Admin Print'),
  131.                 array('label' => 'Manage CMS''description' => 'Admin Cms''children' => array(
  132.                     array('label' => 'Manage Property Listing Type''description' => 'Admin Property Listing Type''children' => array(
  133.                         array('label' => 'New''description' => 'Admin Property Listing Type New'),
  134.                         array('label' => 'Update''description' => 'Admin Property Listing Type Update'),
  135.                         array('label' => 'Delete''description' => 'Admin Property Listing Type Delete'),
  136.                     )),
  137.                     array('label' => 'Manage Property ID''description' => 'Admin Property ID''children' => array(
  138.                         array('label' => 'New''description' => 'Admin Property ID New'),
  139.                         array('label' => 'Update''description' => 'Admin Property ID Update'),
  140.                         array('label' => 'Delete''description' => 'Admin Property ID Delete'),
  141.                     )),
  142.                     array('label' => 'Manage Building''description' => 'Admin Building''children' => array(
  143.                         array('label' => 'New''description' => 'Admin Building New'),
  144.                         array('label' => 'Update''description' => 'Admin Building Update'),
  145.                         array('label' => 'Delete''description' => 'Admin Building Delete'),
  146.                     )),
  147.                     array('label' => 'Manage City''description' => 'Admin City''children' => array(
  148.                         array('label' => 'New''description' => 'Admin City New'),
  149.                         array('label' => 'Update''description' => 'Admin City Update'),
  150.                         array('label' => 'Delete''description' => 'Admin City Delete'),
  151.                     )),
  152.                     array('label' => 'Manage Tower''description' => 'Admin Tower''children' => array(
  153.                         array('label' => 'New''description' => 'Admin Tower New'),
  154.                         array('label' => 'Update''description' => 'Admin Tower Update'),
  155.                         array('label' => 'Delete''description' => 'Admin Tower Delete'),
  156.                     )),
  157.                     array('label' => 'Manage Parking Space''description' => 'Admin Parking Space''children' => array(
  158.                         array('label' => 'New''description' => 'Admin Parking Space New'),
  159.                         array('label' => 'Update''description' => 'Admin Parking Space Update'),
  160.                         array('label' => 'Delete''description' => 'Admin Parking Space Delete'),
  161.                     )),
  162.                     array('label' => 'Manage Contact Type''description' => 'Admin Contact Type''children' => array(
  163.                         array('label' => 'New''description' => 'Admin Contact Type New'),
  164.                         array('label' => 'Update''description' => 'Admin Contact Type Update'),
  165.                         array('label' => 'Delete''description' => 'Admin Contact Type Delete'),
  166.                     )),
  167.                     array('label' => 'Manage Remark''description' => 'Admin Remark''children' => array(
  168.                         array('label' => 'New''description' => 'Admin Remark New'),
  169.                         array('label' => 'Update''description' => 'Admin Remark Update'),
  170.                         array('label' => 'Delete''description' => 'Admin Remark Delete'),
  171.                     )),
  172.                     array('label' => 'Manage Title''description' => 'Admin Title''children' => array(
  173.                         array('label' => 'New''description' => 'Admin Title New'),
  174.                         array('label' => 'Update''description' => 'Admin Title Update'),
  175.                         array('label' => 'Delete''description' => 'Admin Title Delete'),
  176.                     )),
  177.                     array('label' => 'Manage Type''description' => 'Admin Type''children' => array(
  178.                         array('label' => 'New''description' => 'Admin Type New'),
  179.                         array('label' => 'Update''description' => 'Admin Type Update'),
  180.                         array('label' => 'Delete''description' => 'Admin Type Delete'),
  181.                     )),
  182.                     array('label' => 'Manage Furnishing''description' => 'Admin Furnished''children' => array(
  183.                         array('label' => 'New''description' => 'Admin Furnished New'),
  184.                         array('label' => 'Update''description' => 'Admin Furnished Update'),
  185.                         array('label' => 'Delete''description' => 'Admin Furnished Delete'),
  186.                     )),
  187.                     array('label' => 'Manage Status''description' => 'Admin Status''children' => array(
  188.                         array('label' => 'New''description' => 'Admin Status New'),
  189.                         array('label' => 'Update''description' => 'Admin Status Update'),
  190.                         array('label' => 'Delete''description' => 'Admin Status Delete'),
  191.                     )),
  192.                     array('label' => 'Manage Ownership''description' => 'Admin Ownership''children' => array(
  193.                         array('label' => 'New''description' => 'Admin Ownership New'),
  194.                         array('label' => 'Update''description' => 'Admin Ownership Update'),
  195.                         array('label' => 'Delete''description' => 'Admin Ownership Delete'),
  196.                     )),
  197.                     array('label' => 'Manage Barangay''description' => 'Admin Barangay''children' => array(
  198.                         array('label' => 'New''description' => 'Admin Barangay New'),
  199.                         array('label' => 'Update''description' => 'Admin Barangay Update'),
  200.                         array('label' => 'Delete''description' => 'Admin Barangay Delete'),
  201.                     ))
  202.                 )),
  203.             )),
  204.         );
  205.     }
  206.     /**
  207.      * Checks if the user is logged in or not
  208.      */
  209.     public function accountIsLoggedIn($requestUri true) {
  210.         $session $this->container->get('session');
  211.         if($session->has('accountUserData')) {
  212.             return true;
  213.         } else {
  214.             if($requestUri) {
  215.                 $req_uri $_SERVER['REQUEST_URI'];
  216.                 if($req_uri !== $this->container->get('router')->generate('home_login') &&
  217.                     $req_uri !== $this->container->get('router')->generate('home_logout') &&
  218.                     $req_uri !== $this->container->get('router')->generate('home_forgot_password') &&
  219.                     //$req_uri !== $this->container->get('router')->generate('auth_forgot_password_confirmation') &&
  220.                     strpos($req_uri'ajax') === false$session->set('req_uri'$req_uri);
  221.             }
  222.             return false;
  223.         }
  224.     }
  225.     /**
  226.      * Redirects to login page of user
  227.      */
  228.     public function account_redirectToLogin() {
  229.         return new RedirectResponse($this->container->get('router')->generate('admin'), 302);
  230.     }
  231.     public function calculateDaysEarned($amount$startDate$endDate){
  232.         $today = new \DateTime();  
  233.         $daysPassed $today->diff($startDate)->days;
  234.         $days $endDate->diff($startDate)->days;
  235.         $earnedPerDays round(($amount $days), 2);
  236.         if($daysPassed && $endDate >= $today){ 
  237.             return $daysPassed $earnedPerDays;
  238.         }
  239.         
  240.         return 0;
  241.     }
  242. }