MidCOM svn: r21521 - in trunk/midcom/com_rohea_facebook: configuration controllers
tepehe
midcom-commits at lists.midgard-project.org
Fri Mar 27 14:25:42 CET 2009
Author: tepehe
Date: Fri Mar 27 14:25:39 2009
New Revision: 21521
URL: http://trac.midgard-project.org/changeset/21521
Log:
autologin supportfor fb-auth
Modified:
trunk/midcom/com_rohea_facebook/configuration/defaults.yml
trunk/midcom/com_rohea_facebook/controllers/facebookregistration.php
Modified: trunk/midcom/com_rohea_facebook/configuration/defaults.yml
==============================================================================
--- trunk/midcom/com_rohea_facebook/configuration/defaults.yml (original)
+++ trunk/midcom/com_rohea_facebook/configuration/defaults.yml Fri Mar 27 14:25:39 2009
@@ -2,9 +2,10 @@
facebook_api_key: abc
facebook_secret_key: def
fb_receiver_file: /midcom-static/com_rohea_facebook/xd_receiver.htm
+automatic_account_creation: false
redirect_url:
- component:
- route_id:
+ url: http://path/to/some/static/url
+ url_variables:
routes:
registration:
controller: com_rohea_facebook_controllers_facebookregistration
Modified: trunk/midcom/com_rohea_facebook/controllers/facebookregistration.php
==============================================================================
--- trunk/midcom/com_rohea_facebook/controllers/facebookregistration.php (original)
+++ trunk/midcom/com_rohea_facebook/controllers/facebookregistration.php Fri Mar 27 14:25:39 2009
@@ -42,7 +42,7 @@
$this->object = $_MIDCOM->authentication->get_person();
}
- public function prepare_new_object(&$data, $args)
+ public function prepare_new_object($args)
{
$this->object = new com_rohea_account_db();
}
@@ -61,7 +61,6 @@
public function action_registration($route_id, &$data, $args)
{
-
if($_MIDCOM->authentication->is_user())
{
$this->load_object($args);
@@ -75,6 +74,9 @@
$this->load_creation_datamanager($data, $schemadb, 'account_registration');
}
+ $automatic_account_creaton = $this->configuration->get('automatic_account_creation');
+ $config_redirect_url = $this->configuration->get('redirect_url');
+
// load facebook class
$api_key = trim($this->configuration->get("facebook_api_key"));
$secret_key = trim($this->configuration->get("facebook_secret_key"));
@@ -103,14 +105,14 @@
{
$redirect_url = '/';
-
header("Location: " . $redirect_url);
exit();
}
- $fb->login_midgard_person($midgardperson);
-
+ $fb->login_midgard_person($midgardperson);
+ // After login setting password for the newly generated user via API
+
$redirect_url = '/';
header("Location: " . $redirect_url);
@@ -133,6 +135,50 @@
$data['show_newuser_dialog'] = true;
+ /* Do automatic account creation if option is set*/
+
+ if ($automatic_account_creaton == true)
+ {
+ if (!empty($fbid) && !empty($facebook_details))
+ {
+ $username = $facebook_details['first_name'];
+ if (empty($username) || strlen($username) < 3)
+ {
+ $username = substr(md5(time()), 0, 8);
+ }
+ $password = substr(md5(time() . 'asdfsd' . rand() ), 0, 8);
+ $username = $this->generate_unique_username($username);
+ $config_redirect_url['url'];
+
+ $this->object->username = $username;
+ $this->object->firstname = $facebook_details['first_name'];
+ $this->object->lastname = $facebook_details['last_name'];
+ $this->object->create();
+
+ $this->object->set_parameter('com_rohea_facebook', 'avatar', $facebook_details['pic_square']);
+ // Doing trusted login with newly created user (we do not have password yet)
+ $_MIDCOM->authentication->trusted_login($this->object->username);
+
+ // After login setting password for the newly generated user via API
+ $user = $_MIDCOM->authentication->get_user();
+ $user->password($this->object->username, $password);
+
+ // Password is now set, logging in with it
+ $_MIDCOM->authentication->login($this->object->username, $password);
+
+ $fb->addfacebooklink($fbid, $user->guid);
+
+ header('Location: ' . $config_redirect_url['url']);
+ exit();
+ }
+ else
+ {
+ echo 'Connection error to Facebook';
+ exit();
+ }
+
+ }
+
/* If form is posted and existing midgard userid is being joined to a facebook id */
if (isset($_POST['com_rohea_facebook_linking']) && $_POST['com_rohea_facebook_linking'] == 'true')
@@ -180,7 +226,8 @@
/* Link facebook id and midgard userid */
$fb->addfacebooklink($fbid, $user->guid);
-
+ $this->object->set_parameter('com_rohea_facebook', 'avatar', $facebook_details['pic_square']);
+
$redirect_url = '/';
header("Location: " . $redirect_url);
@@ -196,6 +243,29 @@
}
}
+ // FIXME: tepheikk will refactor this as soon as there's time
+ private function generate_unique_username($starting_value)
+ {
+ $start_int = 1;
+ $current_int = 1;
+
+ $starting_value = strtolower($starting_value . $start_int);
+ $qb = new midgard_query_builder('midgard_person');
+ $qb->add_constraint('username', '=' , $starting_value);
+ $res = $qb->execute();
+ while (count($res) > 0 )
+ {
+ $start_int++;
+ $starting_value = substr($starting_value, 0, strlen($starting_value) - strlen($current_int)) . $start_int;
+ $qb = new midgard_query_builder('midgard_person');
+ $qb->add_constraint('username', '=' , $starting_value);
+ $res = $qb->execute();
+ $current_int++;
+ }
+
+ return $starting_value;
+ }
+
public function action_checkusername($route_id, &$data, $args)
{
$username = '';
More information about the midcom-commits
mailing list