TYPO3  7.6
AuthenticationService.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Sv;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
23 {
32  public function processLoginData(array &$loginData, $passwordTransmissionStrategy)
33  {
34  $isProcessed = false;
35  if ($passwordTransmissionStrategy === 'normal') {
36  $loginData['uident_text'] = $loginData['uident'];
37  $isProcessed = true;
38  }
39  return $isProcessed;
40  }
41 
47  public function getUser()
48  {
49  if ($this->login['status'] !== 'login') {
50  return false;
51  }
52  if (!$this->login['uident']) {
53  // Failed Login attempt (no password given)
54  $this->writelog(255, 3, 3, 2, 'Login-attempt from %s (%s) for username \'%s\' with an empty password!', array(
55  $this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname']
56  ));
57  GeneralUtility::sysLog(sprintf('Login-attempt from %s (%s), for username \'%s\' with an empty password!', $this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname']), 'Core', GeneralUtility::SYSLOG_SEVERITY_WARNING);
58  return false;
59  }
60 
61  $user = $this->fetchUserRecord($this->login['uname']);
62  if (!is_array($user)) {
63  // Failed login attempt (no username found)
64  $this->writelog(255, 3, 3, 2, 'Login-attempt from %s (%s), username \'%s\' not found!!', array($this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname']));
65  // Logout written to log
66  GeneralUtility::sysLog(sprintf('Login-attempt from %s (%s), username \'%s\' not found!', $this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname']), 'core', GeneralUtility::SYSLOG_SEVERITY_WARNING);
67  } else {
68  if ($this->writeDevLog) {
69  GeneralUtility::devLog('User found: ' . GeneralUtility::arrayToLogString($user, array($this->db_user['userid_column'], $this->db_user['username_column'])), AuthenticationService::class);
70  }
71  }
72  return $user;
73  }
74 
88  public function authUser(array $user)
89  {
90  $OK = 100;
91  if ($this->login['uident'] && $this->login['uname']) {
92  // Checking password match for user:
93  $OK = $this->compareUident($user, $this->login);
94  if (!$OK) {
95  // Failed login attempt (wrong password) - write that to the log!
96  if ($this->writeAttemptLog) {
97  $this->writelog(255, 3, 3, 1, 'Login-attempt from %s (%s), username \'%s\', password not accepted!', array($this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname']));
98  GeneralUtility::sysLog(sprintf('Login-attempt from %s (%s), username \'%s\', password not accepted!', $this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $this->login['uname']), 'core', GeneralUtility::SYSLOG_SEVERITY_WARNING);
99  }
100  if ($this->writeDevLog) {
101  GeneralUtility::devLog('Password not accepted: ' . $this->login['uident'], AuthenticationService::class, 2);
102  }
103  }
104  // Checking the domain (lockToDomain)
105  if ($OK && $user['lockToDomain'] && $user['lockToDomain'] !== $this->authInfo['HTTP_HOST']) {
106  // Lock domain didn't match, so error:
107  if ($this->writeAttemptLog) {
108  $this->writelog(255, 3, 3, 1, 'Login-attempt from %s (%s), username \'%s\', locked domain \'%s\' did not match \'%s\'!', array($this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $user[$this->db_user['username_column']], $user['lockToDomain'], $this->authInfo['HTTP_HOST']));
109  GeneralUtility::sysLog(sprintf('Login-attempt from %s (%s), username \'%s\', locked domain \'%s\' did not match \'%s\'!', $this->authInfo['REMOTE_ADDR'], $this->authInfo['REMOTE_HOST'], $user[$this->db_user['username_column']], $user['lockToDomain'], $this->authInfo['HTTP_HOST']), 'core', GeneralUtility::SYSLOG_SEVERITY_WARNING);
110  }
111  $OK = 0;
112  }
113  }
114  return $OK;
115  }
116 
124  public function getGroups($user, $knownGroups)
125  {
126  /*
127  * Attention: $knownGroups is not used within this method, but other services can use it.
128  * This parameter should not be removed!
129  * The FrontendUserAuthentication call getGroups and handover the previous detected groups.
130  */
131  $groupDataArr = array();
132  if ($this->mode === 'getGroupsFE') {
133  $groups = array();
134  if (is_array($user) && $user[$this->db_user['usergroup_column']]) {
135  $groupList = $user[$this->db_user['usergroup_column']];
136  $groups = array();
137  $this->getSubGroups($groupList, '', $groups);
138  }
139  // ADD group-numbers if the IPmask matches.
140  if (is_array($GLOBALS['TYPO3_CONF_VARS']['FE']['IPmaskMountGroups'])) {
141  foreach ($GLOBALS['TYPO3_CONF_VARS']['FE']['IPmaskMountGroups'] as $IPel) {
142  if ($this->authInfo['REMOTE_ADDR'] && $IPel[0] && GeneralUtility::cmpIP($this->authInfo['REMOTE_ADDR'], $IPel[0])) {
143  $groups[] = (int)$IPel[1];
144  }
145  }
146  }
147  $groups = array_unique($groups);
148  if (!empty($groups)) {
149  $list = implode(',', $groups);
150  if ($this->writeDevLog) {
151  GeneralUtility::devLog('Get usergroups with id: ' . $list, __CLASS__);
152  }
153  $lockToDomain_SQL = ' AND (lockToDomain=\'\' OR lockToDomain IS NULL OR lockToDomain=\'' . $this->authInfo['HTTP_HOST'] . '\')';
154  $hiddenP = !$this->authInfo['showHiddenRecords'] ? 'AND hidden=0 ' : '';
155  $res = $this->getDatabaseConnection()->exec_SELECTquery('*', $this->db_groups['table'], 'deleted=0 ' . $hiddenP . ' AND uid IN (' . $list . ')' . $lockToDomain_SQL);
156  while ($row = $this->getDatabaseConnection()->sql_fetch_assoc($res)) {
157  $groupDataArr[$row['uid']] = $row;
158  }
159  if ($res) {
160  $this->getDatabaseConnection()->sql_free_result($res);
161  }
162  } else {
163  if ($this->writeDevLog) {
164  GeneralUtility::devLog('No usergroups found.', AuthenticationService::class, 2);
165  }
166  }
167  }
168  return $groupDataArr;
169  }
170 
182  public function getSubGroups($grList, $idList = '', &$groups)
183  {
184  // Fetching records of the groups in $grList (which are not blocked by lockedToDomain either):
185  $lockToDomain_SQL = ' AND (lockToDomain=\'\' OR lockToDomain IS NULL OR lockToDomain=\'' . $this->authInfo['HTTP_HOST'] . '\')';
186  $hiddenP = !$this->authInfo['showHiddenRecords'] ? 'AND hidden=0 ' : '';
187  $res = $this->getDatabaseConnection()->exec_SELECTquery('uid,subgroup', 'fe_groups', 'deleted=0 ' . $hiddenP . ' AND uid IN (' . $grList . ')' . $lockToDomain_SQL);
188  // Internal group record storage
189  $groupRows = array();
190  // The groups array is filled
191  while ($row = $this->getDatabaseConnection()->sql_fetch_assoc($res)) {
192  if (!in_array($row['uid'], $groups)) {
193  $groups[] = $row['uid'];
194  }
195  $groupRows[$row['uid']] = $row;
196  }
197  // Traversing records in the correct order
198  $include_staticArr = GeneralUtility::intExplode(',', $grList);
199  // traversing list
200  foreach ($include_staticArr as $uid) {
201  // Get row:
202  $row = $groupRows[$uid];
203  // Must be an array and $uid should not be in the idList, because then it is somewhere previously in the grouplist
204  if (is_array($row) && !GeneralUtility::inList($idList, $uid)) {
205  // Include sub groups
206  if (trim($row['subgroup'])) {
207  // Make integer list
208  $theList = implode(',', GeneralUtility::intExplode(',', $row['subgroup']));
209  // Call recursively, pass along list of already processed groups so they are not processed again.
210  $this->getSubGroups($theList, $idList . ',' . $uid, $groups);
211  }
212  }
213  }
214  }
215 
221  protected function getDatabaseConnection()
222  {
223  return $GLOBALS['TYPO3_DB'];
224  }
225 }