2017 © Pedro Peláez
 

library security

Voters and Firewall features from Symfony\Security integration to Nette.

image

symnedi/security

Voters and Firewall features from Symfony\Security integration to Nette.

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 12 Versions
  • 1 % Grown

The README.md

Symnedi/Security

Build Status Quality Score Code Coverage Downloads Latest stable, (*1)

Install

composer require symnedi/security

Register the extension:, (*2)

# app/config/config.neon
extensions:
    - Symnedi\Security\DI\SecurityExtension
    - Symnedi\EventDispatcher\DI\EventDispatcherExtension

Usage

Voters

First, read Symfony cookbook, (*3)

Then create new voter implementing Symfony\Component\Security\Core\Authorization\Voter\VoterInterface and register it as service in config.neon:, (*4)

services:
    - App\SomeModule\Security\Voter\MyVoter

Then in place, where we need to validate access, we'll just use AuthorizationChecker:, (*5)

use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;


class Presenter
{

    /**
     * @var AuthorizationCheckerInterface
     */
    private $authorizationChecker;


    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
    {
        $this->authorizationChecker = $authorizationChecker;
    }


    /**
     * @param PresenterComponentReflection $element
     */
    public function checkRequirements($element)
    {
        if ($this->authorizationChecker->isGranted('access', $element) === FALSE) {
            throw new ForbiddenRequestException;
        }
    }

}

Firewalls

Original Symfony firewalls pretty simplified and with modular support by default., (*6)

All we need to create is a matcher and a listener., (*7)

Request Matcher

This service will match all sites in admin module - urls starting with /admin:, (*8)

use Symfony\Component\HttpFoundation\Request;
use Symnedi\Security\Contract\HttpFoundation\RequestMatcherInterface;


class AdminRequestMatcher implements RequestMatcherInterface
{

    /**
     * {@inheritdoc}
     */
    public function getFirewallName()
    {
        return 'adminSecurity';
    }


    /**
     * {@inheritdoc}
     */
    public function matches(Request $request)
    {
        $url = $request->getPathInfo();
        return strpos($url, '/admin') === 0;
    }

}

Firewall Listener

It will ensure that user is logged in and has 'admin' role, otherwise redirect., (*9)

use Nette\Application\AbortException;
use Nette\Application\Application;
use Nette\Application\Request;
use Nette\Security\User;
use Symnedi\Security\Contract\Http\FirewallListenerInterface;


class LoggedAdminFirewallListener implements FirewallListenerInterface
{

    /**
     * @var User
     */
    private $user;


    public function __construct(User $user)
    {
        $this->user = $user;
    }


    /**
     * {@inheritdoc}
     */
    public function getFirewallName()
    {
        return 'adminSecurity';
    }


    /**
     * {@inheritdoc}
     */
    public function handle(Application $application, Request $applicationRequest)
    {
        if ( ! $this->user->isLoggedIn()) {
            throw new AbortException;
        }

        if ( ! $this->user->isInRole('admin')) {
            throw new AbortException;
        }
    }

}

Then we register both services., (*10)

services:
    - AdminRequestMatcher
    - LoggedAdminFirewallListener

That's it!, (*11)

Testing

composer check-cs # see "scripts" section of composer.json for more details 
vendor/bin/phpunit

Contributing

Rules are simple:, (*12)

  • new feature needs tests
  • all tests must pass
  • 1 feature per PR

We would be happy to merge your feature then!, (*13)

The Versions

19/05 2015

v0.0.1

0.0.1.0

Symfony\Security integration to Nette.

  Sources   Download

MIT

The Requires

 

The Development Requires

security symfony nette