]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
added some registration code for first time users
authorCash Costello <cash.costello@gmail.com>
Fri, 16 Dec 2011 03:00:20 +0000 (22:00 -0500)
committerCash Costello <cash.costello@gmail.com>
Fri, 16 Dec 2011 03:00:20 +0000 (22:00 -0500)
actions/openid_client/login.php [new file with mode: 0644]
actions/openid_client/register.php [new file with mode: 0644]
lib/helpers.php [new file with mode: 0644]
return.php
start.php
views/default/forms/openid_client/register.php [new file with mode: 0644]
views/default/openid_client/login.php [new file with mode: 0644]
views/default/openid_client/register.php [new file with mode: 0644]

diff --git a/actions/openid_client/login.php b/actions/openid_client/login.php
new file mode 100644 (file)
index 0000000..5e7ad3a
--- /dev/null
@@ -0,0 +1,20 @@
+<?php
+/**
+ *
+ */
+
+elgg_load_library('openid_consumer');
+
+$store = new Auth_OpenID_FileStore('/tmp');
+
+$consumer = new ElggOpenIDConsumer($store);
+$consumer->setProvider('google');
+$consumer->setReturnURL(elgg_get_site_url() . 'mod/openid_client/return.php');
+
+$html = $consumer->requestAuthentication();
+if ($html) {
+       echo $html;
+       exit;
+} else {
+       register_error('oops');
+}
diff --git a/actions/openid_client/register.php b/actions/openid_client/register.php
new file mode 100644 (file)
index 0000000..e847d23
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+/**
+ * Register an OpenID user
+ */
+
+elgg_set_context('openid_client');
+
+$username = get_input('username');
+$name = get_input('name');
+$email = get_input('email');
+$openid_identifier = get_input('openid_identifier');
+
+$password = 'test';
+
+try {
+       $guid = register_user($username, $password, $name, $email, false);
+} catch (RegistrationException $e) {
+       register_error($e->getMessage());
+       forward(REFERER);
+}
+$user = get_entity($guid);
+
+$user->openid_identifier = $openid_identifier;
+elgg_set_user_validation_status($guid, true, 'openid');
+
+if (!elgg_trigger_plugin_hook('register', 'user', array('user' => $user), true)) {
+       $user->delete();
+       register_error(elgg_echo('registerbad'));
+       forward(REFERER);
+}
+
+login($user);
+system_message($message);
+forward();
diff --git a/lib/helpers.php b/lib/helpers.php
new file mode 100644 (file)
index 0000000..c715823
--- /dev/null
@@ -0,0 +1,66 @@
+<?php
+/**
+ * Helper functions for the OpenID client plugin
+ */
+
+/**
+ * Serves a page to the new user to determine account values
+ *
+ * This should only be called after validating the OpenID response.
+ *
+ * @param array $data Key value pairs extracted from the response
+ * @return bool
+ */
+function openid_client_registration_page_handler(array $data) {
+
+       if (!is_array($data)) {
+               return false;
+       }
+
+       $title = 'register';
+
+       $vars = openid_client_prepare_registration_vars($data);
+       $content = elgg_view('openid_client/register', $vars);
+
+       $body = elgg_view_layout('one_column', array('content' => $content));
+       echo elgg_view_page($title, $body);
+
+       return true;
+}
+
+/**
+ * Create the form vars for registration
+ *
+ * @param array $data
+ * @return array
+ */
+function openid_client_prepare_registration_vars(array $data) {
+       $vars = array();
+
+       $vars['openid_identifier'] = $data['openid_identifier'];
+
+       // username
+       if (isset($data['username'])) {
+               $vars['username'] = $data['username'];
+       } else if (isset($data['email'])) {
+               $vars['username'] = array_pop(explode('@', $data['email']));
+       } else {
+               $vars['username'] = null;
+       }
+
+       // is the username available
+       $vars['is_username_available'] = true;
+
+       // is the username valid
+       try {
+               $vars['is_username_valid'] = validate_username($vars['username']);
+       } catch (RegistrationException $e) {
+               $vars['is_username_valid'] = false;
+       }
+
+       // the rest
+       $vars['email'] = elgg_extract('email', $data);
+       $vars['name'] = elgg_extract('name', $data);
+
+       return $vars;
+}
index 3d949b4922b11343caf696acfdd4bed836100a5e..23751e54ad3b1ac1773364de8c9e0fd242a21578 100644 (file)
 <?php\r
-\r
 /**\r
- * Callback for return_to url redirection. The identity server will\r
- * redirect back to this handler with the results of the\r
- * authentication attempt.\r
+ * Callback for return_to url redirection.\r
  * \r
- * Note: the Elgg action system strips off the query string and is incompatible with\r
- * the JanRain OpenID library, so we need to keep this as an ordinary PHP file\r
- * for now.\r
+ * The identity server will redirect back to this handler with the results of\r
+ * the authentication attempt.\r
  * \r
+ * Note: the Janrain OpenID library is incompatible with Elgg's routing so\r
+ * this script needs to be directly accessed.\r
  */\r
 \r
-require_once(dirname(dirname(dirname(__FILE__))).'/engine/start.php');\r
-require_once(dirname(__FILE__).'/models/model.php');\r
-\r
-global $CONFIG;\r
-\r
-set_context('openid');\r
-$store = new OpenID_ElggStore();\r
-$consumer = new Auth_OpenID_Consumer($store);\r
+require_once dirname(dirname(dirname(__FILE__))).'/engine/start.php';\r
 \r
-$return_url = $CONFIG->wwwroot.'mod/openid_client/return.php';\r
+elgg_load_library('openid_consumer');\r
+elgg_load_library('openid_client');\r
 \r
-// TODO - handle passthru_url properly\r
-// $dest = $query['destination'];\r
-$response = $consumer->complete($return_url);\r
-\r
-if ($response->status == Auth_OpenID_CANCEL) {\r
-    register_error(elgg_echo("openid_client:authentication_cancelled"));\r
-} else if ($response->status != Auth_OpenID_SUCCESS) {\r
-    register_error(sprintf(elgg_echo("openid_client:authentication_failed"),$response->status,$response->message) );\r
-} else { // SUCCESS.\r
-       $openid_url = $response->getDisplayIdentifier();\r
-       \r
-    // Look for sreg data.\r
-    $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);\r
-    $sreg = $sreg_resp->contents();\r
-       if ($sreg) {\r
-        $email = trim($sreg['email']);\r
-        $fullname = trim($sreg['fullname']);\r
-        //print ($email.' '.$fullname);\r
-    }\r
-    \r
-    $entities = get_entities_from_metadata('alias', $openid_url, 'user', 'openid');\r
+// get user data from the response\r
+$store = new Auth_OpenID_FileStore('/tmp');\r
+$consumer = new ElggOpenIDConsumer($store);\r
+$url = elgg_get_site_url() . 'mod/openid_client/return.php';\r
+$consumer->setReturnURL($url);\r
+$data = $consumer->completeAuthentication();\r
+if (!$data || !$data['openid_identifier']) {\r
+       // @todo handle error\r
+}\r
 \r
-       if (!$entities || $entities[0]->active == 'no') {\r
-               if (!$entities) {\r
-                       // this account does not exist\r
-               if (!$email || !validate_email_address($email)) {\r
-               // there is a problem with the email provided by the profile exchange, so generate a form to collect it\r
-                               if ($user = openid_client_create_openid_user($openid_url,$email, $fullname, true)) {\r
-                               $details = openid_client_create_invitation('a',$openid_url,$user->getGUID(),$email,$fullname);\r
-                               $body = openid_client_generate_missing_data_form($openid_url,'',$fullname,true,$details);\r
-                               }\r
-                               $missing_data = true;\r
-                       } elseif (!$fullname) {\r
-                       // the name is missing\r
-                               $email_confirmation = openid_client_check_email_confirmation($openid_url);\r
-                               if ($email_confirmation) {\r
-                                       $prefix = 'a';\r
-                               } else {\r
-                                       $prefix = 'n';\r
-                               }\r
-                               // create the account\r
-                               if ($user = openid_client_create_openid_user($openid_url,$email, $fullname, $email_confirmation)) {\r
-                                       $details = openid_client_create_invitation($prefix,$openid_url,$user->getGUID(),$email,$fullname);\r
-                                       $body = openid_client_generate_missing_data_form($openid_url,$email,'',$email_confirmation,$details);\r
-                               }\r
-                               $missing_data = true;\r
-                       } else {\r
-                               // email address and name look good \r
-                               \r
-                               $login = false;                 \r
-                                                           \r
-                           // create a new account\r
-                                  \r
-                               $email_confirmation = openid_client_check_email_confirmation($openid_url);                                                          \r
-                                                                               \r
-                               $user = openid_client_create_openid_user($openid_url,$email, $fullname, $email_confirmation);\r
-                               $missing_data = false;\r
-                       }\r
-               } else {\r
-                       // this is an inactive account\r
-                       $user = $entities[0];\r
-                       \r
-                       // need to figure out why the account is inactive\r
-                       \r
-                       $email_confirmation = openid_client_check_email_confirmation($openid_url);\r
-                       \r
-                       if ($user->email && $user->name) {\r
-                       $missing_data = false;\r
-                       // no missing information\r
-                           if (!$email_confirmation) {\r
-                           // OK, this is weird - no email confirmation required and all the information has been supplied\r
-                           // this should not happen, so just go ahead and activate the account\r
-                           $user->active = 'yes';\r
-                           $user->save();\r
-                           }\r
-                   } else {                        \r
-                   // missing information\r
-                   $missing_data = true;\r
-                   // does this person have an existing magic code?\r
-                   if ($details = openid_client_get_invitation_by_username($user->alias)) {\r
-                           $body = openid_client_generate_missing_data_form($openid_url,$user->email,$user->name,$email_confirmation,$details);\r
-                   } else {\r
-                           // create a new magic code\r
-                           $details = openid_client_create_invitation('a',$openid_url,$user->getGUID(),$user->email,$user->name);\r
-                               $body = openid_client_generate_missing_data_form($openid_url,$user->email,$user->name,$email_confirmation,$details);\r
-                       }   \r
-                   }\r
-               }\r
-               if ($user && !$missing_data) {\r
-                               \r
-                       if ($email_confirmation) {\r
-                               $i_code = openid_client_create_invitation('a',$openid_url,$user->guid,$email,$fullname);\r
-                               openid_client_send_activate_confirmation_message($i_code);\r
-                               system_message(sprintf(elgg_echo("openid_client:activate_confirmation"), $email));\r
-                       } else {\r
-                               system_message(sprintf(elgg_echo("openid_client:created_openid_account"),$email, $fullname));\r
-                               $login = true;\r
-                       }\r
-               }\r
-                               \r
-       } else {\r
-       \r
-       $user = $entities[0];\r
-               \r
-               // account is active, check to see if this user has been banned\r
-       \r
-           if (isset($user->banned) && $user->banned == 'yes') { // this needs to change.\r
-               register_error(elgg_echo("openid_client:banned"));\r
-           } else {\r
-                   // user has not been banned\r
-                   // check to see if email address has changed\r
-                   if ($email && $email != $user->email && validate_email_address($email)) {\r
-                           // the email on the OpenID server is not the same as the email registered on this local client system\r
-                           $email_confirmation = openid_client_check_email_confirmation($openid_url);\r
-                           if ($CONFIG->openid_client_always_sync == 'yes') {\r
-                                   // this client always forces client/server data syncs\r
-                                   if ($fullname) {\r
-                                       $user->name = $fullname;\r
-                               }\r
-                                   if ($email_confirmation) {\r
-                                           // don't let this user in until the email address change is confirmed\r
-                                           $login = false;\r
-                                           $i_code = openid_client_create_invitation('c',$openid_url,$user->guid,$email,$fullname);\r
-                                           openid_client_send_change_confirmation_message($i_code);\r
-                                           system_message(sprintf(elgg_echo("openid_client:change_confirmation"), $email));\r
-                                       } else {\r
-                                               $login = true;\r
-                                               if (openid_client_get_user_by_email($email)) {\r
-                                                       register_error(elgg_echo("openid_client:email_in_use"),$email);\r
-                                               } else {\r
-                                               $user->email = $email;\r
-                                                       system_message(sprintf(elgg_echo("openid_client:email_updated"),$email));\r
-                                               }\r
-                                       }\r
-                               } else {\r
-                                       $login = true;\r
-                                       if (!$store->getNoSyncStatus($user)) {\r
-                                               // the following conditions are true:\r
-                                               // the email address has changed on the server,\r
-                                               // this client does not *require* syncing with the server,\r
-                                               // but this user has not turned off syncing\r
-                                               // therefore the user needs to be offered the chance to sync his or her data\r
-                                               $body = openid_client_generate_sync_form($email,$fullname,$user,$email_confirmation);\r
-                                       }\r
-                               }\r
-                       } elseif ($fullname && $fullname != $user->name) {\r
-                               // the fullname on the OpenID server is not the same as the name registered on this local client system\r
-                               $login = true;\r
-                               if ($CONFIG->openid_client_always_sync == 'yes') {\r
-                                   // this client always forces client/server data syncs\r
-                                   $user->name = $fullname;\r
-                               } else {\r
-                                       if (!$store->getNoSyncStatus($user)) {\r
-                                       // the following conditions are true:\r
-                                               // the fullname has changed on the server,\r
-                                               // this client does not *require* syncing with the server,\r
-                                               // but this user has not turned off syncing\r
-                                               // therefore the user needs to be offered the chance to sync his or her data\r
-                                               $body = openid_client_generate_sync_form($email,$fullname,$user,false);\r
-                                       }\r
-                               }\r
-                       } else {\r
-                               // nothing has changed or the data is null so let this person in\r
-                               $login = true;\r
-                       }\r
-               }                                                           \r
+// does this user exist\r
+$users = elgg_get_entities_from_metadata(array(\r
+       'type' => 'user',\r
+       'subtype' => 'openid',\r
+       'metadata_name' => 'openid_identifier',\r
+       'metadata_value' => $data['openid_identifier'],\r
+));\r
+if ($users) {\r
+       // log in user and maybe update account (admin setting, user prompt?)\r
+       $user = $users[0];\r
+\r
+       try {\r
+               login($user);\r
+       } catch (LoginException $e) {\r
+               register_error($e->getMessage());\r
+               forward();\r
        }\r
-    \r
-    if ($login) {\r
-               \r
-               $rememberme = get_input('remember',0);\r
-               if (!empty($rememberme)) {\r
-                       login($user,true);\r
-               } else {\r
-               login($user);\r
-               }\r
-       }\r
-} \r
-\r
-if(isset($body) && $body) {\r
-    \r
-    page_draw(elgg_echo('openid_client:information_title'),$body);\r
 \r
-} else {\r
+       system_message(elgg_echo('loginok'));\r
        forward();\r
+} else {\r
+       // register the new user\r
+       $result = openid_client_registration_page_handler($data);\r
+       if (!$result) {\r
+               register_error();\r
+               forward();\r
+       }\r
 }\r
index afbd7e49d8fb3e6351194b0264736e63ae0adbbd..d335d6e5537d5bddb43c9c2582b1bb346ca01656 100644 (file)
--- a/start.php
+++ b/start.php
@@ -1,88 +1,61 @@
 <?php\r
-\r
 /**\r
- * Elgg openid client plugin\r
+ * Elgg OpenID client\r
  * \r
- * @package ElggOpenID\r
- * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2\r
- * @author Kevin Jardine <kevin@radagast.biz>\r
- * @copyright Curverider Ltd 2008-2009\r
- * @link http://elgg.com/\r
  */\r
\r
- global $CONFIG;\r
\r
- set_include_path(get_include_path() . PATH_SEPARATOR . $CONFIG->pluginspath . 'openid_client/models');\r
+\r
+elgg_register_event_handler('init', 'system', 'openid_client_init');\r
 \r
 /**\r
- * OpenID client initialisation\r
- *\r
- * These parameters are required for the event API, but we won't use them:\r
- * \r
- * @param unknown_type $event\r
- * @param unknown_type $object_type\r
- * @param unknown_type $object\r
+ * OpenID client initialization\r
  */\r
-\r
 function openid_client_init() {\r
-               \r
-    elgg_extend_view("login/extend", "openid_client/forms/login");\r
-        \r
-       // Extend system CSS with our own styles\r
-       elgg_extend_view('css','openid_client/css');\r
-               \r
-       // Register a page handler, so we can have nice URLs\r
-       register_page_handler('openid_client','openid_client_page_handler');\r
-       \r
-}\r
-        \r
-function openid_client_pagesetup()\r
-    {\r
-    if (get_context() == 'admin' && isadminloggedin()) {\r
-       global $CONFIG;\r
-       add_submenu_item(elgg_echo('openid_client:admin_title'), $CONFIG->wwwroot . 'pg/openid_client/admin');\r
-    }\r
-}\r
+       elgg_extend_view('core/account/login_box', 'openid_client/login');\r
+       \r
+       $base = elgg_get_plugins_path() . 'openid_client/actions/openid_client';\r
+       elgg_register_action('openid_client/login', "$base/login.php", 'public');\r
+       elgg_register_action('openid_client/register', "$base/register.php", 'public');\r
 \r
-function openid_client_can_edit($hook_name, $entity_type, $return_value, $parameters) {\r
-       $entity = $parameters['entity'];\r
-       $context = get_context();\r
-       if ($context == 'openid' && $entity->getSubtype() == "openid") {\r
-       // should be able to do anything with OpenID user data\r
-       return true;\r
-       }\r
-       return null;  \r
+       $base = elgg_get_plugins_path() . 'openid_client/lib';\r
+       elgg_register_library('openid_client', "$base/helpers.php");\r
+\r
+       elgg_register_event_handler('create', 'user', 'openid_client_set_subtype', 1);\r
+\r
+       elgg_register_page_handler('openid_client', 'openid_client_page_handler');\r
 }\r
 \r
-function openid_client_page_handler($page) {\r
-       if (isset($page[0])) {\r
-               if ($page[0] == 'admin') {\r
-                       include(dirname(__FILE__) . "/pages/admin.php");\r
-                       return true;\r
-               } else if ($page[0] == 'confirm') {\r
-                       include(dirname(__FILE__) . "/pages/confirm.php");\r
-                       return true;\r
-               } else if ($page[0] == 'sso') {\r
-                       include(dirname(__FILE__) . "/pages/sso.php");\r
-                       return true;\r
-               } else if ($page[0] == 'reset') {\r
-                       include(dirname(__FILE__) . "/pages/reset.php");\r
-                       return true;\r
-               }\r
-       }\r
-       return false;\r
+/**\r
+ * Set the correct subtype for OpenID users\r
+ *\r
+ * @param string   $event  Event name\r
+ * @param string   $type   Object type\r
+ * @param ElggUser $user   New user\r
+ */\r
+function openid_client_set_subtype($event, $type, $user) {\r
+       $db_prefix = elgg_get_config('dbprefix');\r
+       $guid = (int)$user->getGUID();\r
+       $subtype_id = (int)add_subtype('user', 'openid');\r
+\r
+       $query = "UPDATE {$db_prefix}entities SET subtype = $subtype_id WHERE guid = $guid";\r
+       update_data($query);\r
 }\r
 \r
-register_elgg_event_handler('init','system','openid_client_init');\r
-register_elgg_event_handler('pagesetup','system','openid_client_pagesetup');\r
+/**\r
+ * OpenID client page handler\r
+ *\r
+ * @param type $page Array of URL segments\r
+ * @return bool\r
+ */\r
+function openid_client_page_handler($page) {\r
 \r
-register_plugin_hook('permissions_check','user','openid_client_can_edit');\r
+       // this is test code for right now\r
+       elgg_load_library('openid_client');\r
+       openid_client_registration_page_handler(array(\r
+               'username' => 'john',\r
+               'email' => 'john@example.org',\r
+               'name' => 'John Doe',\r
+               'openid_identifier' => 'abcdefghijklmnopqrstuvwxyz',\r
+       ));\r
 \r
-// Register actions\r
-global $CONFIG;\r
-register_action("openid_client/login",true,$CONFIG->pluginspath . "openid_client/actions/login.php");\r
-register_action("openid_client/return",true,$CONFIG->pluginspath . "openid_client/actions/return.php");\r
-register_action("openid_client/admin",false,$CONFIG->pluginspath . "openid_client/actions/admin.php");\r
-//register_action("openid_client/confirm",false,$CONFIG->pluginspath . "openid_client/actions/confirm.php");\r
-register_action("openid_client/missing",false,$CONFIG->pluginspath . "openid_client/actions/missing.php");\r
-register_action("openid_client/sync",false,$CONFIG->pluginspath . "openid_client/actions/sync.php");\r
+       return true;\r
+}\r
diff --git a/views/default/forms/openid_client/register.php b/views/default/forms/openid_client/register.php
new file mode 100644 (file)
index 0000000..b8f57fb
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+/**
+ * OpenID register form body
+ *
+ * @uses $vars['openid_identifier']
+ * @uses $vars['username']
+ * @uses $vars['is_username_available']
+ * @uses $vars['is_username_valid']
+ * @uses $vars['email']
+ * @uses $vars['name']
+ */
+
+$username_label = '';
+$username_input = elgg_view('input/text', array(
+       'name' => 'username',
+       'value' => $vars['username'],
+));
+
+$name_label = elgg_echo();
+$name_input = elgg_view('input/text', array(
+       'name' => 'name',
+       'value' => $vars['name'],
+));
+
+$email_label = elgg_echo();
+$email_input = elgg_view('input/email', array(
+       'name' => 'email',
+       'value' => $vars['email'],
+));
+
+$openid_input = elgg_view('input/hidden', array(
+       'name' => 'openid_identifier',
+       'value' => $vars['openid_identifier'],
+));
+$button = elgg_view('input/submit', array('value' => elgg_echo('save')));
+
+echo <<<HTML
+<div>
+       <label>$username_label</label>
+       $username_input
+</div>
+<div>
+       <label>$name_label</label>
+       $name_input
+</div>
+<div>
+       <label>$email_label</label>
+       $email_input
+</div>
+<div class="elgg-foot">
+       $openid_input
+       $button
+</div>
+
+HTML;
diff --git a/views/default/openid_client/login.php b/views/default/openid_client/login.php
new file mode 100644 (file)
index 0000000..f89b790
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+/**
+ *
+ */
+
+echo elgg_view('output/url', array(
+       'text' => 'login with Google',
+       'href' => 'action/openid_client/login',
+       'is_action' => true,
+));
diff --git a/views/default/openid_client/register.php b/views/default/openid_client/register.php
new file mode 100644 (file)
index 0000000..0c3770e
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+/**
+ * Registration content view for OpenID client
+ *
+ */
+
+echo elgg_view_form('openid_client/register', array(), $vars);