避免CakePHP的Auth组件显示身份验证错误消息

时间:2011-02-09 21:03:30

标签: cakephp components authentication

我想摆脱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',
        ...
     ...
}

对于登录注销重定向,我设置了两条路线:

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时,没有问题,没有消息,只有登录表单。但是,在Auth组件重定向到 login 操作之后,我在请求任何其他操作时会收到默认的 authError 消息。出现了两个问题:

  1. 为什么Auth组件在任何地方没有$this->Session->flash()呼叫时显示Flash消息?(参见下面的更新2)
  2. 如何在 authError 属性中设置空/空值​​?
  3. 谢谢!

    更新

    我提出了一个非常难看的解决方案:我创建了一个元素login_error.ctp并在Auth组件初始化中分配给flashElement属性:

    class AppController extends Controller {
        var $components = array(
            'Auth' => array(
                'flashElement' => 'login_error',
            ...
         ...
    }
    

    在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

    感谢dogmatic69的回答,我强迫自己再次检查一切。我终于找到了对$this->Session->flash()的调用的地方。这是我之前写过的一个小视角元素。它与登录/注销的东西无关,所以我没注意那个文件。

    更新3

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

7 个答案:

答案 0 :(得分:8)

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

http://book.cakephp.org/view/1467/flash

答案 1 :(得分:8)

在我的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',
  );

我不确定你是否可以在之前版本的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()
  );
}

这也有效!尼斯!

答案 2 :(得分:1)

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

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

/app/controllers/components/auth.php

并编辑新副本中的__setDefaults函数。您可以通过更改authError中的键$defaults的值来指定您自己的身份验证错误消息。如果您将其设置为空字符串想什么都不显示。

答案 3 :(得分:1)

我刚刚在Cake 2.x中对此进行了测试,但它确实有效。将它放在Controller的beforeFilter()函数中:

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

答案 4 :(得分:0)

我有类似的情况。

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

但是,如果用户请求了任何其他页面,我希望“未授权”的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_name / dashboard,他们收到身份验证错误,因为$ this-&gt; Auth-&gt; allow('dashboard')仅在他们请求'/'时触发。

答案 5 :(得分:0)

我练习另一种比 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';
    ... ... ...
}

我希望它能比定制核心库更好地完成一项简单的工作。

答案 6 :(得分:0)

我遇到了类似的问题,但我正在使用CakePHP 3.4。我解决了编辑config/routes.php

的问题
//$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
$routes->redirect('/', ['controller' => 'Users', 'action' => 'login']);
相关问题