vendor/contao/core-bundle/src/Resources/contao/classes/FrontendUser.php line 90

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. use Contao\CoreBundle\Security\ContaoCorePermissions;
  11. /**
  12.  * Provide methods to manage front end users.
  13.  *
  14.  * @property array  $allGroups
  15.  * @property string $loginPage
  16.  *
  17.  * @author Leo Feyer <https://github.com/leofeyer>
  18.  */
  19. class FrontendUser extends User
  20. {
  21.     /**
  22.      * Symfony Security session key
  23.      * @deprecated Deprecated since Contao 4.8, to be removed in Contao 5.0
  24.      */
  25.     const SECURITY_SESSION_KEY '_security_contao_frontend';
  26.     /**
  27.      * Current object instance (do not remove)
  28.      * @var FrontendUser
  29.      */
  30.     protected static $objInstance;
  31.     /**
  32.      * Name of the corresponding table
  33.      * @var string
  34.      */
  35.     protected $strTable 'tl_member';
  36.     /**
  37.      * Name of the current cookie
  38.      * @var string
  39.      */
  40.     protected $strCookie 'FE_USER_AUTH';
  41.     /**
  42.      * Group login page
  43.      * @var string
  44.      */
  45.     protected $strLoginPage;
  46.     /**
  47.      * Groups
  48.      * @var array
  49.      */
  50.     protected $arrGroups;
  51.     /**
  52.      * Symfony security roles
  53.      * @var array
  54.      */
  55.     protected $roles = array('ROLE_MEMBER');
  56.     /**
  57.      * Initialize the object
  58.      */
  59.     protected function __construct()
  60.     {
  61.         parent::__construct();
  62.         $this->strIp Environment::get('ip');
  63.         $this->strHash Input::cookie($this->strCookie);
  64.     }
  65.     /**
  66.      * Instantiate a new user object
  67.      *
  68.      * @return static|User The object instance
  69.      */
  70.     public static function getInstance()
  71.     {
  72.         if (static::$objInstance !== null)
  73.         {
  74.             return static::$objInstance;
  75.         }
  76.         $objToken System::getContainer()->get('security.token_storage')->getToken();
  77.         // Load the user from the security storage
  78.         if ($objToken !== null && is_a($objToken->getUser(), static::class))
  79.         {
  80.             return $objToken->getUser();
  81.         }
  82.         // Check for an authenticated user in the session
  83.         $strUser System::getContainer()->get('contao.security.token_checker')->getFrontendUsername();
  84.         if ($strUser !== null)
  85.         {
  86.             static::$objInstance = static::loadUserByUsername($strUser);
  87.             return static::$objInstance;
  88.         }
  89.         return parent::getInstance();
  90.     }
  91.     /**
  92.      * Extend parent setter class and modify some parameters
  93.      *
  94.      * @param string $strKey
  95.      * @param mixed  $varValue
  96.      */
  97.     public function __set($strKey$varValue)
  98.     {
  99.         if ($strKey == 'allGroups')
  100.         {
  101.             $this->arrGroups $varValue;
  102.         }
  103.         else
  104.         {
  105.             parent::__set($strKey$varValue);
  106.         }
  107.     }
  108.     /**
  109.      * Extend parent getter class and modify some parameters
  110.      *
  111.      * @param string $strKey
  112.      *
  113.      * @return mixed
  114.      */
  115.     public function __get($strKey)
  116.     {
  117.         switch ($strKey)
  118.         {
  119.             case 'allGroups':
  120.                 return $this->arrGroups;
  121.             case 'loginPage':
  122.                 return $this->strLoginPage;
  123.         }
  124.         return parent::__get($strKey);
  125.     }
  126.     /**
  127.      * Authenticate a user
  128.      *
  129.      * @return boolean
  130.      *
  131.      * @deprecated Deprecated since Contao 4.5, to be removed in Contao 5.0.
  132.      *             Use Symfony security instead.
  133.      */
  134.     public function authenticate()
  135.     {
  136.         trigger_deprecation('contao/core-bundle''4.5''Using "Contao\FrontendUser::authenticate()" has been deprecated and will no longer work in Contao 5.0. Use Symfony security instead.');
  137.         return System::getContainer()->get('contao.security.token_checker')->hasFrontendUser();
  138.     }
  139.     /**
  140.      * Try to login the current user
  141.      *
  142.      * @return boolean True if the user could be logged in
  143.      *
  144.      * @deprecated Deprecated since Contao 4.5, to be removed in Contao 5.0.
  145.      *             Use Symfony security instead.
  146.      */
  147.     public function login()
  148.     {
  149.         trigger_deprecation('contao/core-bundle''4.5''Using "Contao\FrontendUser::login()" has been deprecated and will no longer work in Contao 5.0. Use Symfony security instead.');
  150.         return System::getContainer()->get('contao.security.token_checker')->hasFrontendUser();
  151.     }
  152.     /**
  153.      * Save the original group membership
  154.      *
  155.      * @param string $strColumn
  156.      * @param mixed  $varValue
  157.      *
  158.      * @return boolean
  159.      */
  160.     public function findBy($strColumn$varValue)
  161.     {
  162.         if (parent::findBy($strColumn$varValue) === false)
  163.         {
  164.             return false;
  165.         }
  166.         $this->arrGroups $this->groups;
  167.         return true;
  168.     }
  169.     /**
  170.      * Restore the original group membership
  171.      */
  172.     public function save()
  173.     {
  174.         $groups $this->groups;
  175.         $this->arrData['groups'] = $this->arrGroups;
  176.         parent::save();
  177.         $this->groups $groups;
  178.     }
  179.     /**
  180.      * Set all user properties from a database record
  181.      */
  182.     protected function setUserFromDb()
  183.     {
  184.         $this->intId $this->id;
  185.         // Unserialize values
  186.         foreach ($this->arrData as $k=>$v)
  187.         {
  188.             if (!is_numeric($v))
  189.             {
  190.                 $this->$k StringUtil::deserialize($v);
  191.             }
  192.         }
  193.         $GLOBALS['TL_USERNAME'] = $this->username;
  194.         // Make sure that groups is an array
  195.         if (!\is_array($this->groups))
  196.         {
  197.             $this->groups $this->groups ? array($this->groups) : array();
  198.         }
  199.         // Skip inactive groups
  200.         if (($objGroups MemberGroupModel::findAllActive()) !== null)
  201.         {
  202.             $this->groups array_intersect($this->groups$objGroups->fetchEach('id'));
  203.         }
  204.         // Get the group login page
  205.         if ($this->groups[0] > 0)
  206.         {
  207.             $objGroup MemberGroupModel::findPublishedById($this->groups[0]);
  208.             if ($objGroup !== null && $objGroup->redirect && $objGroup->jumpTo)
  209.             {
  210.                 $this->strLoginPage $objGroup->jumpTo;
  211.             }
  212.         }
  213.     }
  214.     /**
  215.      * Return true if the user is member of a particular group
  216.      *
  217.      * @param mixed $ids A single group ID or an array of group IDs
  218.      *
  219.      * @return boolean True if the user is a member of the group
  220.      *
  221.      * @deprecated Deprecated since Contao 4.12, to be removed in Contao 5.0;
  222.      *             use Symfony security instead
  223.      */
  224.     public function isMemberOf($ids)
  225.     {
  226.         $security System::getContainer()->get('security.helper');
  227.         if ($security->getUser() === $this)
  228.         {
  229.             trigger_deprecation('contao/core-bundle''4.12''Using "Contao\FrontendUser::isMemberOf()" has been deprecated and will no longer work in Contao 5.0. Use Symfony security instead.');
  230.             return $security->isGranted(ContaoCorePermissions::MEMBER_IN_GROUPS$ids);
  231.         }
  232.         return parent::isMemberOf($ids);
  233.     }
  234.     /**
  235.      * {@inheritdoc}
  236.      */
  237.     public function getRoles()
  238.     {
  239.         return $this->roles;
  240.     }
  241. }
  242. class_alias(FrontendUser::class, 'FrontendUser');