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

heimrichhannot / contao-utils-bundle / 6685862485

29 Oct 2023 09:22PM UTC coverage: 74.155% (+52.0%) from 22.152%
6685862485

Pull #67

github

koertho
cleanup composer file
Pull Request #67: Version 3

216 of 216 new or added lines in 19 files covered. (100.0%)

746 of 1006 relevant lines covered (74.16%)

3.69 hits per line

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

97.44
/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\ContaoFramework;
12
use Contao\Date;
13
use Contao\MemberModel;
14
use Contao\Model;
15
use Contao\Model\Collection;
16
use Contao\StringUtil;
17
use Contao\UserModel;
18
use HeimrichHannot\UtilsBundle\Util\DatabaseUtil;
19
use HeimrichHannot\UtilsBundle\Util\ModelUtil;
20
use HeimrichHannot\UtilsBundle\Util\UserUtil\UserType;
21

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

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

38

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

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

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

53
        if (!\is_array($groups) || empty($groups = array_filter($groups, function ($k) {
1✔
54
                return !empty($k) && is_numeric($k);
1✔
55
            }))) {
1✔
56
            return null;
1✔
57
        }
58

59
        /** @var Model $adapter */
60
        $adapter = $this->contaoFramework->getAdapter($modelClass);
1✔
61

62
        $time = Date::floorToMinute();
1✔
63
        $values = [];
1✔
64

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

68
        $blobResult = $this->databaseUtil->createWhereForSerializedBlob('groups', $groups);
1✔
69
        $columns[] = $blobResult->createOrWhere();
1✔
70
        $values = array_merge(array_values($values), array_values($blobResult->values));
1✔
71

72
        return $adapter->findBy($columns, $values, $options);
1✔
73
    }
74

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

84
        if ($user instanceof MemberModel) {
2✔
85
            $groupTable = 'tl_member_group';
1✔
86
        } else {
87
            $groupTable = 'tl_user_group';
2✔
88
        }
89

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

92
        $this->modelUtil->addPublishedCheckToModelArrays($groupTable, $columns, [
2✔
93
            'publishedField' => 'disable',
2✔
94
            'invertPublishedField' => true,
2✔
95
        ]);
2✔
96

97
        return $this->modelUtil->findModelInstancesBy($groupTable, $columns, []);
2✔
98
    }
99

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

107
        if (!$activeGroups) {
1✔
108
            return false;
1✔
109
        }
110

111
        foreach ($activeGroups as $group) {
1✔
112
            if ((int) ($group->id) === $groupId) {
1✔
113
                return true;
1✔
114
            }
115
        }
116

117
        return false;
1✔
118
    }
119
}
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