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…
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…
Introduction
…s get to work.
Over the next hour, we’re going to take things to the next level, aiming
at some of the most difficult areas of Symfony, like security, forms, and
some serious Doctrine topics. Some of this stuff will look pretty tough at…
Creating a Login Form (Part 2)
…
Copy the template code from the docs and create the login.html.twig file:
{# src/Yoda/UserBundle/Resources/views/Security/login.html.twig #}
{% if error %}
{% endif %}
Automatically Authenticating after Registration
…function called authenticateUser inside RegisterController. Normally,
authentication happens automatically, but we can also trigger it manually:
// src/Yoda/UserBundle/Entity/Controller/RegisterController.php
// ...
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
private function authenticateUser(User $user)
{
}
This code might look strange, and I don’t…
FOSUserBundle
…method for installing bundles.
Generating the User Entity¶
Let’s forget about security for a second and pretend that all we care about
is creating a User entity that is stored in the database. Let’s create
a new bundle called UserBundle to house the…
FOSUserBundle FTW!
…you'll learn how to:
Install & setup FOSUserBundle
Understanding and configuring security
Using your own base layout
Overriding templates
Customizing and extending the forms
Removing the username field entirely
Updating any text via translations
Creating an event subscriber to do things before/after registration (or…
FOSUserBundle FTW! (v1.3)
…bundle, and with good reason.
It comes packed with features for login, registration, forgot password
and a lot more. But it's also a big tool, and learning to master it will
go a long way to making the security system in your application great.
Maker Bundle: Let's Generate Some Code!
…I'll start running symfony console, which is the same thing. Thanks
to the new bundle, we have a ton of commands that start with make! Commands for
generating a security system, making a controller, generating doctrine entities to
talk to the database, forms, listeners…
User API Resource
…user or updating their password. Then we will hash it. That's
something we're going to solve in a future tutorial when we talk more about security.
But this will be good enough for now.
Oh, and above username, also add user:read and…
API Tokens? Session Cookies?
…how the end-user will get that token.
So let's talk about that first use-case: the user of your API is your own JavaScript.
Well, before we even dive into security, make sure your frontend and your API live
on the same domain..…
Admin Dashboard
…you want to, instead of using the IsGranted PHP attribute, you
could also say $this->denyAccessUnlessGranted(). And you could also go to
config/packages/security.yaml and, down at the bottom, add an access_control
that protects the entire /admin section:
Actually, adding this access…
API Login Form with json_login
…email & password,
head to config/packages/security.yaml. Under the firewall, add json_login and
below that check_path... which should be set to the name of the route that we
just created. So, app_login:
This activates a security listener: it's a bit…
Handling Authentication Errors
…AJAX call is working great. Though, there is one gotcha with the json_login
security mechanism: it requires you to send a Content-Type header set to
application/json. We are setting this on our Ajax call and you should
to:
But... if someone forgets…
Testing Authentication
…a faster way to log in. Instead of making the POST
request, say ->actingAs($user):
This is a sneaky way of taking the User object and pushing it directly into
Symfony's security system without making any requests. It's easier, and faster.
And now…
Logout & Passing API Data to JavaScript
…to throw an exception from
inside the method. We've created this entirely because we need a route:
Symfony's security system will intercept things before the controller is called:
To activate that magic, in security.yaml, add a key called logout with path
below…
Bonus: Messenger Monitor Bundle
…to access the UI as it
shows sensitive information. We don't have security configured in this app,
so I'll just remove this line:
src/Entity/ProcessedMessage.php is a new entity added by the recipe. This is
also a stub that extends this…
Decorating the Core State Provider
…Shazam! We're green! So let's go set that value for real. This is easy enough: add a
private Security argument... and make sure you first arg has a comma.
Then this is true if $this->security->getUser() equals $treasure->getOwner().
And... then... the…
Allow Admin Users to Edit any Treasure
… Well, at first, it's relatively
easy because we have total control via the security expression. So we can add
something like if is_granted("ROLE_ADMIN") OR and then put parentheses around the
other use-case:
Let's make sure it works!
A 500…
Conditional Fields by User: ApiProperty
…but then this would override that. Watch: if we try the tests:
They pass because the field is gone.
For our mission, we can leverage a super cool option called security. Set it
to is_granted("ROLE_ADMIN"):
That's it! If this expression return…
x
1000+