/**
* Attempt to syncronize user data from remote system into Dashboard.
*
* @access public
* @since 2.0.?
* @author Tim Gunter
*
* @param object $Authenticator
* @param array $UserInfo
* @param array $Payload
*/
public function syncScreen($Authenticator, $UserInfo, $Payload)
{
$this->addJsFile('entry.js');
$this->View = 'handshake';
$this->HandshakeScheme = $Authenticator->getAuthenticationSchemeAlias();
$this->Form->setModel($this->UserModel);
$this->Form->addHidden('ClientHour', date('Y-m-d H:00'));
// Use the server's current hour as a default
$this->Form->addHidden('Target', $this->target());
$PreservedKeys = array('UserKey', 'Token', 'Consumer', 'Email', 'Name', 'Gender', 'HourOffset');
$UserID = 0;
$Target = $this->target();
if ($this->Form->isPostBack() === true) {
$FormValues = $this->Form->formValues();
if (val('StopLinking', $FormValues)) {
$AuthResponse = Gdn_Authenticator::AUTH_ABORTED;
$UserEventData = array_merge(array('UserID' => $UserID, 'Payload' => $Payload), $UserInfo);
Gdn::authenticator()->trigger($AuthResponse, $UserEventData);
$Authenticator->deleteCookie();
Gdn::request()->withRoute('DefaultController');
return Gdn::dispatcher()->dispatch();
} elseif (val('NewAccount', $FormValues)) {
$AuthResponse = Gdn_Authenticator::AUTH_CREATED;
// Try and synchronize the user with the new username/email.
$FormValues['Name'] = $FormValues['NewName'];
$FormValues['Email'] = $FormValues['NewEmail'];
$UserID = $this->UserModel->synchronize($UserInfo['UserKey'], $FormValues);
$this->Form->setValidationResults($this->UserModel->validationResults());
} else {
$AuthResponse = Gdn_Authenticator::AUTH_SUCCESS;
// Try and sign the user in.
$PasswordAuthenticator = Gdn::authenticator()->authenticateWith('password');
$PasswordAuthenticator->hookDataField('Email', 'SignInEmail');
$PasswordAuthenticator->hookDataField('Password', 'SignInPassword');
$PasswordAuthenticator->fetchData($this->Form);
$UserID = $PasswordAuthenticator->authenticate();
if ($UserID < 0) {
$this->Form->addError('ErrorPermission');
} elseif ($UserID == 0) {
$this->Form->addError('ErrorCredentials');
Logger::event('signin_failure', Logger::WARNING, '{username} failed to sign in. Invalid credentials.');
}
if ($UserID > 0) {
$Data = $FormValues;
$Data['UserID'] = $UserID;
$Data['Email'] = val('SignInEmail', $FormValues, '');
$UserID = $this->UserModel->synchronize($UserInfo['UserKey'], $Data);
}
}
if ($UserID > 0) {
// The user has been created successfully, so sign in now
// Finalize the link between the forum user and the foreign userkey
$Authenticator->finalize($UserInfo['UserKey'], $UserID, $UserInfo['ConsumerKey'], $UserInfo['TokenKey'], $Payload);
$UserEventData = array_merge(array('UserID' => $UserID, 'Payload' => $Payload), $UserInfo);
Gdn::authenticator()->trigger($AuthResponse, $UserEventData);
/// ... and redirect them appropriately
$Route = $this->redirectTo();
if ($Route !== false) {
redirect($Route);
}
} else {
// Add the hidden inputs back into the form.
foreach ($FormValues as $Key => $Value) {
if (in_array($Key, $PreservedKeys)) {
$this->Form->addHidden($Key, $Value);
}
}
}
} else {
$Id = Gdn::authenticator()->getIdentity(true);
if ($Id > 0) {
// The user is signed in so we can just go back to the homepage.
redirect($Target);
}
$Name = $UserInfo['UserName'];
$Email = $UserInfo['UserEmail'];
// Set the defaults for a new user.
$this->Form->setFormValue('NewName', $Name);
$this->Form->setFormValue('NewEmail', $Email);
// Set the default for the login.
$this->Form->setFormValue('SignInEmail', $Email);
$this->Form->setFormValue('Handshake', 'NEW');
// Add the handshake data as hidden fields.
$this->Form->addHidden('Name', $Name);
$this->Form->addHidden('Email', $Email);
$this->Form->addHidden('UserKey', $UserInfo['UserKey']);
$this->Form->addHidden('Token', $UserInfo['TokenKey']);
$this->Form->addHidden('Consumer', $UserInfo['ConsumerKey']);
}
$this->setData('Name', val('Name', $this->Form->HiddenInputs));
$this->setData('Email', val('Email', $this->Form->HiddenInputs));
$this->render();
}