Browse Source

ID-Caching (#224)

* ID-Caching
  - Very simple caching mechanism for Group- and Permission-IDS
* Transcations are used in delete-functions
  - Added basic transaction-handling to delete_user(), delete_perm() and delete_group();
* Removed some trailing spaces
master
Christian Land 7 years ago committed by Raphael Jackstadt
parent
commit
5a181c7e75
  1. 110
      application/libraries/Aauth.php

110
application/libraries/Aauth.php

@ -78,6 +78,20 @@ class Aauth {
*/
public $aauth_db;
/**
* Array to cache permission-ids.
* @access private
* @var array
*/
private $cache_perm_id;
/**
* Array to cache group-ids.
* @access private
* @var array
*/
private $cache_group_id;
########################
# Base Functions
########################
@ -106,6 +120,40 @@ class Aauth {
// load error and info messages from flashdata (but don't store back in flashdata)
$this->errors = $this->CI->session->flashdata('errors') ?: array();
$this->infos = $this->CI->session->flashdata('infos') ?: array();
// Pre-Cache IDs
$this->precache_ids();
}
/**
* pre_cache_ids() caches all permission and group IDs for later use.
*/
private function precache_ids() {
// Initialize Variables
$this->cache_perm_id = array();
$this->cache_group_id = array();
// Permissions
$query = $this->aauth_db->get($this->config_vars['perms']);
foreach ($query->result() as $row) {
$key = str_replace(' ', '', trim(strtolower($row->name)));
$this->cache_perm_id[$key] = $row->id;
}
// Groups
$query = $this->aauth_db->get($this->config_vars['groups']);
foreach ($query->result() as $row) {
$key = str_replace(' ', '', trim(strtolower($row->name)));
$this->cache_group_id[$key] = $row->id;
}
}
@ -949,6 +997,8 @@ class Aauth {
*/
public function delete_user($user_id) {
$this->aauth_db->trans_begin();
// delete from perm_to_user
$this->aauth_db->where('user_id', $user_id);
$this->aauth_db->delete($this->config_vars['perm_to_user']);
@ -963,7 +1013,15 @@ class Aauth {
// delete user
$this->aauth_db->where('id', $user_id);
return $this->aauth_db->delete($this->config_vars['users']);
$this->aauth_db->delete($this->config_vars['users']);
if ($this->aauth_db->trans_status() === false) {
$this->aauth_db->trans_rollback();
return false;
} else {
$this->aauth_db->trans_commit();
return true;
}
}
@ -1280,6 +1338,8 @@ class Aauth {
return FALSE;
}
$this->aauth_db->trans_begin();
// bug fixed
// now users are deleted from user_to_group table
$this->aauth_db->where('group_id', $group_id);
@ -1295,7 +1355,16 @@ class Aauth {
$this->aauth_db->delete($this->config_vars['group_to_group']);
$this->aauth_db->where('id', $group_id);
return $this->aauth_db->delete($this->config_vars['groups']);
$this->aauth_db->delete($this->config_vars['groups']);
if ($this->aauth_db->trans_status() === false) {
$this->aauth_db->trans_rollback();
return false;
} else {
$this->aauth_db->trans_commit();
return true;
}
}
//tested
@ -1499,14 +1568,14 @@ class Aauth {
if( is_numeric($group_par) ) { return $group_par; }
$query = $this->aauth_db->where('name', $group_par);
$query = $this->aauth_db->get($this->config_vars['groups']);
$key = str_replace(' ', '', trim(strtolower($group_par)));
if ($query->num_rows() == 0)
return FALSE;
if (isset($this->cache_group_id[$key])) {
return $this->cache_group_id[$key];
} else {
return false;
}
$row = $query->row();
return $row->id;
}
/**
@ -1628,6 +1697,8 @@ class Aauth {
$perm_id = $this->get_perm_id($perm_par);
$this->aauth_db->trans_begin();
// deletes from perm_to_gropup table
$this->aauth_db->where('perm_id', $perm_id);
$this->aauth_db->delete($this->config_vars['perm_to_group']);
@ -1638,7 +1709,16 @@ class Aauth {
// deletes from permission table
$this->aauth_db->where('id', $perm_id);
return $this->aauth_db->delete($this->config_vars['perms']);
$this->aauth_db->delete($this->config_vars['perms']);
if ($this->aauth_db->trans_status() === false) {
$this->aauth_db->trans_rollback();
return false;
} else {
$this->aauth_db->trans_commit();
return true;
}
}
/**
@ -1905,14 +1985,14 @@ class Aauth {
if( is_numeric($perm_par) ) { return $perm_par; }
$query = $this->aauth_db->where('name', $perm_par);
$query = $this->aauth_db->get($this->config_vars['perms']);
$key = str_replace(' ', '', trim(strtolower($perm_par)));
if ($query->num_rows() == 0)
return FALSE;
if (isset($this->cache_perm_id[$key])) {
return $this->cache_perm_id[$key];
} else {
return false;
}
$row = $query->row();
return $row->id;
}
/**

Loading…
Cancel
Save