CRUDlexUser is a library offering an user provider for symfony/security.
The CRUDlexUser API itself is documented here:
Upcoming bleeding edge:
Generated via (assuming APIGen is globally installed like composer global require ...
and the PHP version is at maximum 7.1):
~/.composer/vendor/bin/apigen generate
CRUDlexUser uses SemVer for versioning. Currently, the API changes quickly due to be < 1.0.0, so take care about notes in the changelog when upgrading.
"require": {
"philiplb/crudlexuser": "0.14.0"
}
"require": {
"philiplb/crudlexuser": "0.15.x-dev"
}
This library offers two parts. First, a management interface for your admin panel to perform CRUD operations on your userbase and second, an symfony/security UserProvider offering in order to connect the users with the application.
The admin panel for your users is based on CRUDlex. So all you have to do is to add the needed entities to your crud.yml from the following sub chapters.
In order to get the salt generated and the password hashed, you have to let the library add some CRUDlex events in your initialization:
$crudUserSetup = new CRUDlex\UserSetup();
$crudUserSetup->addEvents($app['crud']->getData('user'));
user:
label: User
table: user
fields:
username:
type: text
label: Username
required: true
unique: true
password:
type: text
label: Password Hash
description: 'Set this to your desired password. Will be automatically converted to an hash value not meant to be readable.'
required: true
salt:
type: text
label: Password Salt
description: 'Auto populated field on user creation. Used internally.'
required: false
userRoles:
type: many
label: Roles
many:
entity: role
nameField: role
thisField: user
thatField: role
Plus any more fields you need. Recommended for the password reset features:
email:
type: text
label: E-Mail
required: true
unique: true
role:
label: Roles
table: role
fields:
role:
type: text
label: Role
required: true
In case you want to use the password reset features:
passwordReset:
label: Password Resets
table: password_reset
fields:
user:
type: reference
label: User
reference:
nameField: username
entity: user
required: true
token:
type: text
label: Token
required: true
reset:
type: datetime
label: Reset
Simply instantiate and add it to your symfony/security configuration:
$userProvider = new CRUDlex\UserProvider($app['crud']->getData('user'), $app['crud']->getData('userRole'));
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'admin' => array(
//...
'users' => $userProvider
),
),
));
In order to get the user data from the logged in user in your controller, you might grab him like this:
$user = $app['security.token_storage']->getToken()
You get back a CRUDlex\User instance having some getters, see the API docs.