-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nick Baker
committed
May 28, 2010
0 parents
commit 92f4d74
Showing
17 changed files
with
1,236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
Author: Nick Baker | ||
Requred: PHP 5.1.2+ | ||
License: MIT | ||
|
||
|
||
Install: | ||
======================== | ||
copy gigya/config/gigya.php.default to app/config/gigya.php and fill out the details below. | ||
|
||
app/config/gigya.php | ||
$config = array( | ||
'Gigya' => array( | ||
'apiKey' => 'GIGYA API KEY', | ||
'secret' => 'GIGYA SECRET KEY' | ||
) | ||
); | ||
|
||
Setup: | ||
======================== | ||
Signup for a free Gigya account and configure all your social network apps to let gigya | ||
handle the connection/login/posting/etc... | ||
http://www.gigya.com/ | ||
|
||
|
||
Usage: | ||
======================== | ||
Use the gigya helper to include the gigya socialize login/connect widget within your app | ||
|
||
//some controller | ||
var $helpers = array('Gigya.Gigya'); | ||
|
||
|
||
Load required scripts in the head, and use any of the built in gigya helper methods | ||
|
||
Example: | ||
======================== | ||
views/layouts/default.ctp | ||
<?= $gigya->loader(); ?> | ||
<?= $gigya->login(); ?> | ||
|
||
Customize the look and feel of the login/connect with tons of options, including custom | ||
callbacks both inline (javascript) or through your CakePHP app (url redirects). | ||
For a full list of available options look at: | ||
http://wiki.gigya.com/030_API_reference/010_Client_API/020_Methods/socialize.login | ||
|
||
Default Login Action: | ||
======================== | ||
By default any login click will be directed to the login action of the plugin. | ||
|
||
NOTE: you can change this behavior by passing in different options in the $gigya->login() function example: | ||
$gigya->login(array('redirecURL' => array('controller' => 'gigyas', 'action' => 'custom_login'))); | ||
|
||
|
||
The login action does a few things based on different senarios. | ||
|
||
1) If the user is authenticated via AuthComponent, the social network connection will | ||
be saved to the database and then linked to the Gigya with the user account. | ||
|
||
2) If the user is not authenticated via AuthComponent, the social network connection will | ||
authenticate the user using the social network decided, then attempt to create the user | ||
based on the AuthComponent settings. | ||
|
||
At anytime, the developer has access to callback functions in and around the login/connection process. | ||
All callbacks need to be defined in app_controller.php in the main app to work. | ||
|
||
|
||
Available Callbacks: | ||
======================== | ||
beforeGigyaLogin($user) //needs to return a $user_id | ||
//handles the authenticated user in, if the function returns a valid $user_id | ||
//the internal handle_user action will be shortcutted and it will proceed straight to linking the user_id | ||
//to the gigya account. | ||
|
||
afterGigyaLogin($user) | ||
//preform some action after a successful login | ||
|
||
beforeGigyaLogout() | ||
//preform some needed logic before the logout process. | ||
|
||
gigyaCreateUser($user) //needs to return a $user_id | ||
//preform the logic to actually create a new user. This lets the developer overwrite the guesswork nand | ||
//introspection the plugin takes to create a new user account. | ||
|
||
|
||
Upon a successful login, the user key will be saved and linked to their account. The benefit of linking the | ||
user_id to the gigya_uuid is you can use the GigyaApi to make gigya calls based on the user_id and the right thing | ||
will happen. | ||
|
||
|
||
Example Usage: | ||
======================== | ||
//after a successful connect. | ||
App::import('Lib', 'Gigya.GigyaUtil'); | ||
$result = GigyaUtil::api('getUserInfo', array('uid' => $this->Auth->user('id'))); | ||
debug($result); | ||
|
||
That will output all the social networks your app is allowed to access, based on this information you can do | ||
multiple things like setStatus or getFriends, or getPhotos, etc.. | ||
|
||
Setting the status on users social networks. | ||
Using the same Static based: | ||
|
||
App::import('Lib', 'Gigya.GigyaUtil'); | ||
$result = GigyaUtil::api('setStatus', array('uid' => $this->Auth->user('id'), 'status' => 'Posting from the Gigya Plugin!')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
/** | ||
* Gigya configuration file | ||
* | ||
* save the file to app/config/gigya.php | ||
*/ | ||
$config = array( | ||
'Gigya' => array( | ||
'apiKey' => 'GIGYA API KEY', | ||
'secret' => 'GIGYA SECRET KEY' | ||
) | ||
); | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
/* SVN FILE: $Id$ */ | ||
/* App schema generated on: 2010-05-07 16:05:54 : 1273270854*/ | ||
class AppSchema extends CakeSchema { | ||
var $name = 'App'; | ||
|
||
function before($event = array()) { | ||
return true; | ||
} | ||
|
||
function after($event = array()) { | ||
} | ||
|
||
var $gigyas = array( | ||
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'), | ||
'gigya_uid' => array('type' => 'string', 'null' => false, 'default' => NULL), | ||
'user_id' => array('type' => 'string', 'null' => false, 'default' => NULL), | ||
'created' => array('type' => 'datetime', 'null' => true, 'default' => NULL), | ||
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)), | ||
'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'MyISAM') | ||
); | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
<?php | ||
/** | ||
* Socialize Controller to handle the login/logout and various other actions needed | ||
* by Gigya to integrate into CakePHP Auth. | ||
* | ||
* @author Nick Baker | ||
* @version 0.1 | ||
* @license MIT | ||
*/ | ||
App::import('Lib', 'Gigya.GigyaUtil'); | ||
class SocializeController extends GigyaAppController{ | ||
|
||
/** | ||
* Load the Gigya helper | ||
*/ | ||
var $helpers = array('Gigya.Gigya'); | ||
|
||
/** | ||
* Use the Gigya Model | ||
*/ | ||
var $uses = array('Gigya.Gigya'); | ||
|
||
/** | ||
* Before filter for controllers, allow each action. | ||
*/ | ||
function beforeFilter(){ | ||
if(isset($this->Auth)){ | ||
$this->Auth->allow('*'); | ||
} | ||
} | ||
|
||
/** | ||
* Parse the successful passed in user. | ||
* the logged in user is passed in by the url | ||
* | ||
* To give the user control over how the user is | ||
* created we provide a callback beforeGigyaLogin | ||
* the user may create in their app_controller.php | ||
* and passed into it is a the logged in user. | ||
* | ||
* If the beforeGigyaLogin function returns a user_id that | ||
* passes a simple boolean check, the internal __handleUser() | ||
* function will be bypassed going straight to __linkAccount() | ||
* that will link the gigya user to the user_id for future use | ||
*/ | ||
function login(){ | ||
$user = $this->__parseUser(); | ||
|
||
if($this->__validateUser($user)){ | ||
$user_id = $this->__runCallback('beforeGigyaLogin', $user); | ||
if(!$user_id){ | ||
$user_id = $this->__handleUser($user); | ||
} | ||
else { | ||
$this->__linkAccount($user_id, $user['UID']); | ||
} | ||
$this->__loginUser($user_id); | ||
$this->__runCallback('afterGigyaLogin', $user); | ||
} | ||
|
||
$this->redirect('/'); | ||
} | ||
|
||
/** | ||
* Logout action that will handle logout for the user | ||
* | ||
*/ | ||
function logout(){ | ||
$this->__runCallback('beforeGigyaLogout'); | ||
$this->Auth->logout(); | ||
} | ||
|
||
/** | ||
* Validate the signature returned by gigya | ||
* | ||
* @param array of logged in user | ||
* @return boolean true if validates, false if not | ||
*/ | ||
function __validateUser($user){ | ||
return (GigyaUtil::generateSignature($user['timestamp'], $user['UID']) == $user['signature']); | ||
} | ||
|
||
/** | ||
* Run the callback if it exists | ||
* @param string callback | ||
* @param mixed passed in variable (optional) | ||
* @return mixed result of the callback function | ||
*/ | ||
function __runCallback($callback, $passedIn = null){ | ||
if(is_callable(array($this, $callback))){ | ||
if($passedIn === null){ | ||
return $this->$callback(); | ||
} | ||
else { | ||
return $this->$callback($passedIn); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Parse the user out of the URL string | ||
* @return array of user | ||
*/ | ||
function __parseUser(){ | ||
return $this->params['url']; | ||
} | ||
|
||
/** | ||
* Create or Update the logged in user. Create user if need be, | ||
* link the account to the user account. | ||
* | ||
* This will first look to see if we've already made the connection | ||
* between gigya user and CakePHP user. If so, we do nothing more | ||
* and return true | ||
* | ||
* If we haven't made the link between gigya user and CakePHP user | ||
* and the user is currently logged in, we will make the connection | ||
* save it, and return true | ||
* | ||
* If we're not logged in and haven't made a previous connection | ||
* with this social network, we must assume this is a new user | ||
* as such we will create the user based on the social network | ||
* generate a password and then link the user to the account | ||
* | ||
* @param array of user | ||
* @return mixed user_id if success, false if failure | ||
*/ | ||
function __handleUser($user){ | ||
$user_id = $this->Gigya->findUserIdByUid($user['UID']); | ||
if($user_id){ | ||
//this user has logged in before, we have a user ID based on it | ||
//so we're finished. | ||
return $user_id; | ||
} | ||
|
||
$user_id = $this->Auth->user('id'); | ||
if($user_id){ | ||
//we're logged in, and this is a new social network login. | ||
//Create the link in database and then link it via Gigya Link it. | ||
$this->__createLinkBetweenUserAndGigya($user_id, $user['UID']); | ||
$this->__linkAccount($user_id, $user['UID']); | ||
return $user_id; | ||
} | ||
|
||
$user_id = $this->__runCallback('gigyaCreateUser', $user); | ||
if($user_id){ | ||
//User creation process has been handled by the developer | ||
$this->__createLinkBetweenUserAndGigya($user_id, $user['UID']); | ||
$this->__linkAccount($user_id, $user['UID']); | ||
return $user_id; | ||
} | ||
|
||
if(!$user_id){ | ||
//If we're here we need to create the user based on what we read from Auth. | ||
//Ideally, this should be handled by the developber but we'll give it our | ||
//best guess by reading the Auth component to create the user. | ||
if(strtolower($this->Auth->fields['username']) == 'email'){ | ||
$username = empty($user['email']) ? '[email protected]' : $user['email']; | ||
} | ||
else { | ||
$username = $user['nickname']; | ||
} | ||
$user_data = array( | ||
$this->Auth->fields['username'] => $username, | ||
$this->Auth->fields['password'] => $this->Auth->password(GigyaUtil::generatePassword()) | ||
); | ||
$UserModel = $this->Auth->getModel(); | ||
if($UserModel->save($user_data)){ | ||
$user_id = $UserModel->id; | ||
$this->__createLinkBetweenUserAndGigya($user_id, $user['UID']); | ||
$this->__linkAccount($user_id, $user['UID']); | ||
return $user_id; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Login the user via user_id | ||
* | ||
* @param mixed user id | ||
* @return void | ||
*/ | ||
function __loginUser($user_id){ | ||
$UserModel = $this->Auth->getModel(); | ||
$UserModel->recursive = -1; | ||
$user = $UserModel->findById($user_id); | ||
$this->Auth->login($user); | ||
} | ||
|
||
/** | ||
* Create the link between the user and Gigya on a local database level | ||
* this is useful so we limit the amount of API callbacks we make to Gigya | ||
* @param int cakephp user_id (UID or int) | ||
* @param array user with UID as | ||
*/ | ||
function __createLinkBetweenUserAndGigya($user_id, $UID){ | ||
$data = array( | ||
'gigya_uid' => $UID, | ||
'user_id' => $user_id | ||
); | ||
return $this->Gigya->save($data); | ||
} | ||
|
||
/** | ||
* Run the API to link the accounts to Gigya | ||
* | ||
* @param CakePHP user_id | ||
* @param gigya UID | ||
* @return array of result of attempt for linking. | ||
*/ | ||
function __linkAccount($user_id, $UID){ | ||
return GigyaUtil::api('linkAccounts', array('siteUID' => $user_id, 'uid' => $UID)); | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
class GigyaAppController extends AppController { | ||
|
||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
class GigyaAppModel extends AppModel { | ||
|
||
} | ||
?> |
Oops, something went wrong.