Sluggable & other Wonderful Behaviors
…main repository is closely controlled for quality. The second - the "contrib"
repository - has some basic checks, but the community can freely contribute recipes.
For security reasons, when you download a package that installs a recipe from that
repository, it will ask you first before installing…
Multiple Pages / Entries
…and... refresh! Bah:
require is not defined
Boo! My bad - I forgot to use the new built file. Open
templates/bundles/FOSUserBundle/Security/login.html.twig. Point the script tag
to build/login.js:
And now... it works! When I type a really long username…
Test Fixtures & Fast Databases!
…the tutorial/ directory, you should have
a DataFixtures directory. Copy that into your AppBundle.
These two classes build 3 Enclosures and also add some security to them. But,
part of this code is using a setEnclosure() method on Dinosaur... and that doesn't exist!
Open…
Clearing the Database
…top of every test method. But another great option
is to override setUp() and add it there. Let's empty all three entities:
Enclosure, Security and Dinosaur.
For this method to work, we need a getEntityManager() method. At the bottom,
add private function getEntityManager(). Then…
Testing Exceptions
…puts the short
name... so go copy the use statement and put it down here.
Try it!
Yes! One more test passing.
I want to go through one more example next... and also add some security to the
enclosures. Our guests have been terrorized enough.
Integration Tests
…talks to the database!
First, we need to finish our entities. Find Security and copy the id field.
Open Dinosaur and paste this in. Do the same for Enclosure. We haven't needed
these yet because we haven't touched the database at all.
Now…
PHPUnit: Secure the Park
…nothing! Just, "Welcome to Dinosaur Park". Instead of creating
a park full of dinosaurs and then worrying about security... ahem... we don't
have any code yet. We're going to build this dino factory and write tests all
at the same time.
The de…
Rock some FOSUserBundle!
…PHP web server. Ok, load this up in your browser: http://localhost:8000.
Welcome to AquaNote! This is the same project we've been building in our main Symfony
tutorials, but without any security logic. Gasp! See that Login link? It's a lie!
It…
Event Hooks
…this is not the right use statement. I'll re-add TokenStorageInterface:
make sure you choose the one from Security\Core\Authentication:
In our method, fetch the user with $user = $this->tokenStorage->getToken()->getUser().
And if the User is not an instanceof our User…
Adding a Custom Action
…match what we put in the config:
Notice the URL for this is just /genus/feed. It does not start with /easyadmin.
And so, it's not protected by our access_control security.
That should be enough to get started. Refresh! There's our link…
Mastering Route config Loading
…the next parts, we're going
to start adding big features: like talking to a database, forms, security and more.
And when we do, I've got some exciting news: because of your work here, it's all going
to make sense. Let's keep…
Caching in the prod Environment Only
…along with
security.yml and services.yml:
The key point is that all of the files are just loading each other: it's all the
same system.
In fact, I could copy all of security.yml, paste it into config.yml, completely
delete security.yml…
Using a Service
…best features of Twig is that it automatically
escapes any HTML that you render. That gives you free security from XSS attacks.
And for those few times when you do want to print HTML, just add the |raw filter:
Refresh again: it's rending in…
Role Hierarchy
…s lame.
Of course, you can solve this with that group system we talked about earlier, but
that's usually overkill. And, there's a simpler way.
In security.yml, let's take advantage of something called role hierarchies. It's
simple, it's awesome!…
Doctrine Listener: Encode the User's Password
…do nothing:
Now, on to encoding that password.
Symfony comes with a built-in service that's really good at encoding passwords. It's
called security.password_encoder and if you looked it up on debug:container, its
class is UserPasswordEncoder. We'll need that…
Authenticator: getUser, checkCredentials & Success/Failure
…RouterInterface:
Use the Option+Enter shortcut again to set up that property:
Down in getLoginUrl(), return $this->router->generate('security_login'):
So what happens when authentication is successful? It's awesome: the user is automatically
redirected back to the last page they tried to visit…
Autowiring Madness
…thing I was talking about earlier. This will
lead you to wonderful applications.
There's really nothing that we can't do now in Symfony. In the next courses, we'll
use all this to master new tools like forms and security. Seeya next time!
Twig Layouts (Template Inheritance)
…that shows you exactly how long each part
of your application took to render. This is amazing for debugging and profiling.
There's also details in here on Twig, security, routes and other cool stuff. We'll
keep exploring this as we go along.
Ok…
Service Autowiring
…a $robot property and set it:
In checkCredentials(), if (!$this->robot->doesRobotAllowAccess()) then throw a
really clear new CustomUserMessageAuthenticationException() that says "RANDOM
SECURITY ROBOT SAYS NO!":
And I'll even put quotes around that.
This is when we would normally go to services.yml and…
New Profiler
…and open up DefaultController. Go down to
sillyLoginAction(). OK, pretend that something went wrong and we can't figure it out.
If this security.authentication_utils is the problem, we might want to use dump()
to print it out. Below that, throw a new Exception…
x
1000+