• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

heimrichhannot / contao-utils-bundle / 21240556887

22 Jan 2026 07:57AM UTC coverage: 78.93% (+0.2%) from 78.761%
21240556887

push

github

koertho
update changelog

1165 of 1476 relevant lines covered (78.93%)

3.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.22
/src/Util/UserUtil.php
1
<?php
2

3
/*
4
 * Copyright (c) 2022 Heimrich & Hannot GmbH
5
 *
6
 * @license LGPL-3.0-or-later
7
 */
8

9
namespace HeimrichHannot\UtilsBundle\Util;
10

11
use Contao\CoreBundle\Framework\Adapter;
12
use Contao\CoreBundle\Framework\ContaoFramework;
13
use Contao\Date;
14
use Contao\MemberModel;
15
use Contao\Model;
16
use Contao\Model\Collection;
17
use Contao\StringUtil;
18
use Contao\UserModel;
19
use HeimrichHannot\UtilsBundle\Util\UserUtil\UserType;
20

21
class UserUtil
22
{
23
    public const TYPE_USER = 'user';
24
    public const TYPE_MEMBER = 'member';
25

26
    /**
27
     * UserUtil constructor.
28
     */
29
    public function __construct(
30
        private readonly ModelUtil $modelUtil,
31
        private readonly DatabaseUtil $databaseUtil,
32
        private readonly ContaoFramework $contaoFramework
33
    )
34
    {
35
    }
3✔
36

37

38
    public function findActiveUsersByGroup(array $groups, UserType $type = UserType::USER, array $options = []): ?Collection
39
    {
40
        $table = match ($type) {
1✔
41
            UserType::USER => UserModel::getTable(),
1✔
42
            UserType::MEMBER => MemberModel::getTable()
1✔
43
        };
1✔
44

45
        /** @var class-string<Model> $modelClass */
46
        $modelClass = $this->contaoFramework->getAdapter(Model::class)->getClassFromTable($table);
1✔
47

48
        if (!$modelClass) {
1✔
49
            return null;
×
50
        }
51

52
        if (!\is_array($groups) || empty($groups = array_filter($groups, fn($k) => !empty($k) && is_numeric($k)))) {
1✔
53
            return null;
1✔
54
        }
55

56
        /** @var Adapter<Model> $adapter */
57
        $adapter = $this->contaoFramework->getAdapter($modelClass);
1✔
58

59
        $time = Date::floorToMinute();
1✔
60
        $values = [];
1✔
61

62
        $columns = ["($table.start='' OR $table.start<='$time') AND ($table.stop='' OR $table.stop>'".($time + 60)."') AND $table.disable=''"];
1✔
63

64
        $blobResult = $this->databaseUtil->createWhereForSerializedBlob('groups', $groups);
1✔
65
        $columns[] = $blobResult->createOrWhere();
1✔
66
        $values = array_merge(array_values($values), array_values($blobResult->values));
1✔
67

68
        return $adapter->findBy($columns, $values, $options);
1✔
69
    }
70

71
    /**
72
     * Returns all active users userGroups as a Collection of Models or null if user do not belong to any active userGroups
73
     */
74
    public function getActiveGroups(UserModel|MemberModel $user): ?Collection
75
    {
76
        if (empty($groups = StringUtil::deserialize($user->groups, true))) {
2✔
77
            return null;
2✔
78
        }
79

80
        if ($user instanceof MemberModel) {
2✔
81
            $groupTable = 'tl_member_group';
1✔
82
        } else {
83
            $groupTable = 'tl_user_group';
2✔
84
        }
85

86
        $columns = [$groupTable.'.id IN('.implode(',', array_map(\intval(...), $groups)).')'];
2✔
87

88
        $this->modelUtil->addPublishedCheckToModelArrays($groupTable, $columns, [
2✔
89
            'publishedField' => 'disable',
2✔
90
            'invertPublishedField' => true,
2✔
91
        ]);
2✔
92

93
        return $this->modelUtil->findModelInstancesBy($groupTable, $columns, []);
2✔
94
    }
95

96
    /**
97
     * Returns true if the user or member (frontend user) is member of given group, false otherwise.
98
     */
99
    public function hasActiveGroup(UserModel|MemberModel $user, int $groupId): bool
100
    {
101
        $activeGroups = $this->getActiveGroups($user);
1✔
102

103
        if (!$activeGroups) {
1✔
104
            return false;
1✔
105
        }
106

107
        foreach ($activeGroups as $group) {
1✔
108
            if ((int) ($group->id) === $groupId) {
1✔
109
                return true;
1✔
110
            }
111
        }
112

113
        return false;
1✔
114
    }
115
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc