简体   繁体   English

在symfony2.8中更新了用户和会话

[英]updated user and session in symfony2.8

I'm working with symfony 2.8 and i'm trying to do : 我正在使用symfony 2.8,并且正在尝试做:

If a user connect for the first time, a popup appear. 如果用户是第一次连接,则会显示一个弹出窗口。 I have a field "last_login" so what i did is to see if "last_login" is null and display the popup. 我有一个字段“ last_login”,所以我要做的是查看“ last_login”是否为空并显示弹出窗口。 But the problem is that my vue is generated after last_login is set so it doesn't work. 但是问题是我的Vue是在设置last_login之后生成的,所以它不起作用。 What i tried then is to create a listener : 然后我尝试创建一个侦听器:

class firstLoginModalListener
{
    public function preUpdate(PreUpdateEventArgs $eventArgs)
    {
        if ($eventArgs->getEntity() instanceof User) {
            if ($eventArgs->hasChangedField('lastLogin')) {
                if($eventArgs->getOldValue('lastLogin')==null){
                    //do something here 
                };
            }
        }
    }
}

But even before trying this I have to know how to send the information to my twig so I tried this (just to test the communication between the twig and the listener) : 但是即使在尝试之前,我也必须知道如何将信息发送到我的树枝,所以我尝试了这一点(只是为了测试树枝和侦听器之间的通信):

class firstLoginModalListener
{
    public function preUpdate(PreUpdateEventArgs $eventArgs)
    {
        if ($eventArgs->getEntity() instanceof User) {
            $session = new Session();
            $session->start();
            $session->set('name','test');
            $session->getFlashBag()->add('notice', 'Profile updated');
        }
    }
}

and tried several things in my twig : 在我的树枝上尝试了几件事:

 <script type="text/javascript">
        $(document).ready(function () {
            {% for flashMessage in app.session.flashbag.get('notice') %}
                console.log('{{ flashMessage }}')
            {% endfor %}
            alert('ok')
        })
    </script>

and : 和:

 <script type="text/javascript">
        $(document).ready(function () {
            console.log('{{ app.session.get('name') }}')
        })
    </script>

But both gave me empty result (I'm pretty sure the user is updated) 但是两者都给了我空洞的结果(我很确定用户已更新)

I think that your issue is that you're creating a new session instead of injecting the current one. 我认为您的问题是您正在创建一个新会话,而不是注入当前会话。 However, rather than extrapolate Doctrine events which are quite complex and listen to too many events for what you need, you should add a subscriber to the Symfony Security component's InteractiveLoginEvent . 但是,您应该推断Symfony Security组件的InteractiveLoginEvent订阅服务器,而不是推断非常复杂的Doctrine事件并听取过多的事件来满足您的需要。 Give it a priority that will catch it before the FOS\\UserBundle\\Security\\InteractiveLoginListener does. FOS\\UserBundle\\Security\\InteractiveLoginListener之前,给它一个优先级,使其可以捕获它。

app.event.listener.first_login_listener:
    class: App\Event\Listener\FirstLoginListener
    arguments: ['@session']
    tags:
        - { name: kernel.event_listener, event: security.interactive_login, method: onSecurityInteractiveLogin, priority: 10 }

Then set the session before last_login is set: 然后在设置last_login之前设置会话:

<?php

namespace App\Event\Listener;

class FirstLoginListener
{
    /**
     * @var Session
     */
    private $session;

    /**
     * @param Session $session
     */
    public function __construct(Session $session)
    {
        $this->session = $session;
    }

    /**
     * @param InteractiveLoginEvent $event
     */
    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
    {
        $user = $event->getAuthenticationToken()->getUser();
        if ($user instanceof UserInterface && $user->getLastLogin() === null) {
            $this->session->set('welcome', 'Welcome to your account');
        }
    }
}

Regarding the twig bit, try just {{ dump(app.session) }} and take it from there. 关于树枝部分,请尝试{{ dump(app.session) }}并从那里拿走。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM