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

mimmi20 / mezzio-generic-authorization-laminasviewrenderer / 9571356031

18 Jun 2024 07:55PM UTC coverage: 93.939% (-6.1%) from 100.0%
9571356031

push

github

web-flow
Merge pull request #273 from mimmi20/dependabot/composer/master/minor-patch-dependencies-4789519206

composer(deps-dev): bump the minor-patch-dependencies group with 4 updates

6 of 8 new or added lines in 1 file covered. (75.0%)

31 of 33 relevant lines covered (93.94%)

1.64 hits per line

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

86.67
/src/Authorization.php
1
<?php
2
/**
3
 * This file is part of the mimmi20/mezzio-generic-authorization-laminasviewrenderer package.
4
 *
5
 * Copyright (c) 2021-2024, Thomas Mueller <mimmi20@live.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10

11
declare(strict_types = 1);
12

13
namespace Mimmi20\Mezzio\GenericAuthorization\LaminasView;
14

15
use Laminas\View\Helper\AbstractHelper;
16
use Mezzio\Authentication\UserInterface;
17
use Mimmi20\Mezzio\GenericAuthorization\AuthorizationInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19

20
use function count;
21

22
final class Authorization extends AbstractHelper
23
{
24
    /** @throws void */
25
    public function __construct(private readonly AuthorizationInterface $authorization)
4✔
26
    {
27
        // nothing to do
28
    }
4✔
29

30
    /**
31
     * Check if a role is granted for a resource
32
     *
33
     * @throws void
34
     *
35
     * @api
36
     */
37
    public function isGranted(
1✔
38
        string | null $role = null,
39
        string | null $resource = null,
40
        string | null $privilege = null,
41
        ServerRequestInterface | null $request = null,
42
    ): bool {
43
        return $this->authorization->isGranted($role, $resource, $privilege, $request);
1✔
44
    }
45

46
    /**
47
     * Check if a role is granted for a user
48
     *
49
     * @throws void
50
     *
51
     * @api
52
     */
53
    public function isGrantedForUser(
2✔
54
        UserInterface $user,
55
        string | null $resource = null,
56
        string | null $privilege = null,
57
        ServerRequestInterface | null $request = null,
58
    ): bool {
59
        $roles = [];
2✔
60

61
        foreach ($user->getRoles() as $role) {
2✔
62
            $roles[] = $role;
2✔
63
        }
64

65
        if (count($roles)) {
2✔
66
            foreach ($roles as $role) {
2✔
67
                if ($this->authorization->isGranted($role, $resource, $privilege, $request)) {
2✔
68
                    return true;
1✔
69
                }
70
            }
NEW
71
        } elseif ($this->authorization->isGranted(null, $resource, $privilege, $request)) {
×
NEW
72
            return true;
×
73
        }
74

75
        return false;
1✔
76
    }
77
}
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

© 2025 Coveralls, Inc