PHP code example of wernerdweight / api-auth-bundle

1. Go to this page and download the library: Download wernerdweight/api-auth-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/ */

    

wernerdweight / api-auth-bundle example snippets


    
    // config/bundles.php
    return [
        // ...
        WernerDweight\ApiAuthBundle\ApiAuthBundle::class => ['all' => true],
    ];


namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use WernerDweight\ApiAuthBundle\Entity\AbstractApiClient;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ApiClientRepository")
 */
class ApiClient extends AbstractApiClient
{
    /* put your custom fields and methods here */
}


namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\PersistentCollection;
use Symfony\Component\Security\Core\User\UserInterface;
use WernerDweight\ApiAuthBundle\Entity\AbstractApiUser;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
final class User extends AbstractApiUser implements UserInterface
{
    /* put your custom fields here */

    /**
     * @var ArrayCollection|PersistentCollection
     *
     * @ORM\OneToMany(targetEntity="App\Entity\UserToken", mappedBy="apiUser")
     */
    protected $apiTokens;
    
    /* put your custom methods here */
    /* FYI: AbstractApiUser already has getter, adder and remover for `$apiTokens` */
}


namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use WernerDweight\ApiAuthBundle\Entity\AbstractApiUserToken;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserTokenRepository")
 */
class UserToken extends AbstractApiUserToken
{
    /* put your custom fields here */

    /**
     * @var User
     *
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="apiTokens")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
     * })
     */
    protected $apiUser;

    /* put your custom methods here */
    /* FYI: AbstractApiUserToken already has getter adn setter for `$apiUser` */
}


namespace App\Repository;

use App\Entity\User;
use Symfony\Bridge\Doctrine\RegistryInterface;
use WernerDweight\ApiAuthBundle\Repository\AbstractApiUserRepository;

class UserRepository extends AbstractApiUserRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, User::class);
    }
}


namespace App\Service;

use WernerDweight\ApiAuthBundle\DTO\AccessScope;
use WernerDweight\ApiAuthBundle\Service\AccessScopeChecker\Checker\AccessScopeCheckerInterface;

final class MyAccessScopeChecker implements AccessScopeCheckerInterface
{
    public function check(AccessScope $scope): string
    {
        if (/* ... */) {
            return ApiAuthEnum::SCOPE_ACCESSIBILITY_ACCESSIBLE;
        }
        
        if (/* ... */) {
            return ApiAuthEnum::SCOPE_ACCESSIBILITY_ON_BEHALF;
        }
        
        return ApiAuthEnum::SCOPE_ACCESSIBILITY_FORBIDDEN;
    }
}



final class User extends AbstractApiUser implements UserInterface
{
    /* ... */
    
    public function jsonSerialize(): array
    {
        return array_merge(
            [
                'id' => $this->getId(),
                'email' => $this->getEmail(),
                // any other attributes you need to