PageRenderTime 63ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Resources/doc/index.md

https://github.com/johannes/FOSUserBundle
Markdown | 429 lines | 320 code | 109 blank | 0 comment | 0 complexity | d8c99db32b230771f77a2c7817260d43 MD5 | raw file
  1. Getting Started With FOSUserBundle
  2. ==================================
  3. The Symfony2 security component provides a flexible security framework that
  4. allows you to load users from configuration, a database, or anywhere else
  5. you can imagine. The FOSUserBundle builds on top of this to make it quick
  6. and easy to store users in a database.
  7. So, if you need to persist and fetch the users in your system to and from
  8. a database, then you're in the right place.
  9. ## Prerequisites
  10. ### Translations
  11. If you wish to use default texts provided in this bundle, you have to make
  12. sure you have translator enabled in your config.
  13. ```
  14. # app/config/config.yml
  15. framework:
  16. translator: ~
  17. ```
  18. For more information about translations, check [Symfony documentation](http://symfony.com/doc/2.0/book/translation.html).
  19. ## Installation
  20. Installation is a quick (I promise!) 8 step process:
  21. 1. Download FOSUserBundle
  22. 2. Configure the Autoloader
  23. 3. Enable the Bundle
  24. 4. Create your User class
  25. 5. Configure your application's security.yml
  26. 6. Configure the FOSUserBundle
  27. 7. Import FOSUserBundle routing
  28. 8. Update your database schema
  29. ### Step 1: Download FOSUserBundle
  30. Ultimately, the FOSUserBundle files should be downloaded to the
  31. `vendor/bundles/FOS/UserBundle` directory.
  32. This can be done in several ways, depending on your preference. The first
  33. method is the standard Symfony2 method.
  34. **Using the vendors script**
  35. Add the following lines in your `deps` file:
  36. ```
  37. [FOSUserBundle]
  38. git=git://github.com/FriendsOfSymfony/FOSUserBundle.git
  39. target=bundles/FOS/UserBundle
  40. ```
  41. Now, run the vendors script to download the bundle:
  42. ``` bash
  43. $ php bin/vendors install
  44. ```
  45. **Using submodules**
  46. If you prefer instead to use git submodules, the run the following:
  47. ``` bash
  48. $ git submodule add git://github.com/FriendsOfSymfony/FOSUserBundle.git vendor/bundles/FOS/UserBundle
  49. $ git submodule update --init
  50. ```
  51. ### Step 2: Configure the Autoloader
  52. Add the `FOS` namespace to your autoloader:
  53. ``` php
  54. <?php
  55. // app/autoload.php
  56. $loader->registerNamespaces(array(
  57. // ...
  58. 'FOS' => __DIR__.'/../vendor/bundles',
  59. ));
  60. ```
  61. ### Step 3: Enable the bundle
  62. Finally, enable the bundle in the kernel:
  63. ``` php
  64. <?php
  65. // app/AppKernel.php
  66. public function registerBundles()
  67. {
  68. $bundles = array(
  69. // ...
  70. new FOS\UserBundle\FOSUserBundle(),
  71. );
  72. }
  73. ```
  74. ### Step 4: Create your User class
  75. The goal of this bundle is to persist some `User` class to a database (MySql,
  76. MongoDB, CouchDB, etc). Your first job, then, is to create the `User` class
  77. for your application. This class can look and act however you want: add any
  78. properties or methods you find useful. This is *your* `User` class.
  79. This class has just two requirements, which allow it to take advantage of
  80. all of the functionality in the FOSUserBundle:
  81. 1. It must extend one of the base `User` classes from the bundle
  82. 2. It must have an `id` field
  83. In the following sections, you'll see examples of how your `User` class should
  84. look, depending on how you're storing your users (Doctrine ORM, MongoDB ODM,
  85. or CouchDB ODM).
  86. Your `User` class can live inside any bundle in your application. For example,
  87. if you work at "Acme" company, then you might create a bundle called `AcmeUserBundle`
  88. and place your `User` class in it.
  89. **Warning:**
  90. > If you override the __construct() method in your User class, be sure
  91. > to call parent::__construct(), as the base User class depends on
  92. > this to initialize some fields.
  93. **a) Doctrine ORM User class**
  94. If you're persisting your users via the Doctrine ORM, then your `User` class
  95. should live in the `Entity` namespace of your bundle and look like this to
  96. start:
  97. ``` php
  98. <?php
  99. // src/Acme/UserBundle/Entity/User.php
  100. namespace Acme\UserBundle\Entity;
  101. use FOS\UserBundle\Entity\User as BaseUser;
  102. use Doctrine\ORM\Mapping as ORM;
  103. /**
  104. * @ORM\Entity
  105. * @ORM\Table(name="fos_user")
  106. */
  107. class User extends BaseUser
  108. {
  109. /**
  110. * @ORM\Id
  111. * @ORM\Column(type="integer")
  112. * @ORM\GeneratedValue(strategy="AUTO")
  113. */
  114. protected $id;
  115. public function __construct()
  116. {
  117. parent::__construct();
  118. // your own logic
  119. }
  120. }
  121. ```
  122. **Note:**
  123. > `User` is a reserved keyword in SQL so you cannot use it as table name.
  124. **b) MongoDB User class**
  125. If you're persisting your users via the Doctrine MongoDB ODM, then your `User`
  126. class should live in the `Document` namespace of your bundle and look like
  127. this to start:
  128. ``` php
  129. <?php
  130. // src/Acme/UserBundle/Document/User.php
  131. namespace Acme\UserBundle\Document;
  132. use FOS\UserBundle\Document\User as BaseUser;
  133. use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
  134. /**
  135. * @MongoDB\Document
  136. */
  137. class User extends BaseUser
  138. {
  139. /**
  140. * @MongoDB\Id(strategy="auto")
  141. */
  142. protected $id;
  143. public function __construct()
  144. {
  145. parent::__construct();
  146. // your own logic
  147. }
  148. }
  149. ```
  150. **c) CouchDB User class**
  151. If you're persisting your users via the Doctrine CouchDB ODM, then your `User`
  152. class should live in the `Document` namespace of your bundle and look like
  153. this to start:
  154. ``` php
  155. <?php
  156. // src/Acme/UserBundle/Document/User.php
  157. namespace Acme\UserBundle\Document;
  158. use FOS\UserBundle\Document\User as BaseUser;
  159. use Doctrine\ODM\CouchDB\Mapping as CouchDB;
  160. /**
  161. * @CouchDB\Document
  162. */
  163. class User extends BaseUser
  164. {
  165. /**
  166. * @CouchDB\Id
  167. */
  168. protected $id;
  169. public function __construct()
  170. {
  171. parent::__construct();
  172. // your own logic
  173. }
  174. }
  175. ```
  176. ### Step 5: Configure your application's security.yml
  177. In order for Symfony's security component to use the FOSUserBundle, you must
  178. tell it to do so in the `security.yml` file. The `security.yml` file is where the
  179. basic configuration for the security for your application is contained.
  180. Below is a minimal example of the configuration necessary to use the FOSUserBundle
  181. in your application:
  182. ``` yaml
  183. # app/config/security.yml
  184. security:
  185. providers:
  186. fos_userbundle:
  187. id: fos_user.user_manager
  188. firewalls:
  189. main:
  190. pattern: ^/
  191. form_login:
  192. provider: fos_userbundle
  193. logout: true
  194. anonymous: true
  195. access_control:
  196. - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  197. - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
  198. - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
  199. - { path: ^/admin/, role: ROLE_ADMIN }
  200. role_hierarchy:
  201. ROLE_ADMIN: ROLE_USER
  202. ROLE_SUPER_ADMIN: ROLE_ADMIN
  203. ```
  204. Under the `providers` section, you are making the bundle's packaged user provider
  205. service available via the alias `fos_userbundle`. The id of the bundle's user
  206. provider service is `fos_user.user_manager`.
  207. Next, take a look at examine the `firewalls` section. Here we have declared a
  208. firewall named `main`. By specifying `form_login`, you have told the Symfony2
  209. framework that any time a request is made to this firewall that leads to the
  210. user needing to authenticate himself, the user will be redirected to a form
  211. where he will be able to enter his credentials. It should come as no surprise
  212. then that you have specified the user provider we declared earlier as the
  213. provider for the firewall to use as part of the authentication process.
  214. **Note:**
  215. > Although we have used the form login mechanism in this example, the FOSUserBundle
  216. > user provider is compatible with many other authentication methods as well. Please
  217. > read the Symfony2 Security component documention for more information on the
  218. > other types of authentication methods.
  219. The `access_control` section is where you specify the credentials necessary for
  220. users trying to access specific parts of your application. The bundle requires
  221. that the login form and all the routes used to create a user and reset the password
  222. be available to unauthenticated users but use the same firewall as
  223. the pages you want to secure with the bundle. This is why you have specified that
  224. the any request matching the `/login` pattern or starting with `/register` or
  225. `/resetting` have been made available to anonymous users. You have also specified
  226. that any request beginning with `/admin` will require a user to have the
  227. `ROLE_ADMIN` role.
  228. For more information on configuring the `security.yml` file please read the Symfony2
  229. security component [documentation](http://symfony.com/doc/current/book/security.html).
  230. **Note:**
  231. > Pay close attention to the name, `main`, that we have given to the firewall which
  232. > the FOSUserBundle is configured in. You will use this in the next step when you
  233. > configure the FOSUserBundle.
  234. ### Step 6: Configure the FOSUserBundle
  235. Now that you have properly configured your application's `security.yml` to work
  236. with the FOSUserBundle, the next step is to configure the bundle to work with
  237. the specific needs of your application.
  238. Add the following configuration to your `config.yml` file according to which type
  239. of datastore you are using.
  240. ``` yaml
  241. # app/config/config.yml
  242. fos_user:
  243. db_driver: orm # other valid values are 'mongodb', 'couchdb'
  244. firewall_name: main
  245. user_class: Acme\UserBundle\Entity\User
  246. ```
  247. Or if you prefer XML:
  248. ``` xml
  249. # app/config/config.xml
  250. <!-- app/config/config.xml -->
  251. <!-- other valid 'db-driver' values are 'mongodb' and 'couchdb' -->
  252. <fos_user:config
  253. db-driver="orm"
  254. firewall-name="main"
  255. user-class="Acme\UserBundle\Entity\User"
  256. />
  257. ```
  258. Only three configuration values are required to use the bundle:
  259. * The type of datastore you are using (`orm`, `mongodb`, or `couchdb`).
  260. * The firewall name which you configured in Step 5.
  261. * The fully qualified class name (FQCN) of the `User` class which you created in Step 2
  262. ### Step 7: Import FOSUserBundle routing files
  263. Now that you have activated and configured the bundle, all that is left to do is
  264. import the FOSUserBundle routing files.
  265. By importing the routing files you will have ready made pages for things such as
  266. logging in, creating users, etc.
  267. In YAML:
  268. ``` yaml
  269. # app/config/routing.yml
  270. fos_user_security:
  271. resource: "@FOSUserBundle/Resources/config/routing/security.xml"
  272. fos_user_profile:
  273. resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
  274. prefix: /profile
  275. fos_user_register:
  276. resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
  277. prefix: /register
  278. fos_user_resetting:
  279. resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
  280. prefix: /resetting
  281. fos_user_change_password:
  282. resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
  283. prefix: /change-password
  284. ```
  285. Or if you prefer XML:
  286. ``` xml
  287. <!-- app/config/routing.xml -->
  288. <import resource="@FOSUserBundle/Resources/config/routing/security.xml"/>
  289. <import resource="@FOSUserBundle/Resources/config/routing/profile.xml" prefix="/profile" />
  290. <import resource="@FOSUserBundle/Resources/config/routing/registration.xml" prefix="/register" />
  291. <import resource="@FOSUserBundle/Resources/config/routing/resetting.xml" prefix="/resetting" />
  292. <import resource="@FOSUserBundle/Resources/config/routing/change_password.xml" prefix="/change-password" />
  293. ```
  294. **Note:**
  295. > In order to use the built-in email functionality (confirmation of the account,
  296. > resetting of the password), you must activate and configure the SwiftmailerBundle.
  297. ### Step 8: Update your database schema
  298. Now that the bundle is configured, the last thing you need to do is update your
  299. database schema because you have added a new entity, the `User` class which you
  300. created in Step 2.
  301. For ORM run the following command.
  302. ``` bash
  303. $ php app/console doctrine:schema:update --force
  304. ```
  305. For MongoDB users you can run the following command to create the indexes.
  306. ``` bash
  307. $ php app/console doctrine:mongodb:schema:create --index
  308. ```
  309. You now can login at `http://app.com/app_dev.php/login`!
  310. ### Next Steps
  311. Now that you have completed the basic installation and configuration of the
  312. FOSUserBundle, you are ready to learn about more advanced features and usages
  313. of the bundle.
  314. The following documents are available:
  315. 1. [Overriding Templates](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_templates.md)
  316. 2. [Overriding Controllers](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_controllers.md)
  317. 3. [Overriding Forms](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_forms.md)
  318. 4. [Command Line Tools](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/command_line_tools.md)
  319. 5. [Supplemental Documenation](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/supplemental.md)
  320. 6. [Configuration Reference](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/configuration_reference.md)