File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 88use App \Models \User ;
99use Exception ;
1010use Illuminate \Support \Facades \Log ;
11+ use LdapRecord \LdapRecordException ;
1112
1213class LdapUtils
1314{
@@ -47,8 +48,14 @@ public static function syncUser(User $user): void
4748 continue ;
4849 }
4950
50- $ matches_ldap_filter = $ user ->ldapguid !== null && $ ldap_provider ::rawFilter ($ project ->ldapfilter )->findByGuid ($ user ->ldapguid ) !== null ;
51- $ relationship_already_exists = $ project ->users ->contains ($ user );
51+ try {
52+ $ matches_ldap_filter = $ user ->ldapguid !== null && $ ldap_provider ::rawFilter ($ project ->ldapfilter )->findByGuid ($ user ->ldapguid ) !== null ;
53+ $ relationship_already_exists = $ project ->users ->contains ($ user );
54+ } catch (LdapRecordException ) {
55+ // Prevent invalid filters from breaking other projects.
56+ Log::warning ("Invalid LDAP filter ' $ project ->ldapfilter ' for project $ project ->name . " );
57+ continue ;
58+ }
5259
5360 if ($ matches_ldap_filter && !$ relationship_already_exists ) {
5461 $ project ->users ()->attach ($ user ->id , ['role ' => Project::PROJECT_USER ]);
Original file line number Diff line number Diff line change 33namespace Tests \Feature ;
44
55use App \Models \Project ;
6+ use Illuminate \Support \Facades \Config ;
67use Illuminate \Support \Facades \Log ;
78use Illuminate \Support \Str ;
89use LdapRecord \LdapRecordException ;
910use LdapRecord \Models \ModelDoesNotExistException ;
1011use LdapRecord \Models \OpenLDAP \Group ;
1112use LdapRecord \Models \OpenLDAP \User ;
13+ use Mockery ;
1214use Mockery \Exception \InvalidCountException ;
1315use Tests \TestCase ;
1416use Tests \Traits \CreatesProjects ;
@@ -416,4 +418,21 @@ public function testSyncsGroupsUponLogin(): void
416418 $ this ->assertCanAccessProject ('group_1_only_1 ' , 'only_group_1 ' );
417419 $ this ->assertCanAccessProject ('group_1_only_1 ' , 'only_group_2 ' );
418420 }
421+
422+ public function testSyncCommandHandlesInvalidProjectFilters (): void
423+ {
424+ // We are testing that nothing fails...
425+ self ::expectNotToPerformAssertions ();
426+
427+ Log::shouldReceive ('warning ' )
428+ ->with ("Invalid LDAP filter 'brokenldapfilter))' for project " . $ this ->projects ['only_group_1 ' ]->name . '. ' );
429+
430+ // Allow other debug messages to be logged without causing a test failure.
431+ Log::shouldReceive ('debug ' )->with (Mockery::any ());
432+
433+ $ this ->projects ['only_group_1 ' ]->ldapfilter = 'brokenldapfilter)) ' ;
434+ $ this ->projects ['only_group_1 ' ]->save ();
435+
436+ $ this ->artisan ('ldap:sync_projects ' );
437+ }
419438}
You can’t perform that action at this time.
0 commit comments