SilverStripe 4 - 注销时的空白页面
SilverStripe 4 - Blank page on logging out
正如我所说 ,我将允许预先生成的用户使用默认的 from 从 SilverStripe 4 网站前端页面注销。注销,因为登录有效。
问题是,如果已登录的普通用户尝试通过单击 Security/logout
(以及 Security/logout?BackURL=home/
)之类的 link 来注销,它会被重定向到空白页面(只有 header/footer 可见,因为实现了默认的 Page.ss
)。显然控制器不起作用或类似,因为 URL 只是将我指向 Security/logout
而没有后续重定向。此外,会话没有被清除,如果我返回用户仪表板页面,结果仍然是登录状态。
因此,我尝试实现自定义身份验证器,就像我通常在 SS 3 中所做的那样,但我注意到了一些细微差别。然后,我就跟着official doc and the suggested example求助了。
情况是这样的:
MemberAuthenticator 自定义 class
(在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 自定义 class
(在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 自定义 class
(在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
有了这个实施,没有任何改变。
谁能给我建议正确的方法?
先谢谢大家了。
经过深入研究,解决方案来自一位勇敢的官方 Slack 社区成员:特别感谢@kinglozzer。
简单地说,SS 4 提供了一个全新的$LogoutURL
默认参数来获得正确的注销url。它包括登录成员 SecurityID
作为参数。旧的 SS 3 Security/logout
已经不足以 运行 这个过程了。所以,通过使用:
{$LogoutURL}&BackURL=<url>
用户将被注销并正确重定向。
感谢任何人的帮助。
正如我所说
问题是,如果已登录的普通用户尝试通过单击 Security/logout
(以及 Security/logout?BackURL=home/
)之类的 link 来注销,它会被重定向到空白页面(只有 header/footer 可见,因为实现了默认的 Page.ss
)。显然控制器不起作用或类似,因为 URL 只是将我指向 Security/logout
而没有后续重定向。此外,会话没有被清除,如果我返回用户仪表板页面,结果仍然是登录状态。
因此,我尝试实现自定义身份验证器,就像我通常在 SS 3 中所做的那样,但我注意到了一些细微差别。然后,我就跟着official doc and the suggested example求助了。
情况是这样的:
MemberAuthenticator 自定义 class (在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 自定义 class (在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 自定义 class (在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
有了这个实施,没有任何改变。
谁能给我建议正确的方法?
先谢谢大家了。
经过深入研究,解决方案来自一位勇敢的官方 Slack 社区成员:特别感谢@kinglozzer。
简单地说,SS 4 提供了一个全新的$LogoutURL
默认参数来获得正确的注销url。它包括登录成员 SecurityID
作为参数。旧的 SS 3 Security/logout
已经不足以 运行 这个过程了。所以,通过使用:
{$LogoutURL}&BackURL=<url>
用户将被注销并正确重定向。
感谢任何人的帮助。