Catch groups that are in use.

This commit is contained in:
James Cole 2021-10-26 06:25:41 +02:00
parent fbf7578fb0
commit 6f8778d87f
No known key found for this signature in database
GPG Key ID: BDE6667570EADBD5

View File

@ -117,9 +117,24 @@ class UserEventHandler
*/
public function createGroupMembership(RegisteredUser $event): bool
{
$user = $event->user;
$user = $event->user;
$groupExists = true;
$groupTitle = $user->email;
$index = 1;
// create a new group.
$group = UserGroup::create(['title' => $user->email]);
while (true === $groupExists) {
$groupExists = UserGroup::where('title', $groupTitle)->count() > 0;
if(false === $groupExists) {
$group = UserGroup::create(['title' => $groupTitle]);
break;
}
$groupTitle = sprintf('%s-%d', $user->email, $index);
$index++;
if($index > 99) {
throw new FireflyException('Email address can no longer be used for registrations.');
}
}
$role = UserRole::where('title', UserRole::OWNER)->first();
if (null === $role) {
throw new FireflyException('The user role is unexpectedly empty. Did you run all migrations?');