Skip to content

Commit

Permalink
feature #1024 Added a new tutorial about FOSUserBundle + EasyAdmin (j…
Browse files Browse the repository at this point in the history
…aviereguiluz)

This PR was squashed before being merged into the master branch (closes #1024).

Discussion
----------

Added a new tutorial about FOSUserBundle + EasyAdmin

This fixes #714 and it's loosely based on [this tutorial](https://www.orbitale.io/manage-fosuser-in-easyadmin/) published by our friend @Pierstoval.

Commits
-------

b2f5e97 Added a new tutorial about FOSUserBundle + EasyAdmin
  • Loading branch information
javiereguiluz committed Mar 22, 2016
2 parents 3b5e508 + b2f5e97 commit 0c8cb55
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ read before using EasyAdmin.
* [How to translate the backend](Resources/doc/tutorials/i18n.md)
* [How to define custom actions](Resources/doc/tutorials/custom-actions.md)
* [How to define custom options for entity properties](Resources/doc/tutorials/custom-property-options.md)
* [How to integrate FOSUserBundle to manage users](Resources/doc/tutorials/fosuserbundle-integration.md)
* [How to use a WYSIWYG editor](Resources/doc/tutorials/wysiwyg-editor.md)
* [How to upload files and images](Resources/doc/tutorials/upload-files-and-images.md)
* [How To integrate FOSRestBundle and EasyAdmin](Resources/doc/tutorials/fosrestbundle-integration.md)
Expand Down
92 changes: 92 additions & 0 deletions Resources/doc/tutorials/fosuserbundle-integration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
How To Integrate FOSUserBundle To Manage Users
==============================================

[FOSUserBundle][1] is a popular Symfony bundle which simplifies the management
of users in Symfony applications. This article explains how to better integrate
it with EasyAdmin to manage users' information. The article assumes that you
have installed FOSUserBundle and have created a user entity as explained in
[its documentation][2].

Creating New Users
------------------

FOSUserBundle defines a [user manager][3] to handle all operations on user
instances, such as creating and editing users. This manager, which is accessed
through the `fos_user.user_manager` service, makes the bundle "agnostic" to
where the users are stored and it's a good practice to use it.

Before using this manager, [create your own AdminController][4] if you haven't
done it already so you can modify the behavior of the new action. Then,
override the `createNewUserEntity()` and `prePersistUserEntity()` methods to
override the way users are created and persisted:

```php
// src/AppBundle/Controller/AdminController.php
namespace AppBundle\Controller;

use JavierEguiluz\Bundle\EasyAdminBundle\Controller\AdminController as BaseAdminController;

class AdminController extends BaseAdminController
{
public function createNewUserEntity()
{
return $this->get('fos_user.user_manager')->createUser();
}

public function prePersistUserEntity($user)
{
$this->get('fos_user.user_manager')->updateUser($user, false);
}
}
```

The `false` value of the second argument of `updateUser()` tells FOSUserBundle
to not save the changes (to not flush the UnitOfWork) at that moment and to let
Doctrine take care of saving those changes when needed.

> **NOTE**
>
> If your user entity is not called `User`, you need to change the above method
> names. For example, if the entity is called `Customers`, the methods to define
> are `createNewCustomersEntity()` and `prePersistCustomersEntity()`.
Editing User Information
------------------------

FOSUserBundle provides a custom `User` entity with some predefined properties,
such as `email`, `enabled` and `lastLogin`. You can manage these properties in
the same way you manage any property of any other entity:

```yaml
easy_admin:
entities:
User:
form:
fields: ['enabled', 'username', 'email', 'roles', 'lastLogin']
```

However, it's recommended to save changes using FOSUserBundle's user manager.
Therefore, open your AdminController and add the following method:

```php
class AdminController extends BaseAdminController
{
// ...

public function preUpdateUserEntity($user)
{
$this->get('fos_user.user_manager')->updateUser($user, false);
}
}
```

> **NOTE**
>
> If your user entity is not called `User`, you need to change the above method
> name. For example, if the entity is called `Customers`, the method to define
> is `preUpdateCustomersEntity()`.
[1]: https://github.com/FriendsOfSymfony/FOSUserBundle
[2]: http://symfony.com/doc/current/bundles/FOSUserBundle/index.html
[3]: http://symfony.com/doc/current/bundles/FOSUserBundle/user_manager.html
[4]: ../book/4-edit-new-configuration.md#customizing-the-behavior-of-edit-and-new-views

0 comments on commit 0c8cb55

Please sign in to comment.