Wednesday, June 5, 2013

Login Bundle :: FOSUser

ref : https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md#LNaN

Install login bundle :

Step 1:  edit composer.json

{
    "require": {
        "friendsofsymfony/user-bundle": "~2.0@dev"
    }
}

Step 2:  download
php composer.phar update friendsofsymfony/user-bundle
 

Step 3:  Enable bundle, edit app/AppKernel.php
 
<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new FOS\UserBundle\FOSUserBundle(),
    );
}
 
  
Step 4:  Creating User Class

<?php 
// src/ASF/PembedaBundle/Entity/User.php

namespace ASF\PembedaBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}
 
 
Step 5: Configure your application's security.yml
 
 
# app/config/security.yml
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
            logout:       true
            anonymous:    true

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN } 
 
 
Step 6: Configure the FOSUserBundle
 
 # app/config/config.yml
fos_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: ASF\PembedaBundle\Entity\User
 
Step 7: Import FOSUserBundle routing files



# app/config/routing.yml
fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /profile

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /register

fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix: /resetting

fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix: /profile


Step 8: Update your database schema


php app/console doctrine:schema:update --force
 
You now can login at http://symfony/app_dev.php/login 


No comments:

Post a Comment