From 92f4d74d03000d093dbcc6a04690f2887880e04d Mon Sep 17 00:00:00 2001 From: Nick Baker Date: Fri, 28 May 2010 05:53:05 -0600 Subject: [PATCH] Initial commit. --- README.txt | 104 +++++++++ config/gigya.php.default | 13 ++ config/schema/schema.php | 23 ++ controllers/socialize_controller.php | 218 ++++++++++++++++++ gigya_app_controller.php | 5 + gigya_app_model.php | 5 + libs/gigya_util.php | 176 ++++++++++++++ models/datasources/gigya_source.php | 212 +++++++++++++++++ models/gigya.php | 33 +++ .../controllers/socialize_controller.test.php | 94 ++++++++ tests/cases/datasources/gigya.test.php | 37 +++ tests/cases/helpers/gigya.test.php | 62 +++++ tests/cases/libs/gigya_util.test.php | 46 ++++ tests/cases/models/gigya.test.php | 37 +++ tests/fixtures/gigya_fixture.php | 30 +++ views/helpers/gigya.php | 140 +++++++++++ views/socialize/logout.ctp | 1 + 17 files changed, 1236 insertions(+) create mode 100644 README.txt create mode 100644 config/gigya.php.default create mode 100644 config/schema/schema.php create mode 100644 controllers/socialize_controller.php create mode 100644 gigya_app_controller.php create mode 100644 gigya_app_model.php create mode 100644 libs/gigya_util.php create mode 100644 models/datasources/gigya_source.php create mode 100644 models/gigya.php create mode 100644 tests/cases/controllers/socialize_controller.test.php create mode 100644 tests/cases/datasources/gigya.test.php create mode 100644 tests/cases/helpers/gigya.test.php create mode 100644 tests/cases/libs/gigya_util.test.php create mode 100644 tests/cases/models/gigya.test.php create mode 100644 tests/fixtures/gigya_fixture.php create mode 100644 views/helpers/gigya.php create mode 100644 views/socialize/logout.ctp diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..088595b --- /dev/null +++ b/README.txt @@ -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 + loader(); ?> + 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!')); diff --git a/config/gigya.php.default b/config/gigya.php.default new file mode 100644 index 0000000..00efb86 --- /dev/null +++ b/config/gigya.php.default @@ -0,0 +1,13 @@ + array( + 'apiKey' => 'GIGYA API KEY', + 'secret' => 'GIGYA SECRET KEY' + ) +); +?> \ No newline at end of file diff --git a/config/schema/schema.php b/config/schema/schema.php new file mode 100644 index 0000000..766ffee --- /dev/null +++ b/config/schema/schema.php @@ -0,0 +1,23 @@ + 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') + ); +} +?> \ No newline at end of file diff --git a/controllers/socialize_controller.php b/controllers/socialize_controller.php new file mode 100644 index 0000000..9ff4d3a --- /dev/null +++ b/controllers/socialize_controller.php @@ -0,0 +1,218 @@ +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']) ? 'no_email@example.com' : $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)); + } +} +?> \ No newline at end of file diff --git a/gigya_app_controller.php b/gigya_app_controller.php new file mode 100644 index 0000000..1faaaf3 --- /dev/null +++ b/gigya_app_controller.php @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/gigya_app_model.php b/gigya_app_model.php new file mode 100644 index 0000000..e539029 --- /dev/null +++ b/gigya_app_model.php @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/libs/gigya_util.php b/libs/gigya_util.php new file mode 100644 index 0000000..70e09ba --- /dev/null +++ b/libs/gigya_util.php @@ -0,0 +1,176 @@ +$method($params); + } + + /** + * Get the Gigya datasource, first attempt to use the already instanciated source + * otherwise create it, cache it and return it. + * + * @return GigyaDataSource Object + * @access private + */ + static private function __getDataSource(){ + if(self::$GigyaSource){ + return self::$GigyaSource; + } + App::import('Core', 'ConnectionManager'); + self::$GigyaSource = ConnectionManager::getDataSource('gigya'); + return self::$GigyaSource; + } +} +?> \ No newline at end of file diff --git a/models/datasources/gigya_source.php b/models/datasources/gigya_source.php new file mode 100644 index 0000000..675378f --- /dev/null +++ b/models/datasources/gigya_source.php @@ -0,0 +1,212 @@ + 'Gigya.GigyaSource', + * ); + * + * $Gigya = ConnectionManager::getDataSource('gigya'); + * $result = $Gigya->getUserInfo(); + * + * @author Nick Baker + * @version 0.1 + * @license MIT + */ +App::import('Core', 'Xml'); +App::import('Core', 'HttpSocket'); +App::import('Lib', 'Gigya.GigyaUtil'); +class GigyaSource extends DataSource{ + /** + * Description of gigya + * @access public + * @var string + */ + var $description = "Gigya datasource"; + + /** + * the host to call from gigya + * @access public + * @var string + */ + var $host = "socialize-api.gigya.com"; + + /** + * The scheme to make the call to gigya by + * @access public + * @var string + */ + var $scheme = 'https'; + + /** + * The method name to call on the API + * @access public + * @var string + */ + var $path = null; + + /** + * HttpSocket object + * @access public + * @var HttpSocket + */ + var $Http = null; + + /** + * Query array + * @access public + * @var array + */ + var $query = null; + + /** + * params array complete with query and signature and apikey to pass to Gigya + * @access protected + * @var array + */ + var $_params = null; + + /** + * Requests Logs + * @access private + * @var array + */ + var $__requestLog = array(); + + /** + * All available available functions + * @access public + * @var array + */ + var $availableMethods = array( + 'disconnect', + 'getAlbums', + 'getFriendsInfo', + 'getPhotos', + 'getRawData', + 'getSessionInfo', + 'getUserInfo', + 'linkAccounts', + 'publishUserAction', + 'sendNotifications', + 'setStatus', + 'unlinkAccounts', + ); + + /** + * Track errors + * @access public + * @var array + */ + var $errors = array(); + + /** + * Append HttpSocket to Http and load the apiKey and secret configurations + * + * @param array of config options + */ + function __construct($config) { + $config = array_merge( + array( + 'apiKey' => GigyaUtil::getApiKey(), + 'secret' => GigyaUtil::getSecret(), + ), + $config + ); + + parent::__construct($config); + $this->Http = new HttpSocket(); + + if(!isset($this->config['apiKey'])){ + $this->_error('apiKey is not detected.'); + } + if(!isset($this->config['secret'])){ + $this->_error('secret is not detected.'); + } + } + + /** + * The magic method to make all REST API calls to Gigya + * + * Example: + * - $Gigya->getUserInfo(array()); + * - $Gigya->getAlbums(); + * - $Gigya->linkAccounts(); + * - $Gigya->setStatus(); + * - $Gigya->publishUserAction(); + * - etc.. + * + * @see availableMethods for a full list of available methods for you to use + * @link http://wiki.gigya.com/030_API_reference/REST_API + * @param array of options to for the method name + * @access public + * @return mixed array of result or false if method not withon availalb methods. + */ + function __call($method, $params){ + if(in_array($method, $this->availableMethods)){ + $this->path = "/socialize.$method"; + $this->query = array_shift($params); + return $this->__makeRequest(); + } + else { + $this->_error("$method is not a valid method"); + return false; + } + } + + /** + * trigger an error and set errors for user to review + */ + function _error($msg){ + $error = __($msg, true); + //trigger_error($error, E_USER_WARNING); + $this->errors[] = $error; + } + + /** + * Actually preform the request to Gigya + * + * @return mixed array of the resulting request or false if unable to contact server + * @access private + */ + function __makeRequest(){ + $this->_params = $this->__buildParams(); + $url = array('scheme' => $this->scheme, 'host' => $this->host, 'path' => $this->path); + $this->__requestLog[] = array('url' => $url, 'params' => $this->_params); + $retval = $this->Http->get($url, $this->_params); + $retval = Set::reverse(new Xml($retval)); + return $retval; + } + + /** + * Play nice with the DebugKit + * + * @param boolean sorted ignored + * @param boolean clear will clear the log if set to true (default) + */ + function getLog($sorted = false, $clear = true){ + $log = $this->__paramsLog; + if($clear){ + $this->__paramsLog = array(); + } + return array('log' => $log, 'count' => count($log), 'time' => 'Unknown'); + } + + /** + * Build the params to send to gigya + * + * @return array of query + */ + function __buildParams(){ + return array_merge( + $this->query, + array( + 'apiKey' => $this->config['apiKey'], + 'secret' => $this->config['secret'], + ) + ); + } +} +?> \ No newline at end of file diff --git a/models/gigya.php b/models/gigya.php new file mode 100644 index 0000000..1119d14 --- /dev/null +++ b/models/gigya.php @@ -0,0 +1,33 @@ +field('user_id', array( + 'OR' => array( + 'gigya_uid' => $uid, + 'user_id' => $uid + ) + )); + } +} +?> \ No newline at end of file diff --git a/tests/cases/controllers/socialize_controller.test.php b/tests/cases/controllers/socialize_controller.test.php new file mode 100644 index 0000000..3083a01 --- /dev/null +++ b/tests/cases/controllers/socialize_controller.test.php @@ -0,0 +1,94 @@ +redirectUrl = $url; + } +} + +Mock::generatePartial('TestSocializeController', 'MockTestSocializeController', array('__linkAccount')); +Mock::generate('AuthComponent'); +class SocializeControllerTestCase extends CakeTestCase { + var $fixtures = array( + 'plugin.gigya.gigya', + 'app.user' + ); + + function startTest() { + $this->Socialize = new MockTestSocializeController(); + $this->Socialize->Gigya = ClassRegistry::init('Gigya.Gigya'); + $this->Socialize->Auth = new MockAuthComponent(); + $this->count = $this->Socialize->Gigya->find('count'); + } + + function testHandleUserThatAlreadyExists(){ + $this->Socialize->setReturnValue('__linkAccount', true); + $this->Socialize->expectNever('__linkAccount', array(1, 1)); + $this->assertTrue($this->Socialize->__handleUser(array('UID' => 1))); + $this->assertTrue($this->Socialize->__handleUser(array('UID' => 'UID'))); + } + + function testHandleUserExistsButNotLinked(){ + $this->Socialize->Auth->setReturnValue('user', 4); + $this->Socialize->expectOnce('__linkAccount', array(4, 'NEWUUID')); + $this->assertEqual(4, $this->Socialize->__handleUser(array('UID' => 'NEWUUID'))); + + $result = $this->Socialize->Gigya->findById($this->Socialize->Gigya->id); + $this->assertEqual($this->count + 1, $this->Socialize->Gigya->find('count')); + $this->assertEqual(4, $result['Gigya']['user_id']); + $this->assertEqual('NEWUUID', $result['Gigya']['gigya_uid']); + } + + function testHandleUserShouldCreateUserIfNotFoundWithUsername(){ + $User = ClassRegistry::init('User'); + $users = $User->find('count'); + $this->Socialize->Auth->setReturnValue('user', null); + $this->Socialize->Auth->setReturnValue('password', 'newpassword'); + $this->Socialize->Auth->setReturnValue('getModel', $User); + $this->Socialize->Auth->fields = array( + 'username' => 'username', + 'password' => 'password' + ); + $this->Socialize->expectOnce('__linkAccount', array('6', 'NEWUUID')); + $this->assertEqual(6, $this->Socialize->__handleUser(array('UID' => 'NEWUUID', 'email' => 'email@example.com', 'nickname' => 'nickname'))); + + $this->assertEqual($users + 1, $User->find('count')); + } + + function testHandleUserShouldCreateUserIfNotFoundWithEmail(){ + $User = ClassRegistry::init('User'); + $users = $User->find('count'); + $gigyas = $this->Socialize->Gigya->find('count'); + $this->Socialize->Auth->setReturnValue('user', null); + $this->Socialize->Auth->setReturnValue('password', 'newpassword'); + $this->Socialize->Auth->setReturnValue('getModel', $User); + $this->Socialize->Auth->fields = array( + 'username' => 'email', + 'password' => 'password' + ); + $this->Socialize->expectOnce('__linkAccount', array('6', 'NEWUUID')); + $this->assertEqual(6, $this->Socialize->__handleUser(array('UID' => 'NEWUUID', 'email' => 'email@example.com', 'nickname' => 'nickname'))); + + $this->assertEqual($users + 1, $User->find('count')); + $results = $User->findById(6); + $this->assertEqual('email@example.com', $results['User']['email']); + $results = $this->Socialize->Gigya->findById($this->Socialize->Gigya->id); + + $this->assertEqual(6, $results['Gigya']['user_id']); + $this->assertEqual('NEWUUID', $results['Gigya']['gigya_uid']); + $this->assertEqual($gigyas + 1, $this->Socialize->Gigya->find('count')); + } + + function endTest() { + unset($this->Socialize); + ClassRegistry::flush(); + } + +} +?> \ No newline at end of file diff --git a/tests/cases/datasources/gigya.test.php b/tests/cases/datasources/gigya.test.php new file mode 100644 index 0000000..d30f1fd --- /dev/null +++ b/tests/cases/datasources/gigya.test.php @@ -0,0 +1,37 @@ +Gigya = new GigyaSource(array()); + $this->Gigya->Http = new MockHttpSocket(); + } + + function testCallGetUserInfo(){ + $this->Gigya->Http->expectOnce('request'); + $result = $this->Gigya->getUserInfo(array('uid' => 1)); + + $this->assertTrue(!empty($this->Gigya->__requestLog[0]['params']['apiKey'])); + $this->assertTrue(!empty($this->Gigya->__requestLog[0]['params']['secret'])); + $this->assertTrue(!empty($this->Gigya->__requestLog[0]['params']['uid'])); + $this->assertEqual('/socialize.getUserInfo', $this->Gigya->__requestLog[0]['url']['path']); + } + + function testCallUnavailableMethod(){ + $this->Gigya->Http->expectNever('request'); + $result = $this->Gigya->getBogusRequest(array('uid' => 1)); + + $this->assertTrue(empty($this->Gigya->__requestLog)); + $this->assertEqual('getBogusRequest is not a valid method', $this->Gigya->errors[0]); + $this->assertFalse($result); + } + + function endTest(){ + unset($this->Gigya); + } +} +?> \ No newline at end of file diff --git a/tests/cases/helpers/gigya.test.php b/tests/cases/helpers/gigya.test.php new file mode 100644 index 0000000..8e6c11d --- /dev/null +++ b/tests/cases/helpers/gigya.test.php @@ -0,0 +1,62 @@ +Gigya = new GigyaHelper(); + $this->Gigya->Html = new HtmlHelper(); + $this->Gigya->Js = new JsHelper(); + $this->Gigya->Js->JqueryEngine = new JqueryEngineHelper(); + $this->apiKey = GigyaUtil::getApiKey(); + $this->secret = GigyaUtil::getSecret(); + } + + function testSetConf(){ + $this->Gigya->setConf(array('key' => 'value')); + $this->assertEqual('{"key":"value"}', $this->Gigya->conf); + } + + function testLoader(){ + $result = $this->Gigya->loader(); + $this->assertEqual('{"APIKey":"'.$this->apiKey.'"}', $this->Gigya->conf); + $this->assertEqual('', $result); + } + + function testLogin(){ + $this->Gigya->setConf(array('key' => 'value')); + + $result = $this->Gigya->login(); + $expected = ''; + $this->assertEqual($expected, $result); + + $result = $this->Gigya->login(array('redirectURL' => array('controller' => 'pages', 'action' => 'home'))); + $expected = ''; + $this->assertEqual($expected, $result); + } + + function testCallBuffer(){ + $this->Gigya->setConf(array('key' => 'value')); + $result = $this->Gigya->connect(array('buffer' => true)); + $expected = 'gigya.services.socialize.connect({"key":"value"}, {"buffer":true})'; + $this->assertEqual($expected, $result); + } + + function endTest() { + unset($this->Gigya); + ClassRegistry::flush(); + } + +} +?> \ No newline at end of file diff --git a/tests/cases/libs/gigya_util.test.php b/tests/cases/libs/gigya_util.test.php new file mode 100644 index 0000000..88ceafa --- /dev/null +++ b/tests/cases/libs/gigya_util.test.php @@ -0,0 +1,46 @@ +GigyaUtil = new MockGigyaUtil(); + } + + function testGenerateSignature(){ + $result = $this->GigyaUtil->generateSignature('2010-08-14 11:15:55', '_UID'); + $this->assertEqual('nElVKN8pA3mUDfF9N0j3DWCfSSQ=', $result); + } + + function testVersion(){ + $result = $this->GigyaUtil->version(); + $this->assertTrue(!empty($result)); + } + + function testDescription(){ + $result = $this->GigyaUtil->description(); + $this->assertTrue(!empty($result)); + } + + function testAuthor(){ + $result = $this->GigyaUtil->author(); + $this->assertTrue(!empty($result)); + } + + function testGetApiKey(){ + $result = $this->GigyaUtil->getApiKey(); + $this->assertTrue(!empty($result)); + } + + function testGetSecret(){ + $result = $this->GigyaUtil->getSecret(); + $this->assertTrue(!empty($result)); + } + + function endTest(){ + unset($this->GigyaUtil); + } +} +?> \ No newline at end of file diff --git a/tests/cases/models/gigya.test.php b/tests/cases/models/gigya.test.php new file mode 100644 index 0000000..3b4bf70 --- /dev/null +++ b/tests/cases/models/gigya.test.php @@ -0,0 +1,37 @@ +Gigya =& ClassRegistry::init('Gigya'); + } + + function testFindUserIdByUid(){ + $result = $this->Gigya->findUserIdByUid('UID'); + $this->assertEqual(1, $result); + + $result = $this->Gigya->findUserIdByUid(1); + $this->assertEqual(1, $result); + + $result = $this->Gigya->findUserIdByUid('UIDID'); + $this->assertEqual(2, $result); + + $result = $this->Gigya->findUserIdByUid(2); + $this->assertEqual(2, $result); + + $result = $this->Gigya->findUserIdByUid('BOGUSIGNOREME'); + $this->assertEqual(null, $result); + } + + function endTest() { + unset($this->Gigya); + ClassRegistry::flush(); + } + +} +?> \ No newline at end of file diff --git a/tests/fixtures/gigya_fixture.php b/tests/fixtures/gigya_fixture.php new file mode 100644 index 0000000..38b662e --- /dev/null +++ b/tests/fixtures/gigya_fixture.php @@ -0,0 +1,30 @@ + 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') + ); + + var $records = array( + array( + 'id' => 1, + 'gigya_uid' => 'UID', + 'user_id' => '1', + 'created' => '2010-05-07 15:07:04' + ), + array( + 'id' => 2, + 'gigya_uid' => 'UIDID', + 'user_id' => '2', + 'created' => '2010-05-07 15:07:04' + ), + ); +} +?> \ No newline at end of file diff --git a/views/helpers/gigya.php b/views/helpers/gigya.php new file mode 100644 index 0000000..43615be --- /dev/null +++ b/views/helpers/gigya.php @@ -0,0 +1,140 @@ +library = $settings[0]; + } + elseif(is_string($settings)){ + $this->library = $settings; + } + + $this->apiKey = GigyaUtil::getApiKey(); + } + + /** + * Set the configuration params for every Gigya call. + * @param array of options to parse into a json object + * @return void + */ + function setConf($options = array()){ + $this->conf = $this->Js->object($options); + } + + /** + * Setup the loader to be put into the head of the layout or anywhere you want to use gigya + * @return scriptblock. + */ + function loader(){ + $this->setConf(array('APIKey' => $this->apiKey)); + return $this->Html->script($this->__loader . $this->apiKey); + } + + /** + * + * @link http://wiki.gigya.com/030_API_reference/010_Client_API/020_Methods/Socialize.showLoginUI + * @param array of options + * @return scriptBlock + */ + function login($options = array()){ + if(isset($options['redirectURL'])){ + $options['redirectURL'] = Router::url($options['redirectURL']); + } + + $options = array_merge( + array( + 'redirectURL' => Router::url(array('plugin' => 'gigya', 'controller' => 'socialize', 'action' => 'login')), + 'enabledProviders' => 'facebook,myspace,twitter,linkedin,google', + ), + $options + ); + return $this->showLoginUI($options); + } + + /** + * The magic method to make all javascript API calls to gigya + * + * Example: + * - $gigya->getUserInfo(array()); + * - $gigya->connect(); + * - $gigya->login(); + * - $gigya->logout(); + * - $gigya->notifyLogin(); + * - etc.. + * + * @link http://wiki.gigya.com/030_API_reference/010_Client_API + * @param array of options to for the method name + * - if option['buffer'] is set to true, returned will be the string instead of the full scriptBlock + * @access public + * @return scriptBlock of call or text of call to make + */ + function __call($method, $params){ + $options = array_shift($params); + $json_params = is_array($options) ? $this->Js->object($options) : "{}"; + + $script = "$this->__host.$method($this->conf, $json_params)"; + + if(is_array($options) && isset($options['buffer']) && $options['buffer']){ + return $script; + } + return $this->Html->scriptBlock($script); + } +} +?> \ No newline at end of file diff --git a/views/socialize/logout.ctp b/views/socialize/logout.ctp new file mode 100644 index 0000000..cb71dc2 --- /dev/null +++ b/views/socialize/logout.ctp @@ -0,0 +1 @@ +Gigya->logout(); ?> \ No newline at end of file