静态页面cakephp的身份验证 [英] Auth for static page cakephp

查看:127
本文介绍了静态页面cakephp的身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的在cakephp ..
我在做一个网站,没有用户层次结构,只有管理员和公共用户。
我想禁止公共用户进入某个静态页面。当然,页面位于视图/页面中,所以其视图/ pages / adminPanel.ctp。
请注明我应该在哪里包括你将给的代码。
提前感谢



这里是我的显示功能

  function __checkLayout($ pageName)
{
// $ pageName =;

$ temp =;

switch($ pageName)
{
casehome:
$ temp =atheer
break;
case:
$ temp =atheer;
break;
caseadminpanel:
$ temp =adminview;
break;
}
return $ temp;
}


公共函数display(){
$ path = func_get_args();
// $ this-> layout ='atheer';
// $ this-> layout = Configure :: read('layout。'。$ page);
$ count = count($ path);
if(!$ count){
return $ this-> redirect('/');
}
$ page = $ subpage = $ title_for_layout = null;

if(!empty($ path [0])){
$ page = $ path [0];
}
if(!empty($ path [1])){
$ subpage = $ path [1];
}
if(!empty($ path [$ count - 1])){
$ title_for_layout = Inflector :: humanize($ path [$ count - 1]);
}
$ this-> layout = $ this-> __ checkLayout($ page);

$ this-> set(compact('page','subpage','title_for_layout'));

try {
$ this-> render(implode('/',$ path));
} catch(MissingViewException $ e){
if(Configure :: read('debug')){
throw $ e;
}
throw new NotFoundException();
}
}


解决方案

ok如果是这种情况,请参阅此允许Cakephp中的特定页面, / p>

请查看修改后的代码:

  public $ allowedPages = array('page1','page2'); //这里你只添加允许的页面

public function beforeFilter(){
$ this-> Auth-> allow('display');
}

function __checkLayout($ pageName)
{
// $ pageName =;

$ temp =;

switch($ pageName)
{
casehome:
$ temp =atheer
break;
case:
$ temp =atheer;
break;
caseadminpanel:
$ temp =adminview;
break;
}
return $ temp;
}


public function display(){
$ path = func_get_args();
// $ this-> layout ='atheer';
// $ this-> layout = Configure :: read('layout。'。$ page);
$ count = count($ path);
if(!$ count){
return $ this-> redirect('/');
}
$ page = $ subpage = $ title_for_layout = null;

if(!empty($ path [0])){
$ page = $ path [0];
}
if(!empty($ path [1])){
$ subpage = $ path [1];
}
if(!empty($ path [$ count - 1])){
$ title_for_layout = Inflector :: humanize($ path [$ count - 1]);
}
$ this-> layout = $ this-> __ checkLayout($ page);

if(!in_array($ page,$ this-> allowedPages)&&!$ this-> Auth-> login()){
return $ this- > redirect('/ login'); //这里重定向到登录页面改变路径如果路径不同
}
$ this-> set(compact('page','subpage','title_for_layout'));

try {
$ this-> render(implode('/',$ path));
} catch(MissingViewException $ e){
if(Configure :: read('debug')){
throw $ e;
}
throw new NotFoundException();
}
}

希望它有帮助


I am new in cakephp.. I am making a website , there no users hierarchy , there is only the admin and public users. I want to disallow the public users from entering a certain static page. of course the page is located in view/pages , so its view/pages/adminPanel.ctp . please specify where I should include the code you will give. Thank you in advance

here's my display function

function __checkLayout($pageName)   
            {
                //$pageName = "";

                $temp = "";

                switch ($pageName) 
                {
                    case "home":
                        $temp = "atheer";
                        break;
                    case "":
                        $temp = "atheer";
                        break;
                    case "adminpanel":
                        $temp = "adminview";
                        break;
                }
                return $temp;
            }


            public function display() {
    $path = func_get_args();
            //$this->layout='atheer';
            //$this->layout = Configure::read('layout.'.$page);
    $count = count($path);
    if (!$count) {
        return $this->redirect('/');
    }
    $page = $subpage = $title_for_layout = null;

    if (!empty($path[0])) {
        $page = $path[0];
    }
    if (!empty($path[1])) {
        $subpage = $path[1];
    }
    if (!empty($path[$count - 1])) {
        $title_for_layout = Inflector::humanize($path[$count - 1]);
    }
            $this->layout = $this->__checkLayout($page);

    $this->set(compact('page', 'subpage', 'title_for_layout'));

    try {
        $this->render(implode('/', $path));
    } catch (MissingViewException $e) {
        if (Configure::read('debug')) {
            throw $e;
        }
        throw new NotFoundException();
    }
}

解决方案

ok if that is the case look at this Allowing a Specific Page in Cakephp,

Please take a look at the modified code:

    public $allowedPages = array('page1', 'page2'); //here you add allowed pages only

public function beforeFilter() {
    $this->Auth->allow('display');
}

function __checkLayout($pageName)   
            {
                //$pageName = "";

                $temp = "";

                switch ($pageName) 
                {
                    case "home":
                        $temp = "atheer";
                        break;
                    case "":
                        $temp = "atheer";
                        break;
                    case "adminpanel":
                        $temp = "adminview";
                        break;
                }
                return $temp;
            }


            public function display() {
    $path = func_get_args();
            //$this->layout='atheer';
            //$this->layout = Configure::read('layout.'.$page);
    $count = count($path);
    if (!$count) {
        return $this->redirect('/');
    }
    $page = $subpage = $title_for_layout = null;

    if (!empty($path[0])) {
        $page = $path[0];
    }
    if (!empty($path[1])) {
        $subpage = $path[1];
    }
    if (!empty($path[$count - 1])) {
        $title_for_layout = Inflector::humanize($path[$count - 1]);
    }
            $this->layout = $this->__checkLayout($page);

        if(!in_array($page, $this->allowedPages) && !$this->Auth->login()) {
            return $this->redirect('/login'); //here redirects to login page change the path if the path is different
        }
    $this->set(compact('page', 'subpage', 'title_for_layout'));

    try {
        $this->render(implode('/', $path));
    } catch (MissingViewException $e) {
        if (Configure::read('debug')) {
            throw $e;
        }
        throw new NotFoundException();
    }
}

Hope it helps

这篇关于静态页面cakephp的身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆