我正在尝试在Symfony 2.1项目中实现安全功能,其中管理员可以使用初始密码创建用户,然后在用户第一次登录时自动触发更改密码处理程序.
我遇到了覆盖FOSUserBundle类的问题,我认为这肯定已经以某种方式建立,至少在某种程度上,尽管我无法在任何地方的文档中看到它.
我想在实体中使用credentials_expired标志.管理员创建用户时,将设置为1.当用户首次登录时,将检查credentials_expired,而不是抛出异常,将触发change-password.我做到了这么远.
然后ChangePasswordController将确保密码实际更改(这似乎不是FOS中的默认行为)并且credentials_expired设置为0.这就是我被困住的地方.有这么多层次的服务我似乎无法正确定制.
我试图删除emailCanonical上的唯一索引,以便多个用户可以共享相同的电子邮件地址.但是,我不想直接编辑FOS/UserBundle/Resources/config/doctrine/User.orm.xml,因为对bundle本身的任何更新都将删除更改.有没有什么办法可以覆盖我自己的bundle中的emailCanonical字段,同时扩展基本用户(FOS/UserBundle/Model/User.php)
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Foo\BarBundle\Constant\SecurityConstant;
class User extends BaseUser {
protected $id;
...
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我是Symfony2的新手,但非常了解它.首先,我使用的是symfony 2.1.7.和FOSUserBundle用于用户设置.我已经使用用户名和密码覆盖了fos_user-login模板.但我想为登录添加验证码.我见过GregwarCaptchaBundle,根据文档,应该在FormType中添加新字段.我的问题是:symfony或FOSUserBundle登录表单类型在哪里,我可以添加这个新字段,还是覆盖它?存在ChangePasswordFormType,ProfileFormType ...等但没有LoginFOrmType.可能它是如此明显,但我没有得到重点,欢迎任何帮助请问
问题是解决方案的一些解决方案
看看下面的评论,帕特帮助了我.我用_username,_password和captcha字段创建了一个新的表单类型.当用户名和密码命名以下划线开头时,足以进行"login_check"路由和Symfony身份验证.但是,Symfony使用侦听器进行登录过程.哪个是UsernamePasswordFormAuthenticationListener班级.虽然我在Form类型中添加了captcha字段,但在登录过程中总是会被忽略.(它在页面上呈现,但该字段永远不会被验证,只会被忽略.)
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('_username', 'email', array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle')) // TODO: user can login with email by inhibit the user to enter username
->add('_password', 'password', array(
'label' => 'form.current_password',
'translation_domain' => 'FOSUserBundle',
'mapped' => false,
'constraints' => new UserPassword()))
->add('captcha', 'captcha');
}
Run Code Online (Sandbox Code Playgroud)
正如我上面提到的,UsernamePasswordFormAuthenticationListener类获取表单输入值,然后重定向到您:
public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, SessionAuthenticationStrategyInterface $sessionStrategy, HttpUtils $httpUtils, $providerKey, AuthenticationSuccessHandlerInterface $successHandler, AuthenticationFailureHandlerInterface $failureHandler, …Run Code Online (Sandbox Code Playgroud) 有没有人已经使用Symfony 2和FOS User Bundle在Bootstrap模式中构建了一个登录表单?
这就是我现在拥有的:
SRC/Webibli/UserBundle /资源/配置/ service.yml
authentication_handler:
class: Webibli\UserBundle\Handler\AuthenticationHandler
arguments: [@router, @security.context, @fos_user.user_manager, @service_container]
Run Code Online (Sandbox Code Playgroud)
应用程序/配置/ security.yml
form_login:
provider: fos_userbundle
success_handler: authentication_handler
failure_handler: authentication_handler
Run Code Online (Sandbox Code Playgroud)
SRC/Webibli/UserBundle /处理器/ AuthenticationHandler.php
<?php
namespace Webibli\UserBundle\Handler;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
class AuthenticationHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
protected $router;
protected $security;
protected $userManager;
protected $service_container;
public function __construct(RouterInterface $router, SecurityContext $security, $userManager, $service_container)
{
$this->router = $router;
$this->security = $security; …Run Code Online (Sandbox Code Playgroud) 安装FOSUserBundle之后,现在我尝试使用user/userpass登录,但我仍然收到"Bad credentials"消息.这是我的security.yml:
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
fos_userbundle:
id: fos_user.user_manager
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
logout: true
anonymous: true
#anonymous: ~
#http_basic:
# realm: "Secured Demo Area"
Run Code Online (Sandbox Code Playgroud)
任何的想法?
symfony 2.0.4
我正在尝试让区域设置切换到我的应用程序的登录屏幕.为了做到这一点,我的登录页面上的链接指向site.com/(默认语言环境)和site.com/en(我支持的第二个语言环境).一旦我登录,切换就像一个魅力.但是,如果我尚未通过身份验证,则登录将始终返回到默认语言环境.我的理解是,如果我使用来自FOSUserBundle的命名路由,那么它应该能够自动处理语言环境,但我不能让它工作.
我的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
login_path: fos_user_security_login
check_path: fos_user_security_check
logout: true
anonymous: true
access_control:
- { path: ^/_wdt, roles: IS_AUTHENTICATED_ANONYMOUSLY } # allow wdt for debugging
- { path: ^/_profiler/, role: IS_AUTHENTICATED_ANONYMOUSLY } # allow profiler for debugging
- { path: ^/bundles/, role: IS_AUTHENTICATED_ANONYMOUSLY } # allow assets to be loaded anonymously
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { …Run Code Online (Sandbox Code Playgroud) 我在我的Symfony应用程序中使用FOSUserBundle非常棒.他们在单独的模板中有登录和注册表单.我希望在一个模板中将它们彼此相邻显示.
因此,我创建app/Resources/FOSUserBundle/Security/login.html.twig并app/Resources/FOSUserBundle/Registration/register.html.twig覆盖两个模板.在login.html.twig我调用寄存器控制器来呈现其模板.
应用程序/资源/ FOSUserBundle /安全/ login.html.twig:
{% extends "FOSUserBundle::layout.html.twig" %}
{% trans_default_domain 'FOSUserBundle' %}
{% block fos_user_content %}
{% if error %}
<div>{{ error|trans }}</div>
{% endif %}
<form action="{{ path("fos_user_security_check") }}" method="post">
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
<input type="text" id="username" name="_username" value="{{ last_username }}" placeholder="{{ 'security.login.username'|trans }}" required="required" />
<input type="password" id="password" name="_password" placeholder="{{ 'security.login.password'|trans }}" required="required" />
<input type="checkbox" id="remember_me" name="_remember_me" value="on" />
<label for="remember_me">{{ 'security.login.remember_me'|trans }}</label>
<input type="submit" id="_submit" …Run Code Online (Sandbox Code Playgroud) 我激活了FOSUserBundle的用户确认.但我不想接受原始听众的回应
$url = $this->router->generate('fos_user_registration_check_email');
$event->setResponse(new RedirectResponse($url));
Run Code Online (Sandbox Code Playgroud)
我想选择另一条路线.我试图扩展EventListener
namespace Acme\MyBundle\EventListener;
use FOS\UserBundle\EventListener\EmailConfirmationListener as BaseListener;
// ...
class EmailConfirmationListener extends BaseListener
{
public function onRegistrationSuccess(FormEvent $event)
{
$url = $this->router->generate('fos_user_registration_check_email');
$event->setResponse(new RedirectResponse($url));
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,EventListener似乎不是可扩展的,就像控制器或表单一样.(万一你想知道:当然我的捆绑是FOSUserBundle的孩子.)
所以我想避免直接在vendor文件夹中编辑这两行(因为这样做非常糟糕!).那么我摆脱这场灾难的方法是什么?
我根据文档安装了SonataUserBundle,一切正常.除了我不能添加自定义验证规则.
我的理解是新规则应该添加到新的验证组,然后更新config.yml以告诉SonataUserBundle(或FosUserBundle)将新规则添加到验证序列.
我已经以各种方式尝试了这一点,但新规则似乎根本没有被接受......
这是我正在使用的配置......
(为了这个例子,我只是尝试将NotNull约束添加到一个新的foo字段.实际上我希望看到这么多工作,然后添加更多的验证规则.)
我已经添加了foo字段,Application\Sonata\UserBundle\Resources\config\doctrine\User.orm.xml并且一切正常,将foo字段添加到User.php类中.
# in Application\Sonata\UserBundle\Resources\config\doctrine\User.orm.xml
...
<field name="foo" type="string" length="100" nullable="true" />
...
Run Code Online (Sandbox Code Playgroud)
在User.php中,我们有getter和setter的属性:
// In Application\Sonata\UserBundle\Entity\User.php
// ...
/**
* @var string
*/
private $foo;
/**
* Set foo
*
* @param string $foo
* @return User
*/
public function setFoo($foo)
{
$this->foo = $foo;
return $this;
}
/**
* Get foo
*
* @return string
*/
public function getFoo()
{
return $this->foo;
}
// ...
Run Code Online (Sandbox Code Playgroud)
然后我将新的验证规则添加到我项目的现有validation.yml文件中:
Application\Sonata\UserBundle\Entity\User:
properties:
foo:
- …Run Code Online (Sandbox Code Playgroud) validation symfony fosuserbundle sonata-admin sonata-user-bundle
框架PHP:Symfony2.6
问题:我想在FosUserBundle中添加以下功能:"管理员可以将注册确认电子邮件重新发送给特定用户"(在网站的管理部分).
我已经构建了"用户详细信息"页面,管理员可以在该页面中查看在注册表单中输入的所有信息以及是否启用/确认了用户.如果未启用该用户,我将添加一个按钮以重新发送确认电子邮件.
另一种解决方案是在尝试使用未确认的凭据登录后显示指向该用户的链接.这是一个类似的问题(遗憾的是没有反馈,对我来说不是很清楚,只涉及第二种方法):
/sf/ask/1764341421/能否请您指出最简单,最快捷的解决方案?
fosuserbundle ×10
symfony ×10
login ×2
php ×2
ajax ×1
captcha ×1
events ×1
sonata-admin ×1
symfony-2.1 ×1
templates ×1
twig ×1
validation ×1