Skip to content

Commit

Permalink
[#11] display users in recent-seen order and update with every time t…
Browse files Browse the repository at this point in the history
…heir user session is echecked
  • Loading branch information
rcoup committed Feb 23, 2011
1 parent 106f592 commit 64c6a2d
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 10 deletions.
2 changes: 1 addition & 1 deletion application/controllers/admin/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function index()
));

$users = ORM::factory('user')
->orderby('name', 'asc')
->orderby('last_login', 'desc')
->find_all((int) Kohana::config('settings.items_per_page_admin'),
$pagination->sql_offset);

Expand Down
1 change: 1 addition & 0 deletions application/i18n/en_US/ui_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
'header_manage_users' => 'Manage Users',
'header_role' => 'Role',
'header_user' => 'User',
'header_last_seen' => 'Last Seen',
'help' => 'help',
'hourly' => 'Hourly',
'incident_feed' => 'Incident feed for',
Expand Down
18 changes: 12 additions & 6 deletions application/views/admin/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@
<th class="col-2">
<?php echo Kohana::lang('ui_admin.header_user'); ?>
</th>
<th class="col-2">
<th class="col-3">
<?php echo Kohana::lang('ui_admin.header_role');?>
</th>
<th class="col-4">
<?php echo Kohana::lang('ui_admin.header_actions')?>
<?php echo Kohana::lang('ui_admin.header_last_seen');?>
</th>
<th class="col-5">
<?php echo Kohana::lang('ui_admin.header_actions');?>
</th>
</tr>
</thead>
<tfoot>
<tr class="foot">
<td colspan="4">
<td colspan="5">
<?php echo $pagination; ?>
</td>
</tr>
Expand All @@ -54,7 +57,7 @@
{
?>
<tr>
<td colspan="4" class="col">
<td colspan="5" class="col">
<h3>
<?php echo Kohana::lang('ui_admin.no_result_display_msg');?>
</h3>
Expand All @@ -69,7 +72,7 @@
$password = $user->password;
$name = $user->name;
$email = $user->email;

$last_seen = date::timespan_string(intval($user->last_login), NULL, 'hours,minutes') or '-';
foreach ($user->roles as $user_role) {
$role = $user_role->name;
}
Expand All @@ -91,6 +94,9 @@
<?php echo strtoupper($role); ?>
</td>
<td class="col-4">
<?php echo $last_seen; ?>
</td>
<td class="col-5">
<ul>
<li class="none-separator"><a href="<?php echo url::site() . 'admin/users/edit/' . $user_id; ?>">
<?php echo Kohana::lang('ui_admin.edit_action');?>
Expand All @@ -111,4 +117,4 @@
</div>
<?php print form::close(); ?>
</div>
</div>
</div>
10 changes: 9 additions & 1 deletion modules/auth/libraries/drivers/Auth/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public function logged_in($role)
}
}

if ( $status )
{
// Set the last login date
// we're repurposing this for last-active
$user->last_login = time();
$user->save();
}

return $status;
}

Expand Down Expand Up @@ -226,4 +234,4 @@ protected function complete_login(User_Model $user)
return parent::complete_login($user);
}

} // End Auth_ORM_Driver
} // End Auth_ORM_Driver
237 changes: 237 additions & 0 deletions modules/auth/libraries/drivers/Auth/ORM.php.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* ORM Auth driver.
*
* $Id: ORM.php 3917 2009-01-21 03:06:22Z zombor $
*
* @package Auth
* @author Kohana Team
* @copyright (c) 2007-2008 Kohana Team
* @license http://kohanaphp.com/license.html
*/
class Auth_ORM_Driver extends Auth_Driver {

/**
* Checks if a session is active.
*
* @param string role name
* @param array collection of role names
* @return boolean
*/
public function logged_in($role)
{
$status = FALSE;

// Get the user from the session
$user = $this->session->get($this->config['session_key']);

if (is_object($user) AND $user instanceof User_Model AND $user->loaded)
{
// Everything is okay so far
$status = TRUE;

if ( ! empty($role))
{

// If role is an array
if (is_array($role))
{
// Check each role
foreach ($role as $role_iteration)
{
if ( ! is_object($role_iteration))
{
$role_iteration = ORM::factory('role', $role_iteration);
}
// If the user doesn't have the role
if( ! $user->has($role_iteration))
{
// Set the status false and get outta here
$status = FALSE;
break;
}
}
}
else
{
// Else just check the one supplied roles
if ( ! is_object($role))
{
// Load the role
$role = ORM::factory('role', $role);
}

// Check that the user has the given role
$status = $user->has($role);
}
}
}

if ( $status )
{
// Set the last login date
// we're repurposing this for last-active
$user->last_login = time();
$user->save();
}

return $status;
}

/**
* Logs a user in.
*
* @param string username
* @param string password
* @param boolean enable auto-login
* @return boolean
*/
public function login($user, $password, $remember)
{
if ( ! is_object($user))
{
// Load the user
$user = ORM::factory('user', $user);
}

// If the passwords match, perform a login
if ($user->has(ORM::factory('role', 'login')) AND $user->password === $password)
{
if ($remember === TRUE)
{
// Create a new autologin token
$token = ORM::factory('user_token');

// Set token data
$token->user_id = $user->id;
$token->expires = time() + $this->config['lifetime'];
$token->save();

// Set the autologin cookie
cookie::set('authautologin', $token->token, $this->config['lifetime']);
}

// Finish the login
$this->complete_login($user);

return TRUE;
}

// Login failed
return FALSE;
}

/**
* Forces a user to be logged in, without specifying a password.
*
* @param mixed username
* @return boolean
*/
public function force_login($user)
{
if ( ! is_object($user))
{
// Load the user
$user = ORM::factory('user', $user);
}

// Mark the session as forced, to prevent users from changing account information
$_SESSION['auth_forced'] = TRUE;

// Run the standard completion
$this->complete_login($user);
}

/**
* Logs a user in, based on the authautologin cookie.
*
* @return boolean
*/
public function auto_login()
{
if ($token = cookie::get('authautologin'))
{
// Load the token and user
$token = ORM::factory('user_token', $token);

if ($token->loaded AND $token->user->loaded)
{
if ($token->user_agent === sha1(Kohana::$user_agent))
{
// Save the token to create a new unique token
$token->save();

// Set the new token
cookie::set('authautologin', $token->token, $token->expires - time());

// Complete the login with the found data
$this->complete_login($token->user);

// Automatic login was successful
return TRUE;
}

// Token is invalid
$token->delete();
}
}

return FALSE;
}

/**
* Log a user out and remove any auto-login cookies.
*
* @param boolean completely destroy the session
* @return boolean
*/
public function logout($destroy)
{
if (cookie::get('authautologin'))
{
// Delete the autologin cookie to prevent re-login
cookie::delete('authautologin');
}

return parent::logout($destroy);
}

/**
* Get the stored password for a username.
*
* @param mixed username
* @return string
*/
public function password($user)
{
if ( ! is_object($user))
{
// Load the user
$user = ORM::factory('user', $user);
}

return $user->password;
}

/**
* Complete the login for a user by incrementing the logins and setting
* session data: user_id, username, roles
*
* @param object user model object
* @return void
*/
protected function complete_login(User_Model $user)
{
// Update the number of logins
$user->logins += 1;

// Set the last login date
$user->last_login = time();

// Save the user
$user->save();

return parent::complete_login($user);
}

} // End Auth_ORM_Driver
4 changes: 2 additions & 2 deletions system/helpers/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ public static function timespan_string($time1, $time2 = NULL, $output = 'years,m
}

// Add the amount to the span
$span[] = ($name === $last ? ' and ' : ', ').$amount.' '.($amount === 1 ? inflector::singular($name) : $name);
$span[] = ((count($span)>0 and $name === $last) ? ' and ' : ', ').$amount.' '.($amount === 1 ? inflector::singular($name) : $name);
}

// Replace difference by making the span into a string
Expand All @@ -396,4 +396,4 @@ public static function timespan_string($time1, $time2 = NULL, $output = 'years,m
return $difference;
}

} // End date
} // End date

0 comments on commit 64c6a2d

Please sign in to comment.