amillot/user-bundle

Bundle that manage user.

Installs: 40

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:symfony-bundle

0.0.2 2020-06-11 14:27 UTC

This package is auto-updated.

Last update: 2024-04-17 15:58:52 UTC


README

To use this bundle, you have to create your own User class which extends to AbstractUser class.

// src\Entity\User.php

<?php

declare(strict_types=1);

namespace App\Entity;

use amillot\UserBundle\Entity\AbstractUser;

class User extends AbstractUser
{
}

Encoding Passwords

You can control how your user password is encoded in security.yaml.

# config/packages/security.yaml
security:
    encoders:
        App\Entity\User: bcrypt

Entity User Provider

#// config/packages/security.yaml
security:
    # ...

    providers:
        users:
            entity:
                # the class of the entity that represents users
                class: 'App\Entity\User'
                # the property to query by - e.g. username, email, etc
                property: 'username'
                # optional: if you're using multiple Doctrine entity
                # managers, this option defines which one to use
                # manager_name: 'customer'

Authentication & firewalls

A firewall is the process which allow to authenticate your system.

Only one firewall is used by request. You can use pattern, host or service to identify the firewall to use.

All real URLs are handled by the main firewall (no pattern key means it matches all URLs). A firewall can have many modes of authentication, in other words many ways to ask the question "Who are you?". Often, the user is unknown (i.e. not logged in) when they first visit your website. The anonymous mode, if enabled, is used for these requests.

# config/packages/security.yaml
security:
    # ...

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            provider: users
            anonymous: lazy
            form_login:
                login_path: home
                check_path: login
                use_referer: true
                default_target_path: dashboard
            logout:
                path: logout

    # ...

Denying access, Roles, and other Authorization

# config/packages/security.yaml
security:
    # ...

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        - { path: '^/login', roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: '^/register', roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: '^/$', roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: '^/', roles: ROLE_USER }

Link Profile entity to User entity

You have to specify the User class to use instead of UserInterface.

# config/packages/doctrine.yaml
doctrine:

    # ...

    orm:

        # ...

        resolve_target_entities:
            amillot\UserBundle\Model\UserInterface: App\Entity\User

Add Mapping information

User mapping

<!-- config/doctrine/ORM/Profile.orm.xml -->
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                          https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity name="App\Entity\User" table="users">
    </entity>

</doctrine-mapping>

Profile mapping

<!-- config/doctrine/ORM/Profile.orm.xml -->
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                          https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity name="App\Entity\Profile" table="profiles">
    </entity>

</doctrine-mapping>