eli*_*lon 2 authentication components cakephp
我想摆脱Auth组件错误消息,特别是当我尝试访问非允许的操作时出现的authError消息.
为了确保,我仔细检查$this->Session->flash()
布局中的任何地方都没有呼叫.此外,设置空值不起作用,因为组件具有默认消息值.
我在AppController类中使用具有以下配置的Auth组件:
class AppController extends Controller {
var $components = array(
'Auth' => array(
'userModel' => 'WebUser',
'loginAction' => '/login',
'loginRedirect' => '/',
'logoutRedirect' => '/login',
'autoRedirect' => false,
),
'Session',
...
...
}
Run Code Online (Sandbox Code Playgroud)
对于登录和注销重定向,我设置了两个路由:
Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
Router::connect('/login', array('controller' => 'web_users', 'action' => 'login'));
Run Code Online (Sandbox Code Playgroud)
WebUser控制器中的登录操作几乎为空; 我只更改默认布局:
function login() {
$this->layout = 'login';
$this->set('title_for_layout', 'Sign in');
}
Run Code Online (Sandbox Code Playgroud)
最后,我有一个非常简单的login.ctp布局文件:
<html>
<head>
...
</head>
<body>
...
<?php echo $content_for_layout; ?>
...
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
当我访问http://example.com/login
时没有问题,没有消息,只有登录表单.但是,在Auth组件重定向到登录操作之后,我在请求任何其他操作时收到默认的authError消息.出现两个问题:
$this->Session->flash()
任何地方都没有呼叫时显示Flash消息?谢谢!
UPDATE
我提出了一个非常难看的解决方案:我创建了一个元素login_error.ctp并在Auth组件初始化中分配给flashElement属性:
class AppController extends Controller {
var $components = array(
'Auth' => array(
'flashElement' => 'login_error',
...
...
}
Run Code Online (Sandbox Code Playgroud)
在login_error.ctp中,我只是与authError默认消息进行比较:
<?php if ( $message !== 'You are not authorized to access that location.' ): ?>
<div id="flashMessage" class="message"><?php echo $message; ?></div>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)
它有效,但我讨厌它!
更新2
感谢dogmatic69的答案,我强迫自己再次检查一切.我终于找到了打电话的地方$this->Session->flash()
.这是我之前写过的一个小视角元素.它与登录/注销的东西无关,所以我没注意那个文件.
更新3
感谢SpawnCxy的回答.复制Auth组件并进行自定义修改比字符串比较更好.
在我的AppController中的CakePHP 2.1中,我使用它来覆盖我的"auth"flash消息.这是我的$组件:
public $components = array(
'Acl',
'Auth' => array(
'flash' => array(
'element' => 'info',
'key' => 'auth',
'params' => array()
),
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
),
),
'Session',
);
Run Code Online (Sandbox Code Playgroud)
我不确定你是否可以在以前版本的Cake中执行此操作.此外,你可以这样做:
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()
);
}
Run Code Online (Sandbox Code Playgroud)
这也有效!太好了!
归档时间: |
|
查看次数: |
13925 次 |
最近记录: |