Skip to content

Commit 307676f

Browse files
committed
Modifying user-invite endpoint to search for users with the same name first.
1 parent 59ec0b7 commit 307676f

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

app/V1Module/presenters/RegistrationPresenter.php

+20-5
Original file line numberDiff line numberDiff line change
@@ -286,17 +286,32 @@ public function actionCreateInvitation()
286286

287287
// gather data
288288
$instanceId = $format->instanceId;
289-
$instance = $this->getInstance($instanceId);
290-
$titlesBeforeName = $format->titlesBeforeName === null ? "" : $format->titlesBeforeName;
291-
$titlesAfterName = $format->titlesAfterName === null ? "" : $format->titlesAfterName;
289+
$instance = $this->getInstance($instanceId); // we don't need it, just to check it exists
290+
$titlesBeforeName = $format->titlesBeforeName === null ? "" : trim($format->titlesBeforeName);
291+
$titlesAfterName = $format->titlesAfterName === null ? "" : trim($format->titlesAfterName);
292+
$firstName = trim($format->firstName);
293+
$lastName = trim($format->lastName);
294+
if (!$firstName || !$lastName) {
295+
throw new BadRequestException("The user's full name must be filled in.");
296+
}
297+
298+
// Check for name collisions, unless the request explicitly says to ignore them.
299+
if (!$format->ignoreNameCollision) {
300+
$sameName = $this->users->findByName($instance, $firstName, $lastName);
301+
if ($sameName) {
302+
// let's report the colliding users
303+
$this->sendSuccessResponse($this->userViewFactory->getUsers($sameName));
304+
return;
305+
}
306+
}
292307

293308
// create the token and send it via email
294309
try {
295310
$this->invitationHelper->invite(
296311
$instanceId,
297312
$email,
298-
$format->firstName,
299-
$format->lastName,
313+
$firstName,
314+
$lastName,
300315
$titlesBeforeName,
301316
$titlesAfterName,
302317
$groupsIds,

app/helpers/MetaFormats/FormatDefinitions/UserFormat.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
use App\Helpers\MetaFormats\Attributes\Format;
66
use App\Helpers\MetaFormats\MetaFormat;
7-
use App\Helpers\MetaFormats\Attributes\FormatParameterAttribute;
87
use App\Helpers\MetaFormats\Attributes\FPost;
9-
use App\Helpers\MetaFormats\Type;
108
use App\Helpers\MetaFormats\Validators\VArray;
9+
use App\Helpers\MetaFormats\Validators\VBool;
1110
use App\Helpers\MetaFormats\Validators\VEmail;
1211
use App\Helpers\MetaFormats\Validators\VString;
1312
use App\Helpers\MetaFormats\Validators\VUuid;
@@ -41,4 +40,7 @@ class UserFormat extends MetaFormat
4140

4241
#[FPost(new VString(2, 2), "Language used in the invitation email (en by default).", required: false)]
4342
public ?string $locale;
43+
44+
#[FPost(new VBool(), "If a use with the same name exists, this needs to be set to true.", required: false)]
45+
public ?bool $ignoreNameCollision;
4446
}

app/model/repository/Users.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Helpers\Pagination;
66
use App\Model\Helpers\PaginationDbHelper;
77
use App\Model\Entity\User;
8+
use App\Model\Entity\Instance;
89
use DateTime;
910
use Doctrine\ORM\EntityManagerInterface;
1011

@@ -75,7 +76,7 @@ public function getPaginated(Pagination $pagination, &$totalCount): array
7576

7677

7778
/**
78-
* Search users first names and surnames based on given string.
79+
* Search users first names and surnames based on given (sub)string.
7980
* @param string|null $search
8081
* @return User[]
8182
*/
@@ -93,6 +94,21 @@ public function findByRoles(string ...$roles): array
9394
return $this->findBy(["role" => $roles]);
9495
}
9596

97+
/**
98+
* Find users by exact match of the whole name.
99+
* @param Instance $instance
100+
* @param string $firstName
101+
* @param string $lastName
102+
* @return User[]
103+
*/
104+
public function findByName(Instance $instance, string $firstName, string $lastName): array
105+
{
106+
$users = $this->findBy(["firstName" => $firstName, "lastName" => $lastName]);
107+
return array_filter($users, function (User $user) use ($instance) {
108+
return $user->belongsTo($instance);
109+
});
110+
}
111+
96112
/**
97113
* Find all users who have not authenticated to the system for some time.
98114
* @param DateTime|null $before Only users with last activity before given date

0 commit comments

Comments
 (0)