-
Notifications
You must be signed in to change notification settings - Fork 26
/
UserRepository.php
152 lines (138 loc) · 5.65 KB
/
UserRepository.php
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
<?php
declare(strict_types=1);
namespace In2code\Luxletter\Domain\Repository;
use Doctrine\DBAL\Exception;
use In2code\Luxletter\Domain\Model\Dto\Filter;
use In2code\Luxletter\Domain\Model\User;
use In2code\Luxletter\Domain\Model\Usergroup;
use In2code\Luxletter\Domain\Service\PermissionTrait;
use In2code\Luxletter\Exception\AuthenticationFailedException;
use In2code\Luxletter\Utility\BackendUserUtility;
use In2code\Luxletter\Utility\DatabaseUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
class UserRepository extends AbstractRepository
{
use PermissionTrait;
protected $defaultOrderings = [
'lastName' => QueryInterface::ORDER_ASCENDING,
];
/**
* Get users grouped by email from groupIdentifiers
* We don't use `group by` anymore because of the problems that came with "sql_mode=only_full_group_by"
*
* @param int[] $groupIdentifiers
* @param int $language -1 = all, otherwise only the users with the specific language are selected
* @param int $limit
* @return array
* @throws AuthenticationFailedException
* @throws InvalidQueryException
* @throws Exception
*/
public function getUsersFromGroups(array $groupIdentifiers, int $language, int $limit = 0): array
{
if ($groupIdentifiers === []) {
return [];
}
$query = $this->createQuery();
$constraints = [
$query->like('email', '%@%'),
];
if ($language !== -1) {
$constraints[] = $query->in('luxletter_language', [-1, $language]);
}
$subConstraints = [];
foreach ($groupIdentifiers as $identifier) {
if ($this->isAuthenticatedForRecord($identifier, Usergroup::TABLE_NAME) === false) {
throw new AuthenticationFailedException('Permission denied for this usergroup', 1709808068);
}
$subConstraints[] = $query->contains('usergroup', $identifier);
}
$constraints[] = $query->logicalOr(...$subConstraints);
if ($limit > 0) {
$query->setLimit($limit * 10);
}
$query->matching($query->logicalAnd(...$constraints));
$query->setOrderings(['email' => QueryInterface::ORDER_ASCENDING]);
$users = $query->execute()->toArray();
return $this->groupResultByEmail($users, $limit);
}
protected function groupResultByEmail(array $users, int $limit): array
{
$result = [];
foreach ($users as $user) {
if (array_key_exists($user->getEmail(), $result) === false) {
$result[$user->getEmail()] = $user;
}
if ($limit > 0 && count($result) >= $limit) {
break;
}
}
return $result;
}
public function getUserAmountFromGroups(array $groupIdentifiers): int
{
if ($groupIdentifiers !== []) {
$connection = DatabaseUtility::getConnectionForTable(User::TABLE_NAME);
/** @noinspection SqlDialectInspection */
$sql = 'select count(distinct email) from ' . User::TABLE_NAME;
$sub = '';
foreach ($groupIdentifiers as $identifier) {
if ($sub !== '') {
$sub .= ' or ';
}
$sub .= 'find_in_set(' . (int)$identifier . ',usergroup)';
}
$sql .= ' where deleted=0 and disable=0 and email like "%@%" and (' . $sub . ')';
return (int)$connection->executeQuery($sql)->fetchOne();
}
return 0;
}
/**
* Get all luxletter receiver users
*
* @param Filter $filter
* @return QueryResultInterface
*/
public function getUsersByFilter(Filter $filter): QueryResultInterface
{
$query = $this->createQuery();
$this->buildQueryForFilter($filter, $query);
return $query->execute();
}
protected function buildQueryForFilter(Filter $filter, QueryInterface $query): void
{
$logicalAnd = [
$query->equals('usergroup.luxletterReceiver', true),
];
if ($filter->isSearchtermSet()) {
foreach ($filter->getSearchterms() as $searchterm) {
$logicalOr = [
$query->like('username', '%' . $searchterm . '%'),
$query->like('email', '%' . $searchterm . '%'),
$query->like('name', '%' . $searchterm . '%'),
$query->like('firstName', '%' . $searchterm . '%'),
$query->like('middleName', '%' . $searchterm . '%'),
$query->like('lastName', '%' . $searchterm . '%'),
$query->like('address', '%' . $searchterm . '%'),
$query->like('title', '%' . $searchterm . '%'),
$query->like('company', '%' . $searchterm . '%'),
];
$logicalAnd[] = $query->logicalOr(...$logicalOr);
}
}
if ($filter->isUsergroupSet()) {
$logicalAnd[] = $query->contains('usergroup', $filter->getUsergroup());
}
if (BackendUserUtility::isAdministrator() === false) {
$usergroupRepository = GeneralUtility::makeInstance(UsergroupRepository::class);
$allowedUsergroupUids = array_keys($usergroupRepository->getReceiverGroups());
$logicalAnd[] = $query->in('usergroup', $allowedUsergroupUids ?: [0]);
}
$constraint = $query->logicalAnd(...$logicalAnd);
$query->matching($constraint);
$query->setLimit($filter->getLimit());
}
}