amillot/user-bundle

管理用户的Bundle

安装: 40

依赖者: 0

建议者: 0

安全: 0

星星: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

0.0.2 2020-06-11 14:27 UTC

This package is auto-updated.

Last update: 2024-09-17 16:58:04 UTC


README

要使用此Bundle,您必须创建自己的User类,该类扩展了AbstractUser类。

// src\Entity\User.php

<?php

declare(strict_types=1);

namespace App\Entity;

use amillot\UserBundle\Entity\AbstractUser;

class User extends AbstractUser
{
}

密码编码

您可以在security.yaml中控制用户密码的编码方式。

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

实体用户提供者

#// 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'

身份验证 & 防火墙

防火墙是允许对您的系统进行身份验证的过程。

每个请求只使用一个防火墙。您可以使用模式、主机或服务来标识要使用的防火墙。

所有真实URL都由主防火墙处理(没有模式键意味着它匹配所有URL)。防火墙可以有多种身份验证模式,换句话说,有多种方式来询问“你是谁?”通常,当用户第一次访问您的网站时,用户是未知的(即未登录)。如果启用匿名模式,则用于这些请求。

# 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

    # ...

拒绝访问、角色和其他授权

# 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 }

将Profile实体链接到User实体

您必须指定要使用的User类而不是UserInterface。

# config/packages/doctrine.yaml
doctrine:

    # ...

    orm:

        # ...

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

添加映射信息

用户映射

<!-- 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>

配置文件映射

<!-- 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>