避免CakePHP的身份验证组件显示身份验证错误消息
我想摆脱Auth组件错误消息,特别是当我尝试访问不允许的操作时会出现的身份验证错误消息。避免CakePHP的身份验证组件显示身份验证错误消息
只是可以肯定,我仔细检查有没有$this->Session->flash()
呼叫布局的任何地方。此外,设置空值不起作用,因为组件具有默认消息值。
我使用的验证部件与AppController的类中的下列配置:
class AppController extends Controller {
var $components = array(
'Auth' => array(
'userModel' => 'WebUser',
'loginAction' => '/login',
'loginRedirect' => '/',
'logoutRedirect' => '/login',
'autoRedirect' => false,
),
'Session',
...
...
}
对于登录和注销重定向我有安装两条路线:
Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
Router::connect('/login', array('controller' => 'web_users', 'action' => 'login'));
的登录动作内WEBUSER控制器几乎是空的;我只更改默认的布局:
function login() {
$this->layout = 'login';
$this->set('title_for_layout', 'Sign in');
}
最后,我有一个非常简单的login.ctp布局文件:
<html>
<head>
...
</head>
<body>
...
<?php echo $content_for_layout; ?>
...
</body>
</html>
当我访问http://example.com/login
是没有问题的,没有任何消息,只是登录表单。不过,我请求任何其他操作时,系统验证组件重定向到登录行动刚过得到默认authError消息。出现了两个问题:
-
为什么验证组件显示的提示信息时,有没有(见更新2下文)$this->Session->flash()
通话地方? - 我怎样才能设置在authError属性的空/空值?
谢谢!
UPDATE
我想出了一个非常丑陋的解决方案:我创建了一个元素login_error.ctp并分配给验证组件初始化的flashElement属性:
class AppController extends Controller {
var $components = array(
'Auth' => array(
'flashElement' => 'login_error',
...
...
}
在login_error.ctp我只是与验证错误默认消息:
<?php if ($message !== 'You are not authorized to access that location.'): ?>
<div id="flashMessage" class="message"><?php echo $message; ?></div>
<?php endif; ?>
它的作品,但我讨厌它!
更新2
感谢dogmatic69回答我强迫自己再次检查一切。我终于找到了致电$this->Session->flash()
的地方。这是我以前写过的一个小视图元素。它与登录/注销无关,所以我没有注意到该文件。
UPDATE 3
由于SpawnCxy answer为好。复制Auth组件并进行自定义修改比字符串比较更好。
还有另一种方式,使验证组件的详细personalized.You可以复制
/cake/libs/controller/components/auth.php
到
/app/controllers/components/auth.php
和编辑新copy.You的__setDefaults
功能可以指定通过更改中的值来更改您自己的身份验证错误消息。如果您不想显示任何内容,请将其设置为空字符串。
好的一个!比让错误倾向字符串比较更好 – elitalon 2011-02-10 10:01:17
在CakePHP 2.1在我AppController中,我使用这个覆盖我的 “身份验证” 的提示信息。 这里是我的$组件:
public $components = array(
'Acl',
'Auth' => array(
'flash' => array(
'element' => 'info',
'key' => 'auth',
'params' => array()
),
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
),
),
'Session',
);
我不确定如果你可以在蛋糕的早期版本中做到这一点。 此外,你可以这样做:
function beforeFilter() {
//Configure AuthComponent.
$this->Auth->authorize = 'actions';
$this->Auth->actionPath = 'controllers/';
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'plugin' => NULL);
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'login', 'plugin' => NULL);
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'plugin' => NULL);
$this->Auth->authError = __('You must be logged in to view this page.');
$this->Auth->loginError = __('Invalid Username or Password entered, please try again.');
$this->Auth->flash = array(
'element' => 'info',
'key' => 'auth',
'params' => array()
);
}
这也行!太好了!
我刚刚在Cake 2.x中测试了它,它工作。把它放在你的Controller的beforeFilter()
函数中:
$this->Session->delete('Message.auth');
我有类似的情况。
当用户登录时,'/'路由到控制器中的仪表板操作。但是,当用户未登录时,我希望用户能够请求mydomain.com或mydomain.com/users/login 而不是收到authError消息。
但是,如果用户请求任何其他页面,我希望显示“未授权”authError消息,因为它通常会显示。
这里是我的解决办法:在beforeFilter在AppController中
if($this->request->here == '/' && !$this->Auth->loggedIn()){
$this->Auth->allow('dashboard'); //temporarily allow the dashboard action
$this->redirect(array('controller' => 'users', 'action' => 'login')); //manually redirect to login screen
}
的路线由于工作方式,如果用户请求“/”,他们会不收到AUTH错误。但是,如果他们请求/ controller_name/dashboard,他们将收到验证错误,因为$ this-> Auth-> allow('dashboard')只会在请求'/'时触发。
我练习另一个更好的方式比Young's Answer。在AppController的请将下面的代码:
function beforeFilter(){
... ... ...
//set auth message custom id, if required.
$this->Auth->flash['key'] = 'YOUR-ID-HERE';
//set auth message custom attributes e.g. class etc., if required
$this->Auth->flash['params']=array('class'=>'YOUR-CLASS-HERE');
//set auth message custom element path, if required
$this->Auth->flash['element'] = 'YOUR-ELEMENT-PATH-HERE';
... ... ...
}
我希望它会比定制核心库更好地为一个简单的工作。
我和一个类似的问题,但我使用CakePHP 3.4。我解决了编辑问题config/routes.php
:
//$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
$routes->redirect('/', ['controller' => 'Users', 'action' => 'login']);
什么是显示的确切消息? – OldWest 2011-02-09 21:16:30
在Auth组件中设置的默认值:'您无权访问该位置。'。当然,我试图避免覆盖组件:) – elitalon 2011-02-09 21:21:38