1

正如我在这里所说,我将允许预生成的用户从 SilverStripe 4 网站前端页面注销,使用默认的 from。注销,因为登录有效。

问题是,如果登录的通用用户尝试通过单击Security/logout(以及Security/logout?BackURL=home/)之类的链接注销,它将被重定向到空白页面(仅显示页眉/页脚,因为默认Page.ss已实现)。显然控制器不起作用或类似,因为 URL 只是指向我Security/logout没有以下重定向。此外,会话没有被清除,如果我返回用户仪表板页面,结果仍然登录。

因此,我尝试实现自定义身份验证器,就像我通常在 SS 3 中所做的那样,但我注意到了一些小的差异。然后,我按照官方文档建议的示例寻求帮助。

这是这种情况:

MemberAuthenticator 自定义类 (在MySite/code中)

<?php
// Definizione Namespace
namespace Greylab\Corporate\Authenticator\UtenteAuthenticator;
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator;

/**
* Classe Autenticazione Utente
*/
class UtenteAuthenticator extends MemberAuthenticator
{
/**
 * Login Paziente - Getter
 * @param string $link URL di autenteicazione utente
 * @return object Form di autenticazione utente
 */
public function getLoginHandler($link)
{
    return UtenteLoginHandler::create($link, $this);
}

/**
 * Logout Paziente - Getter
 * @param string $link URL di deautenteicazione utente
 * @return object Form di deautenteicazione utente
 */
public function getLogoutHandler($link)
{
    return UtenteLogoutHandler::create($link, $this);
}
}

MemberAuthenticator\LoginHandler 自定义类 (在MySite/code中)

<?php
// Definizione Namespace
use SilverStripe\Security\MemberAuthenticator\LoginHandler;

use SilverStripe\Core\Injector\Injector;

/**
 * Clesse Login Utente
 */
class UtenteLoginHandler extends LoginHandler
{
    /**
     * Metodo gestione Login Utente
     * Setter
     * @param array $dati Dati form login
     * @param object $form Form login
     * @return void
     */
    public function doLogin($dati, $form)
    {
        $utente = $this->checkLogin($dati);

        // Controllo Utente
        if ($utente) {
            $request = Injector::inst()->get(HTTPRequest::class);
        $session = $request->getSession();
        $cliente = $session->set('UtenteLoginHandler.MemberID', $utente->ID);
        $profiloPaziente = Member::get()->byID($session->get('UtenteLoginHandler.MemberID'));
        $datiPaziente = $session->set('UtenteLoginHandler.Data', $dati);

            // Controllo Utente
        if ($profiloCliente) {
            $this->performLogin($profiloCliente, $datiCliente);

            return $this->redirectAfterSuccessfulLogin();
        } else {
            // Se utente invalido torna al form
            return $this->redirectBack();
        }
        } else {
            // Se utente invalido torna al form
            return $this->redirectBack();
        }
    }
}

MemberAuthenticator\LogoutHandler 自定义类 (在MySite/code中)

// Definizione Namespace
use SilverStripe\Security\MemberAuthenticator\LogoutHandler;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Security\Security;
use SilverStripe\Security\IdentityStore;
use SilverStripe\Security\Member;
use SilverStripe\Control\HTTPResponse;

/**
 * Clesse Login Utente
 */
class UtenteLogoutHandler extends LogoutHandler
{
    /**
     * Metodo gestione Logout Utente
     * Setter
     * @param array $dati Dati form login
     * @param object $form Form login
     * @return HTTPResponse
     */
    public function doLogOut($utente)
    {
        // Controllo Utente
        if ($utente) {
            $request = Injector::inst()->get(HTTPRequest::class);
        $session = $request->getSession();
        $paziente = $session->get('UtenteLoginHandler.MemberID');
        $datiPaziente = $session->get('UtenteLoginHandler.Data');

        // Controllo Sessione Utente
        if ($paziente && $datiPaziente) {
            $session->clear('UtenteLoginHandler.MemberID');
            $session->clear('UtenteLoginHandler.Data');

            Security::setCurrentUser(null);

            return $this->redirectAfterLogout();
            // Tried with this approach too without success...
            /* if ($utente instanceof Member) {
            Injector::inst()->get(IdentityStore::class)->logOut($this->getRequest());
                return $this->redirectAfterLogout();
            } */
        } else {
            // Se sessione utente invalida torna al form
            return $this->redirectBack();
        }
    }
}

MemberAuthenticator 注入 (在 _MySite/ config/mysite.yml 中

SilverStripe\Core\Injector\Injector:
  SilverStripe\Security\Security:
    properties:
      Authenticators:
        UtenteAuthenticator: %$Greylab\Corporate\Authenticator\UtenteAuthenticator

有了这个实现,什么都没有改变。

任何人都可以建议我正确的方法吗?

提前感谢大家。

4

1 回答 1

2

经过深入研究,解决方案来自一位勇敢的官方 Slack 社区成员:特别感谢@kinglozzer

简单地说,SS 4 提供了一个全新的$LogoutURL默认参数来获取正确的注销 url。它包括登录成员SecurityID作为参数。旧的 SS 3Security/logout已不足以运行该过程。因此,通过使用:

{$LogoutURL}&BackURL=<url>

用户将被注销并正确重定向。

感谢任何人的帮助。

于 2018-08-20T08:56:14.193 回答