src/Service/AppSession.php line 262

Open in your IDE?
  1. <?php
  2.     namespace App\Service;
  3.     use Doctrine\ORM\EntityManagerInterface;
  4.     use Symfony\Component\DependencyInjection\ContainerInterface;
  5.     use Symfony\Component\HttpFoundation\RequestStack;
  6.     use Symfony\Component\HttpKernel\Event\RequestEvent;
  7.     use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8.     class AppSession {
  9.         private $container;
  10.         protected $em;
  11.         protected $requeststack;
  12.         protected $token;
  13.     
  14.         public function __construct(ContainerInterface $containerEntityManagerInterface $emRequestStack $requeststackTokenStorageInterface $token) {
  15.             $this->container $container;
  16.             $this->requeststack $requeststack;
  17.             $this->em $em;
  18.             $this->token $token;
  19.         }
  20.         public function haveRole($curentuser,$roles,$tohave,$route) {
  21.             $haverole=false;
  22.             if($roles=="") {
  23.                 if(empty($tohave)) $haverole=true;
  24.             }
  25.             else {
  26.                 foreach($roles as $role) {
  27.                     if(in_array($role,$tohave))
  28.                         $haverole=true;
  29.                 }
  30.             }
  31.             
  32.             if($route!=null) {
  33.                 if($haverole&&in_array("ROLE_MODO",$roles)) {
  34.                     $permmodo=$this->em->getRepository("App\Entity\PermModo")->findOneBy(["route"=>$route,"visible"=>true,"permmodoprofil"=>$curentuser->getPermmodoprofil()]);
  35.                     if(!$permmodo$haverole=false;
  36.                 } 
  37.             }
  38.             return $haverole;
  39.         }
  40.         public function onDomainParse(RequestEvent $event) {
  41.             $session $this->requeststack->getSession();
  42.             $configs $this->em->getRepository("App\Entity\Config")->findAll();
  43.             foreach($configs as $config) {
  44.                 $session->set($config->getId(), strval($config->getValue()));
  45.             }
  46.             
  47.             // Utilisateur en cours
  48.             $token $this->token->getToken();
  49.             if (!$token)
  50.                 $curentuser "anon.";
  51.             else
  52.                 $curentuser $token->getUser();
  53.             
  54.             // Roles actif
  55.             if($curentuser=="anon."$roles=[];
  56.             else $roles=$curentuser->getRoles();
  57.             // Masteridentity
  58.             $masteridentity  =$this->container->getParameter('masteridentity');
  59.             $session->set('masteridentity',$masteridentity);
  60.             // mode_auth
  61.             $mode_auth  =$this->container->getParameter('mode_auth');
  62.             $session->set('mode_auth',$mode_auth);
  63.             // App activate
  64.             $app=[];
  65.             $app["importuser_activate"]                 =($masteridentity=="SQL");
  66.             
  67.             $app["portal_activate"]                     =$this->container->getParameter('portal_activate');
  68.             $app["cron_activate"]                       =$this->container->getParameter('cron_activate');
  69.             $app["module_activate"]                     =$this->container->getParameter('module_activate');
  70.             $app["page_activate"]                       =$this->container->getParameter('page_activate');
  71.             $app["item_activate"]                       =$this->container->getParameter('item_activate');
  72.             $app["alert_activate"]                      =$this->container->getParameter('alert_activate');
  73.             $app["calendar_activate"]                   =$this->container->getParameter('calendar_activate');
  74.             $app["blog_activate"]                       =$this->container->getParameter('blog_activate');
  75.             $app["flux_activate"]                       =$this->container->getParameter('flux_activate');
  76.             $app["notice_activate"]                     =$this->container->getParameter('notice_activate');
  77.             $app["syncenvole_activate"]                 =$this->container->getParameter('syncenvole_activate');
  78.             $app["widbalado_activate_syncenvole"]       =$this->container->getParameter('widbalado_activate_syncenvole');
  79.             $app["widcdt_activate_syncenvole"]          =$this->container->getParameter('widcdt_activate_syncenvole');
  80.             $app["widgepi_activate_syncenvole"]         =$this->container->getParameter('widgepi_activate_syncenvole');
  81.             $app["widnextcloud_activate_syncenvole"]    =$this->container->getParameter('widnextcloud_activate_syncenvole');
  82.             $app["widpiwik_activate_syncenvole"]        =$this->container->getParameter('widpiwik_activate_syncenvole');
  83.             $app["widsacoche_activate_syncenvole"]      =$this->container->getParameter('widsacoche_activate_syncenvole');
  84.             $app["widlimesurvey_activate_syncenvole"]   =$this->container->getParameter('widlimesurvey_activate_syncenvole');
  85.             $app["widmoodle_activate_syncenvole"]       =$this->container->getParameter('widmoodle_activate_syncenvole');
  86.             $app["widwordpress_activate_syncenvole"]    =$this->container->getParameter('widwordpress_activate_syncenvole');
  87.             // Chargement de la sidebar
  88.             $iconniveau01   "fa ".$this->container->getParameter('iconniveau01');
  89.             $labelsniveau01 $this->container->getParameter('labelsniveau01');
  90.             $labelniveau01  $this->container->getParameter('labelniveau01');
  91.             $session->set('labelniveau01',$labelniveau01);
  92.             $session->set('labelsniveau01',$labelsniveau01);
  93.             $viewniveau02   $this->container->getParameter('viewniveau02');
  94.             $iconniveau02   "fa ".$this->container->getParameter('iconniveau02');
  95.             $labelsniveau02 $this->container->getParameter('labelsniveau02');
  96.             $labelniveau02  $this->container->getParameter('labelniveau02');
  97.             $session->set('viewniveau02',$viewniveau02);
  98.             $session->set('labelniveau02',$labelniveau02);
  99.             $session->set('labelsniveau02',$labelsniveau02);
  100.             $moderegistration  =$this->container->getParameter('moderegistration');
  101.             if($masteridentity!="SQL"$moderegistration="none";
  102.             if(in_array("ROLE_MODO",$roles)) {
  103.                 $request $event->getRequest();
  104.                 $route  $request->attributes->get('_route');
  105.                 if($route!="app_core_config"&&stripos($route,"_config")!==false&&stripos($route,"app_core_config_file")===false) {
  106.                     $permmodoprofil=$curentuser->getPermmodoprofil();
  107.                     if(stripos($route,"app_core_config_logo")!==false$route="app_portal_config_page";
  108.                     if(stripos($route,"app_core_config_header")!==false$route="app_portal_config_page";
  109.                     
  110.                     if(stripos($route,"app_portal_config_panelwidget")!==false$route="app_portal_config_page";
  111.                     if(stripos($route,"app_portal_config_pagewidget")!==false$route="app_portal_config_page";
  112.                     if(stripos($route,"app_portal_config_bookmark")!==false$route="app_portal_config_page";
  113.                     if(stripos($route,"app_portal_config_slide")!==false$route="app_portal_config_page";
  114.                     if(stripos($route,"app_core_config_file")!==false$route="app_portal_config_page";
  115.                     
  116.                     if(stripos($route,"app_portal_config_itemcategory")!==false$route="app_portal_config_item";
  117.                     if(stripos($route,"app_portal_config_alertcategory")!==false$route="app_portal_config_alert";
  118.                     if(stripos($route,"app_portal_config_calendarevent")!==false$route="app_portal_config_calendar";
  119.                     if(stripos($route,"app_portal_config_blogarticle")!==false$route="app_portal_config_blog";
  120.                     if(stripos($route,"app_portal_config_blogcomment")!==false$route="app_portal_config_blog";
  121.                     if(stripos($route,"app_portal_config_projecttask")!==false$route="app_portal_config_project";
  122.                     if(stripos($route,"app_cron_config")!==false$route="app_cron_config";
  123.                     $tbroute=explode("_",$route);
  124.                     $route =(isset($tbroute[0])?$tbroute[0]:"");
  125.                     $route.=(isset($tbroute[1])?"_".$tbroute[1]:"");
  126.                     $route.=(isset($tbroute[2])?"_".$tbroute[2]:"");
  127.                     $route.=(isset($tbroute[3])?"_".$tbroute[3]:"");
  128.                     
  129.                     $permmodo $this->em->getRepository("App\Entity\PermModo")->findOneBy(['permmodoprofil'=>$permmodoprofil,'route'=> $route"visible"=>true]);
  130.                     if(!$permmodo) die('Permission denied');
  131.                 }
  132.             }
  133.             
  134.             $sidebar=array();
  135.             $nvs1 $this->em->getRepository("App\Entity\Sidebar")->findBy(array('parent'=> NULL), array('roworder' => 'ASC'));
  136.             foreach($nvs1 as $nv1) {
  137.                 if($this->haveRole($curentuser,$roles,$nv1->getPermission(),$nv1->getPath())) {
  138.                     $sidebar[$nv1->getRoworder()] = array(
  139.                         "fonticon"  => $nv1->getFonticon(),
  140.                         "label"     => $nv1->getLabel(),
  141.                         "path"      => $nv1->getPath(),
  142.                     );
  143.                     if($nv1->getAppactivate()!=""&&!$app[$nv1->getAppactivate()]) {
  144.                         unset($sidebar[$nv1->getRoworder()]);
  145.                         continue;
  146.                     }
  147.                     if($nv1->getPath()=="app_core_config_niveau01") {
  148.                         $sidebar[$nv1->getRoworder()]["label"]=$labelsniveau01;
  149.                         $sidebar[$nv1->getRoworder()]["fonticon"]=$iconniveau01;
  150.                     }
  151.                     if($nv1->getPath()=="app_core_config_niveau02") {
  152.                         if(!$viewniveau02
  153.                             unset($sidebar[$nv1->getRoworder()]);
  154.                         else {
  155.                             $sidebar[$nv1->getRoworder()]["label"]=$labelsniveau02;
  156.                             $sidebar[$nv1->getRoworder()]["fonticon"]=$iconniveau02;
  157.                         }
  158.                     }
  159.                     if($nv1->getPath()=="app_core_config_whitelist") {
  160.                         if($moderegistration=="none")
  161.                             unset($sidebar[$nv1->getRoworder()]);
  162.                     }
  163.                     foreach($nv1->getChilds() as $nv2) {
  164.                  
  165.                         if($this->haveRole($curentuser,$roles,$nv2->getPermission(),$nv2->getPath())) {
  166.                             $sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()] = array (
  167.                                 "fonticon"  => $nv2->getFonticon(),
  168.                                 "label"     => $nv2->getLabel(),
  169.                                 "path"      => $nv2->getPath(),
  170.                             );
  171.                             if($nv2->getPath()=="app_core_config_niveau01") {
  172.                                 $sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]["label"]=$labelsniveau01;
  173.                                 $sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]["fonticon"]=$iconniveau01;
  174.                             }
  175.                             if($nv2->getPath()=="app_core_config_niveau02") {
  176.                                 if(!$viewniveau02)
  177.                                     unset($sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]);
  178.                                 else {
  179.                                     $sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]["label"]=$labelsniveau02;
  180.                                     $sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]["fonticon"]=$iconniveau02;
  181.                                 }
  182.                             }
  183.                             if($nv2->getPath()=="app_core_config_whitelist") {
  184.                                 if($moderegistration=="none")
  185.                                     unset($sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]);
  186.                             }
  187.                             if($nv2->getPath()=="app_core_config_registration") {
  188.                                 if($moderegistration=="none"
  189.                                     unset($sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]);
  190.                             }
  191.                             if($nv2->getAppactivate()!=""&&!$app[$nv2->getAppactivate()]) {
  192.                                 unset($sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]);
  193.                             }
  194.                             foreach($nv2->getChilds() as $nv3) {
  195.                                 if($this->haveRole($curentuser,$roles,$nv3->getPermission(),$nv3->getPath())) {
  196.                                     $sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]["childs"][$nv3->getRoworder()] = array (
  197.                                         "fonticon"  => $nv3->getFonticon(),
  198.                                         "label"     => $nv3->getLabel(),
  199.                                         "path"      => $nv3->getPath(),
  200.                                     );
  201.                                     if($nv3->getPath()=="app_core_config_niveau01") {
  202.                                         $sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]["childs"][$nv3->getRoworder()]["label"]=$labelsniveau01;
  203.                                         $sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]["childs"][$nv3->getRoworder()]["fonticon"]=$iconniveau01;
  204.                                     }   
  205.                                     if($nv3->getPath()=="app_core_config_niveau02") {
  206.                                         if(!$viewniveau02)
  207.                                             unset($sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]["childs"][$nv3->getRoworder()]);
  208.                                         else {
  209.                                             $sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]["childs"][$nv3->getRoworder()]["label"]=$labelsniveau02;
  210.                                             $sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]["childs"][$nv3->getRoworder()]["fonticon"]=$iconniveau02;
  211.                                         }
  212.                                     }  
  213.                                     if($nv3->getPath()=="app_core_config_whitelist") {
  214.                                         if($moderegistration=="none"||$moderegistration=="byuser"
  215.                                             unset($sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]["childs"][$nv3->getRoworder()]);
  216.                                     }
  217.                                     if($nv3->getPath()=="app_core_config_registration") {
  218.                                         if($moderegistration=="none"
  219.                                             unset($sidebar[$nv1->getRoworder()]["childs"][$nv2->getRoworder()]["childs"][$nv3->getRoworder()]);
  220.                                     }
  221.                                 }
  222.                             }
  223.                         }
  224.                     }
  225.                     if(is_null($sidebar[$nv1->getRoworder()]["path"])&&!isset($sidebar[$nv1->getRoworder()]["childs"]))
  226.                         unset($sidebar[$nv1->getRoworder()]);
  227.                 } 
  228.             }
  229.             $session->set('sidebar'$sidebar);
  230.             if($app["portal_activate"]) {
  231.                 $session->set('cancreatepage',false);
  232.                 $session->set('cancreatecalendar',false);
  233.                 $session->set('cancreateblog',false);
  234.                 $session->set('cancreateproject',false);
  235.                 $session->remove("sublogo");
  236.                 if($curentuser!="anon.") {
  237.                     $groups=$curentuser->getGroups();
  238.                     foreach($groups as $usergroup) {
  239.                         $group=$usergroup->getGroup();
  240.                         if($group->getFgcancreatepage()) $session->set('cancreatepage',true);
  241.                         if($group->getFgcancreatecalendar()) $session->set('cancreatecalendar',true);
  242.                         if($group->getFgcancreateblog()) $session->set('cancreateblog',true);
  243.                         if($group->getFgcancreateproject()) $session->set('cancreateproject',true);
  244.                     }
  245.                     $niveau01=$curentuser->getNiveau01();
  246.                     if($niveau01->getShowsubappname()) 
  247.                         $session->set("subappname"$niveau01->getLabel());
  248.                     if(!is_null($niveau01->getLogo()))
  249.                         $session->set("sublogo"$niveau01->getLogo());
  250.                     if(!is_null($niveau01->getHeader()))
  251.                         $session->set("header"$niveau01->getHeader()); 
  252.                     if(!is_null($niveau01->getColormain()))
  253.                         $session->set("colormain"$niveau01->getColormain());                         
  254.                     if(!is_null($niveau01->getFontcolorhover()))
  255.                         $session->set("fontcolorhover"$niveau01->getFontcolorhover());                         
  256.                     if(!is_null($niveau01->getColorbody()))
  257.                         $session->set("colorbody"$niveau01->getColorbody());                         
  258.                     if(!is_null($niveau01->getFontfacebody()))
  259.                         $session->set("fontfacebody"$niveau01->getFontfacebody());                         
  260.                     if(!is_null($niveau01->getFontfacetitle()))
  261.                         $session->set("fontfacetitle"$niveau01->getFontfacetitle());                         
  262.                         
  263.                 }
  264.             }        
  265.             // Calcul des couleurs
  266.             $this->setColor($session);
  267.             
  268.             if($curentuser!="anon.") {
  269.                 // Préférence
  270.                 $preference=$curentuser->getPreference();
  271.                 if(is_array($preference)) {
  272.                     // Préférence header
  273.                     if(array_key_exists("fguserheader",$preference)) {
  274.                         $session->set("fguserheader"$fguserheader=$preference["fguserheader"][0]);
  275.                     }
  276.                     if($session->get("fgheader")=="0"$session->set("fguserheader","0");
  277.                     // Préférence header
  278.                     if(array_key_exists("pagebookmark",$preference)) {
  279.                         $pagebookmark=$preference["pagebookmark"][0];
  280.                         $session->set("pagebookmark"$pagebookmark);
  281.                     }
  282.                 }
  283.                 // Visite                
  284.                 $now=new \DateTime();
  285.                 if(!$curentuser->getVisitedate()) {
  286.                     $curentuser->setVisitedate($now);
  287.                     $curentuser->setVisitecpt($curentuser->getVisitecpt()+1);
  288.                     $this->em->persist($curentuser);
  289.                     $this->em->flush();                    
  290.                 }
  291.                 else {
  292.                     $visitedate=clone $curentuser->getVisitedate();
  293.                     $visitedate->add(new \DateInterval("PT1H"));
  294.                     if($visitedate<$now) {
  295.                         $curentuser->setVisitedate($now);
  296.                         $curentuser->setVisitecpt($curentuser->getVisitecpt()+1);
  297.                         $this->em->persist($curentuser);
  298.                         $this->em->flush();                    
  299.                     }
  300.                 }
  301.             }
  302.         }
  303.         public function setColor($session) {
  304.             $colormain      =$session->get('colormain');
  305.             $colorlight     =$this->adjustBrightness($colormain,+50);
  306.             $colordark      =$this->adjustBrightness($colormain,-50);
  307.             $colormainrgb   =$this->hexToRgb($colormain);
  308.             $colorlightrgb  =$this->hexToRgb($colorlight);
  309.             $colordarkrgb   =$this->hexToRgb($colordark);        
  310.     
  311.             $fontcolorhover =$session->get('fontcolorhover');
  312.             $colorbody      =$session->get('colorbody');
  313.     
  314.             $tbcolor=array(
  315.                 "main"                  =>$colormain,
  316.                 "light"                 =>$this->adjustBrightness($colormain,+50),
  317.                 "dark"                  =>$this->adjustBrightness($colormain,-50),
  318.                 "mainrgb"               =>$this->hexToRgb($colormain),
  319.                 "lightrgb"              =>$this->hexToRgb($colorlight),
  320.                 "darkrgb"               =>$this->hexToRgb($colordark),
  321.     
  322.                 "fontcolorhover"        =>$fontcolorhover,
  323.                 "fontcolorhoverlight"   =>$this->adjustBrightness($fontcolorhover,+50),
  324.                 "fontcolorhoverdark"    =>$this->adjustBrightness($fontcolorhover,-50),
  325.     
  326.                 "colorbody"             =>$colorbody,
  327.             );
  328.             
  329.             $session->set('color'$tbcolor);
  330.         }
  331.         public function adjustBrightness($hex$steps) {
  332.             // Steps should be between -255 and 255. Negative = darker, positive = lighter
  333.             $steps max(-255min(255$steps));
  334.     
  335.             // Normalize into a six character long hex string
  336.             $hex str_replace('#'''$hex);
  337.             if (strlen($hex) == 3) {
  338.                 $hex str_repeat(substr($hex,0,1), 2).str_repeat(substr($hex,1,1), 2).str_repeat(substr($hex,2,1), 2);
  339.             }
  340.     
  341.             // Split into three parts: R, G and B
  342.             $color_parts str_split($hex2);
  343.             $return '';
  344.     
  345.             foreach ($color_parts as $color) {
  346.                 $color   hexdec($color); // Convert to decimal
  347.                 $color   max(0,min(255,$color $steps)); // Adjust color
  348.                 $return .= str_pad(dechex($color), 2'0'STR_PAD_LEFT); // Make two char hex code
  349.             }
  350.     
  351.             return $return;
  352.         }
  353.     
  354.         public function hexToRgb($hex) {
  355.            $hex      str_replace('#'''$hex);
  356.            $length   strlen($hex);
  357.            $rgb['r'] = hexdec($length == substr($hex02) : ($length == str_repeat(substr($hex01), 2) : 0));
  358.            $rgb['g'] = hexdec($length == substr($hex22) : ($length == str_repeat(substr($hex11), 2) : 0));
  359.            $rgb['b'] = hexdec($length == substr($hex42) : ($length == str_repeat(substr($hex21), 2) : 0));
  360.     
  361.            return $rgb;
  362.        }        
  363.     }