Skip to content

Commit 55d749d

Browse files
committed
Admin
1 parent cc71c78 commit 55d749d

File tree

5 files changed

+74
-277
lines changed

5 files changed

+74
-277
lines changed

src/Core/Doctrine/OnlyOrmTablesFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ public function __invoke(string $assetName): bool
2828
foreach ($this->em->getMetadataFactory()->getAllMetadata() as $m) {
2929
// main table
3030
$table = $m->getTableName();
31-
if (str_starts_with($table, 'phplist_')) {
31+
if ($table) {
3232
$names[] = $table;
3333
}
3434

3535
// many-to-many join tables
3636
foreach ($m->getAssociationMappings() as $assoc) {
3737
if (isset($assoc['joinTable']['name'])) {
3838
$join = $assoc['joinTable']['name'];
39-
if (str_starts_with($join, 'phplist_')) {
39+
if ($join) {
4040
$names[] = $join;
4141
}
4242
}

src/Domain/Common/Model/Interfaces/ModificationDate.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,4 @@ interface ModificationDate
2020
* @return DateTime|null
2121
*/
2222
public function getUpdatedAt(): ?DateTime;
23-
24-
/**
25-
* Updates the modification date to be now.
26-
*
27-
* @Mapping\PrePersist
28-
* @Mapping\PreUpdate
29-
*
30-
* @return DomainModel
31-
*/
32-
public function updateUpdatedAt(): DomainModel;
3323
}

src/Domain/Identity/Model/Administrator.php

Lines changed: 67 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
* @author Tatevik Grigoryan <[email protected]>
2323
*/
2424
#[ORM\Entity(repositoryClass: AdministratorRepository::class)]
25-
#[ORM\Table(name: 'phplist_admin')]
25+
#[ORM\Table(
26+
name: 'phplist_admin',
27+
uniqueConstraints: [new ORM\UniqueConstraint(name: 'loginnameidx', columns: ['loginname'])]
28+
)]
2629
#[ORM\HasLifecycleCallbacks]
2730
class Administrator implements DomainModel, Identity, CreationDate, ModificationDate
2831
{
@@ -31,50 +34,60 @@ class Administrator implements DomainModel, Identity, CreationDate, Modification
3134
#[ORM\GeneratedValue]
3235
private ?int $id = null;
3336

34-
#[ORM\Column(name: 'created', type: 'datetime')]
35-
protected DateTime $createdAt;
37+
#[ORM\Column(name: 'created', type: 'datetime', nullable: true)]
38+
protected ?DateTime $createdAt = null;
3639

37-
#[ORM\Column(name: 'modified', type: 'datetime')]
38-
private ?DateTime $updatedAt;
40+
#[ORM\Column(name: 'modified', type: 'datetime', nullable: false)]
41+
private DateTime $updatedAt;
3942

40-
#[ORM\Column(name: 'loginname')]
43+
#[ORM\Column(name: 'loginname', type: 'string', length: 66, nullable: false)]
4144
private string $loginName;
4245

43-
#[ORM\Column(name: 'namelc', nullable: true)]
44-
private string $namelc;
46+
#[ORM\Column(name: 'namelc', type: 'string', length: 255, nullable: true)]
47+
private ?string $namelc = null;
4548

46-
#[ORM\Column(name: 'email')]
49+
#[ORM\Column(name: 'email', type: 'string', length: 255, nullable: false)]
4750
private string $email;
4851

4952
#[ORM\Column(name: 'modifiedby', type: 'string', length: 66, nullable: true)]
50-
private ?string $modifiedBy;
53+
private ?string $modifiedBy = null;
5154

52-
#[ORM\Column(name: 'password')]
53-
private string $passwordHash;
55+
#[ORM\Column(name: 'password', type: 'string', length: 255, nullable: true)]
56+
private ?string $passwordHash = null;
5457

5558
#[ORM\Column(name: 'passwordchanged', type: 'date', nullable: true)]
56-
private ?DateTime $passwordChangeDate;
59+
private ?DateTime $passwordChangeDate = null;
5760

58-
#[ORM\Column(type: 'boolean')]
59-
private bool $disabled;
61+
#[ORM\Column(name: 'disabled', type: 'boolean', nullable: false)]
62+
private bool $disabled = false;
6063

61-
#[ORM\Column(name: 'superuser', type: 'boolean')]
62-
private bool $superUser;
64+
#[ORM\Column(name: 'superuser', type: 'boolean', nullable: false)]
65+
private bool $superUser = false;
6366

6467
#[ORM\Column(name: 'privileges', type: 'text', nullable: true)]
65-
private ?string $privileges;
68+
private ?string $privileges = null;
6669

6770
public function __construct()
6871
{
69-
$this->disabled = false;
70-
$this->superUser = false;
71-
$this->passwordChangeDate = null;
72-
$this->loginName = '';
73-
$this->passwordHash = '';
7472
$this->createdAt = new DateTime();
75-
$this->updatedAt = null;
73+
$this->updatedAt = new DateTime();
74+
$this->loginName = '';
7675
$this->email = '';
77-
$this->privileges = null;
76+
}
77+
78+
public function getId(): ?int
79+
{
80+
return $this->id;
81+
}
82+
83+
public function getCreatedAt(): ?DateTime
84+
{
85+
return $this->createdAt;
86+
}
87+
88+
public function getUpdatedAt(): DateTime
89+
{
90+
return $this->updatedAt;
7891
}
7992

8093
public function getLoginName(): string
@@ -85,7 +98,6 @@ public function getLoginName(): string
8598
public function setLoginName(string $loginName): self
8699
{
87100
$this->loginName = $loginName;
88-
89101
return $this;
90102
}
91103

@@ -97,20 +109,29 @@ public function getEmail(): string
97109
public function setEmail(string $email): self
98110
{
99111
$this->email = $email;
112+
return $this;
113+
}
114+
115+
public function getNameLc(): ?string
116+
{
117+
return $this->namelc;
118+
}
100119

120+
public function setNameLc(?string $nameLc): self
121+
{
122+
$this->namelc = $nameLc;
101123
return $this;
102124
}
103125

104-
public function getPasswordHash(): string
126+
public function getPasswordHash(): ?string
105127
{
106128
return $this->passwordHash;
107129
}
108130

109-
public function setPasswordHash(string $passwordHash): self
131+
public function setPasswordHash(?string $passwordHash): self
110132
{
111133
$this->passwordHash = $passwordHash;
112-
$this->passwordChangeDate = new DateTime();
113-
134+
$this->passwordChangeDate = $passwordHash !== null ? new DateTime() : null;
114135
return $this;
115136
}
116137

@@ -127,7 +148,6 @@ public function isDisabled(): bool
127148
public function setDisabled(bool $disabled): self
128149
{
129150
$this->disabled = $disabled;
130-
131151
return $this;
132152
}
133153

@@ -139,31 +159,16 @@ public function isSuperUser(): bool
139159
public function setSuperUser(bool $superUser): self
140160
{
141161
$this->superUser = $superUser;
142-
143162
return $this;
144163
}
145164

146-
public function setNameLc(string $nameLc): self
147-
{
148-
$this->namelc = $nameLc;
149-
150-
return $this;
151-
}
152-
153-
public function getNameLc(): string
154-
{
155-
return $this->namelc;
156-
}
157-
158165
public function setPrivileges(Privileges $privileges): self
159166
{
160167
$this->privileges = $privileges->toSerialized();
161-
162168
return $this;
163169
}
164170

165171
/**
166-
* @SuppressWarnings(PHPMD.StaticAccess)
167172
* @throws InvalidArgumentException
168173
*/
169174
public function setPrivilegesFromArray(array $privilegesData): void
@@ -172,46 +177,18 @@ public function setPrivilegesFromArray(array $privilegesData): void
172177
foreach ($privilegesData as $key => $value) {
173178
$flag = PrivilegeFlag::tryFrom($key);
174179
if (!$flag) {
175-
throw new InvalidArgumentException('Unknown privilege key: '. $key);
180+
throw new InvalidArgumentException('Unknown privilege key: ' . $key);
176181
}
177-
178182
$privileges = $value ? $privileges->grant($flag) : $privileges->revoke($flag);
179183
}
180184
$this->setPrivileges($privileges);
181185
}
182186

183-
/**
184-
* @SuppressWarnings(PHPMD.StaticAccess)
185-
*/
186187
public function getPrivileges(): Privileges
187188
{
188189
return Privileges::fromSerialized($this->privileges);
189190
}
190191

191-
public function getCreatedAt(): ?DateTime
192-
{
193-
return $this->createdAt;
194-
}
195-
196-
public function getId(): ?int
197-
{
198-
return $this->id;
199-
}
200-
201-
public function getUpdatedAt(): ?DateTime
202-
{
203-
return $this->updatedAt;
204-
}
205-
206-
#[ORM\PrePersist]
207-
#[ORM\PreUpdate]
208-
public function updateUpdatedAt(): DomainModel
209-
{
210-
$this->updatedAt = new DateTime();
211-
212-
return $this;
213-
}
214-
215192
public function setModifiedBy(?string $modifiedBy): self
216193
{
217194
$this->modifiedBy = $modifiedBy;
@@ -231,4 +208,19 @@ public function owns(OwnableInterface $resource): bool
231208

232209
return $resource->getOwner()->getId() === $this->getId();
233210
}
211+
212+
#[ORM\PrePersist]
213+
public function onPrePersist(): void
214+
{
215+
if ($this->createdAt === null) {
216+
$this->createdAt = new DateTime();
217+
}
218+
$this->updatedAt = new DateTime();
219+
}
220+
221+
#[ORM\PreUpdate]
222+
public function onPreUpdate(): void
223+
{
224+
$this->updatedAt = new DateTime();
225+
}
234226
}

0 commit comments

Comments
 (0)