Gigya Social Network CakePHP Plugin
Date: Fri, May 28th 2010, 16:01 Author: nick Views: 1734 Comments: 9 share
Please Support:
CakePHP Gigya Plugin
Info:
- Version: 0.1
- Requirements: CakePHP 1.3+, PHP 5.1.2+
- Article: CakePHP Gigya Plugin Discussion and Examples
Get it:
Download Now
- svn co http://svn.github.com/webtechnick/CakePHP-Gigya-Plugin gigya
Over the course of the last few weeks, I've been developing a complete social network plugin for CakePHP. The plugin takes advantage of Gigya, a free social network service that allows a developer a single API to interface with multiple social networks, including Facebook, Twitter, Linkedin, Google, OpenID, Yahoo, Myspace, and many more.
Gigya CakePHP Plugin is funded and co-developed by http://flickevents.com. FlickEvents has been nice enough to allow me to open source the plugin for the CakePHP community.
The goal of this plugin is to integrate Gigya's social networking completely with CakePHP Auth component. This means, if everything is setup correctly (and the user allows it), you will be able to get detailed information about your users through all their social networks by using their Auth user_id.
Example:
On top of that, the plugin also acts as a single click registration through a user's social network. Callbacks and overwrites are written within the framework of the plugin. This allows the developer to fine tune any part of the login/logout/connect/user_creation through the use of the built in callbacks.
At the end of the day, by implementing the Gigya Plugin, you are:
- Allowing your users to link their site account to their social networks.
- Allow simple one-click registration.
- Gain detailed information about your user's friends, photos, status
- Post status updates on behalf of your users
- and much more...
To review a full least of features, please review the Gigya site. http://www.gigya.com/
Enough talk, lets get it going!
Installation is straight forward, you'll need to:
- Copy the plugin into app/plugins/gigya
- Copy gigya/config/gigya.php.default to app/config/gigya.php and fill out the details.
- Run the Schema into your database.
You'll need to signup for a free Gigya account and configure all your social networking apps to let gigya handle the connection/login/posting/etc.. callbacks. Gigya will set up all your apps for free, but it will take ~1 week to have them do it for you. I suggestion just doing it yourself. It's easy, and they've written full tutorials on how to do it, complete with screenshots.
Start Here: http://wiki.gigya.com/035_Socialize_Setup
The start to any actions the user partakes in starts with the included Gigya helper.
You will need to load the required scripts in the head of your layout and use any of the many built in Gigya login/connect/logout/etc.. widgets within your app with the helper.
Example Layout:
You can customize the look and feel of the login/connect widget with tons of options, including custom callbacks both inline (javascript) or through your CakePHP app (url redirects). For a full list of available options you can pass into $gigya->login() review:
http://wiki.gigya.com/030_API_reference/010_Client_API/020_Methods/Socialize.showLoginUI
Some useful examples:
By default, any login will redirect the user to the login action of the plugin. This action will decide how to handle the social network connection.
NOTE: You can change this behavior by passing Routes::url parseable redirectURL param.
Example:
However, this is not recommened, it is recommened to let the plugin handle the logged in user as it does some advanced introspection to determine what to do with the login (ie, link the auth_user_id to the gigya_id, create a new user, or return a valid user_id because the link has already been made).
I've created a simple flowchart to show you how a gigya login click is handled internally by the plugin.

You can alter the behavior of the flow by defining action callbacks.
It is important to note that throughout the flow you can bypass or alter the flow with specific callbacks you can define in app_controller.php
Upon a successful login, the user_id along with the gigya_uuid will be saved to the gigyas table and linked to gigya. The benefit of linking the
user_id to the gigya_uuid is you can use the GigyaApi to make gigya calls based on your local user_id instead of gigya's uuid. This is a much nicer approach to retrieving details or preforming actions on behalf of your users.
Some examples using the built in GigyaUtil:
There are tons more things you can do natively with the plugin, as its more of a framework to build your custom gigya integration. Anything you can do via the Gigya API you can do with this plugin.
I encourage you to look over the source, test cases, and of course the gigya developers area. http://wiki.gigya.com/030_API_reference is a good place to start to see what all you can do with this plugin.
As always, comments are much appreciated.
Nick
Gigya CakePHP Plugin is funded and co-developed by http://flickevents.com. FlickEvents has been nice enough to allow me to open source the plugin for the CakePHP community.
The goal
The goal of this plugin is to integrate Gigya's social networking completely with CakePHP Auth component. This means, if everything is setup correctly (and the user allows it), you will be able to get detailed information about your users through all their social networks by using their Auth user_id.
Example:
- //show all connected social networks for the logged in user
- 'uid' => $this->Auth->user('id')
- ));
On top of that, the plugin also acts as a single click registration through a user's social network. Callbacks and overwrites are written within the framework of the plugin. This allows the developer to fine tune any part of the login/logout/connect/user_creation through the use of the built in callbacks.
At the end of the day, by implementing the Gigya Plugin, you are:
- Allowing your users to link their site account to their social networks.
- Allow simple one-click registration.
- Gain detailed information about your user's friends, photos, status
- Post status updates on behalf of your users
- and much more...
To review a full least of features, please review the Gigya site. http://www.gigya.com/
Enough talk, lets get it going!
Install
Installation is straight forward, you'll need to:
- Copy the plugin into app/plugins/gigya
- Copy gigya/config/gigya.php.default to app/config/gigya.php and fill out the details.
- //app/config/gigya.php
- 'apiKey' => 'GIGYA API KEY',
- 'secret' => 'GIGYA SECRET KEY'
- )
- );
- Run the Schema into your database.
- cake schema create -plugin gigya
Setup
You'll need to signup for a free Gigya account and configure all your social networking apps to let gigya handle the connection/login/posting/etc.. callbacks. Gigya will set up all your apps for free, but it will take ~1 week to have them do it for you. I suggestion just doing it yourself. It's easy, and they've written full tutorials on how to do it, complete with screenshots.
Start Here: http://wiki.gigya.com/035_Socialize_Setup
Usage:
The start to any actions the user partakes in starts with the included Gigya helper.
- //some controller
You will need to load the required scripts in the head of your layout and use any of the many built in Gigya login/connect/logout/etc.. widgets within your app with the helper.
Example Layout:
- //views/layouts/default.ctp
- <html>
- <head>
- <?= $gigya->loader(); ?>
- </head>
- <body>
- <?php
- //Pseudo code, check if user is logged in or not, usually via Auth.
- if(!$user_id_logged_in){
- } else {
- 'plugin' => 'gigya',
- 'controller' => 'socialize',
- 'action' => 'logout'
- ));
- }
- ?>
- </body>
- </html>
You can customize the look and feel of the login/connect widget with tons of options, including custom callbacks both inline (javascript) or through your CakePHP app (url redirects). For a full list of available options you can pass into $gigya->login() review:
http://wiki.gigya.com/030_API_reference/010_Client_API/020_Methods/Socialize.showLoginUI
Some useful examples:
- //Load the widget in a container (default is a popup)
- <div id="login-container"></div>
- 'containerID' => 'login-container'
- )); ?>
- //Only allow facebook, twitter, or linkedin logins/connect
- 'enabledProviders' => 'facebook,twitter,linkedin'
- )); ?>
- //Allow everything EXCEPT a certain provicer
- 'disabledProviders' => 'myspace'
- )); ?>
- //Set height and width and add a style
- //styles: standard (default), blue, fullLogo
- 'height' => '300',
- 'width' => '500',
- 'buttonsStyle' => 'fullLogo'
- )); ?>
By default, any login will redirect the user to the login action of the plugin. This action will decide how to handle the social network connection.
NOTE: You can change this behavior by passing Routes::url parseable redirectURL param.
Example:
- 'controller' => 'gigyas',
- 'action' => 'custom_login'
- )
- ));
However, this is not recommened, it is recommened to let the plugin handle the logged in user as it does some advanced introspection to determine what to do with the login (ie, link the auth_user_id to the gigya_id, create a new user, or return a valid user_id because the link has already been made).
Login Flow
I've created a simple flowchart to show you how a gigya login click is handled internally by the plugin.

You can alter the behavior of the flow by defining action callbacks.
Available Callbacks
It is important to note that throughout the flow you can bypass or alter the flow with specific callbacks you can define in app_controller.php
- /**
- * hands the authenticated user in, if the function returns a
- * valid $user_id the internal handle_user function will be
- * shortcutted proceeding straight to linking the user_id
- * to Gigya
- *
- * @param authenticated social network user
- * @return mixed user_id or boolean false to proceed
- */
- function beforeGigyaLogin($user){
- //return valid user_id or false
- }
- /**
- * Preform some needed logic after a successful login
- *
- * @param authenticated social network user
- * @return void
- */
- function afterGigyaLogin($user){
- //Do something with the user if need be.
- }
- /**
- * Preform some needed logic before a logout
- */
- function beforeGigyaLogout(){
- //Do something...
- }
- /**
- * Allow the developer to decide how to create the user
- * instead of the Gigya plugin guessing what to do
- * by introspection on the Auth Component
- *
- * Defining this callback is preferable to the plugin
- * guessing how your users table is constructed.
- * Although the plugin does a good job of creating a valid
- * user for you, its always nicer to do it yourself to be
- * sure there are no errors.
- *
- * @param authenticated gigya user
- * @return mixed user_id of created user, or false to let plugin decide.
- */
- function gigyaCreateUser($user){
- //create the new user and return the created user_id;
- }
Linking Accounts to Gigya
Upon a successful login, the user_id along with the gigya_uuid will be saved to the gigyas table and linked to gigya. The benefit of linking the
user_id to the gigya_uuid is you can use the GigyaApi to make gigya calls based on your local user_id instead of gigya's uuid. This is a much nicer approach to retrieving details or preforming actions on behalf of your users.
Some examples using the built in GigyaUtil:
- //make sure to import the GigyaUtil somewhere before making calls
- App::import('Lib', 'Gigya.GigyaUtil');
- //get the details of all the social networks the user granted you access
- 'uid' => $user_id
- ));
- //Set the status for your user on ALL their social networks
- 'uid' => $user_id,
- 'status' => 'Posting from the Gigya Plugin!'
- ));
That's it!
There are tons more things you can do natively with the plugin, as its more of a framework to build your custom gigya integration. Anything you can do via the Gigya API you can do with this plugin.
I encourage you to look over the source, test cases, and of course the gigya developers area. http://wiki.gigya.com/030_API_reference is a good place to start to see what all you can do with this plugin.
As always, comments are much appreciated.
Nick

Awesome
$this->conf = null?
The plugin looks great...though I'm fighting a little. When I call e($gigya->login()); in my view, as it goes through generating the showLoginUI() call, I noticed that it doesn't insert the conf object into the first arg. Here's the state when I first notice the problem:
Stack:
http://localhost/hv/index.php (suspended)
login(): /hv/www/app/plugins/gigya/views/helpers/gigya.php at line 134
login(): /hv/www/app/plugins/gigya/views/helpers/gigya.php at line 108
/hv/www/app/views/users/login.ctp at line 8
_render(): /hv/www/cake/libs/view/view.php at line 723
render(): /hv/www/cake/libs/view/view.php at line 419
render(): /hv/www/cake/libs/controller/controller.php at line 911
_invoke(): /hv/www/cake/dispatcher.php at line 207
dispatch(): /hv/www/cake/dispatcher.php at line 171
variable expression at line 83
Vars:
$method (string:11) showLoginUI
$params Array [0]
$options Array [2]
redirectURL (string:25) /hv/gigya/socialize/login
enabledProviders (string:40) facebook,myspace,twitter,linkedin,google
$json_params (string:109) {"redirectURL":"\\/hv\\/gigya\\/socialize\\/login","enabledProviders":"facebook,myspace,twitter,linkedin,google"}
$script (string:149) gigya.services.socialize.showLoginUI(, {"redirectURL":"\\/hv\\/gigya\\/socialize\\/login","enabledProviders":"facebook,myspace,twitter,linkedin,google"})
Notice $script variable. Any ideas why this might be happening? I can't seem to pinpoint where the problem is in my trace. Thanks in advance!
$this->conf = null Solution
Thanks again.
gigyas table
gigyas table update
Having trouble setting up..
seems like a great plugin and just the one i need for a project i'm working on. However, I'm stumbling over a problem that I can't run the loader in default.ctp.
i've added the following:
= $gigya->loader(); ?>
This leads to the following error: Notice (8): Undefined variable: gigya [APP\views\layouts\default.ctp, line 22]
Fatal error: Call to a member function loader() on a non-object in app\views\layouts\default.ctp on line 22
I've dropped the plugin content to the right folder (i think) and added the helper 'Gigya.Gigya' to my app_controller and am working with cake 1.3.2. Do you know what could be wrong?
Having trouble setting up.. (pt2)
non-existent data source..twitter email
ConnectionManager::getDataSource() - CORE/cake/libs/model/connection_manager.php, line 102
GigyaUtil::__getDataSource() - /home/jeremy/Dropbox/workspaces/myezteam/leaguelogix_13/leaguesCakeCore/app/plugins/gigya/libs/gigya_util.php, line 172
GigyaUtil::api() - /home/jeremy/Dropbox/workspaces/myezteam/leaguelogix_13/leaguesCakeCore/app/plugins/gigya/libs/gigya_util.php, line 157
SocializeController::__linkAccount() - /home/jeremy/Dropbox/workspaces/myezteam/leaguelogix_13/leaguesCakeCore/app/plugins/gigya/controllers/socialize_controller.php, line 215
SocializeController::__handleUser() - /home/jeremy/Dropbox/workspaces/myezteam/leaguelogix_13/leaguesCakeCore/app/plugins/gigya/controllers/socialize_controller.php, line 142
SocializeController::login() - /home/jeremy/Dropbox/workspaces/myezteam/leaguelogix_13/leaguesCakeCore/app/plugins/gigya/controllers/socialize_controller.php, line 52
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
[main] - APP/webroot/index.php, line 85
Also, if a user logs in via twitter, how can I access their email? I am wondering because I want to link existing accounts via a user's email address.
Apologize its taken me so long to reply
The ConnectionManager issue sounds like you don't have the plugin in a place it can find it the datasource. The plugin should be in app/plugins/gigya and you should be running CakePHP 1.3.x as CakePHP 1.2 doesn't support loading datasources from plugins.
You also need to be sure to copy over the gigya.php into your app/config directory and configure it the way it suggests.
In regards to twitter, you should have access to all of what Gigya gives you via the getUserDetails method call. If you want you can also refer to the getRawData method and create your own parser for the various social networks.
===
Thanks for the $this->conf solution. I'll be sure to look into that issue and post a patch. Much appreciated.
If you're still having issues with the plugin, please be sure to post a comment here or create an issue on the github repo:
http://github.com/webtechnick/CakePHP-Gigya-Plugin
Thanks all for your compliments,
Nick