Installation
composer require banovo/sso-sysuser-bundle
Add to config/app.php under providers:
Banovo\SsoSysUserBundle\SsoSysUserBundle::class,
Publish the config (if needed):
php artisan vendor:publish --provider="Banovo\SsoSysUserBundle\SsoSysUserBundle" --tag="config"
First Use Case: User Authentication via SSO
config/sso_sysuser.php.SysUser model (if needed) by creating a custom model:
use Banovo\SsoSysUserBundle\Models\SysUser;
class CustomSysUser extends SysUser { ... }
AppServiceProvider:
use Banovo\SsoSysUserBundle\Models\SysUser;
public function boot()
{
SysUser::setModel(CustomSysUser::class);
}
Trigger SSO Login
Use the SsoSysUser facade to redirect users to the SSO provider:
use Banovo\SsoSysUserBundle\Facades\SsoSysUser;
Route::get('/login', function () {
return SsoSysUser::login();
});
Login Redirect
Use SsoSysUser::login() to redirect users to the SSO provider.
Route::get('/sso-login', function () {
return SsoSysUser::login(['return_to' => route('dashboard')]);
});
Callback Handling
Configure the SSO callback route (e.g., /sso/callback) to handle the response:
Route::get('/sso/callback', [SsoSysUserController::class, 'callback']);
The controller will automatically:
User Synchronization
Override SsoSysUser::syncUser() to customize user data mapping:
SsoSysUser::syncUser($ssoData, function ($user, $ssoData) {
$user->name = $ssoData['name'];
$user->email = $ssoData['email'];
$user->sso_id = $ssoData['sub']; // Unique SSO identifier
$user->save();
});
Role/Group Assignment Use middleware or events to assign roles after SSO login:
SsoSysUser::afterLogin(function ($user) {
$user->assignRole('sso_user');
});
Laravel Auth Integration
The bundle works seamlessly with Laravel’s built-in auth. Use Auth::user() to access the SysUser model after SSO login.
Custom User Models
Extend SysUser to add custom fields or relationships:
class CustomSysUser extends SysUser {
public function departments() {
return $this->belongsToMany(Department::class);
}
}
Multi-SSO Support
Configure multiple SSO providers in config/sso_sysuser.php and route users dynamically:
SsoSysUser::login(['provider' => 'google']);
Testing Mock SSO responses in tests:
$this->mock(SsoSysUser::class)->shouldReceive('validateResponse')->andReturn($ssoData);
Missing Config
config/sso_sysuser.php is properly set up with your SSO provider credentials (e.g., SAML metadata, OAuth client ID/secret).php artisan vendor:publish --tag="config" and verify the config.Callback Route Mismatch
/sso/callback).User Sync Conflicts
sso_id is not unique, users may be duplicated or overwritten.sso_id is set in the syncUser callback and marked as unique in the database.Session Issues
SESSION_DRIVER=file in .env).redis or database).Deprecated Methods
Enable Logging
Add this to config/sso_sysuser.php:
'debug' => env('SSO_DEBUG', false),
Logs will appear in storage/logs/laravel.log.
Validate SSO Response Dump the raw SSO response in the callback controller:
public function callback() {
$response = SsoSysUser::getResponse();
\Log::info('SSO Response:', $response);
// ...
}
Test with Postman
Simulate SSO callbacks using Postman to verify the /sso/callback endpoint before integrating with the provider.
Custom Providers
Extend the SsoProvider class to support unsupported SSO types (e.g., CAS):
class CustomSsoProvider extends \Banovo\SsoSysUserBundle\Providers\SsoProvider {
public function validateResponse($response) { ... }
}
Event Listeners Listen for SSO events to trigger custom logic:
SsoSysUser::listen('user.synced', function ($user) {
// Send welcome email
});
Middleware Protect routes with SSO-only access:
Route::middleware(['sso'])->group(function () {
// SSO-protected routes
});
Register the middleware in app/Http/Kernel.php:
protected $routeMiddleware = [
'sso' => \Banovo\SsoSysUserBundle\Middleware\SsoMiddleware::class,
];
Database Schema
Customize the sys_users table by publishing and modifying the migration:
php artisan vendor:publish --tag="migrations"
How can I help you explore Laravel packages today?