Skip to content

Commit 797dc60

Browse files
lukewertzMateu Aguiló Bosch
authored andcommitted
[FEATURE] Update user's access time when authenticating
When a user makes an authenticated request, update the access time on the user object. Just like core does.
1 parent 11e213b commit 797dc60

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

plugins/authentication/RestfulAuthenticationManager.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ public function getAccount(array $request = array(), $method = \RestfulInterface
123123
// This is necessary because the page cache system only looks at session
124124
// cookies, but not at HTTP Basic Auth headers.
125125
drupal_page_is_cacheable(!$account->uid && variable_get('restful_page_cache', FALSE));
126+
127+
// Record the access time of this request.
128+
$this->setAccessTime($account);
129+
126130
return $account;
127131
}
128132

@@ -205,5 +209,20 @@ protected function getOriginalUserSession() {
205209
return $this->originalUserSession;
206210
}
207211

208-
212+
/**
213+
* Set the user's last access time.
214+
*
215+
* @param object $account
216+
* A user account.
217+
*
218+
* @see _drupal_session_write()
219+
*/
220+
protected function setAccessTime($account) {
221+
// This logic is pulled directly from _drupal_session_write().
222+
if ($account->uid && REQUEST_TIME - $account->access > variable_get('session_write_interval', 180)) {
223+
db_update('users')->fields(array(
224+
'access' => REQUEST_TIME,
225+
))->condition('uid', $account->uid)->execute();
226+
}
227+
}
209228
}

tests/RestfulAuthenticationTestCase.test

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,68 @@ class RestfulAuthenticationTestCase extends RestfulCurlBaseTestCase {
138138

139139
$this->assertEqual($user->uid, 0, 'Global user is the correct one after an API call that switches the user.');
140140
}
141+
142+
/**
143+
* Test recording of access time.
144+
*/
145+
function testAccessTime() {
146+
global $user;
147+
148+
$user1 = $this->drupalCreateUser();
149+
$user2 = $this->drupalCreateUser();
150+
151+
$handler = restful_get_restful_handler('main', 1, 5);
152+
153+
// Case 1. Ensure that access time is recorded for cookie auth.
154+
$user = $user1;
155+
156+
$user1_access_time_before = db_query('SELECT access FROM {users} WHERE uid = :d', array(':d' => $user1->uid))->fetchObject();
157+
158+
// Perform request authentication.
159+
$handler->getAccount();
160+
161+
$user1_access_time = db_query('SELECT access FROM {users} WHERE uid = :d', array(':d' => $user1->uid))->fetchObject();
162+
$this->assertEqual($user1_access_time->access, REQUEST_TIME, 'Cookie authenticated user access time is updated.');
163+
164+
$this->assertNotEqual($user1_access_time_before->access, $user1_access_time->access, 'Access time before and after request are equal.');
165+
166+
// Case 2. Ensure that access time is recorded for basic auth.
167+
$user = $user2;
168+
169+
$_SERVER['PHP_AUTH_USER'] = $user2->name;
170+
$_SERVER['PHP_AUTH_PW'] = $user2->pass_raw;
171+
$_SERVER['REDIRECT_HTTP_AUTHORIZATION'] = NULL;
172+
$handler = restful_get_restful_handler('articles');
173+
174+
// Perform request authentication.
175+
$handler->getAccount();
176+
177+
$user2_access_time = db_query('SELECT access FROM {users} WHERE uid = :d', array(':d' => $user2->uid))->fetchObject();
178+
$this->assertEqual($user2_access_time->access, REQUEST_TIME, 'Basic authenticated user access time is updated.');
179+
180+
// Case 3. Ensure that the timestamp gets updated.
181+
$user = $user1;
182+
183+
// Get a timestamp that is in the past.
184+
$the_past = REQUEST_TIME - variable_get('session_write_interval');
185+
186+
// To begin, we'll set the timestamp for user1 back a little bit.
187+
$num_updated = db_update('users')
188+
->fields(array('access' => $the_past))
189+
->condition('uid', $user1->uid)
190+
->execute();
191+
192+
$user1_pre_access_time = db_query('SELECT access FROM {users} WHERE uid = :d', array(':d' => $user1->uid))->fetchObject();
193+
$this->assertEqual($user1_pre_access_time->access, $the_past, 'Set user1 access time to a time in the past.');
194+
195+
// Perform an authenticated request.
196+
$this->drupalGet('/api/v1.5/main', array(), array(
197+
'Authorization' => 'Basic ' . base64_encode($user1->name . ':' . $user1->pass_raw))
198+
);
199+
200+
$user1_post_access_time = db_query('SELECT access FROM {users} WHERE uid = :d', array(':d' => $user1->uid))->fetchObject();
201+
202+
$this->assertEqual($user1_post_access_time->access, REQUEST_TIME, 'Basic authenticated user access time is updated.');
203+
}
204+
141205
}

0 commit comments

Comments
 (0)