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

FastyBird / simple-auth / 10030599651

21 Jul 2024 06:48PM UTC coverage: 68.983% (+1.1%) from 67.871%
10030599651

push

github

web-flow
User roles check as was before casbin (#11)

* User roles check as was before casbin

* Fix interface

* Use storage instead of user

* QA fixes

45 of 49 new or added lines in 4 files covered. (91.84%)

1 existing line in 1 file now uncovered.

536 of 777 relevant lines covered (68.98%)

5.97 hits per line

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

77.27
/src/Security/User.php
1
<?php declare(strict_types = 1);
2

3
/**
4
 * User.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:SimpleAuth!
10
 * @subpackage     Security
11
 * @since          0.1.0
12
 *
13
 * @date           29.08.20
14
 */
15

16
namespace FastyBird\SimpleAuth\Security;
17

18
use Casbin;
19
use Closure;
20
use FastyBird\SimpleAuth;
21
use FastyBird\SimpleAuth\Exceptions;
22
use FastyBird\SimpleAuth\Security;
23
use Nette;
24
use Nette\Utils;
25
use Ramsey\Uuid;
26
use function func_get_args;
27

28
/**
29
 * Application user
30
 *
31
 * @package        FastyBird:SimpleAuth!
32
 * @subpackage     Security
33
 *
34
 * @author         Adam Kadlec <adam.kadlec@fastybird.com>
35
 */
36
class User
37
{
38

39
        use Nette\SmartObject;
40

41
        /** @var array<Closure(Security\User $user): void> */
42
        public array $onLoggedIn = [];
43

44
        /** @var array<Closure(Security\User $user): void> */
45
        public array $onLoggedOut = [];
46

47
        public function __construct(
48
                protected readonly Security\IUserStorage $storage,
49
                protected readonly Casbin\CachedEnforcer $enforcer,
50
                protected readonly Security\IAuthenticator|null $authenticator = null,
51
        )
52
        {
53
        }
12✔
54

55
        public function getId(): Uuid\UuidInterface|null
56
        {
57
                $identity = $this->getIdentity();
6✔
58

59
                return $identity?->getId();
6✔
60
        }
61

62
        public function getIdentity(): Security\IIdentity|null
63
        {
64
                return $this->storage->getIdentity();
6✔
65
        }
66

67
        /**
68
         * @param string|Security\IIdentity $user name or instance of Security\IIdentity
69
         *
70
         * @throws Exceptions\Authentication
71
         * @throws Exceptions\InvalidState
72
         */
73
        public function login(string|Security\IIdentity $user, string|null $password = null): void
74
        {
75
                $this->logout();
9✔
76

77
                if (!$user instanceof Security\IIdentity) {
9✔
78
                        if ($this->authenticator === null) {
×
79
                                throw new Exceptions\InvalidState('Authenticator is not defined');
×
80
                        }
81

82
                        $user = $this->authenticator->authenticate(func_get_args());
×
83
                }
84

85
                $this->storage->setIdentity($user);
9✔
86

87
                Utils\Arrays::invoke($this->onLoggedIn, $this);
9✔
88
        }
89

90
        public function logout(): void
91
        {
92
                if ($this->isLoggedIn()) {
12✔
93
                        Utils\Arrays::invoke($this->onLoggedOut, $this);
×
94
                }
95

96
                $this->storage->setIdentity(null);
12✔
97
        }
98

99
        public function isLoggedIn(): bool
100
        {
101
                return $this->storage->isAuthenticated();
12✔
102
        }
103

104
        public function isInRole(string $role): bool
105
        {
106
                return $this->enforcer->hasRoleForUser(
3✔
107
                        $this->getId()?->toString() ?? SimpleAuth\Constants::USER_ANONYMOUS,
3✔
108
                        $role,
3✔
109
                );
3✔
110
        }
111

112
        /**
113
         * @return array<string>
114
         */
115
        public function getRoles(): array
116
        {
117
                if (!$this->isLoggedIn()) {
3✔
NEW
118
                        return [SimpleAuth\Constants::ROLE_ANONYMOUS];
×
119
                }
120

121
                return $this->enforcer->getRolesForUser($this->getId()?->toString() ?? SimpleAuth\Constants::USER_ANONYMOUS);
3✔
122
        }
123

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