public function start($UserID = false, $SetIdentity = true, $Persist = false)
{
if (!c('Garden.Installed', false)) {
return;
}
// Retrieve the authenticated UserID from the Authenticator module.
$UserModel = Gdn::authenticator()->getUserModel();
$this->UserID = $UserID !== false ? $UserID : Gdn::authenticator()->getIdentity();
$this->User = false;
// Now retrieve user information
if ($this->UserID > 0) {
// Instantiate a UserModel to get session info
$this->User = $UserModel->getSession($this->UserID);
if ($this->User) {
if ($SetIdentity) {
Gdn::authenticator()->setIdentity($this->UserID, $Persist);
Logger::event('session_start', Logger::INFO, 'Session started for {username}.');
Gdn::pluginManager()->callEventHandlers($this, 'Gdn_Session', 'Start');
}
$UserModel->EventArguments['User'] =& $this->User;
$UserModel->fireEvent('AfterGetSession');
$this->permissions->setPermissions($this->User->Permissions);
$this->_Preferences = $this->User->Preferences;
$this->_Attributes = $this->User->Attributes;
$this->_TransientKey = is_array($this->_Attributes) ? val('TransientKey', $this->_Attributes) : false;
if ($this->_TransientKey === false) {
$this->_TransientKey = $UserModel->setTransientKey($this->UserID);
}
// Save any visit-level information.
if ($SetIdentity) {
$UserModel->updateVisit($this->UserID);
}
} else {
$this->UserID = 0;
$this->User = false;
$this->_TransientKey = getAppCookie('tk');
if ($SetIdentity) {
Gdn::authenticator()->setIdentity(null);
}
}
} else {
// Grab the transient key from the cookie. This doesn't always get set but we'll try it here anyway.
$this->_TransientKey = getAppCookie('tk');
}
// Load guest permissions if necessary
if ($this->UserID == 0) {
$guestPermissions = $UserModel->getPermissions(0);
$this->permissions->setPermissions($guestPermissions->getPermissions());
}
}