IS_AUTHENTICATED_ & Protecting All URLs
…is because I want
you to know what it is if you see it, and, it leads us towards a few other
interesting things.
Let's play a little bit in security.yaml. Under access_control add a new
entry with path ^/account. Yes…
Symfony Flex & Aliases
…so let's demystify something else, something
that's already been happening behind the scenes. First commit everything, with a
nice message:
Let's install a new feature called the Symfony Security Checker. This is a great
tool.... but... full disclosure: we're mostly installing…
Full Mock Example
…ways... an even better and more common example.
Here's the setup: we're going to need a lot of dinosaurs, a lot of enclosures and
even more security. Instead of creating these by hand each time a new batch of
adorable dinosaurs arrives, let…
FOSUserBundle <3's Guard Authenticators
…The bundle does not provide any authentication. Open app/config/security.yml.
The form_login authentication mechanism we're using is core to Symfony itself,
not this bundle.
So, one of the questions we get a lot is: how can I use Guard authentication with…
Conditional Actions
…let's lock down the actual controller action. How? Now we know two ways:
by overriding the editAction() in UserController and adding a security check
or by adding a PRE_EDIT event listener. Let's use events!
Subscribe to a second event: EasyAdminEvents::PRE_EDIT…
Automatically Login after Registration!
…easy because we're using
Guard authentication. Inside of UserController, instead of redirecting to the
home page: do this: return $this->get() to find a service called
security.authentication.guard_handler. It has a method on it called
authenticateUserAndHandleSuccess(). I'll clear the arguments and…
Impersonation (Login as Someone Else)
…way. We need to be able
switch to that user's account: we need to impersonate them.
Setting up impersonation is super easy. In security.yml, under your firewall,
add a new key called switch_user set to ~ to activate the system:
Now, on…
Denying Access in a Controller
…user has a role,
you'll always use one service: the authorization checker. It looks like this:
if (!$this->get('security.authorization_checker')->isGranted('ROLE_ADMIN'). So,
if we do not have ROLE_ADMIN, then throw $this->createAccessDeniedException():
That message is just for us developers…
Logging out & Pre-filling the Email on Failure
…But, it is not setting the last username on the session...
because it doesn't really know where to look for it.
No worries, fix this with $request->getSession()->set() and pass it the constant -
Security::LAST_USERNAME - and $data['_username']:
Now, try it again…
The Mysterious "User Provider"
…for user AppBundle\Entity\User.
What the heck is a user provider and why do we need one?
A user provider is one of the most misunderstood parts of Symfony's security. It's
an object that does just a few small jobs for you…
Rendering that Login Form
…Great! This template also has a bunch of boilerplate code, so copy that from the
docs too. Paste it here. Update the form action route to security_login:
Well, it ain't fancy, but let's try it out: go to /login. There it is…
The new Voter Class
…unless I'm an
admin... who can view anyone's page. This is a classic situation where security
isn't global, it's dependent on the object being accessed. I can see my user page
but not your user page.
This is the perfect case…
Lock down: Require Authentication Everywhere
…OR, use a cool
trick from SensioFrameworkExtraBundle. Give the controller class a doc-block and
a new annotation: @Security. Auto-complete that to get the use statement.
Then, add "is_granted('ROLE_USER')":
Now we're requiring a valid user on every endpoint.
Re-run…
Registering the Authenticator (Part 2)
…authenticator. Set its class to AppBundle\Security\JwtTokenAuthenticator:
And instead of adding an arguments key: here's your permission to be lazy! Set autowire
to true to make Symfony guess the arguments for us.
Finally, copy the service name and head into security.yml. Under…
Authenticate a Request with JWT
…it alone:
Copy that name and run it:
Instead of the 201, we get a 200 status code after being redirected to /login.
I know we don't have our security system hooked up yet, but pretend that it is
hooked up and working nicely…
Restricting Edit Access to Owners
…your app just by throwing the special AccessDeniedException.
Since we’ll need the same security logic in editAction, updateAction
and deleteAction, let’s create a private function called enforceOwnerSecurity
that holds it:
// src/Yoda/EventBundle/Controller/EventController.php
// ...
use Symfony\Component\Security\Core\Exception\AccessDeniedException…
Remember Me Functionality
…firewall and giving it
a secret, random key:
# app/config/security.yml
security:
Tip
You can also use a secret parameter from parameters.yml as a remember me key
to centralize secret key management for the entire application.
Next, open the login template and add…
Accessing the User
…in a Controller¶
From a controller, it’s just as easy. Go to the controller function for the
homepage and grab an object called the security context. Then call getToken()
and getUser():
public function indexAction()
{
}
Actually, since this is a bit long, the Symfony base…
Whitelisting: Securing all Pages, except a few
…
To fix this, add a new access_control entry above this for any page
starting with /login. For the role, type IS_AUTHENTICATED_ANONYMOUSLY:
# app/config/security.yml
security:
Refresh again. It works! We’re missing our styles, but we’ll fix that next.
The…
After-dinner Mint
…let’s
relax a little and have some fun. In this last part, we’ll check out some
cool things related to forms and security.
Form Field Guessing¶
Remember when we disabled HTML5 validation earlier. Let’s add it back temporarily.
Remove the novalidate attribute…
x
1000+