我正在使用cakephp 2x。我無法根據他們的角色重定向登錄用戶。我使用兩個角色管理員和collegesupervisor。我想如果管理員登錄他重定向到用戶控制器,索引頁面,如果collegesupervisor登錄他重定向到collegeprofiles控制器,添加信息頁。它可以重定向不同的用戶的基礎上,而不使用cakephp Acl組件??在此先感謝。 。這是我的AppController和UserController的代碼....在登錄時,根據用戶的角色將兩個不同的用戶重定向到兩個不同的控制器頁面
//AppController
<?php
/**
* Application level Controller
*
* This file is application-wide controller file. You can put all
* application-wide controller-related methods here.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package app.Controller
* @since CakePHP(tm) v 0.2.9
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Controller', 'Controller');
/**
* Application Controller
*
* Add your application-wide methods in the class below, your controllers
* will inherit them.
*
* @package app.Controller
* @link http://book.cakephp.org/2.0/en/controllers.html#the- app-controller
*/
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'index'),
'authError' => 'You do not have the authority to view this page.',
'loginError' => 'Invalid Username or Password entered, please try again.',
'authorize' => array('Controller'),
));
public function isAuthorized($user) {
// Here is where we should verify the role and give access based on role
return true;
}
// only allow the login controllers only
public function beforeFilter() {
parent::beforeFilter();
$this->layout = 'bootstrap';
$this->Auth->allow("login","logout");
$this->set('logged_in', $this->Auth->loggedIn());
$this->set('current_user', $this->Auth->user());
$wr=$this->webroot;
//$this->set('authUser', $this->Auth->user());
$user1 = $this->Session->read("Auth.User");
$user=$user1['username'];
//pr($user);
$this->set(compact('user','wr'));
$this->set('admin', $this->_isAdmin());
}
function _isAdmin()
{
$admin = FALSE;
if($this->Auth->user('role') == 'admin')
{
$admin = TRUE;
}
return $admin;
}
}
//User Controller
<?php
App::uses('AppController', 'Controller');
/**
* Users Controller
*
* @property User $User
* @property PaginatorComponent $Paginator
*/
class UsersController extends AppController {
/**
* Components
*
* @var array
*/
public $components = array('Paginator');
/**
* index method
*
* @return void
*/ public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('login','logout');
}
public function isAuthorized($user)
{
if($user['role']== 'admin')
return true;
if(in_array($this->action, array('edit', 'delete', 'add')))
{
if($user['id'] != $this->request->params['pass'][0])
{
return false;
}
}
return true;
}
public function login() {
//if already logged-in, redirect
if($this->Session->check('Auth.User')){
$this->redirect(array('controller'=>'football_results','action' => 'index2 '));
}
// if we get the post information, try to authenticate
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->Session->setFlash(__('Welcome, '. $this->Auth- >user('username')));
$this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('Invalid username or password'));
}
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
public function index() {
$this->User->recursive = 0;
$this->set('users', $this->Paginator->paginate());
}
/**
* add method
*
* @return void
*/
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'), 'default', array('class' => 'alert alert-success'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
}
}
}
}