2012-10-31 29 views
12

回到五月,我發佈this question。我試圖在另一個應用上再次做同樣的事情,但我還沒有找到解決這個問題的方法。我確實有更多的信息和更好的代碼,所以我希望你們能幫我解決這個問題。CakePHP 2.x認證與兩個單獨的登錄

使用案例: 醫生的辦公室有一個管理員用戶的網站。用戶通過User模型和UsersController以CakePHP的身份驗證成功登錄。

醫生已經向醫生提出了完全不同的概況和行爲。醫生需要通過example.com/physicians/login登錄。然而,這種登錄與此

authError => 'You are not authorized to access that location.'

這裏失敗是我的代碼在AppController

class AppController extends Controller { 
public $helpers = array('Form', 'Html', 'Time', 'Session', 'Js' => array('Jquery')); 

public $components = array(
    'Session', 
    'Auth' => array(
     'autoRedirect' => false, 
     'authorize' => 'Controller' 
    ) 
); 

public function beforeFilter() { 
    $this->Auth->allow('index', 'view', 'edit', 'display', 'featured', 'events', 'contact', 'signup', 'search', 'view_category', 'view_archive', 'addComment', 'schedule', 'login'); 
} 

}

這裏是我的UsersController正在工作:

class UsersController extends AppController { 

public $components = array(
    'Auth' => array(
     'authenticate' => array(
      'Form' => array(
       'userModel' => 'User', 
       'fields' => array(
        'username' => 'username', 
        'password' => 'password' 
       ) 
      ) 
     ), 
     'loginRedirect' => array('controller' => 'users', 'action' => 'admin'), 
     'logoutRedirect' => array('controller' => 'pages', 'action' => 'index'), 
     'loginAction' => array('controller' => 'users', 'action' => 'login'), 
     'sessionKey' => 'Admin' 
    ) 
); 


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

function isAuthorized() { 
    return true; 
} 

public function login() { 
    if ($this->request->is('post')) { 
     if ($this->Auth->login()) { 
      $this->redirect($this->Auth->redirect()); 
     } else { 
      $this->Session->setFlash(__('Invalid username or password, try again')); 
     } 
    } 
} 

public function logout() { 
    $this->Session->destroy(); 
    $this->redirect($this->Auth->logout()); 
} 

這是我的PhysiciansController代碼不工作:

class PhysiciansController extends AppController { 

public $components = array(
    'Auth' => array(
     'authenticate' => array(
      'Form' => array(
       'userModel' => 'Physician', 
       'fields' => array(
        'username' => 'username', 
        'password' => 'password' 
       ) 
      ) 
     ), 
     'loginRedirect' => array('controller' => 'physicians', 'action' => 'dashboard'), 
     'logoutRedirect' => array('controller' => 'pages', 'action' => 'index'), 
     'loginAction' => array('controller' => 'physicians', 'action' => 'login'), 
     'sessionKey' => 'Physician' 
    ) 
); 

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

    $this->Auth->authorize = array(
     'Actions' => array(
      'userModel' => 'Physician', 
      'actionPath' => 'physicians' 
     ) 
    ); 

    $this->Auth->allow('login', 'logout'); 
// $this->Session->write('Auth.redirect','/physicians/index'); 
} 

function isAuthorized() { 
    return true;  
} 

public function login() { 
    if ($this->request->is('post')) { 
     if ($this->Auth->login()) { 
      $this->redirect(array('controller' => 'physicians', 'action' => 'dashboard')); 
     } else { 
      $this->Session->read(); 
      debug($this->Auth); 
      $this->Session->setFlash(__('Invalid username or password, try again')); 
     } 
    } 
} 

public function logout() { 
    $this->Session->destroy(); 
    $this->redirect($this->Auth->logout()); 
} 

我真的不希望重新開始,並切換到ACL - 我不知道,對於只有兩個登錄是必要的。非常感謝幫助!

編輯:喬舒亞的回答下面是真棒和超級有用。我實現了它,但是當我嘗試通過/ phys/physican/login(前綴/控制器/操作)登錄爲Physician時,仍然收到未經授權的錯誤。管理員設置很好。這裏的調試代碼,當我嘗試登錄:

object(AuthComponent) { 
    components => array(
    (int) 0 => 'Session', 
    (int) 1 => 'RequestHandler' 
) 
authenticate => array(
    'Form' => array(
     'userModel' => 'Physician' 
    ) 
) 
authorize => false 
ajaxLogin => null 
flash => array(
    'element' => 'default', 
    'key' => 'auth', 
    'params' => array() 
) 
loginAction => array(
    'controller' => 'physicians', 
    'action' => 'phys_login' 
) 
loginRedirect => null 
logoutRedirect => '/' 
authError => 'You are not authorized to access that location.' 
allowedActions => array() 
request => object(CakeRequest) { 
    params => array(
     'prefix' => '*****', 
     'plugin' => null, 
     'controller' => 'physicians', 
     'action' => 'phys_login', 
     'named' => array(), 
     'pass' => array(), 
     'phys' => true, 
     '_Token' => array(
      'key' => 'ad1ea69c3b2c7b9e833bbda03ef18b04079b23c3', 
      'unlockedFields' => array() 
     ), 
     'isAjax' => false 
    ) 
    data => array(
     'Physician' => array(
      'password' => '*****', 
      'username' => 'deewilcox' 
     ) 
    ) 
    query => array() 
    url => 'phys/physicians/login' 
    base => '' 
    webroot => '/' 
    here => '/phys/physicians/login' 
} 
response => object(CakeResponse) { 

} 
settings => array() 

}

回答

19

確定我有一個辦法做到這一點。你知道前綴路由嗎?如果沒有,請在這裏閱讀我的答案:CakePHP/MVC Admin functions placement該答案描述瞭如何設置單個路由前綴('admin')。但你可以有任意數量的 - 就像這樣:

Configure::write('Routing.prefixes', array('admin','phys','member','user')); 
// now we have admin, phys, member and user prefix routing enabled. 

你可以做的是擁有所有醫生的方法使用「管理員」前綴的路由,所有的醫生方法使用「物理層」前綴的路由。

因此,下面是代碼我很快就破解了,所以它可能不完美,但它應該顯示的概念。這是僞代碼爲您的應用程序控制器的前過濾方法:

if (USER IS TRYING TO ACCESS AN ADMIN PREFIXED METHOD) { 
    Then use the users table for auth stuff 
} else if (USER IS TRYING TO ACCESS A PHYS PREFIXED METHOD) { 
    Then use the physicians table for auth stuff 
} else { 
    It's neither an admin method, not a physicians method. So just always allow access. Or always deny access - depending on your site 
} 

這裏是我的應用程序控制器代碼:

App::uses('Controller', 'Controller'); 

class AppController extends Controller { 

    public $components = array('Security','Cookie','Session','Auth','RequestHandler'); 
    public $helpers = array('Cache','Html','Session','Form'); 

    function beforeFilter() { 

     if ($this->request->prefix == 'admin') { 
      $this->layout = 'admin'; 
      // Specify which controller/action handles logging in: 
      AuthComponent::$sessionKey = 'Auth.Admin'; // solution from https://stackoverflow.com/questions/10538159/cakephp-auth-component-with-two-models-session 
      $this->Auth->loginAction = array('controller'=>'administrators','action'=>'login'); 
      $this->Auth->loginRedirect = array('controller'=>'some_other_controller','action'=>'index'); 
      $this->Auth->logoutRedirect = array('controller'=>'administrators','action'=>'login'); 
      $this->Auth->authenticate = array(
       'Form' => array(
        'userModel' => 'User', 
       ) 
      ); 
      $this->Auth->allow('login'); 

     } else if ($this->request->prefix == 'phys') { 
      // Specify which controller/action handles logging in: 
      AuthComponent::$sessionKey = 'Auth.Phys'; // solution from https://stackoverflow.com/questions/10538159/cakephp-auth-component-with-two-models-session 
      $this->Auth->loginAction = array('controller'=>'users','action'=>'login'); 
      $this->Auth->logoutRedirect = '/'; 

      $this->Auth->authenticate = array(
       'Form' => array(
        'userModel' => 'Physician', 
       ) 
      ); 
     } else { 
      // If we get here, it is neither a 'phys' prefixed method, not an 'admin' prefixed method. 
      // So, just allow access to everyone - or, alternatively, you could deny access - $this->Auth->deny(); 
      $this->Auth->allow();   
     } 
    } 

    public function isAuthorized($user){ 
     // You can have various extra checks in here, if needed. 
     // We'll just return true though. I'm pretty certain this method has to exist, even if it just returns true. 
     return true; 
    } 

} 

注行:

AuthComponent::$sessionKey = 'Auth.Admin'; // solution from https://stackoverflow.com/questions/10538159/cakephp-auth-component-with-two-models-session 

AuthComponent::$sessionKey = 'Auth.Phys'; // solution from https://stackoverflow.com/questions/10538159/cakephp-auth-component-with-two-models-session 

什麼這樣做可以允許一個人在一個瀏覽器中作爲醫生和管理員登錄,而不會干擾彼此的會話。你可能並不需要它在現場,但它在測試時確實很方便。

現在,在你們各自的控制器中,你需要直接的登錄/註銷方法和適當的前綴。

所以,對於管理前綴,在您的用戶控制器:

public function admin_login() { 
    if ($this->request->is('post')) { 
     if ($this->Auth->login()) { 
      return $this->redirect($this->Auth->redirect()); 
     } else { 
      $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth'); 
     } 
    } 
} 

public function admin_logout() { 
    $this->Session->setFlash('Successfully Logged Out'); 
    $this->redirect($this->Auth->logout()); 
} 

而在你的醫生控制器:

public function phys_login() { 
    if ($this->request->is('post')) { 
     if ($this->Auth->login()) { 
      return $this->redirect($this->Auth->redirect()); 
     } else { 
      $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth'); 
     } 
    } 
} 

public function phys_logout() { 
    $this->Session->setFlash('Successfully Logged Out'); 
    $this->redirect($this->Auth->logout()); 
} 

就像我說的,所有的代碼,我很快砍死在一起,所以它可能不是逐字的,但它應該顯示這個概念。如果您有任何問題,請告訴我。

+0

非常感謝這一點 - 你搖滾。我今天實施了,並且對管理員前綴和用戶控制器沒有任何問題。但是,醫師控制器仍然給我提供相同的「你沒有被授權」的錯誤。所以幾乎沒有改變。如果有幫助,我會發布上面的調試代碼。 – deewilcox

+0

那麼你會得到'無效的用戶名或密碼,再試一次',或者你沒有得到,只有'你無權訪問該位置'。也就是說,你的「if($ this-> Auth-> login()){」是否通過,然後你在登錄後重定向時只會遇到問題?或者那條線路失敗? –

+0

我收到「您沒有授權」錯誤。如果我通過'($ this-> request-> data)',它就可以工作。我使用'$ this-> Auth-> login($ this-> request-> data)'使用無效的用戶名和密碼進行測試;',如果用戶名或密碼錯誤,則登錄失敗(因爲它應該)。 – deewilcox

0

而不是

$this->Session->write('Auth.redirect','/physicians/index'); 

你應該使用

setcookie("keys", value);