Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
CurrentUserAccountStateTrait
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 getUserId
n/a
0 / 0
n/a
0 / 0
0
 accountStateConfiguration
n/a
0 / 0
n/a
0 / 0
0
 isLocalUser
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 isBlocked
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Trait for checking the current user's account state
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2026-02-24
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\User;
21
22use phpMyFAQ\Configuration;
23use phpMyFAQ\Database;
24
25trait CurrentUserAccountStateTrait
26{
27    /**
28     * Returns the user ID of the composing user class.
29     */
30    abstract public function getUserId(): int;
31
32    /**
33     * Returns the configuration of the composing user class.
34     */
35    abstract protected function accountStateConfiguration(): Configuration;
36
37    /**
38     * Returns true if the user is a local user, otherwise false.
39     */
40    public function isLocalUser(): bool
41    {
42        $query = sprintf(
43            "SELECT auth_source FROM %sfaquser WHERE auth_source = 'local' AND user_id = %d",
44            Database::getTablePrefix(),
45            $this->getUserId(),
46        );
47
48        $db = $this->accountStateConfiguration()->getDb();
49        $result = $db->query($query);
50
51        return (bool) $db->fetchRow($result);
52    }
53
54    public function isBlocked(): bool
55    {
56        $query = sprintf(
57            'SELECT account_status FROM %sfaquser WHERE user_id = %d',
58            Database::getTablePrefix(),
59            $this->getUserId(),
60        );
61
62        $db = $this->accountStateConfiguration()->getDb();
63        $result = $db->query($query);
64        $row = $db->fetchArray($result);
65
66        return ($row['account_status'] ?? null) === 'blocked';
67    }
68}