Skip to content

Commit 67f8b7b

Browse files
committed
1 parent 28192ca commit 67f8b7b

File tree

11 files changed

+398
-28
lines changed

11 files changed

+398
-28
lines changed

.env.example

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,16 @@ MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
5353
# Enables or disables VAT number validation through the VIES service.
5454
# Set to 'true' to enable validation, or 'false' to disable it.
5555
VAT_VALIDATION_ENABLED=false
56+
57+
# Use 'web' as the primary authentication driver for Laravel. Aslo use ldap for the secondary driver.
58+
# This means that the application will use the LDAP server to handle user authentication.
59+
AUTH_DRIVER=web
60+
LDAP_HOST=ldap.example.com
61+
LDAP_USERNAME="cn=admin,dc=example,dc=com"
62+
LDAP_PASSWORD=supersecretpassword
63+
LDAP_PORT=636
64+
LDAP_BASE_DN="dc=example,dc=com"
65+
LDAP_TIMEOUT=10
66+
LDAP_SSL=true
67+
LDAP_TLS=false
68+
LDAP_SASL=false
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use LdapRecord\Models\ActiveDirectory\User as LdapUser;
7+
use App\Models\User;
8+
9+
class ImportLdapUsers extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'ldap:import-users';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Import LDAP users into Laravel database';
24+
25+
public function __construct()
26+
{
27+
parent::__construct();
28+
}
29+
30+
/**
31+
* Execute the console command.
32+
*/
33+
public function handle()
34+
{
35+
// Retrieve all LDAP users
36+
$ldapUsers = LdapUser::get();
37+
38+
foreach ($ldapUsers as $ldapUser) {
39+
// Check if the user already exists in the database
40+
$user = User::where('email', $ldapUser->getEmail())->first();
41+
42+
if (!$user) {
43+
// Create the user in Laravel if it does not exist
44+
User::create([
45+
'name' => $ldapUser->getFirstName(),
46+
'email' => $ldapUser->getEmail(),
47+
'password' => bcrypt('password'), // Generate a temporary password
48+
]);
49+
50+
$this->info("User {$ldapUser->getFirstName()} imported successfully.");
51+
}
52+
}
53+
54+
$this->info('Import completed.');
55+
}
56+
}

app/Http/Controllers/Auth/LoginController.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace App\Http\Controllers\Auth;
44

5+
use App\Models\User;
56
use Illuminate\Http\Request;
67
use App\Http\Controllers\Controller;
8+
use Illuminate\Support\Facades\Auth;
79
use App\Providers\RouteServiceProvider;
810
use Illuminate\Foundation\Auth\AuthenticatesUsers;
911

@@ -29,6 +31,8 @@ class LoginController extends Controller
2931
*/
3032
protected $redirectTo = RouteServiceProvider::HOME;
3133

34+
35+
3236
/**
3337
* Create a new controller instance.
3438
*
@@ -50,4 +54,50 @@ protected function authenticated(Request $request, $user)
5054
$this->redirectTo = RouteServiceProvider::HOME;
5155
}
5256
}
57+
58+
public function login(Request $request)
59+
{
60+
$this->validateLogin($request);
61+
62+
// Vérifie si le driver est LDAP
63+
if (env('AUTH_DRIVER') === 'ldap') {
64+
// Authentification via LDAP
65+
$username = $request->input('username');
66+
$password = $request->input('password');
67+
68+
// Tentative de connexion via LDAP
69+
if (Auth::guard('ldap')->attempt(['username' => $username, 'password' => $password])) {
70+
return $this->sendLoginResponse($request);
71+
}
72+
73+
// Si l'authentification échoue
74+
return $this->sendFailedLoginResponse($request);
75+
} else {
76+
// Authentification via email
77+
$credentials = $request->only('email', 'password');
78+
79+
if (Auth::attempt($credentials, $request->filled('remember'))) {
80+
return $this->sendLoginResponse($request);
81+
}
82+
83+
// Si l'authentification échoue
84+
return $this->sendFailedLoginResponse($request);
85+
}
86+
}
87+
88+
protected function validateLogin(Request $request)
89+
{
90+
// Valider les champs selon le driver utilisé
91+
if (env('AUTH_DRIVER') === 'ldap') {
92+
$request->validate([
93+
'username' => 'required|string',
94+
'password' => 'required|string',
95+
]);
96+
} else {
97+
$request->validate([
98+
'email' => 'required|string|email',
99+
'password' => 'required|string',
100+
]);
101+
}
102+
}
53103
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"php": "^8.1.0",
1919
"ext-zip": "*",
2020
"barryvdh/laravel-dompdf": "2.0.1",
21+
"directorytree/ldaprecord-laravel": "^3.3",
2122
"doctrine/dbal": "^3.8",
2223
"dragonbe/vies": "^2.3",
2324
"fruitcake/php-cors": "^1.2",

composer.lock

Lines changed: 144 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/auth.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
'defaults' => [
17-
'guard' => 'web',
17+
'guard' => env('AUTH_DRIVER', 'web'),
1818
'passwords' => 'users',
1919
],
2020

@@ -40,7 +40,10 @@
4040
'driver' => 'session',
4141
'provider' => 'users',
4242
],
43-
43+
'ldap' => [
44+
'driver' => 'session',
45+
'provider' => 'ldap',
46+
],
4447
'api' => [
4548
'driver' => 'token',
4649
'provider' => 'users',
@@ -70,7 +73,10 @@
7073
'driver' => 'eloquent',
7174
'model' => App\Models\User::class,
7275
],
73-
76+
'ldap' => [
77+
'driver' => 'ldap',
78+
'model' => LdapRecord\Models\ActiveDirectory\User::class,
79+
],
7480
// 'users' => [
7581
// 'driver' => 'database',
7682
// 'table' => 'users',

0 commit comments

Comments
 (0)