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

FastyBird / simple-auth / 9954252110

16 Jul 2024 09:26AM UTC coverage: 66.994% (+1.3%) from 65.685%
9954252110

push

github

web-flow
Integrate Casbin authorizator (#6)

114 of 161 new or added lines in 9 files covered. (70.81%)

2 existing lines in 2 files now uncovered.

477 of 712 relevant lines covered (66.99%)

4.59 hits per line

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

39.13
/src/Subscribers/Policy.php
1
<?php declare(strict_types = 1);
2

3
/**
4
 * Policy.php
5
 *
6
 * @license        More in LICENSE.md
7
 * @copyright      https://www.fastybird.com
8
 * @author         Adam Kadlec <adam.kadlec@fastybird.com>
9
 * @package        FastyBird:DevicesModule!
10
 * @subpackage     Subscribers
11
 * @since          1.0.0
12
 *
13
 * @date           22.03.20
14
 */
15

16
namespace FastyBird\SimpleAuth\Subscribers;
17

18
use Casbin;
19
use Doctrine\Common;
20
use Doctrine\ORM;
21
use Doctrine\Persistence;
22
use FastyBird\SimpleAuth\Entities;
23
use Nette;
24
use function count;
25

26
/**
27
 * Casbin policy entity subscriber
28
 *
29
 * @package        FastyBird:DevicesModule!
30
 * @subpackage     Subscribers
31
 *
32
 * @author         Adam Kadlec <adam.kadlec@fastybird.com>
33
 */
34
final class Policy implements Common\EventSubscriber
35
{
36

37
        use Nette\SmartObject;
38

39
        public function __construct(
40
                private readonly ORM\EntityManagerInterface $entityManager,
41
                private readonly Casbin\Enforcer $enforcer,
42
        )
43
        {
44
        }
3✔
45

46
        public function getSubscribedEvents(): array
47
        {
48
                return [
3✔
49
                        0 => ORM\Events::postPersist,
3✔
50
                        1 => ORM\Events::postUpdate,
3✔
51
                        2 => ORM\Events::postRemove,
3✔
52
                ];
3✔
53
        }
54

55
        /**
56
         * @param Persistence\Event\LifecycleEventArgs<ORM\EntityManagerInterface> $eventArgs
57
         */
58
        public function postPersist(Persistence\Event\LifecycleEventArgs $eventArgs): void
59
        {
60
                // onFlush was executed before, everything already initialized
61
                $entity = $eventArgs->getObject();
3✔
62

63
                // Check for valid entity
64
                if (!$entity instanceof Entities\Policies\Policy) {
3✔
NEW
65
                        return;
×
66
                }
67

68
                $this->enforcer->loadPolicy();
3✔
69
        }
70

71
        /**
72
         * @param Persistence\Event\LifecycleEventArgs<ORM\EntityManagerInterface> $eventArgs
73
         */
74
        public function postUpdate(Persistence\Event\LifecycleEventArgs $eventArgs): void
75
        {
NEW
76
                $uow = $this->entityManager->getUnitOfWork();
×
77

78
                // onFlush was executed before, everything already initialized
NEW
79
                $entity = $eventArgs->getObject();
×
80

81
                // Get changes => should be already computed here (is a listener)
NEW
82
                $changeSet = $uow->getEntityChangeSet($entity);
×
83

84
                // If we have no changes left => don't create revision log
NEW
85
                if (count($changeSet) === 0) {
×
NEW
86
                        return;
×
87
                }
88

89
                // Check for valid entity
90
                if (
NEW
91
                        !$entity instanceof Entities\Policies\Policy
×
NEW
92
                        || $uow->isScheduledForDelete($entity)
×
93
                ) {
NEW
94
                        return;
×
95
                }
96

NEW
97
                $this->enforcer->loadPolicy();
×
98
        }
99

100
        /**
101
         * @param Persistence\Event\LifecycleEventArgs<ORM\EntityManagerInterface> $eventArgs
102
         */
103
        public function postRemove(Persistence\Event\LifecycleEventArgs $eventArgs): void
104
        {
105
                // onFlush was executed before, everything already initialized
NEW
106
                $entity = $eventArgs->getObject();
×
107

108
                // Check for valid entity
NEW
109
                if (!$entity instanceof Entities\Policies\Policy) {
×
NEW
110
                        return;
×
111
                }
112

NEW
113
                $this->enforcer->loadPolicy();
×
114
        }
115

116
}
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