public function testRetrieveById() { $credentialsFields = ['username' => 'user', 'password' => 'pass']; $this->baseLdapUserProvider->setCredentialsFields($credentialsFields); $ldapServer = Mockery::mock('LaravelAuthLdap\\Contracts\\LdapServer'); $this->baseLdapUserProvider->setLdapServer($ldapServer); $ldapUser = Mockery::mock('LaravelAuthLdap\\Contracts\\LdapUser'); $ldapServer->shouldReceive('retrieveByUsername')->andReturn($ldapUser); $id = 'foo'; // if not using provider, should retrieve from LDAP server $this->assertEquals($ldapUser, $this->baseLdapUserProvider->retrieveById($id)); // now using provider $provider = Mockery::mock('Illuminate\\Contracts\\Auth\\UserProvider'); $this->baseLdapUserProvider->setProvider($provider); $provider->shouldReceive('retrieveById')->times(2)->andReturn(null); // if not found in provider and doesn't matter if it exists, should default back to LDAP $this->baseLdapUserProvider->setUserMustExistInProvider(false); $this->assertEquals($ldapUser, $this->baseLdapUserProvider->retrieveById($id)); // if not found in provider and must exist $this->baseLdapUserProvider->setUserMustExistInProvider(true); $this->assertNull($this->baseLdapUserProvider->retrieveById($id)); $providerUser = Mockery::mock('Illuminate\\Contracts\\Auth\\Authenticatable'); $provider->shouldReceive('retrieveById')->times(2)->andReturn($providerUser); // if found in provider, should always return from the provider $this->assertEquals($providerUser, $this->baseLdapUserProvider->retrieveById($id)); $this->baseLdapUserProvider->setUserMustExistInProvider(false); $this->assertEquals($providerUser, $this->baseLdapUserProvider->retrieveById($id)); }
/** * Bind the interfaces to their implementations in the service container. */ public function registerLdapInterfaces() { $this->app->bind('LaravelAuthLdap\\Contracts\\LdapServer', function ($app) { $server = new AdLDAPLdapServer(); $server->setAdServer(new adLDAP($app->config['adldap'])); return $server; }); $this->app->bind('LaravelAuthLdap\\Contracts\\LdapUser', function ($app) { $user = new BaseLdapUser(); $user->setConvertFields($app->config['auth-ldap.convert_fields']); $user->setUsernameField($app->config['auth-ldap.username_field']); return $user; }); $this->app->bind('LaravelAuthLdap\\Contracts\\LdapUserProvider', function ($app) { $provider = new BaseLdapUserProvider(); $driverName = array_get($app->config['auth-ldap'], 'provider.driver'); if ($driverName !== null) { $driver = $this->app['auth']->driver($driverName); $provider->setProvider($driver->getProvider()); } $provider->setLdapServer($app->make('LaravelAuthLdap\\Contracts\\LdapServer')); $mustExist = array_get($app->config['auth-ldap'], 'provider.must_exist'); $provider->setUserMustExistInProvider($mustExist === null ? false : $mustExist); $provider->setCredentialsFields($app->config['auth-ldap.credentials_fields']); return $provider; }); }