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

FastyBird / simple-auth / 7822099561

07 Feb 2024 10:39PM UTC coverage: 74.633% (-3.5%) from 78.119%
7822099561

push

github

akadlec
Use native enum

3 of 7 new or added lines in 3 files covered. (42.86%)

29 existing lines in 5 files now uncovered.

356 of 477 relevant lines covered (74.63%)

4.91 hits per line

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

78.26
/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 Closure;
19
use FastyBird\SimpleAuth;
20
use FastyBird\SimpleAuth\Exceptions;
21
use FastyBird\SimpleAuth\Security;
22
use Nette;
23
use Ramsey\Uuid;
24
use function func_get_args;
25
use function in_array;
26

27
/**
28
 * Application user
29
 *
30
 * @package        FastyBird:SimpleAuth!
31
 * @subpackage     Security
32
 *
33
 * @author         Adam Kadlec <adam.kadlec@fastybird.com>
34
 *
35
 * @method void onLoggedIn(Security\User $user)
36
 * @method void onLoggedOut(Security\User $user)
37
 */
38
class User
39
{
40

41
        use Nette\SmartObject;
42

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

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

49
        private IUserStorage $storage;
50

51
        private IAuthenticator|null $authenticator;
52

53
        public function __construct(
54
                Security\IUserStorage $storage,
55
                Security\IAuthenticator|null $authenticator = null,
56
        )
57
        {
58
                $this->storage = $storage;
15✔
59
                $this->authenticator = $authenticator;
15✔
60
        }
61

62
        public function getId(): Uuid\UuidInterface|null
63
        {
64
                $identity = $this->getIdentity();
3✔
65

66
                return $identity?->getId();
3✔
67
        }
68

69
        public function getIdentity(): Security\IIdentity|null
70
        {
71
                return $this->storage->getIdentity();
6✔
72
        }
73

74
        /**
75
         * @param string|Security\IIdentity $user name or instance of Security\IIdentity
76
         *
77
         * @throws Exceptions\Authentication
78
         * @throws Exceptions\InvalidState
79
         */
80
        public function login(string|Security\IIdentity $user, string|null $password = null): void
81
        {
82
                $this->logout();
9✔
83

84
                if (!$user instanceof Security\IIdentity) {
9✔
85
                        if ($this->authenticator === null) {
×
86
                                throw new Exceptions\InvalidState('Authenticator is not defined');
×
87
                        }
88

89
                        $user = $this->authenticator->authenticate(func_get_args());
×
90
                }
91

92
                $this->storage->setIdentity($user);
9✔
93

94
                $this->onLoggedIn($this);
9✔
95
        }
96

97
        public function logout(): void
98
        {
99
                if ($this->isLoggedIn()) {
12✔
UNCOV
100
                        $this->onLoggedOut($this);
×
101
                }
102

103
                $this->storage->setIdentity(null);
12✔
104
        }
105

106
        public function isLoggedIn(): bool
107
        {
108
                return $this->storage->isAuthenticated();
12✔
109
        }
110

111
        public function isInRole(string $role): bool
112
        {
113
                return in_array($role, $this->getRoles(), true);
3✔
114
        }
115

116
        /**
117
         * @return array<string>
118
         */
119
        public function getRoles(): array
120
        {
121
                if (!$this->isLoggedIn()) {
6✔
122
                        return [SimpleAuth\Constants::ROLE_ANONYMOUS];
×
123
                }
124

125
                $identity = $this->getIdentity();
6✔
126

127
                return $identity !== null && $identity->getRoles() !== []
6✔
128
                        ? $identity->getRoles()
6✔
129
                        : [SimpleAuth\Constants::ROLE_USER];
6✔
130
        }
131

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