PHP code example of hslavich / oneloginsaml-bundle

1. Go to this page and download the library: Download hslavich/oneloginsaml-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hslavich / oneloginsaml-bundle example snippets


> public function createUser(SamlTokenInterface $token): UserInterface
> {
>     $username = $token->getUsername();
>     $attributes = $token->getAttributes();
>     ...
> }
> 
 php
return [
    // ...
    Hslavich\OneloginSamlBundle\HslavichOneloginSamlBundle::class => ['all' => true],
]
 php


namespace App\Entity;

use Hslavich\OneloginSamlBundle\Security\User\SamlUserInterface;

class User implements SamlUserInterface
{
    protected $username;
    protected $email;

    // ...

    public function setSamlAttributes(array $attributes)
    {
        $this->email = $attributes['mail'][0];
    }
}
 php
$email = $this->getUser()->getEmail();
 php


namespace App\Security;

use App\Entity\User;
use Hslavich\OneloginSamlBundle\Security\Authentication\Token\SamlTokenInterface;
use Hslavich\OneloginSamlBundle\Security\User\SamlUserFactoryInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class UserFactory implements SamlUserFactoryInterface
{
    public function createUser($username, array $attributes = []): UserInterface
    {
        $user = new User();
        $user->setRoles(['ROLE_USER']);
        $user->setUsername($username);
        $user->setPassword('notused');
        $user->setEmail($attributes['mail'][0]);
        $user->setName($attributes['cn'][0]);
    
        return $user;
    }
}