-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileTrait.php
More file actions
98 lines (82 loc) · 2.11 KB
/
Copy pathProfileTrait.php
File metadata and controls
98 lines (82 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace ADT\FancyAdmin\Model\Entities;
use ADT\FancyAdmin\Model\Entities\Traits\CreatedAt;
use ADT\FancyAdmin\Model\Entities\Traits\CreatedByNullable;
use ADT\FancyAdmin\Model\Entities\Traits\IsActive;
use ADT\FancyAdmin\Model\Entities\Traits\UpdatedAt;
use ADT\FancyAdmin\Model\Entities\Traits\UpdatedBy;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\InverseJoinColumn;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\ManyToOne;
use Nette\Security\Resource;
trait ProfileTrait
{
use IsActive;
use CreatedAt;
use CreatedByNullable;
use UpdatedAt;
use UpdatedBy;
#[ManyToOne(targetEntity: 'Identity', inversedBy: 'profiles')]
#[JoinColumn(nullable: false)]
protected Identity $identity;
#[ManyToOne(targetEntity: 'Account', inversedBy: 'profiles')]
#[JoinColumn(nullable: false)]
protected Account $account;
#[ManyToMany(targetEntity: 'AclRole')]
#[JoinColumn(onDelete: "CASCADE")]
#[InverseJoinColumn(onDelete: "RESTRICT")]
protected Collection $roles;
public function __construct()
{
$this->roles = new ArrayCollection();
}
public function getIdentity(): Identity
{
return $this->identity;
}
public function setIdentity(Identity $identity): static
{
$this->identity = $identity;
$identity->addProfile($this);
return $this;
}
/**
* @return AclRole[]
*/
public function getRoles(): array
{
return $this->roles->toArray();
}
public function addRole(AclRole $role): static
{
if ($this->roles->contains($role)) {
return $this;
}
$this->roles->add($role);
return $this;
}
public function isAllowed(Resource $resource): bool
{
foreach ($this->getRoles() as $_role) {
if ($_role->getIsAdmin()) {
return true;
}
if (array_any($_role->getResources(), fn($_resource) => $_resource->getName() === $resource->getResourceId())) {
return true;
}
}
return false;
}
public function getAccount(): Account
{
return $this->account;
}
public function setAccount(Account $account): static
{
$this->account = $account;
return $this;
}
}