避免CakePHP';s Auth组件以显示身份验证错误消息

避免CakePHP';s Auth组件以显示身份验证错误消息,cakephp,components,authentication,Cakephp,Components,Authentication,我想消除Auth组件错误消息,特别是每当我尝试访问不允许的操作时出现的Auth错误消息 为了确保这一点,我再次检查布局中是否没有调用$this->Session->flash()。此外,设置空值不起作用,因为组件具有默认消息值 我正在AppController类中使用具有以下配置的Auth组件: class AppController extends Controller { var $components = array( 'Auth' => array(

我想消除Auth组件错误消息,特别是每当我尝试访问不允许的操作时出现的Auth错误消息

为了确保这一点,我再次检查布局中是否没有调用
$this->Session->flash()。此外,设置空值不起作用,因为组件具有默认消息值

我正在AppController类中使用具有以下配置的Auth组件:

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>
在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; ?>

它是有效的,但我讨厌它

更新2

多亏了教条主义的回答,我强迫自己再次检查一切。我终于找到了调用
$this->Session->flash()
的地方。这是我以前写过的一个小视图元素。它与登录/注销无关,所以我没有注意那个文件

更新3


也要感谢你。复制Auth组件并进行自定义修改比字符串比较更好。

还有另一种方法可以使Auth组件更个性化。您可以复制

/cake/libs/controller/components/auth.php

/app/controllers/components/auth.php


并在新副本中编辑
\u setDefaults
函数。您可以通过更改
$defaults
中key
authError
的值来指定自己的身份验证错误消息。如果不想显示任何内容,请将其设置为空字符串。

只需从视图/布局中删除
$this->Session->flash('auth')


在我的AppController中的CakePHP 2.1中,我使用它来覆盖我的“auth”flash消息。 以下是我的$components:

  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中测试了这一点,它起了作用。将其放入控制器的
beforeFilter()
函数:

$this->Session->delete('Message.auth');

我也有类似的情况

当用户登录时,“/”将路由到控制器中的仪表板操作。但是,当用户未登录时,我希望该用户能够请求mydomain.com或mydomain.com/users/login,而不会收到authError消息

但是,如果用户请求任何其他页面,我希望“not authorized”(未授权)authError消息能够正常显示

以下是我的解决方案:在AppController的beforeFilter中

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
}

由于路由的工作方式,如果用户请求“/”他们将不会收到身份验证错误。但是,如果他们请求/controller\u name/dashboard,他们将收到一个auth错误,因为$this->auth->allow('dashboard')只有在他们请求“/”时才会触发。

我实践了另一种比这更好的方法。在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']);

显示的确切消息是什么?身份验证组件中的默认设置:“您无权访问该位置。”。当然,我正在努力避免覆盖组件:)谢谢!我再次检查了所有内容(即使使用了
grep-R auth
),最终找到了调用。这是我以前写过的一个小元素。它与登录/注销无关,所以我没有注意到那个文件。真的!有可能标出两个答案吗?我的回答也帮了我的忙:)每个问题只有一个正确答案,不过你们可以投更多的票
//$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
$routes->redirect('/', ['controller' => 'Users', 'action' => 'login']);