47 $this->crypto = $crypto;
48 $this->cookie_config = $cookie_config;
49 $this->cookie_token = $cookie_token;
51 $prefix = $this->db->getTablePrefix();
52 $this->
table =
"{$prefix}users_remember_me_cookies";
53 $this->time = is_numeric($time) ? (int)$time : time();
64 $token = $this->generateToken();
65 $hash = $this->hashToken(
$token);
67 $this->storeHash($user, $hash);
78 if ($this->cookie_token) {
79 $client_hash = $this->hashToken($this->cookie_token);
80 $this->removeHash($client_hash);
84 $this->setSession(
"");
96 $this->removeAllHashes($subject);
97 if (!$modifier || ($modifier->guid !== $subject->guid) || !$this->cookie_token) {
101 $this->makeLoginPersistent($modifier);
111 if (!$this->cookie_token) {
116 $cookie_hash = $this->hashToken($this->cookie_token);
117 $user = $this->getUserFromHash($cookie_hash);
119 $this->setSession($this->cookie_token);
124 if ($this->isLegacyToken($this->cookie_token)) {
126 call_user_func($this->_callable_sleep, 1);
128 $this->setCookie(
'');
140 if (!$this->cookie_token || !$this->isLegacyToken($this->cookie_token)) {
145 $this->removeHash($this->hashToken($this->cookie_token));
146 $this->makeLoginPersistent($logged_in_user);
161 $hash = $this->db->sanitizeString($hash);
162 $query =
"SELECT guid FROM {$this->table} WHERE code = '$hash'";
164 $user_row = $this->db->getDataRow($query);
166 return $this->handleDbException($e);
172 $user = call_user_func($this->_callable_get_user, $user_row->guid);
187 $this->removeHash($hash);
190 $hash = $this->db->sanitizeString($hash);
193 INSERT INTO {$this->table} (code, guid, timestamp) 194 VALUES ('$hash', {$user->guid}, $time) 197 $this->db->insertData($query);
199 $this->handleDbException($e);
210 $hash = $this->db->sanitizeString($hash);
212 $query =
"DELETE FROM {$this->table} WHERE code = '$hash'";
214 $this->db->deleteData($query);
216 $this->handleDbException($e);
231 if (
false !== strpos($exception->getMessage(),
"users_remember_me_cookies' doesn't exist")) {
247 $query =
"DELETE FROM {$this->table} WHERE guid = '{$user->guid}'";
249 $this->db->deleteData($query);
251 $this->handleDbException($e);
276 $cookie = new \ElggCookie($this->cookie_config[
'name']);
277 foreach (array(
'expire',
'path',
'domain',
'secure',
'httponly') as
$key) {
278 $cookie->$key = $this->cookie_config[
$key];
282 $cookie->expire = $this->time - (86400 * 30);
284 call_user_func($this->_callable_elgg_set_cookie, $cookie);
296 $this->session->set(
'code',
$token);
298 $this->session->remove(
'code');
311 return 'z' . $this->crypto->getRandomString(31);
364 public $_callable_get_user =
'get_user';
370 public $_callable_elgg_set_cookie =
'elgg_set_cookie';
376 public $_callable_sleep =
'sleep';
handleDbException(\DatabaseException $exception, $default=null)
Swallow a schema not upgraded exception, otherwise rethrow it.
removeHash($hash)
Remove a hash from the DB.
makeLoginPersistent(\ElggUser $user)
Make the user's login persistent.
removeAllHashes(\ElggUser $user)
Remove all the hashes associated with a user.
bootSession()
Boot the persistent login session, possibly returning the user who should be silently logged in...
__construct(Database $db,\ElggSession $session,\ElggCrypto $crypto, array $cookie_config, $cookie_token, $time=null)
Constructor.
storeHash(\ElggUser $user, $hash)
Store a hash in the DB.
setCookie($token)
Store the token in the client cookie (or remove the cookie)
getUserFromHash($hash)
Find a user with the given hash.
isLegacyToken($token)
Is the given token a legacy MD5 hash?
generateToken()
Generate a random token (base 64 URL)
hashToken($token)
Create a hash from the token.
removePersistentLogin()
Remove the persisted login token from client and server.
setSession($token)
Store the token in the session (or remove it from the session)
handlePasswordChange(\ElggUser $subject,\ElggUser $modifier=null)
Handle a password change.
replaceLegacyToken(\ElggUser $logged_in_user)
Replace the user's token if it's a legacy hexadecimal token.