Passed
Push — master ( 930682...ea88c1 )
by Jeff
04:00
created

User::getSalt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the mailserver-admin package.
6
 * (c) Jeffrey Boehm <https://github.com/jeboehm/mailserver-admin>
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace App\Entity;
12
13
use Doctrine\ORM\Mapping as ORM;
14
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
15
use Symfony\Component\Security\Core\User\UserInterface;
16
use Symfony\Component\Validator\Constraints as Assert;
17
18
/**
19
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
20
 * @ORM\Table(name="mail_users", uniqueConstraints={@ORM\UniqueConstraint(name="user_idx", columns={"name", "domain_id"})})
21
 * @UniqueEntity({"name", "domain"})
22
 */
23
class User implements UserInterface
24
{
25
    /**
26
     * @ORM\Id()
27
     * @ORM\GeneratedValue()
28
     * @ORM\Column(type="integer")
29
     */
30
    private $id;
31
32
    /**
33
     * @ORM\ManyToOne(targetEntity="Domain", inversedBy="users")
34
     * @Assert\NotNull()
35
     */
36
    private $domain;
37
38
    /**
39
     * @ORM\Column(type="string", name="name", options={"collation":"utf8_unicode_ci"})
40
     * @Assert\NotBlank()
41
     * @Assert\Regex(pattern="/^[a-z0-9\-\_.]{1,50}$/")
42
     */
43
    private $name = '';
44
45
    /**
46
     * @ORM\Column(type="string", name="password", options={"collation":"utf8_unicode_ci"})
47
     */
48
    private $password = '';
49
50
    /**
51
     * @Assert\Length(min="6", max="5000")
52
     */
53
    private $plainPassword;
54
55
    /**
56
     * @ORM\Column(type="boolean", name="admin")
57
     */
58
    private $admin = false;
59
60
    /**
61
     * @ORM\Column(type="boolean", name="enabled")
62
     */
63
    private $enabled = true;
64
65
    /**
66
     * @ORM\Column(type="boolean", name="send_only")
67
     */
68
    private $sendOnly = false;
69
70
    /**
71
     * @ORM\Column(type="integer", name="quota")
72
     * @Assert\Range(min="0")
73
     * @Assert\NotBlank()
74
     */
75
    private $quota = 0;
76
77
    private $roles = ['ROLE_USER'];
78
79
    public function __toString(): string
80
    {
81
        if ($this->getDomain()) {
82
            return sprintf('%s@%s', $this->name, $this->getDomain()->getName());
83
        }
84
85
        return '';
86
    }
87
88
    public function getDomain(): ?Domain
89
    {
90
        return $this->domain;
91
    }
92
93
    public function setDomain(Domain $domain): void
94
    {
95
        $this->domain = $domain;
96
    }
97
98
    public function getId(): ?int
99
    {
100
        return $this->id;
101
    }
102
103
    public function getName(): string
104
    {
105
        return $this->name;
106
    }
107
108
    public function setName(string $name): void
109
    {
110
        $this->name = $name;
111
    }
112
113
    public function getPassword(): string
114
    {
115
        return $this->password;
116
    }
117
118
    public function setPassword(string $password): void
119
    {
120
        $this->password = $password;
121
    }
122
123
    public function getRoles(): array
124
    {
125
        return $this->roles;
126
    }
127
128
    public function addRole(string $role): void
129
    {
130
        $this->roles[] = $role;
131
    }
132
133
    public function isAdmin(): bool
134
    {
135
        return $this->admin;
136
    }
137
138
    public function setAdmin(bool $admin): void
139
    {
140
        $this->admin = $admin;
141
    }
142
143
    public function getSalt(): string
144
    {
145
        $parts = explode('$', $this->password, 5);
146
147
        return $parts[3] ?? '';
148
    }
149
150
    public function getUsername(): string
151
    {
152
        return (string) $this;
153
    }
154
155
    public function eraseCredentials(): void
156
    {
157
        $this->plainPassword = '';
158
    }
159
160
    public function getPlainPassword(): ?string
161
    {
162
        return $this->plainPassword;
163
    }
164
165
    public function setPlainPassword(?string $plainPassword): void
166
    {
167
        $this->plainPassword = $plainPassword;
168
    }
169
170
    public function getEnabled(): bool
171
    {
172
        return $this->enabled;
173
    }
174
175
    public function setEnabled(bool $enabled): void
176
    {
177
        $this->enabled = $enabled;
178
    }
179
180
    public function getSendOnly(): bool
181
    {
182
        return $this->sendOnly;
183
    }
184
185
    public function setSendOnly(bool $sendOnly): void
186
    {
187
        $this->sendOnly = $sendOnly;
188
    }
189
190
    public function getQuota(): int
191
    {
192
        return $this->quota;
193
    }
194
195
    public function setQuota(int $quota): void
196
    {
197
        $this->quota = $quota;
198
    }
199
}
200