src/Helper/LoginHelper.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Helper;
  3. use App\DTOs\UserMe;
  4. use App\Entity\Monolith\User;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  7. class LoginHelper
  8. {
  9.     public static function fetchUser(
  10.         EntityManagerInterface $entityManager,
  11.         string $identifierField,
  12.         mixed $identifierValue,
  13.         ?string $userType null,
  14.         ?int $kindergarten null
  15.     ): ?UserMe
  16.     {
  17.         if (!in_array($userType, [User::APP_TYPE_PARENTUser::APP_TYPE_TEACHERnull])) {
  18.             throw new AccessDeniedHttpException('Undefined USER-TYPE');
  19.         }
  20.         $sql "
  21.             select u.*,
  22.                    GROUP_CONCAT(DISTINCT fu2.entity_id SEPARATOR ',') AS my_children,
  23.                    GROUP_CONCAT(DISTINCT pc.user_id SEPARATOR ',') AS my_children2,
  24.                    kr.modules_access,
  25.                    kr.role_id,
  26.                    CASE
  27.                        when c.student_id is not null then 'student'
  28.                        else ur.role_id
  29.                     end as type
  30.             from user u
  31.                 inner join user_role_linker url on u.user_id = url.user_id
  32.                     inner join user_role ur on url.role_id = ur.id
  33.             
  34.                 left join kg_role_users kru on (u.user_id = kru.user_id and kru.kindergarten_id = :kgId)
  35.                     left join kg_roles kr on (:kgId = kr.kindergarten_id and (kru.role_id = kr.role_id or (kru.role_id is null and (kr.role_id=ur.role_id or (kr.role_id='parent' and ur.role_id='parinte')))))
  36.             
  37.                 left join family_users fu on (fu.user_id = u.user_id and fu.entity_type = 'parinte' and ur.role_id = 'parinte')
  38.                 left join family_users fu2 on (fu.family_id = fu2.family_id and fu2.entity_type = 'child' and ur.role_id = 'parinte')
  39.             
  40.                 left join children c on u.user_id = c.student_id
  41.                 
  42.                 left join parents p on p.user_id = u.user_id
  43.                 left join children pc on p.id = pc.parent_id
  44.             
  45.             where u.{$identifierField}=:identifierValue
  46.             group by u.user_id
  47.         ";
  48.         $stmt $entityManager->getConnection()->prepare($sql);
  49.         if (
  50.             $user $stmt->executeQuery([
  51.                 'identifierValue' => $identifierValue,
  52.                 'kgId' => $kindergarten ?? 0
  53.             ])->fetchAssociative()
  54.         ) {
  55.             if ($user['my_children2']) {
  56.                 $user['my_children'] .= ',' $user['my_children2'];
  57.             }
  58.             unset($user['my_children2']);
  59.             return UserMe::map($user)
  60.                 ->setKindergartens(self::getKindergartens($entityManager'user_id'$user['user_id']))
  61.                 ->setGroups(self::getGroups($entityManager'user_id'$user['user_id']))
  62.             ;
  63.         }
  64.         return null;
  65.     }
  66.     private static function getKindergartens(EntityManagerInterface $entityManagerstring $identifierFieldmixed $identifierValue): ?array
  67.     {
  68.         $sql "
  69.             select
  70.                 DISTINCT IFNULL(uke.kindergarten_id,
  71.                     IFNULL(ukka.kindergarten_id,
  72.                         IFNULL(ukp.kindergarten_id,
  73.                             IFNULL(ukc2.kindergarten_id, IFNULL(ukc3.kindergarten_id, ukc.kindergarten_id))
  74.                         )
  75.                     )
  76.                 ) as kg,
  77.                 IFNULL(uke.from_date,
  78.                     IFNULL(ukka.from_date,
  79.                         IFNULL(ukp.from_date,
  80.                             IFNULL(ukc.from_date, ukc2.from_date)
  81.                         )
  82.                     )
  83.                 ) as from_date,
  84.                 IFNULL(uke.end_date,
  85.                     IFNULL(ukka.end_date,
  86.                         IFNULL(ukp.end_date,
  87.                             IFNULL(ukc.end_date, ukc2.end_date)
  88.                         )
  89.                     )
  90.                 ) as end_date
  91.             from user u
  92.                  inner join user_role_linker url on u.user_id = url.user_id
  93.                  inner join user_role ur on url.role_id = ur.id
  94.             
  95.                  left join educators e on u.user_id = e.user_id
  96.             
  97.                  left join parents p on u.user_id = p.user_id
  98.             
  99.                  left join family_users fu on (fu.user_id = u.user_id and fu.entity_type = 'parinte' and ur.role_id = 'parinte')
  100.                  left join family_users fu2 on (fu.family_id = fu2.family_id and fu2.entity_type = 'child' and ur.role_id = 'parinte')
  101.             
  102.                  left join children c on u.user_id = c.student_id
  103.                  left join children c2 on p.id = c2.parent_id
  104.                  left join kg_admins ka on u.user_id = ka.user_id
  105.             
  106.                  left join user_kindergartens uke on (uke.entity_id = e.id and uke.entity_type = 'educator' and ur.role_id = 'educator')
  107.                  left join user_kindergartens ukka on (ukka.entity_id = ka.id and ukka.entity_type = 'gradinita_admin' and ur.role_id = 'gradinita_admin')
  108.                  left join user_kindergartens ukp on (ukp.entity_id = p.id and ukp.entity_type = 'parinte' and ur.role_id = 'parinte')
  109.                  left join user_kindergartens ukc on (ukc.entity_id = c.user_id and ukc.entity_type = 'copil' and ur.role_id = 'parinte')
  110.             
  111.                  left join user_kindergartens ukc2 on (ukc2.entity_id = fu2.user_id and ukc2.entity_type = 'copil' and ur.role_id = 'parinte')
  112.                  left join user_kindergartens ukc3 on (ukc3.entity_id = c2.user_id and ukc3.entity_type = 'copil' and ur.role_id = 'parinte')
  113.             
  114.             where u.{$identifierField}=:identifierValue
  115.         ";
  116.         $stmt $entityManager->getConnection()->prepare($sql);
  117.         $list $stmt->executeQuery(['identifierValue' => $identifierValue])->fetchAllAssociative();
  118.         $list array_filter($list);
  119.         $result = [];
  120.         foreach ($list as $item) {
  121.             if (!$item['kg']) {
  122.                 continue;
  123.             }
  124.             if ('0000-00-00 00:00:00' == $item['end_date']) {
  125.                 $item['end_date'] = null;
  126.             }
  127.             if (!isset($result[$item['kg']])) {
  128.                 $result[$item['kg']] = [$item['from_date'], $item['end_date']];
  129.                 continue;
  130.             }
  131.             if (strtotime($item['from_date']) < strtotime($result[$item['kg']][0])) {
  132.                 $result[$item['kg']][0] = $item['from_date'];
  133.             }
  134.             if (is_null($item['end_date']) || is_null($result[$item['kg']][1])) {
  135.                 $result[$item['kg']][1] = null;
  136.             } elseif (strtotime($item['end_date']) < strtotime($result[$item['kg']][1])) {
  137.                 $result[$item['kg']][1] = $item['end_date'];
  138.                 if (strtotime($result[$item['kg']][1]) < time()) {
  139.                     $result[$item['kg']][1] = null;
  140.                 }
  141.             }
  142.         }
  143.         return $result;
  144.     }
  145.     private static function getGroups(EntityManagerInterface $entityManagerstring $identifierFieldmixed $identifierValue): ?array
  146.     {
  147.         $sql "
  148.             select
  149.                 DISTINCT IFNULL(eg.group_id,
  150.                    IFNULL(cg2.group_id, cg.group_id)
  151.                 ) as `group`,
  152.                 IFNULL(eg.from_date,
  153.                    IFNULL(cg2.from_date, cg.from_date)
  154.                 ) as from_date,
  155.                 IFNULL(eg.end_date,
  156.                    IFNULL(cg2.end_date, cg.end_date)
  157.                 ) as end_date                
  158.             from user u
  159.                 inner join user_role_linker url on u.user_id = url.user_id
  160.                 inner join user_role ur on url.role_id = ur.id
  161.                 
  162.                 left join educators e on u.user_id = e.user_id
  163.                 left join educators_groups eg on (eg.educator_id = e.id)
  164.                 
  165.                 left join family_users fu on (fu.user_id = u.user_id and fu.entity_type = 'parinte' and ur.role_id = 'parinte')
  166.                 left join family_users fu2 on (fu.family_id = fu2.family_id and fu2.entity_type = 'child' and ur.role_id = 'parinte')
  167.                 left join children_groups cg2 on (cg2.child_id = fu2.entity_id)
  168.                 
  169.                 left join children c on u.user_id = c.student_id
  170.                 left join children_groups cg on (cg.child_id = c.user_id)            
  171.             where u.{$identifierField}=:identifierValue
  172.         ";
  173.         $stmt $entityManager->getConnection()->prepare($sql);
  174.         $list $stmt->executeQuery(['identifierValue' => $identifierValue])->fetchAllAssociative();
  175.         $list array_filter($list);
  176.         $result = [];
  177.         foreach ($list as $item) {
  178.             if (!$item['group']) {
  179.                 continue;
  180.             }
  181.             if ('0000-00-00 00:00:00' == $item['end_date']) {
  182.                 $item['end_date'] = null;
  183.             }
  184.             if (!isset($result[$item['group']])) {
  185.                 $result[$item['group']] = [$item['from_date'], $item['end_date']];
  186.                 continue;
  187.             }
  188.             if (strtotime($item['from_date']) < strtotime($result[$item['group']][0])) {
  189.                 $result[$item['group']][0] = $item['from_date'];
  190.             }
  191.             if (is_null($item['end_date']) || is_null($result[$item['group']][1])) {
  192.                 $result[$item['group']][1] = null;
  193.             } elseif (strtotime($item['end_date']) < strtotime($result[$item['group']][1])) {
  194.                 $result[$item['group']][1] = $item['end_date'];
  195.                 if (strtotime($result[$item['group']][1]) < time()) {
  196.                     $result[$item['group']][1] = null;
  197.                 }
  198.             }
  199.         }
  200.         return $result;
  201.     }
  202. }