我正在使用FOSuserbundle开始用户注册https://github.com/FriendsOfSymfony/FOSUserBundle
我已经注册/登录和注销了.我现在要做的是获取登录用户数据并将其呈现在我网站的每个页面上.像标题类型的东西中的"Hi username".
似乎在我的app/Resources/views/base.html.twig中嵌入一个控制器是执行此操作的最佳方式http://symfony.com/doc/current/book/templating.html#embedding-controllers
所以我编写了我的控制器来访问用户配置文件数据.我无法弄清楚如何在我的嵌入式控制器中访问FOS方法.所以从我的Acme/UserBundle/Controller/UserController.php我想这样做:
public function showAction()
{
$user = $this->container->get('security.context')->getToken()->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException(
'This user does not have access to this section.');
}
return $this->container->get('templating')
->renderResponse('FOSUserBundle:Profile:show.html.'.$this->container
->getParameter('fos_user.template.engine'), array('user' => $user));
}
Run Code Online (Sandbox Code Playgroud)
我从中获取:vendor/bundles/FOS/UserBundle/Controller/ProfileController.php
我只想将电子邮件作为登录模式,我不想拥有用户名.是否可以使用symfony2/symfony3和FOSUserbundle?
我在这里阅读http://groups.google.com/group/symfony2/browse_thread/thread/92ac92eb18b423fe
但后来我遇到了两个违反约束的问题.
问题是如果用户将电子邮件地址留空,我会遇到两个约束违规:
有没有办法禁用给定字段的验证,或者更好的方法从表单中删除字段?
我试图从命令行创建一个新用户并收到此错误:
Warning: array_search() expects parameter 2 to be array, null given
in /vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Model/User.php line 368
Run Code Online (Sandbox Code Playgroud)
当试图通过webinterface注册来创建用户时,我得到了这个:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'salt' cannot be null
Run Code Online (Sandbox Code Playgroud)
使用现有用户登录有效.还要更新配置文件并更改密码.只是创建新用户不起作用.
我在非常简单的设置中使用v 1.3.1并且还没有找到任何解决方案.
有任何想法吗?
我正在尝试使用一些User对象预先填充数据库,但是当我调用$user->setPassword('some-password');
然后保存用户对象时,字符串'some-password'直接存储在数据库中,而不是散列+ salted密码.
我的DataFixture类:
// Acme/SecurityBundle/DataFixtures/ORM/LoadUserData.php
<?php
namespace Acme\SecurityBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Acme\SecurityBundle\Entity\User;
class LoadUserData implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$userAdmin = new User();
$userAdmin->setUsername('System');
$userAdmin->setEmail('system@example.com');
$userAdmin->setPassword('test');
$manager->persist($userAdmin);
$manager->flush();
}
}
Run Code Online (Sandbox Code Playgroud)
以及相关的数据库输出:
id username email salt password
1 System system@example.com 3f92m2tqa2kg8cookg84s4sow80880g test
Run Code Online (Sandbox Code Playgroud) 我有一个例子,我试图使用Symfony2和FOSUserBundle创建一个AJAX登录.我设置我自己success_handler
和failure_handler
在form_login
我的security.yml
文件.
这是班级:
class AjaxAuthenticationListener implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
/**
* This is called when an interactive authentication attempt succeeds. This
* is called by authentication listeners inheriting from
* AbstractAuthenticationListener.
*
* @see \Symfony\Component\Security\Http\Firewall\AbstractAuthenticationListener
* @param Request $request
* @param TokenInterface $token
* @return Response the response to return
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
if ($request->isXmlHttpRequest()) {
$result = array('success' => true);
$response = new Response(json_encode($result));
$response->headers->set('Content-Type', 'application/json');
return …
Run Code Online (Sandbox Code Playgroud) 如果相当容易,在Symfony中使用基于URL的不同实体管理器/连接.使用以下路由配置
connection:
pattern: /a/{connection}
defaults: { _controller: AcmeTestBundle:User:index }
Run Code Online (Sandbox Code Playgroud)
并从以下食谱;
我的控制器看起来像这样;
class UserController extends Controller
{
public function indexAction($connection)
{
$products = $this->get('doctrine')
->getRepository('AcmeStoreBundle:Product', $connection)
->findAll()
;
..................
Run Code Online (Sandbox Code Playgroud)
我将能够从不同的em/connection /数据库中获取产品信息.
现在,如果我在路由中添加这样的内容;
login:
pattern: /a/{connection}/login
defaults: { _controller: FOSUserBundle:Security:login }
Run Code Online (Sandbox Code Playgroud)
如何轻松地使登录使用连接变量中定义的连接?
此设置假设每个数据库都有自己的用户登录信息(fos_user表).
编辑:更新的路由信息
EDIT2:
我仍然是PHP/Symfony/Doctrine的新手,所以如果我在这里完全错了,请原谅我.我尝试在FOS\UserBundle\Doctrine\UserManager中手动设置连接.以下是该类的构造函数
//
use Doctrine\Common\Persistence\ObjectManager;
//
public function __construct(EncoderFactoryInterface $encoderFactory, CanonicalizerInterface $usernameCanonicalizer, CanonicalizerInterface $emailCanonicalizer, ObjectManager $om, $class)
{
parent::__construct($encoderFactory, $usernameCanonicalizer, $emailCanonicalizer);
$this->objectManager = $om;
$this->repository = $om->getRepository($class);
$metadata = …
Run Code Online (Sandbox Code Playgroud) 我试图将当前登录的用户注入服务.我的目标是扩展一些树枝功能,根据用户偏好输出它.在这个例子中,我想使用用户特定的时区输出任何日期函数.
似乎没有任何方法可以将当前用户注入服务,这对我来说似乎很奇怪.注入安全上下文时,即使用户已登录,它也没有令牌
我正在使用FOS用户包.
services:
...
twigdate.listener.request:
class: App\AppBundle\Services\TwigDateRequestListener
arguments: [@twig, @security.context]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
<?php
namespace App\AppBundle\Services;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class TwigDateRequestListener
{
protected $twig;
function __construct(\Twig_Environment $twig, SecurityContext $context) {
$this->twig = $twig;
//$this->user = $context->get...;
var_dump($context); die;
}
public function onKernelRequest(GetResponseEvent $event) {
// $this->twig->getExtension('core')->setDateFormat($user->getProfile()->getFormat());
// $this->twig->getExtension('core')->setTimeZone($user->getProfile()->getTimezone());
}
}
output:
object(Symfony\Component\Security\Core\SecurityContext)[325]
private 'token' => null
private 'accessDecisionManager' =>
object(Symfony\Component\Security\Core\Authorization\AccessDecisionManager)[150]
private 'voters' =>
array
0 =>
object(Symfony\Component\Security\Core\Authorization\Voter\RoleHierarchyVoter)[151]
...
1 =>
object(Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter)[153] …
Run Code Online (Sandbox Code Playgroud) 我正在使用FOSUserBundle来验证我的用户.
我正在尝试获取Controller内的用户对象来注册一个行程,我应该在保存之前将用户对象添加到此Trip.
我没有找到如何做到这一点因为我在symfony doc中找到它的下一个方法:
$user = $this->container->get('security.context')->getToken()->getUser();
Run Code Online (Sandbox Code Playgroud)
将用户名呈现为字符串,但我需要整个对象.
目前,我使用此方法,但它无法正常工作.
$username = $this->container->get('security.context')->getToken()->getUser();
$em = $this->container->get('doctrine')->getEntityManager();
$user = $em->getRepository('SiteUtilisateurBundle:Utilisateur')->find($username);
Run Code Online (Sandbox Code Playgroud)
我怎样才能正确地做到这一点?
因此,在我为几个Symfony项目使用FOSUserBundle之后,突然发生了上述错误.
我已经尝试过包含模板服务(现在两次),看起来安装得很好.这是我的composer.json中的需求列表:
"require": {
"php": ">=5.5.9",
"doctrine/doctrine-bundle": "^1.6",
"doctrine/orm": "^2.5",
"friendsofsymfony/user-bundle": "^2.0",
"incenteev/composer-parameter-handler": "^2.0",
"sensio/distribution-bundle": "^5.0.19",
"sensio/framework-extra-bundle": "^5.0.0",
"symfony/monolog-bundle": "^3.1.0",
"symfony/polyfill-apcu": "^1.0",
"symfony/swiftmailer-bundle": "^2.6.4",
"symfony/symfony": "3.4.*",
"symfony/templating": "^3.4",
"twig/twig": "^1.0||^2.0"
},
Run Code Online (Sandbox Code Playgroud)
我按照惯例设置了config.yml,security.yml和routing.yml文件,并在AppKernel.php文件中包含了bundle.我还创建了User.php实体,但每次我尝试清除缓存或更新数据库时,都会收到此错误.
服务"fos_user.mailer"依赖于不存在的服务"模板化"
经过多次搜索,我看不出在哪里解决这个问题.对此的任何帮助都非常感谢,因为它以前从未发生过,我总是使用FOSUserBundle来满足我的安全需求.
我已经安装FOSUserBundle,我想自定义URL中/account/login
,/account/register
,/account/logout
而不是/login
,/register
,/logout
我知道我可以修改捆绑包的路由配置,但它似乎不是正确的方法.