0

这是我的项目:

我有一个可供用户或管理员使用的网站。我创建了诸如索引、视图等方法……这些视图必须可供用户和管理员访问。我也有admin_index、admin_edit等方法。它们必须由管理员查看。

目前所有用户页面都是公开的,管理页面受登录保护。我也想要求登录用户页面。唯一的公共页面是/users/login

如何使用 AuthComponent 做到这一点?

当我输入:

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

我有一个重定向循环。

谢谢您的帮助


我仍然有问题。这是我的代码。

应用控制器

class AppController extends Controller {
    public $helpers = array('Text', 'Form', 'Html', 'Session', 'Cache');

    public $components = array(
        'Session', 
        'Auth' => array(
            'loginAction'   => array('controller' => 'users', 'action' => 'login', 'admin' => false),
            'loginRedirect' => array('controller' => 'index', 'action' => 'index', 'admin' => true),
            'logoutRedirect'=> array('controller' => 'users', 'action' => 'login', 'admin' => false),
        )
    );


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

还有我的用户控制器

class UsersController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow(array('login', 'logout'));
    }

    public function login(){

        if(isset($_SESSION['Auth']['User'])){
            return $this->redirect($this->Auth->redirect());
        }

        if($this->request->is('post')){
            if($this->Auth->login()){
                return $this->redirect($this->Auth->redirect());
            } else{
                $this->Session->setFlash('Identifiants incorrects', 'notif');
            }
        }
    }

    public function logout() {
        $this->Session->setFlash('Vous êtes déconnecté', 'notif', array('type' => 'success'));
        $this->redirect($this->Auth->logout());
    }
}

我仍然有重定向循环。我不明白为什么。

4

2 回答 2

0

这段代码也有问题:

class AppController extends Controller {
public $helpers = array('Text', 'Form', 'Html', 'Session', 'Cache');

    public $components = array(
        'Session', 
        'Auth' => array(
            'loginAction'   => array('controller' => 'users', 'action' => 'login', 'admin' => false),
            // Change controller = index to controller = users
            //'loginRedirect' => array('controller' => 'index', 'action' => 'index', 'admin' => true),
            'loginRedirect' => array('controller' => 'users', 'action' => 'index', 'admin' => true),
            'logoutRedirect'=> array('controller' => 'users', 'action' => 'login', 'admin' => false),
        )
    );


    public function beforeFilter() {
        $this->Auth->allow('login');
    }
}
于 2012-05-16T14:43:16.047 回答
0

最好的方法是使用 ACL,但是,如果您仍然只想使用 AuthComponent,我建议您照常使用 beforeFilter() 但此外,我还会在此处添加一个已登录检查,例如:

public function beforeFilter(){
    parent::beforeFilter();

    if($this->Auth->user()){
        $this->Auth->allow('*');
    }else{
        $this->Auth->allow('login');
    }
}

如果您仍想仅使用 AuthComponent 更进一步,您可以设置更多检查,例如:

    // group_id = 2 is for users who are paid subscribers for example
    // and group_id = 1 is for administrators
    if($this->Auth->user('group_id') == 1){
        $this->Auth->allow('*');
    }elseif($this->Auth->user('group_id') == 2 ){
        $this->Auth->allow('paidcontent','video','photo','etc');
    }elseif($this->Auth->user()){
        $this->Auth->allow('index','view');
    }else{
        $this->Auth->allow('login');
    }

上面的代码未经测试。如果您需要任何进一步的帮助,请告诉我

于 2012-05-16T02:19:13.340 回答