【问题标题】:Block access to pages in cakePHP阻止访问 cakePHP 中的页面
【发布时间】:2013-03-13 11:49:18
【问题描述】:

如何阻止对 cakePHP 中任何页面的访问。对于页面,我指的是位于页面文件夹中的实际视图。

当我删除这一行时,它可以工作,但它也会阻止用户登录。它会创建一个直接循环:

$this->Auth->allow('display');

基本上,当用户想要查看任何页面并且他们没有登录时,他们将被重定向到登录 (app/users/login) 页面。在他们登录后,他们将被引导到他们上次尝试访问的页面。

我该怎么办?

【问题讨论】:

  • 你需要允许登录页面$this->Auth->allow('login');,同时拒绝显示页面。

标签: acl cakephp-2.2


【解决方案1】:

您的情况的问题是 pagesController 显示的所有页面都是 same 操作 (display()),仅使用不同的参数(要显示的页面)。因此,您不能阻止对显示操作的访问,因为这将阻止对所有页的访问。

如果页面数量有限,那么最简单的实现方法是ControllerAuthorize。在此处阅读文档; Using ControllerAuthorize

class AppController extends Controller {
    public $components = array(
        'Auth' => array('authorize' => 'Controller'),
    );
    public function isAuthorized($user = null) {
        // Make all actions public 
        return true;
    }
}

然后,在您的页面控制器中;

class PagesController extends AppController {

    public function isAuthorized($user = null) {
        if ('display' !== $this->request->action) {
            // other actions; let he AppController handle access
            return parent::isAuthorized($user);
        }

        if (!empty($user)) {
            // Logged-in users have access to any page
            return true;
        }

        $page = empty($this->request->params['pass'][0]) ? null : $this->request->params['pass'][0];

        switch($page) {
            case 'home':
            case 'about':
            // etc
               return true;
        }

        // all other pages are 'private'
        return false;
    }
}

只是一个例子,当然,根据你的需要进行修改

【讨论】:

  • 我在两周前修复了它,但还没有时间发布我的解决方案。不过,您的解决方案是正确的,并且是我使用的确切解决方案。
  • 很高兴听到您已经修复了它,在这种情况下,您可能会发现您的解决方案“已确认”是 (a) 正确的方法。
  • 我很高兴这是正确的方法。我一直认为修改带有干净 cakePHP 安装的代码是相当冒险和“肮脏”的。但意识到有时,它是必要的。
  • 一个干净的 CakePHP 安装只是让你开始的“样板”。以此为起点,您将根据需要构建应用程序。默认PagesController::display()主要是为了方便(快速添加一些静态页面)。因为大多数网站都是数据库驱动的,所以它们会将页面的内容存储在数据库中,包括(例如)访问权限。这真的取决于你的使用。如果您的网站开始增长,这可能值得考虑。否则,您可以随时尝试“作为练习”来提高您的技能。
【解决方案2】:

使用 $this->Auth->allow('\','display'); 它允许'\'页面之后的所有内容.. 或者 如果你不允许除了显示页面你什么都不做。

【讨论】:

    猜你喜欢
    • 2015-10-23
    • 1970-01-01
    • 2017-09-02
    • 2017-08-01
    • 2015-03-09
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多