简体   繁体   English

typo3 7 + extbase:手动登录前端用户

[英]typo3 7 + extbase: manually login frontend user

I'm trying to manually login a frontend user (and do some other basic operations while I'm at it) in an ajaxAction and it doesn't seem to work as I intend. 我试图在ajaxAction手动登录前端用户(并在进行其他一些基本操作的同时),但它似乎并没有达到我的ajaxAction What I do when the loginform is submitted: 提交登录表单时我该怎么做:

$loginData = array(
    'username' => $username,
    'uident_text' => $password,
    'status' => 'login',
);

$GLOBALS['TSFE']->fe_user->checkPid = 0;
$info = $GLOBALS['TSFE']->fe_user->getAuthInfoArray();
$user = $GLOBALS['TSFE']->fe_user->fetchUserRecord($info['db_user'], $loginData['username']);

$GLOBALS['TSFE']->loginUser = 1;
$GLOBALS['TSFE']->fe_user->createUserSession($user);
$GLOBALS['TSFE']->fe_user->setAndSaveSessionData('user', TRUE);

return json_encode(['login' => 'true']);

when trying to print out $GLOBALS['TSFE']->fe_user before the return, the data are set and $GLOBALS['TSFE']->loginUser = 1 , so everything looks file. 尝试在返回之前打印$GLOBALS['TSFE']->fe_user ,将设置数据,并且$GLOBALS['TSFE']->loginUser = 1 ,因此所有内容看起来都是文件。 When I reload the login form afterwards though, the fe_user data are still there, but the fluid security.ifAuthenticated does not work. 不过,当我之后重新加载登录表单时,fe_user数据仍然存在,但是流畅的security.ifAuthenticated无法正常工作。

The Form view looks like this: 窗体视图如下所示:

<f:security.ifAuthenticated>
    <f:then>
        user is authenticated!
    </f:then>
    <f:else>
        <f:render partial="LoginForm.html"/>
    </f:else>
</f:security.ifAuthenticated>

Username: {user.firstName}

And it corretly outputs the Firstname of the logged in user, but also still shows the loginform. 并正确输出已登录用户的名字,但仍显示loginform。 security.ifAuthenticated always enters the ELSE. security.ifAuthenticated始终进入ELSE。 And when I looked at the viewHelper it is because $GLOBALS['TSFE']->loginUser is not set. 当我查看viewHelper时,是因为未设置$GLOBALS['TSFE']->loginUser

Any hints/ideas as to why this is happening? 关于为什么发生这种情况的任何提示/想法?

From what I experimented, you need to set a cookie for it to work. 根据我的实验,您需要设置一个cookie使其正常工作。

$reflection = new \ReflectionClass($GLOBALS['TSFE']->fe_user);
$setSessionCookieMethod = $reflection->getMethod('setSessionCookie');
$setSessionCookieMethod->setAccessible(TRUE);
$setSessionCookieMethod->invoke($GLOBALS['TSFE']->fe_user);

If I played only with $GLOBALS['TSFE']->fe_users, it didn't work even if the fe_session was created in the database. 如果我只使用$ GLOBALS ['TSFE']-> fe_users进行游戏,即使在数据库中创建了fe_session,它也不会起作用。

Turns out my inexperience with typo3 and the fe_login was at fault. 原来我对typo3的经验不足,并且fe_login有错。 What I did not know is that $GLOBALS['TSFE']->loginUser is an indicator of fe_groups. 我不知道的是$ GLOBALS ['TSFE']-> loginUser是fe_groups的指标。 The problem was, that my frontend user was not assigned to a group, the column "usergroup" was empty in the database. 问题是,我的前端用户未分配给组,数据库中的“用户组”列为空。 Therefore Typo3 sets loginUser back to false and that's why ifAuthenticated does not work. 因此,Typo3将loginUser设置为false,这就是为什么ifAuthenticated不起作用的原因。

I wrote about the solution here , but basically the only thing I did was assign the user to a group and afterwards the login as I tried it worked fine. 我在这里写了有关该解决方案的文章,但基本上,我所做的唯一一件事就是将用户分配给一个组,然后在尝试正常运行后进行登录。

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

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