-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdentityQueryTrait.php
More file actions
86 lines (71 loc) · 2.47 KB
/
Copy pathIdentityQueryTrait.php
File metadata and controls
86 lines (71 loc) · 2.47 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
<?php
declare(strict_types=1);
namespace ADT\FancyAdmin\Model\Queries;
use ADT\DoctrineComponents\QueryObject\QueryObjectByMode;
use ADT\FancyAdmin\Model\Entities\Account;
use ADT\FancyAdmin\Model\Entities\AclRole;
use ADT\FancyAdmin\Model\Entities\Profile;
use ADT\FancyAdmin\Model\Queries\Abstract\BaseQuery;
use Doctrine\ORM\QueryBuilder;
trait IdentityQueryTrait
{
public function byEmailOrPhoneNumber(?string $email = null, ?string $phoneNumber = null): static
{
$this->filter[] = function (QueryBuilder $qb) use ($email, $phoneNumber) {
if ($email && $phoneNumber) {
$qb->andWhere('e.email = :email OR e.phoneNumber = :phoneNumber')
->setParameter('email', $email)
->setParameter('phoneNumber', $phoneNumber);
} elseif ($email) {
$qb->andWhere('e.email = :email')
->setParameter('email', $email);
} else {
$qb->andWhere('e.phoneNumber = :phoneNumber')
->setParameter('phoneNumber', $phoneNumber);
}
};
return $this;
}
public function byUsername(string $username): static
{
return $this->by('username', $username);
}
public function byEmail(string $email): static
{
return $this->by('email', $email);
}
public function byEmailNot(string $email): static
{
return $this->by('email', $email, QueryObjectByMode::NOT_EQUALS);
}
public function byPhoneNumber(string $phoneNumber): static
{
return $this->by('phoneNumber', $phoneNumber);
}
public function bySelectedAccount(Account $account): static
{
return $this->by('selectedAccount', $account);
}
public function byContext(?string $context): static
{
return $this->by('context', $context);
}
public function byAllowedResource(string ...$resourceNames): static
{
$this->filter[] = function (QueryBuilder $qb) use ($resourceNames) {
$em = $this->getEntityManager();
$roleClass = $em->findEntityClassByInterface(AclRole::class);
$profileClass = $em->findEntityClassByInterface(Profile::class);
$sub = $em->getRepository($roleClass)
->createQueryBuilder('_role')
->select('1')
->leftJoin('_role.acls', '_acl', 'WITH', '_acl.isActive = true')
->leftJoin('_acl.resource', '_res')
->andWhere('_role MEMBER OF e.roles OR EXISTS (SELECT 1 FROM ' . $profileClass . ' _prof WHERE _prof.identity = e AND _role MEMBER OF _prof.roles)')
->andWhere('_role.isAdmin = true OR _res.name IN (:_allowedResourceNames)');
$qb->andWhere('EXISTS (' . $sub->getDQL() . ')')
->setParameter('_allowedResourceNames', $resourceNames);
};
return $this;
}
}