6fc9f2356b6a833c64f6730355f200f20b3fd5a7
[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\Service\AbstractService;
18
19 /**
20 * Authentication services class
21 */
22 class AbstractAuthenticationService extends AbstractService
23 {
24 /**
25 * User object
26 *
27 * @var AbstractUserAuthentication
28 */
29 public $pObj;
30
31 /**
32 * Subtype of the service which is used to call the service.
33 *
34 * @var string
35 */
36 public $mode;
37
38 /**
39 * Submitted login form data
40 *
41 * @var array
42 */
43 public $login = [];
44
45 /**
46 * Various data
47 *
48 * @var array
49 */
50 public $authInfo = [];
51
52 /**
53 * User db table definition
54 *
55 * @var array
56 */
57 public $db_user = [];
58
59 /**
60 * Usergroups db table definition
61 *
62 * @var array
63 */
64 public $db_groups = [];
65
66 /**
67 * If the writelog() functions is called if a login-attempt has be tried without success
68 *
69 * @var bool
70 */
71 public $writeAttemptLog = false;
72
73 /**
74 * Initialize authentication service
75 *
76 * @param string $mode Subtype of the service which is used to call the service.
77 * @param array $loginData Submitted login form data
78 * @param array $authInfo Information array. Holds submitted form data etc.
79 * @param AbstractUserAuthentication $pObj Parent object
80 */
81 public function initAuth($mode, $loginData, $authInfo, $pObj)
82 {
83 $this->pObj = $pObj;
84 // Sub type
85 $this->mode = $mode;
86 $this->login = $loginData;
87 $this->authInfo = $authInfo;
88 $this->db_user = $this->getServiceOption('db_user', $authInfo['db_user'] ?? [], false);
89 $this->db_groups = $this->getServiceOption('db_groups', $authInfo['db_groups'] ?? [], false);
90 $this->writeAttemptLog = $this->pObj->writeAttemptLog ?? true;
91 }
92
93 /**
94 * Check the login data with the user record data for builtin login methods
95 *
96 * @param array $user User data array
97 * @param array $loginData Login data array
98 * @param string $passwordCompareStrategy Password compare strategy
99 * @return bool TRUE if login data matched
100 * @deprecated since TYPO3 v9, will be removed in TYPO3 v10.
101 */
102 public function compareUident(array $user, array $loginData, $passwordCompareStrategy = '')
103 {
104 trigger_error('This method will be removed in TYPO3 v10.', E_USER_DEPRECATED);
105 return $this->pObj->compareUident($user, $loginData, $passwordCompareStrategy);
106 }
107
108 /**
109 * Writes to log database table in pObj
110 *
111 * @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
112 * @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 !!)
113 * @param int $error flag. 0 = message, 1 = error (user problem), 2 = System Error (which should not happen), 3 = security notice (admin)
114 * @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
115 * @param string $details Default text that follows the message
116 * @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...
117 * @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.)
118 * @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.)
119 * @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.)
120 */
121 public function writelog($type, $action, $error, $details_nr, $details, $data, $tablename = '', $recuid = '', $recpid = '')
122 {
123 if ($this->writeAttemptLog) {
124 $this->pObj->writelog($type, $action, $error, $details_nr, $details, $data, $tablename, $recuid, $recpid);
125 }
126 }
127
128 /**
129 * Get a user from DB by username
130 *
131 * @param string $username User name
132 * @param string $extraWhere Additional WHERE clause: " AND ...
133 * @param array|string $dbUserSetup User db table definition, or empty string for $this->db_user
134 * @return mixed User array or FALSE
135 */
136 public function fetchUserRecord($username, $extraWhere = '', $dbUserSetup = '')
137 {
138 $dbUser = is_array($dbUserSetup) ? $dbUserSetup : $this->db_user;
139 $user = $this->pObj->fetchUserRecord($dbUser, $username, $extraWhere);
140 return $user;
141 }
142 }