I'm messing about with Symfony3 FOSUserBundle by following this Symfony docs
I managed to successfully install it and set it up as show in the docs. Checked by database and FSOUserBundle has created this table for me:
+-----------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(255) | NO | | NULL | |
| username_canonical | varchar(255) | NO | UNI | NULL | |
| email | varchar(255) | NO | | NULL | |
| email_canonical | varchar(255) | NO | UNI | NULL | |
| enabled | tinyint(1) | NO | | NULL | |
| salt | varchar(255) | NO | | NULL | |
| password | varchar(255) | NO | | NULL | |
| last_login | datetime | YES | | NULL | |
| locked | tinyint(1) | NO | | NULL | |
| expired | tinyint(1) | NO | | NULL | |
| expires_at | datetime | YES | | NULL | |
| confirmation_token | varchar(255) | YES | | NULL | |
| password_requested_at | datetime | YES | | NULL | |
| roles | longtext | NO | | NULL | |
| credentials_expired | tinyint(1) | NO | | NULL | |
| credentials_expire_at | datetime | YES | | NULL | |
+-----------------------+--------------+------+-----+---------+----------------+
Understand that these fields come from BasuUser which I extend in User entity.
Next step I am using Fixtures to load data into this table but what I am wondering is this wether I need to set every single field in this table i see by default the are NULL so when and how I should populate these fields?
Thx
If you look at User class from FOSUser, you see that there are setters, and constructor, that do some needed work. Look at constructor of User class:
public function __construct()
{
$this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
$this->enabled = false;
$this->locked = false;
$this->expired = false;
$this->roles = array();
$this->credentialsExpired = false;
}
If you fill username by, $user->setUsername('username')
, field username_canonical
will be populated too. Analogically with email
and email_canonical
. You should set Enabled
as true if you not require confirmation by e-mail. Salt
is populated by constructor. Next you set plainPassword
then password
is automatically encoded and populated. Last login
can be whichever. It is managed automatically when user login. Expired
is connected only with accounts that have restricted time of being available. Confirmation_token
is connected with e-mail confirmation of account. Password_requested_at
allow to avoid sending hundreds e-mails with password reset. Managing roles use add
rather than set
because of it is array rather not field. Do not worry about empty roles
in database for ROLE_USER
, it is to save table volume. credentials_expired
is default false, but you can find creative application of this column.
Finally look at my code of fixtures that create user super admin and 10 users.
<?php
namespace Application\Sonata\UserBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
use AppBundle\Entity\User;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LoadUserData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
private $container;
/**
* {@inheritDoc}
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
/** @var $manager \FOS\UserBundle\Doctrine\UserManager */
$manager = $this->container->get('fos_user.user_manager');
/** @var $user User */
$user = $manager->createUser();
$user->setUsername('admin');
$user->setEmail('admin@example.com');
$user->setRoles(array('ROLE_SUPER_ADMIN'));
$user->setEnabled(true);
$user->setPlainPassword('admin_pass');
$manager->updateUser($user);
unset($user);
$faker = \Faker\Factory::create();
for ($i = 0; $i < 10; $i++)
{
/** @var $user User */
$user = $manager->createUser();
$user->setUsername($faker->userName);
$user->setEmail($faker->safeEmail);
$user->setRoles(array('ROLE_USER'));
$user->setEnabled(true);
$user->setPlainPassword('pass');
$manager->updateUser($user);
$this->addReference('user.demo_'.$i, $user);
}
}
/**
* @return integer
*/
function getOrder()
{
return 10;
}
}
As long a you have this set in your "app/config/security.yml" file:
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
Then you should be able to go to the URL /register
, then you are prompted to register, and then you can query the fos_user
table and you will see it get populated.
If you need to use AD Authentication, you can go through my blog on that .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.