Passed
Push — master ( b567c0...c4a4cd )
by Andreas
24:57
created

midcom_services_auth::can_user_do()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 6
nop 3
dl 0
loc 18
ccs 9
cts 10
cp 0.9
crap 5.025
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.services
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use Symfony\Component\HttpFoundation\Request;
10
11
/**
12
 * Main Authentication/Authorization service class, it provides means to authenticate
13
 * users and to check for permissions.
14
 *
15
 * <b>Authentication</b>
16
 *
17
 * Whenever the system successfully creates a new login session (during auth service startup),
18
 * it checks whether the key <i>midcom_services_auth_login_success_url</i> is present in the HTTP
19
 * Request data. If this is the case, it relocates to the URL given in it. This member isn't set
20
 * by default in the MidCOM core, it is intended for custom authentication forms. The MidCOM
21
 * relocate function is used to for relocation, thus you can take full advantage of the
22
 * convenience functions in there. See midcom_application::relocate() for details.
23
 *
24
 * <b>Checking Privileges</b>
25
 *
26
 * This class offers various methods to verify the privilege state of a user, all of them prefixed
27
 * with can_* for privileges and is_* for membership checks.
28
 *
29
 * Each function is available in a simple check version, which returns true or false, and a
30
 * require_* prefixed variant, which has no return value. The require variants of these calls
31
 * instead check if the given condition is met, if yes, they return silently, otherwise they
32
 * throw an access denied error.
33
 *
34
 * @todo Fully document authentication.
35
 * @package midcom.services
36
 */
37
class midcom_services_auth
38
{
39
    /**
40
     * The currently authenticated user or null in case of anonymous access.
41
     * It is to be considered read-only.
42
     *
43
     * @var midcom_core_user
44
     */
45
    public $user;
46
47
    /**
48
     * Admin user level state. This is true if the currently authenticated user is an
49
     * Midgard Administrator, false otherwise.
50
     *
51
     * This effectively maps to midcom_connection::is_admin(); but it is suggested to use the auth class
52
     * for consistency reasons nevertheless.
53
     *
54
     * @var boolean
55
     */
56
    public $admin = false;
57
58
    /**
59
     * The ACL management system.
60
     *
61
     * @var midcom_services_auth_acl
62
     */
63
    public $acl;
64
65
    /**
66
     * Internal cache of all loaded groups, indexed by their identifiers.
67
     *
68
     * @var Array
69
     */
70
    private $_group_cache = [];
71
72
    /**
73
     * Internal cache of all loaded users, indexed by their identifiers.
74
     *
75
     * @var Array
76
     */
77
    private $_user_cache = [];
78
79
    /**
80
     * This flag indicates if sudo mode is active during execution. This will only be the
81
     * case if the sudo system actually grants this privileges, and only until components
82
     * release the rights again. This does override the full access control system at this time
83
     * and essentially give you full admin privileges (though this might change in the future).
84
     *
85
     * Note, that this is no boolean but an int, otherwise it would be impossible to trace nested
86
     * sudo invocations, which are quite possible with multiple components calling each others
87
     * callback. A value of 0 indicates that sudo is inactive. A value greater than zero indicates
88
     * sudo mode is active, with the count being equal to the depth of the sudo callers.
89
     *
90
     * It is thus still safely possible to evaluate this member in a boolean context to check
91
     * for an enabled sudo mode.
92
     *
93
     * @var int
94
     * @see request_sudo()
95
     * @see drop_sudo()
96
     */
97
    private $_component_sudo = 0;
98
99
    /**
100
     * The authentication backend we should use by default.
101
     *
102
     * @var midcom_services_auth_backend
103
     */
104
    private $backend;
105
106
    /**
107
     * The authentication frontend we should use by default.
108
     *
109
     * @var midcom_services_auth_frontend
110
     */
111
    private $frontend;
112
113
    /**
114
     * Loads all configured authentication drivers.
115
     */
116
    public function __construct(midcom_services_auth_acl $acl)
117
    {
118
        $this->acl = $acl;
119
120
        $classname = midcom::get()->config->get('auth_backend');
121
        if (!str_contains($classname, "_")) {
0 ignored issues
show
Bug introduced by
It seems like $classname can also be of type null; however, parameter $haystack of str_contains() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
        if (!str_contains(/** @scrutinizer ignore-type */ $classname, "_")) {
Loading history...
122
            $classname = 'midcom_services_auth_backend_' . $classname;
123
        }
124
        $this->backend = new $classname($this);
125
126
        $classname = midcom::get()->config->get('auth_frontend');
127
        if (!str_contains($classname, "_")) {
128
            $classname = 'midcom_services_auth_frontend_' . $classname;
129
        }
130
        $this->frontend = new $classname();
131
    }
132
133
    /**
134
     * Checks if the current authentication fronted has new credentials
135
     * ready. If yes, it processes the login accordingly. Otherwise look for existing session
136
     *
137
     * @param Request $request The request object
138
     */
139 1
    public function check_for_login_session(Request $request)
140
    {
141
        // Try to start up a new session, this will authenticate as well.
142 1
        if ($credentials = $this->frontend->read_login_data($request)) {
143
            if (!$this->login($credentials['username'], $credentials['password'], $request->getClientIp())) {
144
                return;
145
            }
146
            debug_add('Authentication was successful, we have a new login session now. Updating timestamps');
147
148
            $person_class = midcom::get()->config->get('person_class');
149
            $person = new $person_class($this->user->guid);
150
151
            if (!$person->get_parameter('midcom', 'first_login')) {
152
                $person->set_parameter('midcom', 'first_login', time());
153
            } elseif (midcom::get()->config->get('auth_save_prev_login')) {
154
                $person->set_parameter('midcom', 'prev_login', $person->get_parameter('midcom', 'last_login'));
155
            }
156
            $person->set_parameter('midcom', 'last_login', time());
157
158
            // Now we check whether there is a success-relocate URL given somewhere.
159
            if ($request->get('midcom_services_auth_login_success_url')) {
160
                return new midcom_response_relocate($request->get('midcom_services_auth_login_success_url'));
0 ignored issues
show
Bug introduced by
It seems like $request->get('midcom_se...uth_login_success_url') can also be of type null; however, parameter $url of midcom_response_relocate::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

160
                return new midcom_response_relocate(/** @scrutinizer ignore-type */ $request->get('midcom_services_auth_login_success_url'));
Loading history...
161
            }
162
        }
163
        // No new login detected, so we check if there is a running session.
164 1
        elseif ($user = $this->backend->check_for_active_login_session($request)) {
165
            $this->set_user($user);
166
        }
167 1
    }
168
169
    /**
170
     * @param midcom_core_user $user
171
     */
172 62
    private function set_user(midcom_core_user $user)
173
    {
174 62
        $this->user = $user;
175 62
        $this->admin = $user->is_admin();
176 62
    }
177
178
    /**
179
     * Checks whether a user has a certain privilege on the given content object.
180
     * Works on the currently authenticated user by default, but can take another
181
     * user as an optional argument.
182
     *
183
     * @param string $privilege The privilege to check for
184
     * @param MidgardObject $content_object A Midgard Content Object
0 ignored issues
show
Bug introduced by
The type MidgardObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
185
     * @param midcom_core_user $user The user against which to check the privilege, defaults to the currently authenticated user.
186
     *     You may specify "EVERYONE" instead of an object to check what an anonymous user can do.
187
     */
188 440
    public function can_do(string $privilege, object $content_object, $user = null) : bool
189
    {
190 440
        if ($this->is_admin($user)) {
191
            // Administrators always have access.
192 2
            return true;
193
        }
194
195 440
        $user_id = $this->acl->get_user_id($user);
196
197
        //if we're handed the correct object type, we use its class right away
198 440
        if (midcom::get()->dbclassloader->is_midcom_db_object($content_object)) {
199 440
            $content_object_class = get_class($content_object);
200
        }
201
        //otherwise, we assume (hope) that it's a midgard object
202
        else {
203
            $content_object_class = midcom::get()->dbclassloader->get_midcom_class_name_for_mgdschema_object($content_object);
204
        }
205
206 440
        return $this->acl->can_do_byguid($privilege, $content_object->guid, $content_object_class, $user_id);
207
    }
208
209 450
    private function is_admin($user) : bool
210
    {
211 450
        if ($user === null) {
212 450
            return $this->user && $this->admin;
213
        }
214 5
        if (is_a($user, midcom_core_user::class)) {
215 2
            return $user->is_admin();
216
        }
217 3
        return false;
218
    }
219
220
    /**
221
     * Checks, whether the given user have the privilege assigned to him in general.
222
     * Be aware, that this does not take any permissions overridden by content objects
223
     * into account. Whenever possible, you should user the can_do() variant of this
224
     * call therefore. can_user_do is only of interest in cases where you do not have
225
     * any content object available, for example when creating root topics.
226
     *
227
     * @param midcom_core_user $user The user against which to check the privilege, defaults to the currently authenticated user,
228
     *     you may specify 'EVERYONE' here to check what an anonymous user can do.
229
     * @param string $class Optional parameter to set if the check should take type specific permissions into account. The class must be default constructible.
230
     */
231 334
    public function can_user_do(string $privilege, $user = null, $class = null) : bool
232
    {
233 334
        if ($this->is_admin($user)) {
234
            // Administrators always have access.
235 2
            return true;
236
        }
237 334
        if ($this->_component_sudo) {
238 331
            return true;
239
        }
240 7
        if ($user === null) {
241 7
            $user =& $this->user;
242
        }
243
244 7
        if ($user == 'EVERYONE') {
0 ignored issues
show
introduced by
The condition $user == 'EVERYONE' is always false.
Loading history...
245
            $user = null;
246
        }
247
248 7
        return $this->acl->can_do_byclass($privilege, $user, $class);
249
    }
250
251
    /**
252
     * Request superuser privileges for the domain passed.
253
     *
254
     * STUB IMPLEMENTATION ONLY, WILL ALWAYS GRANT SUDO.
255
     *
256
     * You have to call midcom_services_auth::drop_sudo() as soon as you no longer
257
     * need the elevated privileges, which will reset the authentication data to the
258
     * initial credentials.
259
     *
260
     * @param string $domain The domain to request sudo for. This is a component name.
261
     */
262 603
    public function request_sudo(string $domain = null) : bool
263
    {
264 603
        if (!midcom::get()->config->get('auth_allow_sudo')) {
265 1
            debug_add("SUDO is not allowed on this website.", MIDCOM_LOG_ERROR);
266 1
            return false;
267
        }
268
269 603
        if ($domain === null) {
270 1
            $domain = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT);
271 1
            debug_add("Domain was not supplied, falling back to '{$domain}' which we got from the current component context.");
272
        }
273
274 603
        if ($domain == '') {
275 1
            debug_add("SUDO request for an empty domain, this should not happen. Denying sudo.", MIDCOM_LOG_INFO);
276 1
            return false;
277
        }
278
279 603
        $this->_component_sudo++;
280
281 603
        debug_add("Entered SUDO mode for domain {$domain}.", MIDCOM_LOG_INFO);
282
283 603
        return true;
284
    }
285
286
    /**
287
     * Drops previously acquired superuser privileges.
288
     *
289
     * @see request_sudo()
290
     */
291 603
    public function drop_sudo()
292
    {
293 603
        if ($this->_component_sudo > 0) {
294 603
            debug_add('Leaving SUDO mode.');
295 603
            $this->_component_sudo--;
296
        } else {
297 1
            debug_add('Requested to leave SUDO mode, but sudo was already disabled. Ignoring request.', MIDCOM_LOG_INFO);
298
        }
299 603
    }
300
301 580
    public function is_component_sudo() : bool
302
    {
303 580
        return $this->_component_sudo > 0;
304
    }
305
306
    /**
307
     * Check, whether a user is member of a given group. By default, the query is run
308
     * against the currently authenticated user.
309
     *
310
     * It always returns true for administrative users.
311
     *
312
     * @param mixed $group Group to check against, this can be either a midcom_core_group object or a group string identifier.
313
     * @param midcom_core_user $user The user which should be checked, defaults to the current user.
314
     */
315
    public function is_group_member($group, $user = null) : bool
316
    {
317
        if ($this->is_admin($user)) {
318
            // Administrators always have access.
319
            return true;
320
        }
321
        // Default parameter
322
        if ($user === null) {
323
            if ($this->user === null) {
324
                // not authenticated
325
                return false;
326
            }
327
            $user = $this->user;
328
        }
329
330
        return $user->is_in_group($group);
331
    }
332
333
    /**
334
     * Returns true if there is an authenticated user, false otherwise.
335
     */
336 149
    public function is_valid_user() : bool
337
    {
338 149
        return $this->user !== null;
339
    }
340
341
    /**
342
     * Validates that the current user has the given privilege granted on the
343
     * content object passed to the function.
344
     *
345
     * If this is not the case, an Access Denied error is generated, the message
346
     * defaulting to the string 'access denied: privilege %s not granted' of the
347
     * MidCOM main L10n table.
348
     *
349
     * The check is always done against the currently authenticated user. If the
350
     * check is successful, the function returns silently.
351
     *
352
     * @param MidgardObject $content_object A Midgard Content Object
353
     */
354 185
    public function require_do(string $privilege, object $content_object, string $message = null)
355
    {
356 185
        if (!$this->can_do($privilege, $content_object)) {
357
            throw $this->access_denied($message, 'privilege %s not granted', $privilege);
358
        }
359 185
    }
360
361
    /**
362
     * Validates, whether the given user have the privilege assigned to him in general.
363
     * Be aware, that this does not take any permissions overridden by content objects
364
     * into account. Whenever possible, you should user the require_do() variant of this
365
     * call therefore. require_user_do is only of interest in cases where you do not have
366
     * any content object available, for example when creating root topics.
367
     *
368
     * If this is not the case, an Access Denied error is generated, the message
369
     * defaulting to the string 'access denied: privilege %s not granted' of the
370
     * MidCOM main L10n table.
371
     *
372
     * The check is always done against the currently authenticated user. If the
373
     * check is successful, the function returns silently.
374
     *
375
     * @param string $class Optional parameter to set if the check should take type specific permissions into account. The class must be default constructible.
376
     */
377 77
    public function require_user_do(string $privilege, string $message = null, string $class = null)
378
    {
379 77
        if (!$this->can_user_do($privilege, null, $class)) {
380
            throw $this->access_denied($message, 'privilege %s not granted', $privilege);
381
        }
382 77
    }
383
384
    /**
385
     * Validates that the current user is a member of the given group.
386
     *
387
     * If this is not the case, an Access Denied error is generated, the message
388
     * defaulting to the string 'access denied: user is not member of the group %s' of the
389
     * MidCOM main L10n table.
390
     *
391
     * The check is always done against the currently authenticated user. If the
392
     * check is successful, the function returns silently.
393
     *
394
     * @param mixed $group Group to check against, this can be either a midcom_core_group object or a group string identifier.
395
     * @param string $message The message to show if the user is not member of the given group.
396
     */
397
    function require_group_member($group, $message = null)
398
    {
399
        if (!$this->is_group_member($group)) {
400
            if (is_object($group)) {
401
                $group = $group->name;
402
            }
403
            throw $this->access_denied($message, 'user is not member of the group %s', $group);
404
        }
405
    }
406
407
    /**
408
     * Validates that we currently have admin level privileges, which can either
409
     * come from the current user, or from the sudo service.
410
     *
411
     * If the check is successful, the function returns silently.
412
     */
413 4
    public function require_admin_user(string $message = null)
414
    {
415 4
        if (!$this->admin && !$this->_component_sudo) {
416
            throw $this->access_denied($message, 'admin level privileges required');
417
        }
418 4
    }
419
420
    private function access_denied(?string $message, string $fallback, string $data = null) : midcom_error_forbidden
421
    {
422
        if ($message === null) {
423
            $message = midcom::get()->i18n->get_string('access denied: ' . $fallback, 'midcom');
424
            if ($data !== null) {
425
                $message = sprintf($message, $data);
426
            }
427
        }
428
        debug_print_function_stack("access_denied was called from here:");
429
        return new midcom_error_forbidden($message);
430
    }
431
432
    /**
433
     * Require either a configured IP address or admin credentials
434
     */
435
    public function require_admin_or_ip(string $domain) : bool
436
    {
437
        $ips = midcom::get()->config->get('indexer_reindex_allowed_ips');
438
        if (   $ips
439
            && in_array($_SERVER['REMOTE_ADDR'], $ips)) {
440
            if (!$this->request_sudo($domain)) {
441
                throw new midcom_error('Failed to acquire SUDO rights. Aborting.');
442
            }
443
            return true;
444
        }
445
446
        // Require user to Basic-authenticate for security reasons
447
        $this->require_valid_user('basic');
448
        $this->require_admin_user();
449
        return false;
450
    }
451
452
    /**
453
     * Validates that there is an authenticated user.
454
     *
455
     * If this is not the case, midcom_error_forbidden is thrown, or a
456
     * basic auth challenge is triggered
457
     *
458
     * If the check is successful, the function returns silently.
459
     *
460
     * @param string $method Preferred authentication method: form or basic
461
     */
462 147
    public function require_valid_user(string $method = 'form')
463
    {
464 147
        if ($method === 'basic') {
465 3
            $this->_http_basic_auth();
466
        }
467 147
        if (!$this->is_valid_user()) {
468
            throw new midcom_error_forbidden(null, MIDCOM_ERRAUTH, $method);
469
        }
470 147
    }
471
472
    /**
473
     * Handles HTTP Basic authentication
474
     */
475 3
    private function _http_basic_auth()
476
    {
477 3
        if (isset($_SERVER['PHP_AUTH_USER'])) {
478
            if ($user = $this->backend->authenticate($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) {
479
                $this->set_user($user);
480
            } else {
481
                // Wrong password
482
                unset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
483
            }
484
        }
485 3
    }
486
487
    /**
488
     * Resolve any assignee identifier known by the system into an appropriate user/group object.
489
     *
490
     * @param string $id A valid user or group identifier usable as assignee (e.g. the $id member
491
     *     of any midcom_core_user or midcom_core_group object).
492
     * @return object|null corresponding object or false on failure.
493
     */
494 125
    public function get_assignee(string $id) : ?object
495
    {
496 125
        $parts = explode(':', $id);
497
498 125
        if ($parts[0] == 'user') {
499 124
            return $this->get_user($id);
500
        }
501 4
        if ($parts[0] == 'group') {
502 3
            return $this->get_group($id);
503
        }
504 1
        debug_add("The identifier {$id} cannot be resolved into an assignee, it cannot be mapped to a type.", MIDCOM_LOG_WARN);
505
506 1
        return null;
507
    }
508
509
    /**
510
     * This is a wrapper for get_user, which allows user retrieval by its name.
511
     * If the username is unknown, false is returned.
512
     */
513 3
    public function get_user_by_name(string $name) : ?midcom_core_user
514
    {
515 3
        $mc = new midgard_collector('midgard_user', 'login', $name);
516 3
        $mc->set_key_property('person');
517 3
        $mc->add_constraint('authtype', '=', midcom::get()->config->get('auth_type'));
518 3
        $mc->execute();
519 3
        $keys = $mc->list_keys();
520 3
        if (count($keys) != 1) {
521
            return null;
522
        }
523
524 3
        $person_class = midcom::get()->config->get('person_class');
525 3
        $person = new $person_class(key($keys));
526
527 3
        return $this->get_user($person);
528
    }
529
530
    /**
531
     * This is a wrapper for get_group, which allows Midgard Group retrieval by its name.
532
     * If the group name is unknown, false is returned.
533
     *
534
     * In the case that more than one group matches the given name, the first one is returned.
535
     * Note, that this should not happen as midgard group names should be unique according to the specs.
536
     */
537
    public function get_midgard_group_by_name(string $name) : ?midcom_core_group
538
    {
539
        $qb = new midgard_query_builder('midgard_group');
540
        $qb->add_constraint('name', '=', $name);
541
542
        $result = $qb->execute();
543
        if (empty($result)) {
544
            return null;
545
        }
546
        return $this->get_group($result[0]);
547
    }
548
549
    /**
550
     * Load a user from the database and returns an object instance.
551
     *
552
     * @param mixed $id A valid identifier for a MidgardPerson: An existing midgard_person class
553
     *     or subclass thereof, a Person ID or GUID or a midcom_core_user identifier.
554
     */
555 174
    public function get_user($id) : ?midcom_core_user
556
    {
557 174
        $param = $id;
558
559 174
        if (isset($param->id)) {
560 3
            $id = $param->id;
561 174
        } elseif (!is_string($id) && !is_int($id)) {
562
            debug_add('The passed argument was an object of an unsupported type: ' . gettype($param), MIDCOM_LOG_WARN);
563
            debug_print_r('Complete object dump:', $param);
564
            return null;
565
        }
566 174
        if (!array_key_exists($id, $this->_user_cache)) {
567
            try {
568 111
                if (is_a($param, midcom_db_person::class)) {
569
                    $param = $param->__object;
570
                }
571 111
                $this->_user_cache[$id] = new midcom_core_user($param);
572 4
            } catch (midcom_error $e) {
573
                // Keep it silent while missing user object can mess here
574 4
                $this->_user_cache[$id] = null;
575
            }
576
        }
577
578 174
        return $this->_user_cache[$id];
579
    }
580
581
    /**
582
     * Returns a midcom_core_group instance. Valid arguments are either a valid group identifier
583
     * (group:...), any valid identifier for the midcom_core_group
584
     * constructor or a valid object of that type.
585
     *
586
     * @param mixed $id The identifier of the group as outlined above.
587
     */
588 3
    public function get_group($id) : ?midcom_core_group
589
    {
590 3
        $param = $id;
591
592 3
        if (isset($param->id)) {
593
            $id = $param->id;
594 3
        } elseif (!is_string($id) && !is_int($id)) {
595
            debug_add('The group identifier is of an unsupported type: ' . gettype($param), MIDCOM_LOG_WARN);
596
            debug_print_r('Complete dump:', $param);
597
            return null;
598
        }
599
600 3
        if (!array_key_exists($id, $this->_group_cache)) {
601
            try {
602 3
                if (is_a($param, midcom_core_dbaobject::class)) {
603
                    $param = $param->__object;
604
                }
605 3
                $this->_group_cache[$id] = new midcom_core_group($param);
606
            } catch (midcom_error $e) {
607
                debug_add("Group with identifier {$id} could not be loaded: " . $e->getMessage(), MIDCOM_LOG_WARN);
608
                $this->_group_cache[$id] = null;
609
            }
610
        }
611 3
        return $this->_group_cache[$id];
612
    }
613
614
    /**
615
     * This call tells the backend to log in.
616
     */
617 62
    public function login(string $username, string $password, string $clientip = null) : bool
618
    {
619 62
        if ($user = $this->backend->login($username, $password, $clientip)) {
620 62
            $this->set_user($user);
621 62
            return true;
622
        }
623
        debug_add('The login information for ' . $username . ' was invalid.', MIDCOM_LOG_WARN);
624
        return false;
625
    }
626
627
    public function trusted_login(string $username) : bool
628
    {
629
        if (midcom::get()->config->get('auth_allow_trusted') !== true) {
630
            debug_add("Trusted logins are prohibited", MIDCOM_LOG_ERROR);
631
            return false;
632
        }
633
634
        if ($user = $this->backend->login($username, '', null, true)) {
635
            $this->set_user($user);
636
            return true;
637
        }
638
        return false;
639
    }
640
641
    /**
642
     * This call clears any authentication state
643
     */
644 1
    public function logout()
645
    {
646 1
        if ($this->user === null) {
647
            debug_add('The backend has no authenticated user set, so we should be fine');
648
        } else {
649 1
            $this->backend->logout($this->user);
650 1
            $this->user = null;
651
        }
652 1
        $this->admin = false;
653 1
    }
654
655
    /**
656
     * Render the main login form.
657
     * This only includes the form, no heading or whatsoever.
658
     *
659
     * It is recommended to call this function only as long as the headers are not yet sent (which
660
     * is usually given thanks to MidCOMs output buffering).
661
     *
662
     * What gets rendered depends on the authentication frontend, but will usually be some kind
663
     * of form.
664
     */
665
    public function show_login_form()
666
    {
667
        $this->frontend->show_login_form();
668
    }
669
670
    public function has_login_data() : bool
671
    {
672
        return $this->frontend->has_login_data();
673
    }
674
}
675