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

avoutic / web-framework / 19992775795

06 Dec 2025 06:45PM UTC coverage: 72.97% (-0.009%) from 72.979%
19992775795

push

github

avoutic
Use modern find() variants

27 of 29 new or added lines in 6 files covered. (93.1%)

31 existing lines in 4 files now uncovered.

1995 of 2734 relevant lines covered (72.97%)

2.76 hits per line

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

78.0
/src/Security/UserRightService.php
1
<?php
2

3
/*
4
 * This file is part of WebFramework.
5
 *
6
 * (c) Avoutic <avoutic@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace WebFramework\Security;
13

14
use Psr\Log\LoggerInterface;
15
use WebFramework\Database\Database;
16
use WebFramework\Entity\User;
17
use WebFramework\Repository\RightRepository;
18
use WebFramework\Repository\UserRightRepository;
19

20
/**
21
 * Manages user rights and permissions.
22
 */
23
class UserRightService
24
{
25
    /**
26
     * UserRightService constructor.
27
     *
28
     * @param Database            $database            The database service
29
     * @param LoggerInterface     $logger              The logger service
30
     * @param RightRepository     $rightRepository     The right repository
31
     * @param UserRightRepository $userRightRepository The user right repository
32
     */
33
    public function __construct(
×
34
        private Database $database,
35
        private LoggerInterface $logger,
36
        private RightRepository $rightRepository,
37
        private UserRightRepository $userRightRepository,
38
    ) {}
×
39

40
    /**
41
     * Add a right to a user.
42
     *
43
     * @param User   $user      The user to add the right to
44
     * @param string $shortName The short name of the right to add
45
     *
46
     * @throws \InvalidArgumentException If the right is unknown
47
     */
48
    public function addRight(User $user, string $shortName): void
3✔
49
    {
50
        $this->logger->info('Adding right to user', ['user_id' => $user->getId(), 'short_name' => $shortName]);
3✔
51

52
        $right = $this->rightRepository->getRightByShortName($shortName);
3✔
53

54
        if ($right === null)
3✔
55
        {
56
            $this->logger->error('Cannot add unknown right to user', ['user_id' => $user->getId(), 'short_name' => $shortName]);
1✔
57

58
            throw new \InvalidArgumentException('Right unknown');
1✔
59
        }
60

61
        $userRight = $this->userRightRepository
2✔
62
            ->findOneBy([
2✔
63
                'user_id' => $user->getId(),
2✔
64
                'right_id' => $right->getId(),
2✔
65
            ])
2✔
66
        ;
2✔
67

68
        if ($userRight === null)
2✔
69
        {
70
            $this->userRightRepository->create([
1✔
71
                'user_id' => $user->getId(),
1✔
72
                'right_id' => $right->getId(),
1✔
73
            ]);
1✔
74
        }
75
    }
76

77
    /**
78
     * Delete a right from a user.
79
     *
80
     * @param User   $user      The user to remove the right from
81
     * @param string $shortName The short name of the right to remove
82
     *
83
     * @throws \InvalidArgumentException If the right is unknown
84
     */
85
    public function deleteRight(User $user, string $shortName): void
3✔
86
    {
87
        $this->logger->info('Deleting right from user', ['user_id' => $user->getId(), 'short_name' => $shortName]);
3✔
88

89
        $right = $this->rightRepository->getRightByShortName($shortName);
3✔
90

91
        if ($right === null)
3✔
92
        {
93
            $this->logger->error('Cannot delete unknown right from user', ['user_id' => $user->getId(), 'short_name' => $shortName]);
1✔
94

95
            throw new \InvalidArgumentException('Right unknown');
1✔
96
        }
97

98
        $userRight = $this->userRightRepository->findOneBy([
2✔
99
            'user_id' => $user->getId(),
2✔
100
            'right_id' => $right->getId(),
2✔
101
        ]);
2✔
102

103
        if ($userRight === null)
2✔
104
        {
105
            return;
1✔
106
        }
107

108
        $this->userRightRepository->delete($userRight);
1✔
109
    }
110

111
    /**
112
     * Check if a user has a specific right.
113
     *
114
     * @param User   $user      The user to check
115
     * @param string $shortName The short name of the right to check for
116
     *
117
     * @return bool True if the user has the right, false otherwise
118
     */
119
    public function hasRight(User $user, string $shortName): bool
3✔
120
    {
121
        $right = $this->rightRepository->getRightByShortName($shortName);
3✔
122

123
        if ($right === null)
3✔
124
        {
125
            return false;
1✔
126
        }
127

128
        $userRight = $this->userRightRepository->findOneBy([
2✔
129
            'user_id' => $user->getId(),
2✔
130
            'right_id' => $right->getId(),
2✔
131
        ]);
2✔
132

133
        return ($userRight !== null);
2✔
134
    }
135

136
    /**
137
     * Get all rights for a user.
138
     *
139
     * @param User $user The user to get rights for
140
     *
141
     * @return array<string> An array of right short names
142
     */
UNCOV
143
    public function getRights(User $user): array
×
144
    {
145
        $query = <<<'SQL'
×
146
        SELECT short_name
147
        FROM rights AS r,
148
             user_rights AS ur
149
        WHERE ur.user_id = ? AND
150
              ur.right_id = r.id
151
        ORDER BY r.short_name
UNCOV
152
SQL;
×
153

UNCOV
154
        $params = [$user->getId()];
×
155

UNCOV
156
        $result = $this->database->query($query, $params, 'Failed to retrieve rights');
×
157

158
        $data = [];
×
159

UNCOV
160
        foreach ($result as $row)
×
161
        {
UNCOV
162
            $data[] = $row['short_name'];
×
163
        }
164

UNCOV
165
        return $data;
×
166
    }
167
}
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