Test Failed
Push — main ( 30c6a0...16fa93 )
by Bingo
05:54
created

DbIdentityServiceProvider::saveTenant()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 15
rs 9.8666
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Jabe\Engine\Impl\Identity\Db;
4
5
use Jabe\Engine\Authorization\{
6
    Permissions,
7
    Resources
8
};
9
use Jabe\Engine\Identity\{
10
    GroupInterface,
11
    TenantInterface,
12
    UserInterface
13
};
14
use Jabe\Engine\Impl\ProcessEngineLogger;
15
use Jabe\Engine\Impl\Cfg\ProcessEngineConfigurationImpl;
16
use Jabe\Engine\Impl\Context\Context;
17
use Jabe\Engine\Impl\Identity\{
18
    IdentityOperationResult,
19
    IndentityLogger,
20
    WritableIdentityProviderInterface
21
};
22
use Jabe\Engine\Impl\Persistence\Entity\{
23
    GroupEntity,
24
    MembershipEntity,
25
    TenantEntity,
26
    TenantMembershipEntity,
27
    UserEntity
28
};
29
use Jabe\Engine\Impl\Util\{
30
    ClockUtil,
31
    EnsureUtil
32
};
33
34
class DbIdentityServiceProvider extends DbReadOnlyIdentityServiceProvider implements WritableIdentityProviderInterface
35
{
36
    //protected static final IndentityLogger LOG = ProcessEngineLogger.INDENTITY_LOGGER;
37
38
    // users ////////////////////////////////////////////////////////
39
40
    public function createNewUser(string $userId): UserEntity
41
    {
42
        $this->checkAuthorization(Permissions::create(), Resources::user(), null);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $resourceId of Jabe\Engine\Impl\Identit...r::checkAuthorization(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        $this->checkAuthorization(Permissions::create(), Resources::user(), /** @scrutinizer ignore-type */ null);
Loading history...
43
        return new UserEntity($userId);
44
    }
45
46
    public function saveUser(UserInterface $user): IdentityOperationResult
47
    {
48
        $userEntity = $user;
49
50
        // encrypt password
51
        $userEntity->encryptPassword();
0 ignored issues
show
Bug introduced by
The method encryptPassword() does not exist on Jabe\Engine\Identity\UserInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Jabe\Engine\Identity\UserInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        $userEntity->/** @scrutinizer ignore-call */ 
52
                     encryptPassword();
Loading history...
52
53
        $operation = null;
54
        if ($userEntity->getRevision() == 0) {
0 ignored issues
show
Bug introduced by
The method getRevision() does not exist on Jabe\Engine\Identity\UserInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Jabe\Engine\Identity\UserInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        if ($userEntity->/** @scrutinizer ignore-call */ getRevision() == 0) {
Loading history...
55
            $operation = IdentityOperationResult::OPERATION_CREATE;
56
            $this->checkAuthorization(Permissions::create(), Resources::user(), null);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $resourceId of Jabe\Engine\Impl\Identit...r::checkAuthorization(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
            $this->checkAuthorization(Permissions::create(), Resources::user(), /** @scrutinizer ignore-type */ null);
Loading history...
57
            $this->getDbEntityManager()->insert($userEntity);
58
            $this->createDefaultAuthorizations($userEntity);
59
        } else {
60
            $operation = IdentityOperationResult::OPERATION_UPDATE;
61
            $this->checkAuthorization(Permissions::update(), Resources::user(), $user->getId());
62
            $this->getDbEntityManager()->merge($userEntity);
63
        }
64
65
        return new IdentityOperationResult($userEntity, $operation);
0 ignored issues
show
Bug introduced by
It seems like $userEntity can also be of type Jabe\Engine\Impl\Db\DbEntityInterface; however, parameter $value of Jabe\Engine\Impl\Identit...onResult::__construct() does only seem to accept Serializable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        return new IdentityOperationResult(/** @scrutinizer ignore-type */ $userEntity, $operation);
Loading history...
66
    }
67
68
    public function deleteUser(string $userId): IdentityOperationResult
69
    {
70
        $this->checkAuthorization(Permissions::delete(), Resources::user(), $userId);
71
        $user = $this->findUserById($userId);
72
        if ($user != null) {
73
            $this->deleteMembershipsByUserId($userId);
74
            $this->deleteTenantMembershipsOfUser($userId);
75
76
            $this->deleteAuthorizations(Resources::user(), $userId);
77
78
            Context::getCommandContext()->runWithoutAuthorization(function () use ($scope, $userId) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $scope seems to be never defined.
Loading history...
79
                $tenants = $scope->createTenantQuery()->userMember($userId)->list();
80
                if (!empty($tenants)) {
81
                    foreach ($tenants as $tenant) {
82
                        $scope->deleteAuthorizationsForUser(Resources::tenant(), $tenant->getId(), $userId);
83
                    }
84
                }
85
                return null;
86
            });
87
88
            $this->getDbEntityManager()->delete($user);
89
            return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_DELETE);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type Serializable expected by parameter $value of Jabe\Engine\Impl\Identit...onResult::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
            return new IdentityOperationResult(/** @scrutinizer ignore-type */ null, IdentityOperationResult::OPERATION_DELETE);
Loading history...
90
        }
91
        return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_NONE);
92
    }
93
94
    public function checkPassword(string $userId, string $password): bool
95
    {
96
        $user = $this->findUserById($userId);
97
        if ($user == null || empty($password)) {
98
            return false;
99
        }
100
101
        if ($this->isUserLocked($user)) {
102
            return false;
103
        }
104
105
        if ($this->matchPassword($password, $user)) {
106
            $this->unlockUser($user);
107
            return true;
108
        } else {
109
            $this->lockUser($user);
110
            return false;
111
        }
112
    }
113
114
    protected function isUserLocked(UserEntity $user): bool
115
    {
116
        $processEngineConfiguration = Context::getProcessEngineConfiguration();
117
118
        $maxAttempts = $processEngineConfiguration->getLoginMaxAttempts();
0 ignored issues
show
Bug introduced by
The method getLoginMaxAttempts() does not exist on Jabe\Engine\Impl\Cfg\Pro...EngineConfigurationImpl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

118
        /** @scrutinizer ignore-call */ 
119
        $maxAttempts = $processEngineConfiguration->getLoginMaxAttempts();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
        $attempts = $user->getAttempts();
120
121
        if ($attempts >= $maxAttempts) {
122
            return true;
123
        }
124
125
        $lockExpirationTime = $user->getLockExpirationTime();
126
        $currentTime = ClockUtil::getCurrentTime();
127
128
        return $lockExpirationTime !== null && (new \DateTime($lockExpirationTime)) > $currentTime;
129
    }
130
131
    protected function lockUser(UserEntity $user): void
132
    {
133
        $processEngineConfiguration = Context::getProcessEngineConfiguration();
134
135
        $max = $processEngineConfiguration->getLoginDelayMaxTime();
0 ignored issues
show
Bug introduced by
The method getLoginDelayMaxTime() does not exist on Jabe\Engine\Impl\Cfg\Pro...EngineConfigurationImpl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
        /** @scrutinizer ignore-call */ 
136
        $max = $processEngineConfiguration->getLoginDelayMaxTime();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
        $baseTime = $processEngineConfiguration->getLoginDelayBase();
0 ignored issues
show
Bug introduced by
The method getLoginDelayBase() does not exist on Jabe\Engine\Impl\Cfg\Pro...EngineConfigurationImpl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
        /** @scrutinizer ignore-call */ 
137
        $baseTime = $processEngineConfiguration->getLoginDelayBase();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
        $factor = $processEngineConfiguration->getLoginDelayFactor();
0 ignored issues
show
Bug introduced by
The method getLoginDelayFactor() does not exist on Jabe\Engine\Impl\Cfg\Pro...EngineConfigurationImpl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

137
        /** @scrutinizer ignore-call */ 
138
        $factor = $processEngineConfiguration->getLoginDelayFactor();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
        $attempts = $user->getAttempts() + 1;
139
140
        $delay = $baseTime * pow($factor, $attempts - 1);
141
        $delay = min($delay, $max);
142
143
        $currentTime = ClockUtil::getCurrentTime()->getTimestamp();
144
        $lockExpirationTime = (new \DateTime())->setTimestamp($currentTime + $delay);
145
146
        if ($attempts >= $processEngineConfiguration->getLoginMaxAttempts()) {
147
            //LOG.infoUserPermanentlyLocked(user.getId());
148
        } else {
149
            //LOG.infoUserTemporarilyLocked(user.getId(), lockExpirationTime);
150
        }
151
152
        $this->getIdentityInfoManager()->updateUserLock($user, $attempts, $lockExpirationTime);
0 ignored issues
show
Bug introduced by
$lockExpirationTime of type DateTime is incompatible with the type string expected by parameter $lockExpirationTime of Jabe\Engine\Impl\Persist...nager::updateUserLock(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

152
        $this->getIdentityInfoManager()->updateUserLock($user, $attempts, /** @scrutinizer ignore-type */ $lockExpirationTime);
Loading history...
153
    }
154
155
    public function unlockUser($userOrUserId): IdentityOperationResult
156
    {
157
        if (is_string($userOrUserId)) {
158
            $user = $this->findUserById($userOrUserId);
159
            if ($user != null) {
160
                return $this->unlockUser($user);
161
            }
162
            return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_NONE);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type Serializable expected by parameter $value of Jabe\Engine\Impl\Identit...onResult::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

162
            return new IdentityOperationResult(/** @scrutinizer ignore-type */ null, IdentityOperationResult::OPERATION_NONE);
Loading history...
163
        } elseif ($userOrUserId instanceof UserEntity) {
164
            if ($user->getAttempts() > 0 || $user->getLockExpirationTime() !== null) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user seems to be never defined.
Loading history...
165
                $this->getIdentityInfoManager()->updateUserLock($user, 0, null);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $lockExpirationTime of Jabe\Engine\Impl\Persist...nager::updateUserLock(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

165
                $this->getIdentityInfoManager()->updateUserLock($user, 0, /** @scrutinizer ignore-type */ null);
Loading history...
166
                return new IdentityOperationResult($user, IdentityOperationResult::OPERATION_UNLOCK);
167
            }
168
            return new IdentityOperationResult($user, IdentityOperationResult::OPERATION_NONE);
169
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Jabe\Engine\Impl\Identity\IdentityOperationResult. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
170
    }
171
172
    // groups ////////////////////////////////////////////////////////
173
174
    public function createNewGroup(string $groupId): GroupEntity
175
    {
176
        $this->checkAuthorization(Permissions::create(), Resources::group(), null);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $resourceId of Jabe\Engine\Impl\Identit...r::checkAuthorization(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

176
        $this->checkAuthorization(Permissions::create(), Resources::group(), /** @scrutinizer ignore-type */ null);
Loading history...
177
        return new GroupEntity($groupId);
178
    }
179
180
    public function saveGroup(GroupInterface $group): IdentityOperationResult
181
    {
182
        $groupEntity = $group;
183
        $operation = null;
184
        if ($groupEntity->getRevision() == 0) {
0 ignored issues
show
Bug introduced by
The method getRevision() does not exist on Jabe\Engine\Identity\GroupInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Jabe\Engine\Identity\GroupInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

184
        if ($groupEntity->/** @scrutinizer ignore-call */ getRevision() == 0) {
Loading history...
185
            $operation = IdentityOperationResult::OPERATION_CREATE;
186
            $this->checkAuthorization(Permissions::create(), Resources::group(), null);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $resourceId of Jabe\Engine\Impl\Identit...r::checkAuthorization(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

186
            $this->checkAuthorization(Permissions::create(), Resources::group(), /** @scrutinizer ignore-type */ null);
Loading history...
187
            $this->getDbEntityManager()->insert($groupEntity);
188
            $this->createDefaultAuthorizations($group);
189
        } else {
190
            $operation = IdentityOperationResult::OPERATION_UPDATE;
191
            $this->checkAuthorization(Permissions::update(), Resources::group(), $group->getId());
192
            $this->getDbEntityManager()->merge($groupEntity);
193
        }
194
        return new IdentityOperationResult($groupEntity, $operation);
0 ignored issues
show
Bug introduced by
It seems like $groupEntity can also be of type Jabe\Engine\Impl\Db\DbEntityInterface; however, parameter $value of Jabe\Engine\Impl\Identit...onResult::__construct() does only seem to accept Serializable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

194
        return new IdentityOperationResult(/** @scrutinizer ignore-type */ $groupEntity, $operation);
Loading history...
195
    }
196
197
    public function deleteGroup(string $groupId): IdentityOperationResult
198
    {
199
        $this->checkAuthorization(Permissions::delete(), Resources::group(), $groupId);
200
        $group = $this->findGroupById($groupId);
201
        if ($group !== null) {
202
            $this->deleteMembershipsByGroupId($groupId);
203
            $this->deleteTenantMembershipsOfGroup($groupId);
204
205
            $this->deleteAuthorizations(Resources::group(), $groupId);
206
207
            $scope = $this;
208
            Context::getCommandContext()->runWithoutAuthorization(function () use ($scope, $groupId) {
209
                $tenants = $scope->createTenantQuery()->groupMember($groupId)->list();
210
                if (!empty($tenants)) {
211
                    foreach ($tenants as $tenant) {
212
                        $this->deleteAuthorizationsForGroup(Resources::tenant(), $tenant->getId(), $groupId);
213
                    }
214
                }
215
                return null;
216
            });
217
            $this->getDbEntityManager()->delete(group);
0 ignored issues
show
Bug introduced by
The constant Jabe\Engine\Impl\Identity\Db\group was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
218
            return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_DELETE);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type Serializable expected by parameter $value of Jabe\Engine\Impl\Identit...onResult::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

218
            return new IdentityOperationResult(/** @scrutinizer ignore-type */ null, IdentityOperationResult::OPERATION_DELETE);
Loading history...
219
        }
220
        return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_NONE);
221
    }
222
223
    // tenants //////////////////////////////////////////////////////
224
225
    public function createNewTenant(string $tenantId): TenantInterface
226
    {
227
        $this->checkAuthorization(Permissions::create(), Resources::tenant(), null);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $resourceId of Jabe\Engine\Impl\Identit...r::checkAuthorization(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

227
        $this->checkAuthorization(Permissions::create(), Resources::tenant(), /** @scrutinizer ignore-type */ null);
Loading history...
228
        return new TenantEntity($tenantId);
229
    }
230
231
    public function saveTenant(TenantInterface $tenant): IdentityOperationResult
232
    {
233
        $tenantEntity = $tenant;
234
        $operation = null;
235
        if ($tenantEntity->getRevision() == 0) {
0 ignored issues
show
Bug introduced by
The method getRevision() does not exist on Jabe\Engine\Identity\TenantInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Jabe\Engine\Identity\TenantInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

235
        if ($tenantEntity->/** @scrutinizer ignore-call */ getRevision() == 0) {
Loading history...
236
            $operation = IdentityOperationResult::OPERATION_CREATE;
237
            $this->checkAuthorization(Permissions::create(), Resources::tenant(), null);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $resourceId of Jabe\Engine\Impl\Identit...r::checkAuthorization(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

237
            $this->checkAuthorization(Permissions::create(), Resources::tenant(), /** @scrutinizer ignore-type */ null);
Loading history...
238
            $this->getDbEntityManager()->insert($tenantEntity);
239
            $this->createDefaultAuthorizations($tenant);
240
        } else {
241
            $operation = IdentityOperationResult::OPERATION_UPDATE;
242
            $this->checkAuthorization(Permissions::update(), Resources::tenant(), $tenant->getId());
243
            $this->getDbEntityManager()->merge($tenantEntity);
244
        }
245
        return new IdentityOperationResult($tenantEntity, $operation);
0 ignored issues
show
Bug introduced by
It seems like $tenantEntity can also be of type Jabe\Engine\Impl\Db\DbEntityInterface; however, parameter $value of Jabe\Engine\Impl\Identit...onResult::__construct() does only seem to accept Serializable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

245
        return new IdentityOperationResult(/** @scrutinizer ignore-type */ $tenantEntity, $operation);
Loading history...
246
    }
247
248
    public function deleteTenant(string $tenantId): IdentityOperationResult
249
    {
250
        $this->checkAuthorization(Permissions::delete(), Resources::tenant(), $tenantId);
251
        $tenant = $this->findTenantById($tenantId);
252
        if ($tenant !== null) {
253
            $this->deleteTenantMembershipsOfTenant($tenantId);
254
            $this->deleteAuthorizations(Resources::tenant(), $tenantId);
255
            $this->getDbEntityManager()->delete($tenant);
256
            return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_DELETE);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type Serializable expected by parameter $value of Jabe\Engine\Impl\Identit...onResult::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

256
            return new IdentityOperationResult(/** @scrutinizer ignore-type */ null, IdentityOperationResult::OPERATION_DELETE);
Loading history...
257
        }
258
        return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_NONE);
259
    }
260
261
    // membership //////////////////////////////////////////////////////
262
263
    public function createMembership(string $userId, string $groupId): IdentityOperationResult
264
    {
265
        $this->checkAuthorization(Permissions::create(), Resources::groupMembership(), $groupId);
266
        $user = $this->findUserById($userId);
267
        EnsureUtil::ensureNotNull("No user found with id '" . $userId . "'.", "user", $user);
268
        $group = $this->findGroupById($groupId);
269
        EnsureUtil::ensureNotNull("No group found with id '" . $groupId . "'.", "group", $group);
270
        $membership = new MembershipEntity();
271
        $membership->setUser($user);
272
        $membership->setGroup($group);
273
        $this->getDbEntityManager()->insert($membership);
274
        $this->createDefaultMembershipAuthorizations($userId, $groupId);
275
        return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_CREATE);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type Serializable expected by parameter $value of Jabe\Engine\Impl\Identit...onResult::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

275
        return new IdentityOperationResult(/** @scrutinizer ignore-type */ null, IdentityOperationResult::OPERATION_CREATE);
Loading history...
276
    }
277
278
    public function deleteMembership(string $userId, string $groupId): IdentityOperationResult
279
    {
280
        $this->checkAuthorization(Permissions::delete(), Resources::groupMembership(), $groupId);
281
        if ($this->existsMembership($userId, $groupId)) {
282
            $this->deleteAuthorizations(Resources::groupMembership(), $groupId);
283
            $parameters = [];
284
            $parameters["userId"] = $userId;
285
            $parameters["groupId"] = $groupId;
286
            $this->getDbEntityManager()->delete(MembershipEntity::class, "deleteMembership", $parameters);
287
            return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_DELETE);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type Serializable expected by parameter $value of Jabe\Engine\Impl\Identit...onResult::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

287
            return new IdentityOperationResult(/** @scrutinizer ignore-type */ null, IdentityOperationResult::OPERATION_DELETE);
Loading history...
288
        }
289
        return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_NONE);
290
    }
291
292
    protected function deleteMembershipsByUserId(string $userId): void
293
    {
294
        $this->getDbEntityManager()->delete(MembershipEntity::class, "deleteMembershipsByUserId", $userId);
295
    }
296
297
    protected function deleteMembershipsByGroupId(string $groupId): void
298
    {
299
        $this->getDbEntityManager()->delete(MembershipEntity::class, "deleteMembershipsByGroupId", $groupId);
300
    }
301
302
    public function createTenantUserMembership(string $tenantId, string $userId): IdentityOperationResult
303
    {
304
        $this->checkAuthorization(Permissions::create(), Resources::tenantMembership(), $tenantId);
305
306
        $tenant = $this->findTenantById($tenantId);
307
        $user = $this->findUserById($userId);
308
309
        EnsureUtil::ensureNotNull("No tenant found with id '" . $tenantId . "'.", "tenant", $tenant);
310
        EnsureUtil::ensureNotNull("No user found with id '" . $userId . "'.", "user", $user);
311
312
        $membership = new TenantMembershipEntity();
313
        $membership->setTenant($tenant);
314
        $membership->setUser($user);
315
316
        $this->getDbEntityManager()->insert($membership);
317
318
        $this->createDefaultTenantMembershipAuthorizations($tenant, $user);
319
        return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_CREATE);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type Serializable expected by parameter $value of Jabe\Engine\Impl\Identit...onResult::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

319
        return new IdentityOperationResult(/** @scrutinizer ignore-type */ null, IdentityOperationResult::OPERATION_CREATE);
Loading history...
320
    }
321
322
    public function createTenantGroupMembership(string $tenantId, string $groupId): IdentityOperationResult
323
    {
324
        $this->checkAuthorization(Permissions::create(), Resources::tenantMembership(), $tenantId);
325
326
        $tenant = $this->findTenantById($tenantId);
327
        $group = $this->findGroupById($groupId);
328
329
        EnsureUtil::ensureNotNull("No tenant found with id '" . $tenantId . "'.", "tenant", $tenant);
330
        EnsureUtil::ensureNotNull("No group found with id '" . $groupId . "'.", "group", $group);
331
332
        $membership = new TenantMembershipEntity();
333
        $membership->setTenant($tenant);
334
        $membership->setGroup($group);
335
336
        $this->getDbEntityManager()->insert($membership);
337
338
        $this->createDefaultTenantMembershipAuthorizations($tenant, $group);
339
        return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_CREATE);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type Serializable expected by parameter $value of Jabe\Engine\Impl\Identit...onResult::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

339
        return new IdentityOperationResult(/** @scrutinizer ignore-type */ null, IdentityOperationResult::OPERATION_CREATE);
Loading history...
340
    }
341
342
    public function deleteTenantUserMembership(string $tenantId, string $userId): IdentityOperationResult
343
    {
344
        $this->checkAuthorization(Permissions::delete(), Resources::tenantMembership(), $tenantId);
345
        if ($this->existsTenantMembership($tenantId, $userId, null)) {
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $groupId of Jabe\Engine\Impl\Identit...xistsTenantMembership(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

345
        if ($this->existsTenantMembership($tenantId, $userId, /** @scrutinizer ignore-type */ null)) {
Loading history...
346
            $this->deleteAuthorizations(Resources::tenantMembership(), $userId);
347
348
            $this->deleteAuthorizationsForUser(Resources::tenant(), $tenantId, $userId);
349
350
            $parameters = [];
351
            $parameters["tenantId"] = $tenantId;
352
            $parameters["userId"] = $userId;
353
            $this->getDbEntityManager()->delete(TenantMembershipEntity::class, "deleteTenantMembership", $parameters);
354
            return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_DELETE);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type Serializable expected by parameter $value of Jabe\Engine\Impl\Identit...onResult::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

354
            return new IdentityOperationResult(/** @scrutinizer ignore-type */ null, IdentityOperationResult::OPERATION_DELETE);
Loading history...
355
        }
356
        return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_NONE);
357
    }
358
359
    public function deleteTenantGroupMembership(string $tenantId, string $groupId): IdentityOperationResult
360
    {
361
        $this->checkAuthorization(Permissions::delete(), Resources::tenantMembership(), $tenantId);
362
363
        if ($this->existsTenantMembership($tenantId, null, $groupId)) {
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $userId of Jabe\Engine\Impl\Identit...xistsTenantMembership(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

363
        if ($this->existsTenantMembership($tenantId, /** @scrutinizer ignore-type */ null, $groupId)) {
Loading history...
364
            $this->deleteAuthorizations(Resources::tenantMembership(), $groupId);
365
366
            $this->deleteAuthorizationsForGroup(Resources::tenant(), $tenantId, $groupId);
367
368
            $parameters = [];
369
            $parameters["tenantId"] = $tenantId;
370
            $parameters["groupId"] = $groupId;
371
            $this->getDbEntityManager()->delete(TenantMembershipEntity::class, "deleteTenantMembership", $parameters);
372
            return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_DELETE);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type Serializable expected by parameter $value of Jabe\Engine\Impl\Identit...onResult::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

372
            return new IdentityOperationResult(/** @scrutinizer ignore-type */ null, IdentityOperationResult::OPERATION_DELETE);
Loading history...
373
        }
374
        return new IdentityOperationResult(null, IdentityOperationResult::OPERATION_NONE);
375
    }
376
377
    protected function deleteTenantMembershipsOfUser(string $userId): void
378
    {
379
        $this->getDbEntityManager()->delete(TenantMembershipEntity::class, "deleteTenantMembershipsOfUser", $userId);
380
    }
381
382
    protected function deleteTenantMembershipsOfGroup(string $groupId): void
383
    {
384
        $this->getDbEntityManager()->delete(TenantMembershipEntity::class, "deleteTenantMembershipsOfGroup", $groupId);
385
    }
386
387
    protected function deleteTenantMembershipsOfTenant(string $tenant): void
388
    {
389
        $this->getDbEntityManager()->delete(TenantMembershipEntity::class, "deleteTenantMembershipsOfTenant", $tenant);
390
    }
391
392
    // authorizations ////////////////////////////////////////////////////////////
393
394
    protected function createDefaultAuthorizations(/*UserEntity|GroupInterface|TenantInterface*/$data): void
395
    {
396
        if ($data instanceof UserEntity) {
397
            if (Context::getProcessEngineConfiguration()->isAuthorizationEnabled()) {
398
                $this->saveDefaultAuthorizations($this->getResourceAuthorizationProvider()->newUser($data));
399
            }
400
        } elseif ($data instanceof GroupInterface) {
401
            if ($this->AuthorizationEnabled()) {
0 ignored issues
show
Bug introduced by
The method AuthorizationEnabled() does not exist on Jabe\Engine\Impl\Identit...IdentityServiceProvider. Did you maybe mean isAuthorizationEnabled()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

401
            if ($this->/** @scrutinizer ignore-call */ AuthorizationEnabled()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
402
                $this->saveDefaultAuthorizations($this->getResourceAuthorizationProvider()->newGroup($data));
403
            }
404
        } elseif ($data instanceof TenantInterface) {
405
            if ($this->AuthorizationEnabled()) {
406
                $this->saveDefaultAuthorizations($this->getResourceAuthorizationProvider()->newTenant($data));
407
            }
408
        }
409
    }
410
411
    protected function createDefaultMembershipAuthorizations(string $userId, string $groupId): void
412
    {
413
        if ($this->AuthorizationEnabled()) {
414
            $this->saveDefaultAuthorizations($this->getResourceAuthorizationProvider()->groupMembershipCreated($groupId, $userId));
415
        }
416
    }
417
418
    protected function createDefaultTenantMembershipAuthorizations(TenantInterface $tenant, /*UserInterface|GroupInterface*/$data): void
419
    {
420
        if ($this->AuthorizationEnabled()) {
421
            $this->saveDefaultAuthorizations($this->getResourceAuthorizationProvider()->tenantMembershipCreated($tenant, $data));
422
        }
423
    }
424
}
425