Conditions | 27 |
Paths | 736 |
Total Lines | 137 |
Code Lines | 77 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
96 | public function __isAllowed() |
||
97 | { |
||
98 | // phpcs:enable |
||
99 | global $conf, $db, $user; |
||
100 | |||
101 | $login = ''; |
||
102 | $stored_key = ''; |
||
103 | |||
104 | $userClass = Defaults::$userIdentifierClass; |
||
105 | |||
106 | /*foreach ($_SERVER as $key => $val) |
||
107 | { |
||
108 | dol_syslog($key.' - '.$val); |
||
109 | }*/ |
||
110 | |||
111 | // api key can be provided in url with parameter api_key=xxx or ni header with header DOLAPIKEY:xxx |
||
112 | $api_key = ''; |
||
113 | if (isset($_GET['api_key'])) { // For backward compatibility. Keep $_GET here. |
||
114 | // TODO Add option to disable use of api key on url. Return errors if used. |
||
115 | $api_key = $_GET['api_key']; |
||
116 | } |
||
117 | if (isset($_GET['DOLAPIKEY'])) { |
||
118 | // TODO Add option to disable use of api key on url. Return errors if used. |
||
119 | $api_key = $_GET['DOLAPIKEY']; // With GET method |
||
120 | } |
||
121 | if (isset($_SERVER['HTTP_DOLAPIKEY'])) { // Param DOLAPIKEY in header can be read with HTTP_DOLAPIKEY |
||
122 | $api_key = $_SERVER['HTTP_DOLAPIKEY']; // With header method (recommended) |
||
123 | } |
||
124 | |||
125 | $api_key = dol_string_nounprintableascii($api_key); |
||
126 | |||
127 | if (preg_match('/^dolcrypt:/i', $api_key)) { |
||
128 | throw new RestException(503, 'Bad value for the API key. An API key should not start with dolcrypt:'); |
||
129 | } |
||
130 | |||
131 | if ($api_key) { |
||
132 | $userentity = 0; |
||
133 | |||
134 | $sql = "SELECT u.login, u.datec, u.api_key,"; |
||
135 | $sql .= " u.tms as date_modification, u.entity"; |
||
136 | $sql .= " FROM " . MAIN_DB_PREFIX . "user as u"; |
||
137 | $sql .= " WHERE u.api_key = '" . $this->db->escape($api_key) . "' OR u.api_key = '" . $this->db->escape(dolEncrypt($api_key, '', '', 'dolibarr')) . "'"; |
||
138 | |||
139 | $result = $this->db->query($sql); |
||
140 | if ($result) { |
||
141 | $nbrows = $this->db->num_rows($result); |
||
142 | if ($nbrows == 1) { |
||
143 | $obj = $this->db->fetch_object($result); |
||
144 | $login = $obj->login; |
||
145 | $stored_key = dolDecrypt($obj->api_key); |
||
146 | $userentity = $obj->entity; |
||
147 | |||
148 | if (!defined("DOLENTITY") && $conf->entity != ($obj->entity ? $obj->entity : 1)) { // If API was not forced with HTTP_DOLENTITY, and user is on another entity, so we reset entity to entity of user |
||
149 | $conf->entity = ($obj->entity ? $obj->entity : 1); |
||
150 | // We must also reload global conf to get params from the entity |
||
151 | dol_syslog("Entity was not set on http header with HTTP_DOLAPIENTITY (recommended for performance purpose), so we switch now on entity of user (" . $conf->entity . ") and we have to reload configuration.", LOG_WARNING); |
||
152 | $conf->setValues($this->db); |
||
153 | } |
||
154 | } elseif ($nbrows > 1) { |
||
155 | throw new RestException(503, 'Error when fetching user api_key : More than 1 user with this apikey'); |
||
156 | } |
||
157 | } else { |
||
158 | throw new RestException(503, 'Error when fetching user api_key :' . $this->db->error_msg); |
||
159 | } |
||
160 | |||
161 | if ($login && $stored_key != $api_key) { // This should not happen since we did a search on api_key |
||
162 | $userClass::setCacheIdentifier($api_key); |
||
163 | return false; |
||
164 | } |
||
165 | |||
166 | $genericmessageerroruser = 'Error user not valid (not found with api key or bad status or bad validity dates) (conf->entity=' . $conf->entity . ')'; |
||
167 | |||
168 | if (!$login) { |
||
169 | dol_syslog("functions_isallowed::check_user_api_key Authentication KO for api key: Error when searching login user from api key", LOG_NOTICE); |
||
170 | sleep(1); // Anti brute force protection. Must be same delay when user and password are not valid. |
||
171 | throw new RestException(401, $genericmessageerroruser); |
||
172 | } |
||
173 | |||
174 | $fuser = new User($this->db); |
||
175 | $result = $fuser->fetch('', $login, '', 0, (empty($userentity) ? -1 : $conf->entity)); // If user is not entity 0, we search in working entity $conf->entity (that may have been forced to a different value than user entity) |
||
176 | if ($result <= 0) { |
||
177 | dol_syslog("functions_isallowed::check_user_api_key Authentication KO for '" . $login . "': Failed to fetch on entity", LOG_NOTICE); |
||
178 | sleep(1); // Anti brute force protection. Must be same delay when user and password are not valid. |
||
179 | throw new RestException(401, $genericmessageerroruser); |
||
180 | } |
||
181 | |||
182 | // Check if user status is enabled |
||
183 | if ($fuser->statut != $fuser::STATUS_ENABLED) { |
||
184 | // Status is disabled |
||
185 | dol_syslog("functions_isallowed::check_user_api_key Authentication KO for '" . $login . "': The user has been disabled", LOG_NOTICE); |
||
186 | sleep(1); // Anti brute force protection. Must be same delay when user and password are not valid. |
||
187 | throw new RestException(401, $genericmessageerroruser); |
||
188 | } |
||
189 | |||
190 | // Check if session was unvalidated by a password change |
||
191 | if (($fuser->flagdelsessionsbefore && !empty($_SESSION["dol_logindate"]) && $fuser->flagdelsessionsbefore > $_SESSION["dol_logindate"])) { |
||
192 | // Session is no more valid |
||
193 | dol_syslog("functions_isallowed::check_user_api_key Authentication KO for '" . $login . "': The user has a date for session invalidation = " . $fuser->flagdelsessionsbefore . " and a session date = " . $_SESSION["dol_logindate"] . ". We must invalidate its sessions."); |
||
194 | sleep(1); // Anti brute force protection. Must be same delay when user and password are not valid. |
||
195 | throw new RestException(401, $genericmessageerroruser); |
||
196 | } |
||
197 | |||
198 | // Check date validity |
||
199 | if ($fuser->isNotIntoValidityDateRange()) { |
||
200 | // User validity dates are no more valid |
||
201 | dol_syslog("functions_isallowed::check_user_api_key Authentication KO for '" . $login . "': The user login has a validity between [" . $fuser->datestartvalidity . " and " . $fuser->dateendvalidity . "], current date is " . dol_now()); |
||
202 | sleep(1); // Anti brute force protection. Must be same delay when user and password are not valid. |
||
203 | throw new RestException(401, $genericmessageerroruser); |
||
204 | } |
||
205 | |||
206 | // User seems valid |
||
207 | $fuser->getrights(); |
||
208 | |||
209 | // Set the property $user to the $user of API |
||
210 | static::$user = $fuser; |
||
211 | |||
212 | // Set also the global variable $user to the $user of API |
||
213 | $user = $fuser; |
||
214 | |||
215 | if ($fuser->socid) { |
||
216 | static::$role = 'external'; |
||
217 | } |
||
218 | |||
219 | if ($fuser->admin) { |
||
220 | static::$role = 'admin'; |
||
221 | } |
||
222 | } else { |
||
223 | throw new RestException(401, "Failed to login to API. No parameter 'HTTP_DOLAPIKEY' on HTTP header (and no parameter DOLAPIKEY in URL)."); |
||
224 | } |
||
225 | |||
226 | $userClass::setCacheIdentifier(static::$role); |
||
227 | Resources::$accessControlFunction = 'DolibarrApiAccess::verifyAccess'; |
||
228 | $requirefortest = static::$requires; |
||
229 | if (!is_array($requirefortest)) { |
||
230 | $requirefortest = explode(',', $requirefortest); |
||
231 | } |
||
232 | return in_array(static::$role, (array) $requirefortest) || static::$role == 'admin'; |
||
233 | } |
||
265 |