Using Directory Emulator in development environment #586
-
I don't have access to Active Directory on my developer PC making it a pain to create applications using AD for authentication. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 16 replies
-
Hi @acharseth, I know your pain! I think you should be able to do this with the Directory Emulator, but there's a bit of setup work involved. I just did this myself and it works -- I'll do my best to help walk you through it. To start, create a
touch database/ldap_test_database.sqlite Then, inside of your // app/Providers/AppServiceProvider
public function boot()
{
DirectoryEmulator::setup(config: [
'database' => database_path('ldap_test_database.sqlite')
]);
} Once that's setup, you'll have to create a database seeder to populate the fake LDAP database with a user so you can login: php artisan make:seeder LdapUserSeeder Inside the seeder, import the LdapRecord <?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use LdapRecord\Models\ActiveDirectory\User;
class LdapUserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
User::create([
'cn' => 'John Doe',
'mail' => 'jdoe@local.com', // This will be the login email address.
]);
}
} Then, run the database seeder: php artisan db:seed LdapUserSeeder Next, update the use LdapRecord\Container;
use LdapRecord\Models\ActiveDirectory\User;
Fortify::authenticateUsing(function ($request) {
if ($user = User::where('mail', $request->email)->first()) {
Container::getDefaultConnection()->actingAs($user);
}
$validated = Auth::validate([
'mail' => $request->email,
'password' => $request->password
]);
return $validated ? Auth::getLastAttempted() : null;
}); Then, try logging into your application as the user you seeded in the previous step! Let me know if you have further questions or run into issues 👍 |
Beta Was this translation helpful? Give feedback.
Hi @acharseth,
I know your pain! I think you should be able to do this with the Directory Emulator, but there's a bit of setup work involved.
I just did this myself and it works -- I'll do my best to help walk you through it.
To start, create a
ldap_test_database.sqlite
database file inside of your applicationsdatabase/
directory:Then, inside of your
AppServiceProvider::boot()
(like you mentioned), setup the directory emulator: