Skip to content

Commit 2652ff3

Browse files
committed
Integrate the register frontend to help handle our frontend
1 parent 630a710 commit 2652ff3

File tree

1 file changed

+108
-88
lines changed

1 file changed

+108
-88
lines changed

net/xenapi/XenAPI/api.php

+108-88
Original file line numberDiff line numberDiff line change
@@ -4951,117 +4951,137 @@ public function register($user_data) {
49514951
return array('error' => 10, 'errors' => 'Missing required parameter: email');
49524952
}
49534953

4954-
// Create a new variable for the password.
4955-
$password = $user_data['password'];
4956-
4957-
// Unset the password from the user data array.
4958-
unset($user_data['password']);
4959-
4960-
if (!empty($user_data['ip_address'])) {
4961-
// Create a new variable for the ip address.
4962-
$ip_address = $user_data['ip_address'];
4954+
$usr = $this->getUser($user_data['username']);
4955+
if ($usr->isRegistered()) {
4956+
if ($usr->getEmail() != $user_data['email']) {
4957+
$edit_results = $this->editUser($usr, array('email' => $user_data['email'], 'user_state' => 'email_confirm'));
4958+
if (!empty($edit_results['error'])) {
4959+
return $edit_results;
4960+
} else {
4961+
$user = $this->getUser($user_data['username']);
4962+
XenForo_Model::create('XenForo_Model_UserConfirmation')->sendEmailConfirmation($user->getData());
4963+
return $user->getData();
4964+
}
4965+
} else {
4966+
return array('error' => 40, 'errors' => 'User already exists');
4967+
}
4968+
} else {
4969+
// Create a new variable for the password.
4970+
$password = $user_data['password'];
49634971

4964-
// Unset the ip address from the user data array.
4965-
unset($user_data['ip_address']);
4966-
}
4972+
// Unset the password from the user data array.
4973+
unset($user_data['password']);
49674974

4968-
// Get the default options from XenForo.
4969-
$options = XenForo_Application::get('options');
4975+
if (!empty($user_data['ip_address'])) {
4976+
// Create a new variable for the ip address.
4977+
$ip_address = $user_data['ip_address'];
49704978

4971-
// Create the data writer object for registrations, and set the defaults.
4972-
$writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
4973-
if ($options->registrationDefaults) {
4974-
// Set the default registration options if it's set in the XenForo options.
4975-
$writer->bulkSet($options->registrationDefaults, array('ignoreInvalidFields' => TRUE));
4976-
}
4979+
// Unset the ip address from the user data array.
4980+
unset($user_data['ip_address']);
4981+
}
49774982

4978-
if (!empty($user_data['group_id'])) {
4979-
// Group ID is set.
4980-
$writer->set('user_group_id', $user_data['group_id']);
4983+
// Get the default options from XenForo.
4984+
$options = XenForo_Application::get('options');
49814985

4982-
// We need to unset the group id as we don't want it to be included into the bulk set.
4983-
unset($user_data['group_id']);
4984-
} else {
4985-
// Group ID is not set, default back to default.
4986-
$writer->set('user_group_id', XenForo_Model_User::$defaultRegisteredGroupId);
4987-
}
4986+
// Create the data writer object for registrations, and set the defaults.
4987+
$writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
4988+
if ($options->registrationDefaults) {
4989+
// Set the default registration options if it's set in the XenForo options.
4990+
$writer->bulkSet($options->registrationDefaults, array('ignoreInvalidFields' => TRUE));
4991+
}
49884992

4989-
if (!empty($user_data['user_state'])) {
4990-
// User state is set.
4991-
$writer->set('user_state', $user_data['user_state']);
4992-
} else {
4993-
// User state is not set, default back to default.
4994-
$writer->advanceRegistrationUserState();
4995-
}
4993+
if (!empty($user_data['group_id'])) {
4994+
// Group ID is set.
4995+
$writer->set('user_group_id', $user_data['group_id']);
49964996

4997-
if (!empty($user_data['language_id'])) {
4998-
// Language ID is set.
4999-
$writer->set('language_id', $user_data['language_id']);
5000-
} else {
5001-
// Language ID is not set, default back to default.
5002-
$writer->set('language_id', $options->defaultLanguageId);
5003-
}
4997+
// We need to unset the group id as we don't want it to be included into the bulk set.
4998+
unset($user_data['group_id']);
4999+
} else {
5000+
// Group ID is not set, default back to default.
5001+
$writer->set('user_group_id', XenForo_Model_User::$defaultRegisteredGroupId);
5002+
}
50045003

5005-
if (!empty($user_data['custom_fields'])) {
5006-
// Custom fields are set.
5004+
if (!empty($user_data['user_state'])) {
5005+
// User state is set.
5006+
$writer->set('user_state', $user_data['user_state']);
5007+
} else {
5008+
// User state is not set, default back to default.
5009+
$writer->advanceRegistrationUserState();
5010+
}
50075011

5008-
// Check if there are any custom fields in the data array.
5009-
if (count($user_data['custom_fields']) > 0) {
5010-
// There were one or more custom fields set, set them in the writer.
5011-
$writer->setCustomFields($user_data['custom_fields']);
5012+
if (!empty($user_data['language_id'])) {
5013+
// Language ID is set.
5014+
$writer->set('language_id', $user_data['language_id']);
5015+
} else {
5016+
// Language ID is not set, default back to default.
5017+
$writer->set('language_id', $options->defaultLanguageId);
50125018
}
5013-
// We need to unset the custom fields as we don't want it to be included into the bulk set.
5014-
unset($user_data['custom_fields']);
5015-
}
50165019

5017-
if (!empty($user_data['add_groups'])) {
5018-
// Add group is set.
5020+
if (!empty($user_data['custom_fields'])) {
5021+
// Custom fields are set.
50195022

5020-
// Check if there are any custom fields in the data array.
5021-
if (!is_array($user_data['add_groups']) || count($user_data['add_groups']) == 0) {
5022-
// The edit failed, return errors.
5023-
return array('error' => 7, 'errors' => 'The add_groups parameter needs to be an array and have at least 1 item.');
5023+
// Check if there are any custom fields in the data array.
5024+
if (count($user_data['custom_fields']) > 0) {
5025+
// There were one or more custom fields set, set them in the writer.
5026+
$writer->setCustomFields($user_data['custom_fields']);
5027+
}
5028+
// We need to unset the custom fields as we don't want it to be included into the bulk set.
5029+
unset($user_data['custom_fields']);
50245030
}
50255031

5026-
// Set the secondary group(s) of the user.
5027-
$writer->setSecondaryGroups($user_data['add_groups']);
5032+
if (!empty($user_data['add_groups'])) {
5033+
// Add group is set.
50285034

5029-
// We need to unset the group id as we don't want it to be included into the bulk set.
5030-
unset($user_data['add_groups']);
5031-
}
5035+
// Check if there are any custom fields in the data array.
5036+
if (!is_array($user_data['add_groups']) || count($user_data['add_groups']) == 0) {
5037+
// The edit failed, return errors.
5038+
return array('error' => 7, 'errors' => 'The add_groups parameter needs to be an array and have at least 1 item.');
5039+
}
50325040

5033-
// Check if Gravatar is enabled, set the gravatar if it is and there's a gravatar for the email.
5034-
if ($options->gravatarEnable && XenForo_Model_Avatar::gravatarExists($data['email'])) {
5035-
$writer->set('gravatar', $user_data['email']);
5036-
}
5041+
// Set the secondary group(s) of the user.
5042+
$writer->setSecondaryGroups($user_data['add_groups']);
50375043

5038-
// Set the data for the data writer.
5039-
$writer->bulkSet($user_data);
5044+
// We need to unset the group id as we don't want it to be included into the bulk set.
5045+
unset($user_data['add_groups']);
5046+
}
50405047

5041-
// Set the password for the data writer.
5042-
$writer->setPassword($password, $password);
5048+
// Check if Gravatar is enabled, set the gravatar if it is and there's a gravatar for the email.
5049+
if ($options->gravatarEnable && XenForo_Model_Avatar::gravatarExists($data['email'])) {
5050+
$writer->set('gravatar', $user_data['email']);
5051+
}
50435052

5044-
// Pre save the data.
5045-
$writer->preSave();
5053+
// Set the data for the data writer.
5054+
$writer->bulkSet($user_data);
50465055

5047-
if ($writer->hasErrors()) {
5048-
// The registration failed, return errors.
5049-
return array('error' => TRUE, 'errors' => $writer->getErrors());
5050-
}
5056+
// Set the password for the data writer.
5057+
$writer->setPassword($password, $password);
50515058

5052-
// Save the user to the database.
5053-
$writer->save();
5054-
5055-
// Get the User as a variable:
5056-
$user = $writer->getMergedData();
5059+
// Pre save the data.
5060+
$writer->preSave();
5061+
5062+
if ($writer->hasErrors()) {
5063+
// The registration failed, return errors.
5064+
return array('error' => TRUE, 'errors' => $writer->getErrors());
5065+
}
50575066

5058-
// Check if IP is set.
5059-
if (!empty($user_data['ip_address'])) {
5060-
// Log the IP of the user that registered.
5061-
XenForo_Model_Ip::log($user['user_id'], 'user', $user['user_id'], 'register', $ip_address);
5067+
// Save the user to the database.
5068+
$writer->save();
5069+
5070+
// Get the User as a variable:
5071+
$user = $writer->getMergedData();
5072+
5073+
// Check if IP is set.
5074+
if (!empty($user_data['ip_address'])) {
5075+
// Log the IP of the user that registered.
5076+
XenForo_Model_Ip::log($user['user_id'], 'user', $user['user_id'], 'register', $ip_address);
5077+
}
5078+
if ($user['user_state'] == 'email_confirm') {
5079+
XenForo_Model::create('XenForo_Model_UserConfirmation')->sendEmailConfirmation($user);
5080+
} else if ($user['user_state'] == 'valid') {
5081+
XenForo_Model::create('XenForo_Model_UserConfirmation')->resetPassword($user['user_id'], true);
5082+
}
5083+
return $user;
50625084
}
5063-
5064-
return $user;
50655085
}
50665086
}
50675087

0 commit comments

Comments
 (0)