connectholland / user-bundle
Symfony 4项目的用户扩展包
3.0.0
2023-08-14 09:56 UTC
Requires
- php: >=8.2
- ext-json: *
- doctrine/collections: ^1.8
- doctrine/doctrine-bundle: ^2.10
- doctrine/orm: ^2.16
- doctrine/persistence: ^2.5
- gisostallenberg/response-content-negotiation-bundle: ^2.0
- haydenpierce/class-finder: ^0.5.3
- league/html-to-markdown: ^5.1
- php-http/guzzle7-adapter: ^1.0
- php-http/httplug-bundle: ^1.29
- rollerworks/password-strength-validator: ^1.7
- sensio/framework-extra-bundle: ^6.2
- stof/doctrine-extensions-bundle: ^1.8
- symfony/config: ^6.3
- symfony/console: ^6.3
- symfony/css-selector: ^6.3
- symfony/dependency-injection: ^6.3
- symfony/doctrine-bridge: ^6.3
- symfony/dom-crawler: ^6.3
- symfony/event-dispatcher: ^6.3
- symfony/form: ^6.3
- symfony/framework-bundle: ^6.3
- symfony/http-foundation: ^6.3
- symfony/http-kernel: ^6.3
- symfony/mailer: ^6.3
- symfony/options-resolver: ^6.3
- symfony/routing: ^6.3
- symfony/security-bundle: ^6.3
- symfony/security-core: ^6.3
- symfony/security-csrf: ^6.3
- symfony/security-http: ^6.3
- symfony/templating: ^6.3
- symfony/translation: ^6.3
- symfony/twig-bundle: ^6.3
- symfony/validator: ^6.3
- twig/twig: ^3.7
Suggests
- api-platform/api-pack: Add api-platform/api-pack to add API support to the user bundle, run 'composer req api-pack' to install and follow api platform installation instructions.
- hwi/oauth-bundle: Add hwi/oauth-bundle to be able using OAuth logins, run 'composer require hwi/oauth-bundle' to install.
- lexik/jwt-authentication-bundle: Add lexik/jwt-authentication-bundle to add JWT token support, run 'composer require lexik/jwt-authentication-bundle' to install.
- nelmio/api-doc-bundle: Add nelmio/api-doc-bundle to add comprehensive API documentation, run 'composer require nelmio/api-doc-bundle' to install the Nelmio API Doc bundle and follow the bundle installation and configuration instructions.
README
Symfony 4.4和5项目的用户扩展包
功能
此扩展包将可扩展并提供
- 简单的注册表单
- 简单的登录表单
- 创建用户及其角色的命令
- 包含安全链接的电子邮件消息以完成账户注册
- 密码恢复功能
- 启用OAuth(Google/GitHub/Facebook等)的能力
- API访问能力
- 启用多因素认证的能力
环境
设置环境变量以发送电子邮件。
USERBUNDLE_FROM_EMAILADDRESS=example@example.com
创建用户
要创建新用户,请运行
./bin/console connectholland:user:create example@example.com p@$$w0rd --role=ROLE_USER
OAuth
如果应用需要OAuth登录,则应要求HWI OAuth扩展包,并添加适当的配置。
安装扩展包
composer require hwi/oauth-bundle
在路由中取消注释oauth包含。
添加环境变量以启用特定的OAuth提供者(资源)。例如,对于Google
USERBUNDLE_OAUTH_GOOGLE_ID=xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com USERBUNDLE_OAUTH_GOOGLE_SECRET=XXXXXXXXXXX-xx_xx_xxxxx USERBUNDLE_OAUTH_GOOGLE_SCOPE='email profile' # Options specific for the provider can be added in a json encoded string like below. USERBUNDLE_OAUTH_GOOGLE_OPTIONS={"hd": "connectholland.nl"}
添加(自动)路由配置
# config/routes/connectholland_user.yaml connectholland_user_oauth: resource: '@ConnecthollandUserBundle/Resources/config/routing_oauth.yaml' prefix: '/'
JTW令牌支持
如果应用需要JTW令牌支持,则应要求Lexik JWT认证扩展包,并添加适当的配置。
安装扩展包
composer require lexik/jwt-authentication-bundle
根据Lexik JWT认证扩展包文档所述生成公钥和私钥。将密钥位置相对于项目根目录作为环境变量JWT_SECRET_KEY
和JWT_PUBLIC_KEY
设置,并将使用的密码短语作为JWT_PASSPHRASE
设置。
API支持
要添加API支持,请安装API平台并JWT认证配置防火墙并添加认证路由。
composer req api-pack jwt-auth
# Example of the security settings for your project. # config/packages/security.yaml firewalls: api_login: pattern: ^/api/users/authenticate stateless: true anonymous: true provider: app_user_provider json_login: check_path: /api/users/authenticate success_handler: lexik_jwt_authentication.handler.authentication_success failure_handler: lexik_jwt_authentication.handler.authentication_failure require_previous_session: false api: pattern: ^/api stateless: true anonymous: true provider: app_user_provider json_login: check_path: /api/users/authenticate success_handler: lexik_jwt_authentication.handler.authentication_success failure_handler: lexik_jwt_authentication.handler.authentication_failure require_previous_session: false guard: authenticators: - lexik_jwt_authentication.jwt_token_authenticator access_control: - { path: ^/api/authenticate, roles: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/api, roles: ROLE_USER }
# config/routes.yaml api_authenticate: path: /api/users/authenticate
例如
###> lexik/jwt-authentication-bundle ### JWT_SECRET_KEY=config/jwt/private.pem JWT_PUBLIC_KEY=config/jwt/public.pem JWT_PASSPHRASE=DEVELOPMENT-KSZEW-YHMIE-XWWCL-DBGPQ-MSYCU-RJRWL-UIYBH-TPNXM-GJTVU-BRDQI-XWXHX ###< lexik/jwt-authentication-bundle ###
安全配置示例
security: encoders: Symfony\Component\Security\Core\User\UserInterface: algorithm: auto providers: app_user_provider: entity: class: ConnectHolland\UserBundle\Entity\User property: email firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false main: anonymous: true guard: authenticators: - ConnectHolland\UserBundle\Security\UserBundleAuthenticator logout: path: connectholland_user_logout oauth: use_forward: false resource_owners: # The resource_owners routing postfixes are a composition of the firewall name and the resource name google: connectholland_user_oauth_check_main_google facebook: connectholland_user_oauth_check_main_facebook linkedin: connectholland_user_oauth_check_main_linkedin # etcetera login_path: connectholland_user_login failure_path: connectholland_user_login oauth_user_provider: service: ConnectHolland\UserBundle\Security\OAuthUserProvider access_control: - { path: ^/(login|inloggen|register|registreren|password-reset|wachtwoord-vergeten), roles: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/, roles: [ROLE_OAUTH, ROLE_ADMIN ] }
扩展用户实体
如果您想扩展用户实体,应克隆User并将其添加为您的项目中的实体。