Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache users groups on demand to prevent multiple sql queries #308

Merged
merged 2 commits into from
Sep 20, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions libraries/Ion_auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class Ion_auth
**/
public $_extra_set = array();

/**
* caching of users and their groups
*
* @var array
**/
public $_cache_user_in_group;

/**
* __construct
*
Expand All @@ -62,6 +69,8 @@ public function __construct()
$this->load->model('ion_auth_mongodb_model', 'ion_auth_model') :
$this->load->model('ion_auth_model');

$this->_cache_user_in_group =& $this->ion_auth_model->_cache_user_in_group;

//auto-login the user if they are remembered
if (!$this->logged_in() && get_cookie('identity') && get_cookie('remember_code'))
{
Expand Down Expand Up @@ -424,41 +433,39 @@ public function is_admin($id=false)
public function in_group($check_group, $id=false)
{
$this->ion_auth_model->trigger_events('in_group');

$id || $id = $this->session->userdata('user_id');

$users_groups = $this->ion_auth_model->get_users_groups($id)->result();

$groups_by_name = array();
$groups_by_id = array();

foreach ($users_groups as $group)
if (!is_array($check_group))
{
$groups_by_name[] = $group->name;
$groups_by_id[] = $group->id;
$check_group = array($check_group);
}

if (is_array($check_group))
if (isset($this->_cache_user_in_group[$id]))
{
foreach($check_group as $key => $value)
$groups_array = $this->_cache_user_in_group[$id];
}
else
{
$users_groups = $this->ion_auth_model->get_users_groups($id)->result();
$groups_array = array();
foreach ($users_groups as $group)
{
$groups = (is_string($value)) ? $groups_by_name : $groups_by_id;

if (in_array($value, $groups))
{
return TRUE;
}
$groups_array[$group->id] = $group->name;
}
$this->_cache_user_in_group[$id] = $groups_array();
}
else
foreach ($check_group as $key => $value)
{
$groups = (is_string($check_group)) ? $groups_by_name : $groups_by_id;
$groups = (is_string($value)) ? $groups_array : array_keys($groups_array);

if (in_array($check_group, $groups))
if (in_array($value, $groups))
{
return TRUE;
}
}

return FALSE;
}

}
Loading