Recent Thoughts

Downloads

Contact Us

Email:
Topic:
Message:

Google Ad

Gigya Social Network CakePHP Plugin

Date: Fri, May 28th 2010, 16:01 Author: nick Views: 1734 Comments: 9 share

CakePHP Gigya Plugin

Info:
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


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:
  1. //show all connected social networks for the logged in user
  2. $details = GigyaUtil::api('getUserInfo', array(
  3.   'uid' => $this->Auth->user('id')
  4. ));


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.
  1. //app/config/gigya.php
  2. $config = array(
  3.   'Gigya' => array(
  4.     'apiKey' => 'GIGYA API KEY',
  5.     'secret' => 'GIGYA SECRET KEY'
  6.   )
  7. );


- Run the Schema into your database.
  1. 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.
  1. //some controller
  2. var $helpers = array('Gigya.Gigya');


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:
  1. //views/layouts/default.ctp
  2. <html>
  3. <head>
  4.   <?= $gigya->loader(); ?>
  5. </head>
  6. <body>
  7.   <?php
  8.   //Pseudo code, check if user is logged in or not, usually via Auth.
  9.   if(!$user_id_logged_in){
  10.     echo $gigya->login();
  11.   } else {
  12.     $html->link('Logout', array(
  13.       'plugin' => 'gigya',
  14.       'controller' => 'socialize',
  15.       'action' => 'logout'
  16.     ));
  17.   }
  18.   ?>
  19. </body>
  20. </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:
  1. //Load the widget in a container (default is a popup)
  2. <div id="login-container"></div>
  3. <?= $gigya->login(array(
  4.          'containerID' => 'login-container'
  5.        )); ?>
  6.  
  7. //Only allow facebook, twitter, or linkedin logins/connect
  8. <?= $gigya->login(array(
  9.          'enabledProviders' => 'facebook,twitter,linkedin'
  10.        )); ?>
  11.  
  12. //Allow everything EXCEPT a certain provicer
  13. <?= $gigya->login(array(
  14.          'disabledProviders' => 'myspace'
  15.        )); ?>
  16.  
  17. //Set height and width and add a style
  18. //styles: standard (default), blue, fullLogo
  19. <?= $gigya->login(array(
  20.          'height' => '300',
  21.          'width' => '500',
  22.          'buttonsStyle' => 'fullLogo'
  23.        )); ?>


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:
  1. $gigya->login(array(
  2.   'redirectURL' => array(
  3.      'controller' => 'gigyas',
  4.      'action' => 'custom_login'
  5.   )
  6. ));


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.

Gigya Flow Chart

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

  1. /**
  2. * hands the authenticated user in, if the function returns a
  3. * valid $user_id the internal handle_user function will be
  4. * shortcutted proceeding straight to linking the user_id
  5. * to Gigya
  6. *
  7. * @param authenticated social network user
  8. * @return mixed user_id or boolean false to proceed
  9. */
  10. function beforeGigyaLogin($user){
  11.   //return valid user_id or false
  12. }
  13.  
  14. /**
  15. * Preform some needed logic after a successful login
  16. *
  17. * @param authenticated social network user
  18. * @return void
  19. */
  20. function afterGigyaLogin($user){
  21.   //Do something with the user if need be.
  22. }
  23.  
  24. /**
  25. * Preform some needed logic before a logout
  26. */
  27. function beforeGigyaLogout(){
  28.   //Do something...
  29. }
  30.  
  31. /**
  32. * Allow the developer to decide how to create the user
  33. * instead of the Gigya plugin guessing what to do
  34. * by introspection on the Auth Component
  35. *
  36. * Defining this callback is preferable to the plugin
  37. * guessing how your users table is constructed.  
  38. * Although the plugin does a good job of creating a valid
  39. * user for you, its always nicer to do it yourself to be
  40. * sure there are no errors.
  41. *
  42. * @param authenticated gigya user
  43. * @return mixed user_id of created user, or false to let plugin decide.
  44. */
  45. function gigyaCreateUser($user){
  46.   //create the new user and return the created user_id;
  47. }


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:


  1. //make sure to import the GigyaUtil somewhere before making calls
  2. App::import('Lib', 'Gigya.GigyaUtil');
  3.  
  4. //get the details of all the social networks the user granted you access
  5. $details = GigyaUtil::api('getUserInfo', array(
  6.   'uid' => $user_id
  7. ));
  8.  
  9. //Set the status for your user on ALL their social networks
  10. $result = GigyaUtil::api('setStatus', array(
  11.   'uid' => $user_id,
  12.   'status' => 'Posting from the Gigya Plugin!'
  13. ));


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

Comments

05/30/2010 11:33 am

Awesome

Thanks! I cant wait to try this out.
06/18/2010 10:47 pm

$this->conf = null?

Hey Nick,
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!
06/18/2010 11:21 pm

$this->conf = null Solution

So here's the problem. I'm calling the loader() within my layout template and the login() within the view for the action. It seems when going from Layout context to View context, the configuration is lost. So I edited your login() function to recall $this->setConf(array('APIKey' => $this->apiKey)); which solved the problem. Can you think of a more polite way to do this? Or is this solution just as good as any other...

Thanks again.
07/03/2010 3:58 pm

gigyas table

I see the schema.php in the gigya config dir, but I'm not quite sure how to get the table created. Should cake be doing this for me automatically, or do I need to run some command?
07/03/2010 4:32 pm

gigyas table update

Ok, I figured out I need to do cake -plugin gigya create, however, when I do, I get an error saying that the /full/path/app/plugins/gigya/config/schema/schema.php I verified that that is the right path.
07/05/2010 5:15 am

Having trouble setting up..

Hey Nick,

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:
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?
07/05/2010 5:16 am

Having trouble setting up.. (pt2)

Hmm, I see the loader(); line gets stripped of the php-signs. Well, to calrify: I added the line as it read in the installation instructions above.
07/05/2010 7:39 pm

non-existent data source..twitter email

So I was finally able to run the schema create command, but now I get this error: ConnectionManager::getDataSource - Non-existent data source gigya.

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.
07/16/2010 2:36 am

Apologize its taken me so long to reply

My apologizes it's taken me so long to reply. I'm not sure if these are still issues but I can hope to shed some light on some of them.

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

Add Comment

Please login or register to submit a comment.