Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix LDAP sync when creating new users #767

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Repository/AuthorizationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public function getOrgaAuthorizationsFor(User $user, mixed $scope): array
*/
public function loadUserAuthorizations(User $user): array
{
if ($user->getId() === null) {
return [];
}

$keyCache = $user->getUid();

if (!isset($this->cacheAuthorizations[$keyCache])) {
Expand Down
48 changes: 40 additions & 8 deletions tests/MessageHandler/SynchronizeLdapHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace App\Tests\MessageHandler;

use App\Message\SynchronizeLdap;
use App\Tests\Factory\UserFactory;
use App\Message;
use App\Tests\Factory;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Messenger\MessageBusInterface;
use Zenstruck\Foundry\Test\Factories;
Expand All @@ -24,13 +24,13 @@ public function testInvokeCreatesUsers(): void
/** @var MessageBusInterface */
$bus = $container->get(MessageBusInterface::class);

$this->assertSame(0, UserFactory::count());
$this->assertSame(0, Factory\UserFactory::count());

$bus->dispatch(new SynchronizeLdap());
$bus->dispatch(new Message\SynchronizeLdap());

$this->assertSame(2, UserFactory::count());
$this->assertSame(2, Factory\UserFactory::count());

$users = UserFactory::all();
$users = Factory\UserFactory::all();
$this->assertSame('charlie', $users[0]->getLdapIdentifier());
$this->assertSame('[email protected]', $users[0]->getEmail());
$this->assertSame('Charlie Gature', $users[0]->getName());
Expand All @@ -44,17 +44,49 @@ public function testInvokeUpdateUsers(): void
$container = static::getContainer();
/** @var MessageBusInterface */
$bus = $container->get(MessageBusInterface::class);
$user = UserFactory::createOne([
$user = Factory\UserFactory::createOne([
'email' => '[email protected]',
'name' => 'C. Gature',
'ldapIdentifier' => 'charlie',
]);

$bus->dispatch(new SynchronizeLdap());
$bus->dispatch(new Message\SynchronizeLdap());

$user->_refresh();
$this->assertSame('[email protected]', $user->getEmail());
$this->assertSame('Charlie Gature', $user->getName());
$this->assertSame('charlie', $user->getLdapIdentifier());
}

public function testInvokeCanSetDefaultAuthorizations(): void
{
$container = static::getContainer();
/** @var MessageBusInterface */
$bus = $container->get(MessageBusInterface::class);
$defaultRole = Factory\RoleFactory::createOne([
'type' => 'user',
'isDefault' => true,
]);
$defaultOrganization = Factory\OrganizationFactory::createOne([
'domains' => ['example.com'], // don't include example.org
]);

$bus->dispatch(new Message\SynchronizeLdap());

$this->assertSame(2, Factory\UserFactory::count());
$this->assertSame(1, Factory\AuthorizationFactory::count());

$users = Factory\UserFactory::all();
$this->assertSame('[email protected]', $users[0]->getEmail());
Factory\AuthorizationFactory::assert()->exists([
'holder' => $users[0],
'role' => $defaultRole,
'organization' => $defaultOrganization,
]);
$this->assertSame('[email protected]', $users[1]->getEmail());
// example.org is not handled by any organization
Factory\AuthorizationFactory::assert()->notExists([
'holder' => $users[1],
]);
}
}
Loading