WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.166 --> 00:00:04.406 align:middle
Let's see what this all looks
like in the database!

00:00:05.836 --> 00:00:09.266 align:middle
To query the user table, we
can actually use the console:

00:00:09.266 --> 00:00:17.936 align:middle
php bin/console doctrine:query:sql
'SELECT * FROM user' Nice!

00:00:18.346 --> 00:00:24.406 align:middle
We inherited a bunch of columns from the base
User class, like username, email and enabled.

00:00:24.866 --> 00:00:26.346 align:middle
It even tracks our last login.

00:00:26.806 --> 00:00:34.496 align:middle
Thanks! But there are two weird fields:
username_canonical and email_canonical.

00:00:35.386 --> 00:00:36.316 align:middle
What the heck?

00:00:37.286 --> 00:00:40.766 align:middle
These are one of the more
controversial things about FOSUserBundle.

00:00:42.036 --> 00:00:48.016 align:middle
Before we explore them, first, just know
that when you set username or email,

00:00:48.466 --> 00:00:52.546 align:middle
the corresponding canonical field
is automatically set for you.

00:00:52.656 --> 00:00:58.836 align:middle
So, these canonical fields are not something
you normally need to worry or think about.

00:00:59.636 --> 00:01:00.846 align:middle
So why do they exist?

00:01:01.516 --> 00:01:06.226 align:middle
Suppose that when you registered, you used
some capital letters in your username.

00:01:07.046 --> 00:01:12.186 align:middle
The username column will be exactly as
you typed it: with the capital letters.

00:01:13.076 --> 00:01:16.456 align:middle
But username_canonical will be lowercased.

00:01:17.406 --> 00:01:22.366 align:middle
Then, when you login, FOSUserBundle
lowercases the submitted username

00:01:22.526 --> 00:01:25.926 align:middle
and queries via the username_canonical column.

00:01:26.926 --> 00:01:31.856 align:middle
Why? Because some databases - like
Postgresql - are case sensitive.

00:01:32.506 --> 00:01:37.716 align:middle
The canonical fields allow a user to
login with a case insensitive username -

00:01:38.096 --> 00:01:42.086 align:middle
aquanaut in all lowercase,
uppercase or any combination.

00:01:43.406 --> 00:01:44.976 align:middle
But mostly...

00:01:44.976 --> 00:01:48.246 align:middle
this is just a detail you shouldn't think about.

00:01:48.716 --> 00:01:53.766 align:middle
It's all handled for you and other than being
ugly in the database, it doesn't hurt anything.

00:01:55.046 --> 00:01:59.686 align:middle
And by the way, right now you can
only login with your username.

00:02:00.386 --> 00:02:04.096 align:middle
If you want to be able to login
with username or email, no problem!

00:02:04.756 --> 00:02:06.836 align:middle
The documentation has a section about this.

00:02:07.406 --> 00:02:14.656 align:middle
Just change your user provider to
fos_user.user_provider.username_email.

00:02:16.006 --> 00:02:17.536 align:middle
What does this do?

00:02:18.306 --> 00:02:22.336 align:middle
When you submit your login form,
the provider section is responsible

00:02:22.336 --> 00:02:26.106 align:middle
for taking what you entered and
finding the correct User record.

00:02:26.306 --> 00:02:30.896 align:middle
Our current user provider finds the
User by the username_canonical field.

00:02:31.686 --> 00:02:35.046 align:middle
This other one looks up a
User by username or email.

00:02:35.766 --> 00:02:40.176 align:middle
And you're 100% free to create
your own user provider,

00:02:40.536 --> 00:02:43.396 align:middle
if you need to login with
some other, weird logic.

00:02:43.606 --> 00:02:46.616 align:middle
FOSUserBundle won't notice or care.

00:02:47.716 --> 00:02:50.546 align:middle
Check out the database result
again and look at roles.

00:02:51.396 --> 00:02:54.656 align:middle
I know, it's strange: this is an array field.

00:02:54.656 --> 00:03:01.526 align:middle
I'll hold command and click to open
the base User class from FOSUserBundle.

00:03:02.916 --> 00:03:05.276 align:middle
See, roles holds an array.

00:03:05.946 --> 00:03:10.026 align:middle
When you save, it automatically
serializes to a string in the database.

00:03:10.976 --> 00:03:14.116 align:middle
This is done with the Doctrine array field type.

00:03:15.576 --> 00:03:23.316 align:middle
Notice that even though it's empty in the
database, when we login, our user has ROLE_USER.

00:03:23.386 --> 00:03:27.556 align:middle
This is thanks to the base
User class from FOSUserBundle:

00:03:28.006 --> 00:03:34.346 align:middle
it makes sure the User has whatever roles
are stored in the database plus ROLE_USER.

00:03:34.346 --> 00:03:39.706 align:middle
Let's try an example of a User
that has a different role.

00:03:39.706 --> 00:03:47.256 align:middle
Run the console: php bin/console Ah, so the
bundle comes with a few handy console commands,

00:03:47.556 --> 00:03:51.436 align:middle
for activating, creating,
promoting and demoting users.

00:03:51.506 --> 00:03:59.956 align:middle
Let's create a new one: php bin/console
fos:user:create How about admin,

00:04:00.866 --> 00:04:05.766 align:middle
admin@aquanote.com and password admin.

00:04:07.156 --> 00:04:08.386 align:middle
And now promote it!

00:04:08.626 --> 00:04:15.746 align:middle
php bin/console fos:user:promote
Hmm, let's give admin, ROLE_ADMIN.

00:04:16.596 --> 00:04:23.836 align:middle
Ok, try the query again: php bin/console
doctrine:query:sql 'SELECT * FROM user' Booya!

00:04:23.836 --> 00:04:24.986 align:middle
Our new user has ROLE_ADMIN!

00:04:25.076 --> 00:04:26.276 align:middle
Quick, go login!

00:04:26.596 --> 00:04:29.386 align:middle
Well, logout first, then go login!

00:04:30.876 --> 00:04:32.676 align:middle
Use admin and admin.

00:04:36.806 --> 00:04:41.266 align:middle
Woohoo! We have both ROLE_USER and ROLE_ADMIN.

00:04:41.266 --> 00:04:46.856 align:middle
In your app, if you want to give different
roles to your users, you have 2 options.

00:04:47.326 --> 00:04:51.606 align:middle
First, via the command line
by using fos:user:promote.

00:04:52.036 --> 00:04:57.076 align:middle
If you only have a few users that need
special permissions, this is a great option.

00:04:58.036 --> 00:05:04.526 align:middle
Or, you can create a user admin area and use
the ChoiceType with the 'multiple' =&gt; true

00:05:04.636 --> 00:05:08.616 align:middle
and 'expanded' =&gt; true to
select the roles as checkboxes.

00:05:09.596 --> 00:05:15.806 align:middle
Ok, time to squash the ugly and make
the FOSUserBundle pages use our layout!

