将cakephp的auth和hybridauth(oauth)结合起来

问题描述:

我想我只是寻找一些关于如何在我的网站上用cakephp 2.0设置我的User + Authentication区域的指导。将cakephp的auth和hybridauth(oauth)结合起来

我有一个基本的登录和注册功能设置在我的UserController.php,其中还包含查找和显示其他用户的功​​能。

我还希望使用hybridauth插件允许用户使用他们的社交媒体/ oauth帐户登录/注册(允许数据库中的每个用户有多个社交帐户,以便同一用户可以使用他们的例如Twitter或Facebook的详细信息)。我目前只有3个提供者,所以我打算在我的用户数据库中创建6个列,使用该令牌并将其置于同一用户行中。这是在另一个控制器 - HybridauthController.php。

我设置了hybridauth以便它从帐户详细信息中创建它的'hybridauth'对象,这很好 - 但我希望能够将整个'认证'合并在一起,以便我的cakephp会话包含hybridauth会话数据(对象)创建我可以使用的通用“当前用户”数组,并根据它们是否来自oauth设置一些通用变量。

我目前没有将会话存储在数据库中,理想情况下,我希望允许所有人使用永久会话,无论他们是否使用oauth帐户。我真的不明白持久会话应该如何与hybridauth一起工作,因为在这个示例中,当用户返回第二天时如何填充$ current_user_id?不是通过cookie,当然?

http://hybridauth.sourceforge.net/userguide/HybridAuth_Sessions.html

因此,在总结我在寻找一个简单的解决方案,我所有的“会议” /“权威性” /“用户”数据组合成在用户会话中的一个简单的数组。希望这一切都合情合理!

谢谢

迟到回答,但希望它会帮助你或其他人搜索。

我很难将HybridAuth集成到已经使用Cake Sessions和Auth组件的预先存在的CakePHP(1.3)应用程序中。我最初尝试使用从HybridAuth网站下载的Cake示例,但这不适用于我。

我们预先存在的用户控制器已经有了一堆逻辑,包括一个beforeFilter方法。这就是我插入我的session_start(),而不是在HybridAuth蛋糕例子类声明之上:

class UsersController extends AppController { 
    ... 
    var $components = array('Session','Auth','Email'); 
    ... 
    public function beforeFilter(){ 
     // following two lines may not be necessary for your setup 
     $this->Auth->allow('oauth', 'oauthLogout'); 
     $excludeBeforeFilter = array('oauth', 'oauthLogout'); 
     if (! in_array($this->action, $excludeBeforeFilter)) { 
      // preexisting Auth logic -- I had to bypass it because 
      // calling `session_start` was messing up Cake's Auth 
      ... 
     } else { 
      /* THIS IS NECCESSARY FOR HYBRIDAUTH (OAUTH) */ 
      /* setting the session ID made it so users could NOT log 
       in from a 3rd party and our site */ 
      session_id($_COOKIE['CAKEPHP']); 
      session_start(); 
     } 
    } 

    ... 

    public function oauth($provider){ 
     $this->autoRender = false; 
     // include HybridAuth 
     require_once(WWW_ROOT . 'hybridauth/Hybrid/Auth.php'); 

     try { 
      // I stored my provider config in a Cake config file 
      // It's essentially a copy from the site/Cake example 
      $hybridAuth = new Hybrid_Auth(Configure::read('HAuth.config')); 
      $adapter  = $hybridAuth->authenticate($provider); 
      $userProfile = $adapter->getUserProfile(); 

      // method on the User model to find or create the 3rd party user 
      $user  = $this->User->findOrCreate($userProfile, $provider); 

      if (! $user) { 
       echo 'Unable to find/create user'; 
      } else { 
       // configure your fields as necessary 
       $this->Auth->fields = array(...); 
       $this->Auth->login($user); 
      } 
     } catch (Exception $e) { 
      echo $e->getMessage(); 
     } 
    } 
} 

此时第三方(HybridAuth)用户在验证组件访问。通过这种方式,用户只能登录一个第三方服务或预先登录(但不能同时登录)。

我也有一个问题注销。为此,我基本销毁了所有会话/ cookie以确保安全。在oauthLogout方法UsersController的:

// probably overkill/paranoia 
$this->Auth->logout(); 
$this->Session->destroy(); 
session_destroy($_COOKIE['CAKEPHP']); 
/* these two seemed to make the difference 
    especially the 4th parameter */ 
setcookie('PHPSESSID', '', 1, '/'); 
setcookie('CAKEPHP', '', 1, '/'); 

似乎超级哈克 - 对不起了点。希望它有帮助,但!