This tutorial has a new version, check it out!

Authorization with Access Control

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

Authorization with Access Control

Before we keep going with authentication and make it possible to login, let’s try out our first piece of authorization and start denying access!

Head back to security.yml. The easiest way to deny access is via the access_control section. Let’s use its regular expression coolness to protect any URLs that start with “/new” or “/create”.

Roles are given to a user when they login and if you’re not logged in, you don’t have any. Here, we’re saying that you at least need ROLE_USER to access these URLs:

# app/config/security.yml
security:
    # ...
    access_control:
        - { path: ^/new, roles: ROLE_USER }
        - { path: ^/create, roles: ROLE_USER }

Try it out! When we try to add an event, we’re redirected to /my-login-url. Hey! I know that URL! That’s what we put for the login_path config key.

So here’s the magic that just happened behind the scenes:

  1. We tried to go to /new. Since our anonymous user doesn’t have any roles, the access controls kicked us out;
  2. The firewall saves the day. Instead of giving us an access denied screen, it decides to give us a chance to login. The form_login key in tells the firewall that we want to use a good old fashioned login form, and that the login form should live at /my-login-url.

It’s our job to actually create the login page. And since we haven’t yet, we see the big ugly 404 error.

More access_control options

The access_control has a few more tricks to it. Head over to the Security chapter of the book and find the section on access_control. I want you to read this, but the most important thing to know is that only one access_control entry is matched on a request. Symfony goes down the list, finds the first match, and uses only it to check authorization. I’ll show you an example during the last chapter.

There’s also other goodies, like different access controls based on the user’s IP address or depending on which hostname is being accessed. You can even make it so that a user is redirected to https.

Leave a comment!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "~2.4", // v2.4.2
        "doctrine/orm": "~2.2,>=2.2.3", // v2.4.2
        "doctrine/doctrine-bundle": "~1.2", // v1.2.0
        "twig/extensions": "~1.0", // v1.0.1
        "symfony/assetic-bundle": "~2.3", // v2.3.0
        "symfony/swiftmailer-bundle": "~2.3", // v2.3.5
        "symfony/monolog-bundle": "~2.4", // v2.5.0
        "sensio/distribution-bundle": "~2.3", // v2.3.4
        "sensio/framework-extra-bundle": "~3.0", // v3.0.0
        "sensio/generator-bundle": "~2.3", // v2.3.4
        "incenteev/composer-parameter-handler": "~2.0", // v2.1.0
        "doctrine/doctrine-fixtures-bundle": "~2.2.0", // v2.2.0
        "ircmaxell/password-compat": "~1.0.3", // 1.0.3
        "phpunit/phpunit": "~4.1" // 4.1.0
    }
}