|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bagisto\AzureAuth\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use Bagisto\AzureAuth\Helpers\AzureConfigHelper; |
| 7 | + |
| 8 | +use Illuminate\Support\Str; |
| 9 | +use Illuminate\Support\Facades\Log; |
| 10 | +use Laravel\Socialite\Facades\Socialite; |
| 11 | + |
| 12 | +use Webkul\User\Repositories\AdminRepository; |
| 13 | + |
| 14 | +class SessionController extends Controller |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Create a new controller instance. |
| 18 | + * |
| 19 | + * @return void |
| 20 | + */ |
| 21 | + public function __construct( |
| 22 | + protected AdminRepository $adminRepository |
| 23 | + ) { |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Redirect the user to the Azure authentication page. |
| 28 | + * |
| 29 | + * @return \Illuminate\Http\RedirectResponse |
| 30 | + */ |
| 31 | + public function redirectToAzure() |
| 32 | + { |
| 33 | + if (!AzureConfigHelper::isConfigured()) { |
| 34 | + return view('azure-auth::errors.config'); |
| 35 | + } |
| 36 | + |
| 37 | + return Socialite::driver('azure')->redirect(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Handle the callback from Azure. |
| 42 | + * |
| 43 | + * @return \Illuminate\Http\RedirectResponse |
| 44 | + */ |
| 45 | + public function handleCallback() |
| 46 | + { |
| 47 | + try { |
| 48 | + $user = Socialite::driver('azure')->user(); |
| 49 | + |
| 50 | + $localUser = $this->adminRepository->where('email', $user->getEmail())->first(); |
| 51 | + |
| 52 | + if (!$localUser) { |
| 53 | + $randomPass = Str::random(80); |
| 54 | + |
| 55 | + $userData = [ |
| 56 | + 'name' => $user->getName(), |
| 57 | + 'email' => $user->getEmail(), |
| 58 | + 'password' => bcrypt($randomPass), |
| 59 | + 'role_id' => 1, |
| 60 | + 'status' => true |
| 61 | + ]; |
| 62 | + |
| 63 | + $adminUser = $this->adminRepository->create($userData); |
| 64 | + |
| 65 | + if ($adminUser) { |
| 66 | + Log::info('Local user created for ', ['user_email' => $user->getEmail()]); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + auth()->guard('admin')->login($userData); |
| 71 | + |
| 72 | + if (! auth()->guard('admin')->user()->status) { |
| 73 | + session()->flash('warning', trans('admin::app.settings.users.activate-warning')); |
| 74 | + |
| 75 | + auth()->guard('admin')->logout(); |
| 76 | + |
| 77 | + return redirect()->route('admin.session.create'); |
| 78 | + } |
| 79 | + |
| 80 | + Log::info('Azure Authentication Successful', ['user_email' => $user->getEmail()]); |
| 81 | + |
| 82 | + return redirect()->route('admin.dashboard.index'); |
| 83 | + } catch (\Exception $e) { |
| 84 | + Log::error('Azure Authentication Error: ' . $e->getMessage()); |
| 85 | + |
| 86 | + return redirect()->route('admin.session.create')->with('warning', 'Unable to authenticate with your Microsoft account. Please try again.'); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Show the error view for missing or invalid Azure configuration. |
| 92 | + * |
| 93 | + * @return \Illuminate\View\View |
| 94 | + */ |
| 95 | + public function showConfigError() |
| 96 | + { |
| 97 | + return view('azure-auth::errors.config'); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments