]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
move user registration to a single static method
authorEvan Prodromou <evan@prodromou.name>
Thu, 14 Aug 2008 00:20:38 +0000 (20:20 -0400)
committerEvan Prodromou <evan@prodromou.name>
Thu, 14 Aug 2008 00:20:38 +0000 (20:20 -0400)
darcs-hash:20080814002038-84dde-8505d4e083056b770db128129a95be639d8e7f0a.gz

actions/finishopenidlogin.php
actions/register.php
classes/User.php

index 89f4ef0bd2305f3e1a4e66f40ccce693c1a03fe8..827a4e9c7ae6418b7ec93473f449e1c491ee31b8 100644 (file)
@@ -192,64 +192,35 @@ class FinishopenidloginAction extends Action {
                        return;
                }
 
-               $profile = new Profile();
-
-               $profile->nickname = $nickname;
-
-               if ($sreg['fullname'] && strlen($sreg['fullname']) <= 255) {
-                       $profile->fullname = $sreg['fullname'];
-               }
-
                if ($sreg['country']) {
                        if ($sreg['postcode']) {
                                # XXX: use postcode to get city and region
                                # XXX: also, store postcode somewhere -- it's valuable!
-                               $profile->location = $sreg['postcode'] . ', ' . $sreg['country'];
+                               $location = $sreg['postcode'] . ', ' . $sreg['country'];
                        } else {
-                               $profile->location = $sreg['country'];
+                               $location = $sreg['country'];
                        }
                }
-
-               # XXX save language if it's passed
-               # XXX save timezone if it's passed
-
-               $profile->profileurl = common_profile_url($nickname);
-
-               $profile->created = DB_DataObject_Cast::dateTime(); # current time
-
-               $id = $profile->insert();
-               if (!$id) {
-                       common_server_error(_('Error saving the profile.'));
-                       return;
+               
+               if ($sreg['fullname'] && strlen($sreg['fullname']) <= 255) {
+                       $fullname = $sreg['fullname'];
                }
-
-               $user = new User();
-               $user->id = $id;
-               $user->nickname = $nickname;
-               $user->uri = common_user_uri($user);
-
+               
                if ($sreg['email'] && Validate::email($sreg['email'], true)) {
-                       $user->email = $sreg['email'];
+                       $email = $sreg['email'];
                }
 
-               $user->created = DB_DataObject_Cast::dateTime(); # current time
-
-               $result = $user->insert();
-
-               if (!$result) {
-                       # Try to clean up...
-                       $profile->delete();
-               }
+               # XXX: add language
+               # XXX: add timezone
+               
+               $user = User::register(array('nickname' => $nickname, 
+                                                                        'email' => $email,
+                                                                        'fullname' => $fullname, 
+                                                                        'location' => $location));
 
                $result = oid_link_user($user->id, $canonical, $display);
-
-               if (!$result) {
-                       # Try to clean up...
-                       $user->delete();
-                       $profile->delete();
-               }
-
-               oid_set_last($display);
+               
+               oid_set_last($display);                                                    
                common_set_user($user->nickname);
                common_real_login(true);
                common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)));
index c539108420095f81da74412f26df49c2fccaf628..e6b1931cef6de26752c7d9bbd1ed7abfaba02146 100644 (file)
@@ -82,7 +82,9 @@ class RegisterAction extends Action {
                        return;
                } else if ($password != $confirm) {
                        $this->show_form(_('Passwords don\'t match.'));
-               } else if ($user = $this->register_user($nickname, $password, $email, $fullname, $homepage, $bio, $location)) {
+               } else if ($user = User::register(array('nickname' => $nickname, 'password' => $password, 'email' => $email,
+                                                                                               'fullname' => $fullname, 'homepage' => $homepage, 'bio' => $bio, 
+                                                                                               'location' => $location))) {
                        if (!$user) {
                                $this->show_form(_('Invalid username or password.'));
                                return;
@@ -121,88 +123,6 @@ class RegisterAction extends Action {
                return ($user !== false);
        }
 
-       function register_user($nickname, $password, $email, $fullname, $homepage, $bio, $location) {
-
-               $profile = new Profile();
-
-               $profile->query('BEGIN');
-
-               $profile->nickname = $nickname;
-               $profile->profileurl = common_profile_url($nickname);
-               if ($fullname) {
-                       $profile->fullname = $fullname;
-               }
-               if ($homepage) {
-                       $profile->homepage = $homepage;
-               }
-               if ($bio) {
-                       $profile->bio = $bio;
-               }
-               if ($location) {
-                       $profile->location = $location;
-               }
-               $profile->created = DB_DataObject_Cast::dateTime(); # current time
-               
-               $id = $profile->insert();
-
-               if (!$id) {
-                       common_log_db_error($profile, 'INSERT', __FILE__);
-                   return FALSE;
-               }
-               $user = new User();
-               $user->id = $id;
-               $user->nickname = $nickname;
-               $user->password = common_munge_password($password, $id);
-               $user->created =  DB_DataObject_Cast::dateTime(); # current time
-               $user->uri = common_user_uri($user);
-
-               $result = $user->insert();
-
-               if (!$result) {
-                       common_log_db_error($user, 'INSERT', __FILE__);
-                       return FALSE;
-               }
-
-               # Everyone is subscribed to themself
-
-               $subscription = new Subscription();
-               $subscription->subscriber = $user->id;
-               $subscription->subscribed = $user->id;
-               $subscription->created = $user->created;
-               
-               $result = $subscription->insert();
-               
-               if (!$result) {
-                       common_log_db_error($subscription, 'INSERT', __FILE__);
-                       return FALSE;
-               }
-               
-               if ($email) {
-
-                       $confirm = new Confirm_address();
-                       $confirm->code = common_confirmation_code(128);
-                       $confirm->user_id = $user->id;
-                       $confirm->address = $email;
-                       $confirm->address_type = 'email';
-
-                       $result = $confirm->insert();
-                       if (!$result) {
-                               common_log_db_error($confirm, 'INSERT', __FILE__);
-                               return FALSE;
-                       }
-               }
-
-               $profile->query('COMMIT');
-
-               if ($email) {
-                       mail_confirm_address($confirm->code,
-                                                                $profile->nickname,
-                                                                $email);
-               }
-
-               return $user;
-       }
-
        function show_top($error=NULL) {
                if ($error) {
                        common_element('p', 'error', $error);
index f2ca13530b739b80ef96fdf42e210c20163b065d..491f68df24e77cde5d2c5650d77005da527ab36c 100644 (file)
@@ -145,4 +145,91 @@ class User extends DB_DataObject
                
                return $notice;
        }
+       
+       static function register($fields) {
+
+               extract($fields);
+               
+               $profile = new Profile();
+
+               $profile->query('BEGIN');
+
+               $profile->nickname = $nickname;
+               $profile->profileurl = common_profile_url($nickname);
+               
+               if ($fullname) {
+                       $profile->fullname = $fullname;
+               }
+               if ($homepage) {
+                       $profile->homepage = $homepage;
+               }
+               if ($bio) {
+                       $profile->bio = $bio;
+               }
+               if ($location) {
+                       $profile->location = $location;
+               }
+               $profile->created = DB_DataObject_Cast::dateTime(); # current time
+               
+               $id = $profile->insert();
+
+               if (!$id) {
+                       common_log_db_error($profile, 'INSERT', __FILE__);
+                   return FALSE;
+               }
+               
+               $user = new User();
+               
+               $user->id = $id;
+               $user->nickname = $nickname;
+               $user->password = common_munge_password($password, $id);
+               $user->created =  DB_DataObject_Cast::dateTime(); # current time
+               $user->uri = common_user_uri($user);
+
+               $result = $user->insert();
+
+               if (!$result) {
+                       common_log_db_error($user, 'INSERT', __FILE__);
+                       return FALSE;
+               }
+
+               # Everyone is subscribed to themself
+
+               $subscription = new Subscription();
+               $subscription->subscriber = $user->id;
+               $subscription->subscribed = $user->id;
+               $subscription->created = $user->created;
+               
+               $result = $subscription->insert();
+               
+               if (!$result) {
+                       common_log_db_error($subscription, 'INSERT', __FILE__);
+                       return FALSE;
+               }
+               
+               if ($email) {
+
+                       $confirm = new Confirm_address();
+                       $confirm->code = common_confirmation_code(128);
+                       $confirm->user_id = $user->id;
+                       $confirm->address = $email;
+                       $confirm->address_type = 'email';
+
+                       $result = $confirm->insert();
+                       if (!$result) {
+                               common_log_db_error($confirm, 'INSERT', __FILE__);
+                               return FALSE;
+                       }
+               }
+
+               $profile->query('COMMIT');
+
+               if ($email) {
+                       mail_confirm_address($confirm->code,
+                                                                $profile->nickname,
+                                                                $email);
+               }
+
+               return $user;
+       }
 }