1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jabe\Engine\Impl\Identity\Db; |
4
|
|
|
|
5
|
|
|
use Jabe\Engine\Authorization\{ |
6
|
|
|
PermissionInterface, |
7
|
|
|
Permissions, |
8
|
|
|
ResourceInterface, |
9
|
|
|
Resources |
10
|
|
|
}; |
11
|
|
|
use Jabe\Engine\Identity\{ |
12
|
|
|
GroupInterface, |
13
|
|
|
GroupQueryInterface, |
14
|
|
|
NativeUserQueryInterface, |
15
|
|
|
TenantInterface, |
16
|
|
|
UserInterface, |
17
|
|
|
UserQueryInterface |
18
|
|
|
}; |
19
|
|
|
use Jabe\Engine\Impl\{ |
20
|
|
|
AbstractQuery, |
21
|
|
|
NativeUserQueryImpl, |
22
|
|
|
UserQueryImp |
23
|
|
|
}; |
24
|
|
|
use Jabe\Engine\Impl\Context\Context; |
25
|
|
|
use Jabe\Engine\Impl\Identity\ReadOnlyIdentityProviderInterface; |
26
|
|
|
use Jabe\Engine\Impl\Interceptor\CommandContext; |
27
|
|
|
use Jabe\Engine\Impl\Persistence\AbstractManager; |
28
|
|
|
use Jabe\Engine\Impl\Persistence\Entity\{ |
29
|
|
|
GroupEntity, |
30
|
|
|
TenantEntity, |
31
|
|
|
UserEntity |
32
|
|
|
}; |
33
|
|
|
use Jabe\Engine\Impl\Util\EncryptionUtil; |
34
|
|
|
|
35
|
|
|
class DbReadOnlyIdentityServiceProvider extends AbstractManager implements ReadOnlyIdentityProviderInterface |
36
|
|
|
{ |
37
|
|
|
// users ///////////////////////////////////////// |
38
|
|
|
public function findUserById(string $userId): ?UserEntity |
39
|
|
|
{ |
40
|
|
|
$this->checkAuthorization(Permissions::read(), Resources::user(), $userId); |
41
|
|
|
return $this->getDbEntityManager()->selectById(UserEntity::class, $userId); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function createUserQuery(CommandContext $commandContext = null): UserQueryImpl |
45
|
|
|
{ |
46
|
|
|
if ($commandContext === null) { |
47
|
|
|
return new DbUserQueryImpl(Context::getProcessEngineConfiguration()->getCommandExecutorTxRequired()); |
|
|
|
|
48
|
|
|
} else { |
49
|
|
|
return new DbUserQueryImpl(); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function createNativeUserQuery(): NativeUserQueryInterface |
54
|
|
|
{ |
55
|
|
|
return new NativeUserQueryImpl(Context::getProcessEngineConfiguration()->getCommandExecutorTxRequired()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function findUserCountByQueryCriteria(DbUserQueryImpl $query): int |
59
|
|
|
{ |
60
|
|
|
$this->configureQuery($query, Resources::user()); |
61
|
|
|
return $this->getDbEntityManager()->selectOne("selectUserCountByQueryCriteria", $query); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function findUserByQueryCriteria(DbUserQueryImpl $query): array |
65
|
|
|
{ |
66
|
|
|
$this->configureQuery($query, Resources::user()); |
67
|
|
|
return $this->getDbEntityManager()->selectList("selectUserByQueryCriteria", $query); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function findUserByNativeQuery(array $parameterMap, int $firstResult, int $maxResults): array |
71
|
|
|
{ |
72
|
|
|
return $this->getDbEntityManager()->selectListWithRawParameter("selectUserByNativeQuery", $parameterMap, $firstResult, $maxResults); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function findUserCountByNativeQuery(array $parameterMap): int |
76
|
|
|
{ |
77
|
|
|
return $this->getDbEntityManager()->selectOne("selectUserCountByNativeQuery", $parameterMap); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function checkPassword(string $userId, string $password): bool |
81
|
|
|
{ |
82
|
|
|
$user = $this->findUserById($userId); |
83
|
|
|
if (($user !== null) && ($password !== null) && $this->matchPassword($password, $user)) { |
84
|
|
|
return true; |
85
|
|
|
} else { |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function matchPassword(string $password, UserEntity $user): bool |
91
|
|
|
{ |
92
|
|
|
$saltedPassword = EncryptionUtil::saltPassword($password, $user->getSalt()); |
93
|
|
|
return Context::getProcessEngineConfiguration() |
94
|
|
|
->getPasswordManager() |
|
|
|
|
95
|
|
|
->check($saltedPassword, $user->getPassword()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// groups ////////////////////////////////////////// |
99
|
|
|
|
100
|
|
|
public function findGroupById(string $groupId): GroupEntity |
101
|
|
|
{ |
102
|
|
|
$this->checkAuthorization(Permissions::read(), Resources::group(), $groupId); |
103
|
|
|
return $this->getDbEntityManager()->selectById(GroupEntity::class, $groupId); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function createGroupQuery(CommandContext $commandContext = null): GroupQueryInterface |
107
|
|
|
{ |
108
|
|
|
if ($commandContext === null) { |
109
|
|
|
return new DbGroupQueryImpl(Context::getProcessEngineConfiguration()->getCommandExecutorTxRequired()); |
110
|
|
|
} |
111
|
|
|
return new DbGroupQueryImpl(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function findGroupCountByQueryCriteria(DbGroupQueryImpl $query): int |
115
|
|
|
{ |
116
|
|
|
$this->configureQuery($query, Resources::group()); |
117
|
|
|
return $this->getDbEntityManager()->selectOne("selectGroupCountByQueryCriteria", $query); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function findGroupByQueryCriteria(DbGroupQueryImpl $query): array |
121
|
|
|
{ |
122
|
|
|
$this->configureQuery($query, Resources::group()); |
123
|
|
|
return $this->getDbEntityManager()->selectList("selectGroupByQueryCriteria", $query); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
//tenants ////////////////////////////////////////// |
127
|
|
|
|
128
|
|
|
public function findTenantById(string $tenantId): TenantEntity |
129
|
|
|
{ |
130
|
|
|
$this->checkAuthorization(Permissions::read(), Resources::tenant(), $tenantId); |
131
|
|
|
return $this->getDbEntityManager()->selectById(TenantEntity::class, $tenantId); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function createTenantQuery(CommandContext $commandContext = null): TenantQueryInterface |
135
|
|
|
{ |
136
|
|
|
if ($commandContext === null) { |
137
|
|
|
return new DbTenantQueryImpl(Context::getProcessEngineConfiguration()->getCommandExecutorTxRequired()); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
return new DbTenantQueryImpl(); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function findTenantCountByQueryCriteria(DbTenantQueryImpl $query): int |
143
|
|
|
{ |
144
|
|
|
$this->configureQuery($query, Resources::tenant()); |
145
|
|
|
return $this->getDbEntityManager()->selectOne("selectTenantCountByQueryCriteria", $query); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function findTenantByQueryCriteria(DbTenantQueryImpl $query): array |
149
|
|
|
{ |
150
|
|
|
$this->configureQuery($query, Resources::tenant()); |
151
|
|
|
return $this->getDbEntityManager()->selectList("selectTenantByQueryCriteria", $query); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
//memberships ////////////////////////////////////////// |
155
|
|
|
protected function existsMembership(string $userId, string $groupId): bool |
156
|
|
|
{ |
157
|
|
|
$key = []; |
158
|
|
|
$key["userId"] = $userId; |
159
|
|
|
$key["groupId"] = $groupId; |
160
|
|
|
return $this->getDbEntityManager()->selectOne("selectMembershipCount", $key) > 0; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
protected function existsTenantMembership(string $tenantId, string $userId, string $groupId): bool |
164
|
|
|
{ |
165
|
|
|
$key = []; |
166
|
|
|
$key["tenantId"] = $tenantId; |
167
|
|
|
if (!empty($userId)) { |
168
|
|
|
$key["userId"] = $userId; |
169
|
|
|
} |
170
|
|
|
if (!empty($groupId)) { |
171
|
|
|
$key["groupId"] = $groupId; |
172
|
|
|
} |
173
|
|
|
return $this->getDbEntityManager()->selectOne("selectTenantMembershipCount", $key) > 0; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
protected function configureQuery(AbstractQuery $query, ResourceInterface $resource): void |
177
|
|
|
{ |
178
|
|
|
Context::getCommandContext() |
179
|
|
|
->getAuthorizationManager() |
180
|
|
|
->configureQuery($query, $resource); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
protected function checkAuthorization(PermissionInterface $permission, ResourceInterface $resource, string $resourceId): void |
184
|
|
|
{ |
185
|
|
|
Context::getCommandContext() |
186
|
|
|
->getAuthorizationManager() |
187
|
|
|
->checkAuthorization($permission, $resource, $resourceId); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|