在FOSUserBundle中登录后,哪种方法可以实现基于角色的重定向?
我应该使用成功处理程序并在onAuthenticationSuccess中编写重定向逻辑
或者我应该使用安全侦听器并在onSecurityInteractiveLogin中编写重定向逻辑
我使用Symfony 2.1 RC1与FOSUserbundleWindows服务器上运行PHP 5.3.13.
我已按照此处的说明进行操作, 但Doctrine不会在数据库中为从基本FOS User类继承的属性创建字段(仅限我的类中的字段).
尝试使用FOS登录表单登录会产生错误:
Unrecognized field: usernameCanonical
Run Code Online (Sandbox Code Playgroud)
我有以下Doctrine配置:
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
Run Code Online (Sandbox Code Playgroud)
FOSUserBundle配置如下所示:
fos_user:
db_driver: orm
firewall_name: main
user_class: SP\PickList\UserBundle\Entity\User
Run Code Online (Sandbox Code Playgroud)
我的用户实体:
namespace SP\PickList\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Document\User as BaseUser;
/**
* SP\PickList\UserBundle\Entity\User
*
* @ORM\Table(name="fos_user")
* @ORM\Entity
*/
class User extends BaseUser
{
/**
* @var integer $id
* …Run Code Online (Sandbox Code Playgroud) 我想在我的用户实体上设置不需要的电子邮件.怎么办?我在FOSUserBundle/config /中搜索过但我没有找到任何关于电子邮件的"必需"参数.
我想了解一些关于Symfony和"超级管理员"的事情.
php app/console fos:user:create adminuser --super-admin
Run Code Online (Sandbox Code Playgroud)
我首先想知道是什么意思(来自doc)
[...]指定--super-admin选项会将用户标记为超级管理员[...]
我想这意味着授予ROLE_SUPER_ADMIN用户,因为我没有在用户表中看到任何超级管理员字段.
其次,虽然(仍然来自文档)
超级管理员可以访问您的应用程序的任何部分
security:
role_hierarchy:
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH, ...]
Run Code Online (Sandbox Code Playgroud)
为什么我们仍然需要为它配置访问层次结构?
目前我的项目运作良好.我使用FOSUserBundle来管理我的用户.现在,我想实现OAuth,所以我使用的是FOSOAuthServerBundle.大多数开发人员推荐使用此捆绑包来实现OAuth.
我按照FOSOAuthServerBundle的文档.通常,我必须在我的security.yml中添加更多信息,但我不确切知道我要做什么......
这是我的security.yml:
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
Moodress\Bundle\UserBundle\Entity\User: sha512
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
main:
id: fos_user.user_provider.username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
oauth_token:
pattern: ^/oauth/v2/token
security: false
oauth_authorize:
pattern: ^/oauth/v2/auth
main:
pattern: ^/
fos_oauth: true
stateless: true
anonymous: true
Run Code Online (Sandbox Code Playgroud)
我想有一些信息可以添加到防火墙中吗?
我真的不知道如何使用FOSUserBundle制作FOSOAuthServerBundle.之前,只使用FOSUserBundle,我使用了登录表单和FOSUserBundle的登录检查.既然我已经完成了FOSOAuthServerBundle的所有基本配置,那我接下来要做什么?我应该使用哪种表格?哪个登录检查?令牌是由FOSOAuthServerBundle自动创建的?在文档中,它们展示了如何创建客户端...我应该在我的项目中添加此代码吗?如果是的话...在哪里?:/
我在网上找到了这篇文章:http://blog.logicexception.com/2012/04/securing-syfmony2-rest-service-wiith.html
我试图实现这个,但我不相信我们需要添加所有这些文件才能使它工作......
如果有人知道如何使用FOSUserBundle制作FOSOAuthServerBundle,那将非常有用.
我们为我们的用户使用 Symfony2 框架和 FOSUserBundle。所以我们有我们自己的 UserBundle,它继承自 FOSUserBundle。问题是:当我们使用错误的密码发送用于编辑用户的表单时,由于表单不正确,显示在标题中的 app.user.username 在不应该更改时发生了变化。然后当我们重新加载页面时, app.user.username 具有正常的用户名(因为它在数据库中没有改变)。问题是,为什么 app.user.username 会临时更改我们刚刚使用错误密码发送的表单(编辑用户)的值?
我们使用 FOSUser 的正常路由:
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
Run Code Online (Sandbox Code Playgroud)
FOSUser 的默认控制器:
public function editAction(Request $request)
{
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->get('event_dispatcher');
$event = new GetResponseUserEvent($user, $request); …Run Code Online (Sandbox Code Playgroud) 我将 Synfony2 与 FOSUserBundle 一起使用,并且我有一个自定义 userChecker,我想在其中验证用户的主机(我们有多个主机指向同一 IP)。我的问题是,在我的自定义 userChecker 中,我无法访问 REQUEST,因此无法访问请求的主机。
这是我的用户检查代码
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
//Override by Mattias
namespace BizTV\UserBundle\Controller;
//namespace Symfony\Component\Security\Core\User;
use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
use Symfony\Component\Security\Core\Exception\LockedException;
use Symfony\Component\Security\Core\Exception\DisabledException;
use Symfony\Component\Security\Core\Exception\AccountExpiredException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserChecker as OriginalUserChecker;
use Symfony\Component\HttpFoundation\Request as Request; //ADDED BY MW
/**
* …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个控制器,我可以在其中编辑用户的角色(就是这样,没有其他的东西)而且我是卡住的王者.
我创建了一个表单类型:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'roles', 'choice', [
'choices' => ['ROLE_ADMIN', 'ROLE_USER', 'ROLE_CUSTOMER'],
'expanded' => true,
'multiple' => true,
]
)
->add('send', 'submit');
}
Run Code Online (Sandbox Code Playgroud)
首先,检索角色的最佳方法是什么?有没有办法将标签与它们联系起来?
在控制器中我有这个:
/**
* User role edition
*
* @Route(
* path="/edit-roles",
* name = "backoffice_user_edit_roles",
* requirements = {
* "id_user" = "\d*",
* },
* methods = {"GET"}
* )
*
* @Security("has_role('ROLE_ADMIN')")
*
* @Template
*/
public function editRolesAction($id_user)
{
$user = $this->user_repository->findOneById($id_user);
$form = $this->form_factory->create('dirital_user_roles_form_type', $user); …Run Code Online (Sandbox Code Playgroud) 我想根据用户的角色在登录后自定义重定向.
仅供参考:我用 symfony 2.8
我创建这个类:
<?php
namespace Users\UsersBundle\Redirection;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
class AfterLoginRedirection implements AuthenticationSuccessHandlerInterface
{
protected $router;
protected $security;
/**
* AfterLoginRedirection constructor.
* @param Router $router
* @param Security $security
*/
public function __construct(Router $router, Security $security)
{
$this->router = $router;
$this->security = $security;
}
/**
* @param Request $request
* @param TokenInterface $token
* @return RedirectResponse
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
$response …Run Code Online (Sandbox Code Playgroud) 编辑:做一些挖掘,看起来我的问题源于我正在创建的表单建立在FOSUserBundle的注册表单之上.为什么这是一个问题?因为如果表单无效,它们RegistrationController会抛出一个REGISTRATION_FAILURE事件,但事件看起来并不像是一个监听器.因此,除了系统根本不处理之外,无效状态实际上没有做任何事情.它甚至没有返回表单(有错误).
我有一个相对简单的表单主题我想使用,以便在我的表单顶部的块中显示我的表单错误:
{% form_theme form _self %}
{% block form_errors %}
{% spaceless %}
{% if errors|length > 0 %}
<div class="errors pt-2 mb-2">
<ul>
{% for error in errors %}
<li>{{ error.message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endspaceless %}
{% endblock form_errors %}
Run Code Online (Sandbox Code Playgroud)
我在表单模板中引用它:
{% form_theme form 'Form/errors.html.twig' %}
Run Code Online (Sandbox Code Playgroud)
然后我尝试用一个简单的方法调用它:
{{ form_errors(form) }}
Run Code Online (Sandbox Code Playgroud)
但是,当我故意向表单输入提供不正确的数据并尝试提交时,不会出现错误div.由于存在6个验证错误,因此提交失败,但HTML源中未显示错误.
看看文档,我认为我做得对,但我显然错过了一些东西.
fosuserbundle ×10
symfony ×9
php ×6
forms ×2
doctrine-orm ×1
editing ×1
oauth ×1
security ×1
symfony-2.1 ×1
twig ×1