[TASK] Deprecate AbstractUserAuthentication->fetchUserRecord
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Authentication / AbstractAuthenticationService.php
1 <?php
2 namespace TYPO3\CMS\Core\Authentication;
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
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Database\Query\QueryHelper;
19 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
20 use TYPO3\CMS\Core\Service\AbstractService;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Authentication services class
25 */
26 class AbstractAuthenticationService extends AbstractService
27 {
28 /**
29 * User object
30 *
31 * @var AbstractUserAuthentication
32 */
33 public $pObj;
34
35 /**
36 * Subtype of the service which is used to call the service.
37 *
38 * @var string
39 */
40 public $mode;
41
42 /**
43 * Submitted login form data
44 *
45 * @var array
46 */
47 public $login = [];
48
49 /**
50 * Various data
51 *
52 * @var array
53 */
54 public $authInfo = [];
55
56 /**
57 * User db table definition
58 *
59 * @var array
60 */
61 public $db_user = [];
62
63 /**
64 * Usergroups db table definition
65 *
66 * @var array
67 */
68 public $db_groups = [];
69
70 /**
71 * If the writelog() functions is called if a login-attempt has be tried without success
72 *
73 * @var bool
74 */
75 public $writeAttemptLog = false;
76
77 /**
78 * Initialize authentication service
79 *
80 * @param string $mode Subtype of the service which is used to call the service.
81 * @param array $loginData Submitted login form data
82 * @param array $authInfo Information array. Holds submitted form data etc.
83 * @param AbstractUserAuthentication $pObj Parent object
84 */
85 public function initAuth($mode, $loginData, $authInfo, $pObj)
86 {
87 $this->pObj = $pObj;
88 // Sub type
89 $this->mode = $mode;
90 $this->login = $loginData;
91 $this->authInfo = $authInfo;
92 $this->db_user = $this->getServiceOption('db_user', $authInfo['db_user'] ?? [], false);
93 $this->db_groups = $this->getServiceOption('db_groups', $authInfo['db_groups'] ?? [], false);
94 $this->writeAttemptLog = $this->pObj->writeAttemptLog ?? true;
95 }
96
97 /**
98 * Check the login data with the user record data for builtin login methods
99 *
100 * @param array $user User data array
101 * @param array $loginData Login data array
102 * @param string $passwordCompareStrategy Password compare strategy
103 * @return bool TRUE if login data matched
104 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.
105 */
106 public function compareUident(array $user, array $loginData, $passwordCompareStrategy = '')
107 {
108 trigger_error('This method will be removed in TYPO3 v10.', E_USER_DEPRECATED);
109 return $this->pObj->compareUident($user, $loginData, $passwordCompareStrategy);
110 }
111
112 /**
113 * Writes to log database table in pObj
114 *
115 * @param int $type denotes which module that has submitted the entry. This is the current list: 1=tce_db; 2=tce_file; 3=system (eg. sys_history save); 4=modules; 254=Personal settings changed; 255=login / out action: 1=login, 2=logout, 3=failed login (+ errorcode 3), 4=failure_warning_email sent
116 * @param int $action denotes which specific operation that wrote the entry (eg. 'delete', 'upload', 'update' and so on...). Specific for each $type. Also used to trigger update of the interface. (see the log-module for the meaning of each number !!)
117 * @param int $error flag. 0 = message, 1 = error (user problem), 2 = System Error (which should not happen), 3 = security notice (admin)
118 * @param int $details_nr The message number. Specific for each $type and $action. in the future this will make it possible to translate error messages to other languages
119 * @param string $details Default text that follows the message
120 * @param array $data Data that follows the log. Might be used to carry special information. If an array the first 5 entries (0-4) will be sprintf'ed the details-text...
121 * @param string $tablename Special field used by tce_main.php. These ($tablename, $recuid, $recpid) holds the reference to the record which the log-entry is about. (Was used in attic status.php to update the interface.)
122 * @param int|string $recuid Special field used by tce_main.php. These ($tablename, $recuid, $recpid) holds the reference to the record which the log-entry is about. (Was used in attic status.php to update the interface.)
123 * @param int|string $recpid Special field used by tce_main.php. These ($tablename, $recuid, $recpid) holds the reference to the record which the log-entry is about. (Was used in attic status.php to update the interface.)
124 */
125 public function writelog($type, $action, $error, $details_nr, $details, $data, $tablename = '', $recuid = '', $recpid = '')
126 {
127 if ($this->writeAttemptLog) {
128 $this->pObj->writelog($type, $action, $error, $details_nr, $details, $data, $tablename, $recuid, $recpid);
129 }
130 }
131
132 /**
133 * Get a user from DB by username
134 *
135 * @param string $username User name
136 * @param string $extraWhere Additional WHERE clause: " AND ...
137 * @param array|string $dbUserSetup User db table definition, or empty string for $this->db_user
138 * @return mixed User array or FALSE
139 */
140 public function fetchUserRecord($username, $extraWhere = '', $dbUserSetup = '')
141 {
142 $dbUser = is_array($dbUserSetup) ? $dbUserSetup : $this->db_user;
143 $user = false;
144 if ($username || $extraWhere) {
145 $query = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($dbUser['table']);
146 $query->getRestrictions()->removeAll()
147 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
148 $constraints = array_filter([
149 QueryHelper::stripLogicalOperatorPrefix($dbUser['check_pid_clause']),
150 QueryHelper::stripLogicalOperatorPrefix($dbUser['enable_clause']),
151 QueryHelper::stripLogicalOperatorPrefix($extraWhere),
152 ]);
153 if (!empty($username)) {
154 array_unshift(
155 $constraints,
156 $query->expr()->eq(
157 $dbUser['username_column'],
158 $query->createNamedParameter($username, \PDO::PARAM_STR)
159 )
160 );
161 }
162 $user = $query->select('*')
163 ->from($dbUser['table'])
164 ->where(...$constraints)
165 ->execute()
166 ->fetch();
167 }
168 return $user;
169 }
170 }