-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUserRepository.php
More file actions
213 lines (188 loc) · 6.17 KB
/
UserRepository.php
File metadata and controls
213 lines (188 loc) · 6.17 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
declare(strict_types=1);
/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/
namespace Pimcore\Bundle\StudioBackendBundle\User\Repository;
use Exception;
use Pimcore\Bundle\StaticResolverBundle\Models\User\UserResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\DatabaseException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Model\User;
use Pimcore\Model\User\Listing as UserListing;
use Pimcore\Model\UserInterface;
use function count;
use function sprintf;
/**
* @internal
*/
final readonly class UserRepository implements UserRepositoryInterface
{
public function __construct(
private SecurityServiceInterface $securityService,
private UserResolverInterface $userResolver,
) {
}
public function getUserListingByParentId(int $parentId): UserListing
{
$listing = new UserListing();
$listing->setCondition('parentId = ?', $parentId);
$listing->setOrder('ASC');
$listing->setOrderKey('name');
$listing->load();
return $listing;
}
/**
* @throws NotFoundException
*/
public function getUserById(int $userId): UserInterface
{
$user = $this->userResolver->getById($userId);
if (!$user instanceof User) {
throw new NotFoundException('User', $userId);
}
return $user;
}
/**
* @throws Exception
*/
public function deleteUser(UserInterface $user): void
{
$user->delete();
}
/**
* @throws Exception
*/
public function createUser(string $username, int $folderId): UserInterface
{
return $this->userResolver->create([
'parentId' => $folderId,
'name' => $username,
'password' => '',
'active' => true,
]);
}
public function updateUser(UserInterface $user): void
{
try {
$user->save();
} catch (Exception $exception) {
throw new DatabaseException(
sprintf(
'Error updating user with id %d: %s',
$user->getId(),
$exception->getMessage()
)
);
}
}
public function getUserListingByRoleId(int $roleId, ?int $excludeUserId = null): UserListing
{
$listing = new UserListing();
$listing->setCondition('`type` = :type', ['type' => 'user']);
if ($excludeUserId !== null) {
$listing->addConditionParam('id != :excludeUser', ['excludeUser' => $excludeUserId]);
}
$roleCondition = '(roles = :roleId ' .
'OR roles LIKE :roleIdEnds OR roles LIKE :roleIdStarts OR roles LIKE :roleIdContains)';
$roleParams = [
'roleId' => $roleId,
'roleIdEnds' => '%,' . $roleId,
'roleIdStarts' => $roleId . ',%',
'roleIdContains' => '%,' . $roleId . ',%',
];
$listing->addConditionParam('active = :active', ['active' => 1]);
$listing->addConditionParam($roleCondition, $roleParams);
$listing->setOrder('ASC');
$listing->setOrderKey('name');
$listing->load();
return $listing;
}
/**
* @return UserInterface[]
*
* @throws DatabaseException
*/
public function getUsersWithPermission(string $permission, bool $includeCurrentUser): array
{
$users = $this->getUsers($includeCurrentUser);
$usersWithPermission = [];
foreach ($users as $user) {
if ($user->isAllowed($permission)) {
$usersWithPermission[] = $user;
}
}
return $usersWithPermission;
}
/**
* @return UserInterface[]
*
* @throws DatabaseException
*/
public function getUsers(bool $includeCurrentUser = true): array
{
try {
$userListing = new UserListing();
$userListing->setCondition('`type` = :type', ['type' => 'user']);
if (!$includeCurrentUser) {
$userListing->addConditionParam(
'id != :currentUser',
['currentUser' => $this->securityService->getCurrentUser()->getId()]
);
}
$userListing->load();
return $userListing->getUsers();
} catch (Exception $e) {
throw new DatabaseException(sprintf('Error while fetching users: %s', $e->getMessage()));
}
}
/**
* @return UserInterface[]
*
* @throws DatabaseException
*/
public function searchUser(string $searchQuery): array
{
$list = [];
$q = '%' . $searchQuery . '%';
try {
$userListing = new UserListing();
$userListing->setCondition(
'type = ? AND (name LIKE ? OR firstname LIKE ? OR lastname LIKE ? OR email LIKE ? OR id = ?)',
['user', $q, $q, $q, $q, (int)$searchQuery]
);
$userListing->setOrder('ASC');
$userListing->setOrderKey('name');
$userListing->load();
foreach ($userListing->getUsers() as $user) {
if ($user->getName() !== 'system') {
$list[] = $user;
}
}
return $list;
} catch (Exception $e) {
throw new DatabaseException(sprintf('Error while searching for users: %s', $e->getMessage()));
}
}
/**
* {@inheritdoc}
*/
public function getUsersByNames(array $names): array
{
if (empty($names)) {
return [];
}
$listing = new UserListing();
$placeholders = implode(',', array_fill(0, count($names), '?'));
$listing->setCondition('name IN (' . $placeholders . ') AND type = ?', [...$names, 'user']);
$listing->load();
return $listing->getUsers();
}
}