-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAclRoleTrait.php
More file actions
126 lines (101 loc) · 2.49 KB
/
Copy pathAclRoleTrait.php
File metadata and controls
126 lines (101 loc) · 2.49 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
namespace ADT\FancyAdmin\Model\Entities;
use ADT\FancyAdmin\Model\Entities\Enums\AclRoleTypeEnum;
use ADT\FancyAdmin\Model\Entities\Traits\CreatedAt;
use ADT\FancyAdmin\Model\Entities\Traits\CreatedByNullable;
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 as ORM;
use Nette\Security\Resource;
trait AclRoleTrait
{
use CreatedAt;
use CreatedByNullable;
use UpdatedAt;
use UpdatedBy;
#[ORM\Column(unique: true, nullable: false)]
protected string $name;
#[ORM\OneToMany(targetEntity: 'Acl', mappedBy: 'role')]
protected Collection $acls;
#[ORM\Column(nullable: true)]
protected ?string $context = null;
#[ORM\Column(nullable: false)]
protected AclRoleTypeEnum $type;
#[ORM\Column(nullable: false, options: ["default" => 0])]
protected bool $isAdmin = false;
#[ORM\Column(nullable: false, options: ["default" => 0])]
protected bool $needsSso = false;
public function __construct()
{
$this->acls = new ArrayCollection();
}
public function getRoleId(): string
{
return (string) $this->getName();
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function getName(): string
{
return $this->name;
}
/**
* @return AclResource[]
*/
public function getResources(): array
{
return array_map(
fn(Acl $acl) => $acl->getResource(),
array_filter($this->acls->toArray(), fn(Acl $acl) => $acl->getIsActive()),
);
}
public function getIsAdmin(): bool
{
return $this->isAdmin;
}
public function setIsAdmin(bool $isAdmin): static
{
$this->isAdmin = $isAdmin;
return $this;
}
public function isAllowed(Resource $resource): bool
{
if ($this->isAdmin) {
return true;
}
return array_any($this->getResources(), fn(AclResource $_resource) => $_resource->getName() === $resource->getResourceId());
}
public function getContext(): ?string
{
return $this->context;
}
public function setContext(?string $context): static
{
$this->context = $context;
return $this;
}
public function getNeedsSso(): bool
{
return $this->needsSso;
}
public function setNeedsSso(bool $needsSso): static
{
$this->needsSso = $needsSso;
return $this;
}
public function getType(): AclRoleTypeEnum
{
return $this->type;
}
public function setType(AclRoleTypeEnum $type): static
{
$this->type = $type;
return $this;
}
}