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

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