Skip to content

Instantly share code, notes, and snippets.

@joelmandell
Created November 2, 2015 21:04
Show Gist options
  • Save joelmandell/5fba5171b33edfba6e8c to your computer and use it in GitHub Desktop.
Save joelmandell/5fba5171b33edfba6e8c to your computer and use it in GitHub Desktop.
CodeIgniter kod snutt 1 - Authklass.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*TODO Make comment about the goal with this class and quick examples!!!!!
All those code-duplicated $CI = & get_instance(); references, need to do something about it.
Make use of the now null initialized $this->CI variable, so the procedure doesn't get repated all the time.
*/
/*
Auth class is an helper to easy interact with user information.
And of course it's easy to move this and reuse code for other projects!!!
* Get their permissions.
* Log in.
* Log out.
* Get user id
* User email
*/
class Auth {
var $userid=null,$CI=null;
function Auth()
{
$CI =& get_instance(); //Reference to the CodeIgniter class Library, so we can call it's API functions and helper, library functions.
$CI->load->database();
$CI->load->library('session');
}
function no_auth()
{
$CI->load->model('auth');
return $CI->auth->no_auth();
}
function get_groups()
{
$CI =& get_instance(); //Reference to the CodeIgniter class Library, so we can call it's API functions and helper, library functions.
$groups=array();
$query = $CI->db->query("SELECT * FROM users WHERE id LIKE ".$CI->db->escape($this->get_session_user_id())."");
foreach ($query->result() as $row)
{
$groups[]=$row->groups;
}
return $groups;
}
function set_session_group_id_to_names()
{
$CI =& get_instance();
/*
Parse selected mysql database for tables with nameprefix prefix_permissions.
Store them in an session, tokenizing the permissions to their according function.
session(blog_permissions,
*/
}
function get_group_names()
{
$CI =& get_instance(); //Reference to the CodeIgniter class Library, so we can call it's API functions and helper, library functions.
$session_groups=explode("|",$this->get_session_groups());
$groups = array();
foreach($session_groups as $session_group)
{
$query = $CI->db->query("SELECT * FROM groups WHERE id LIKE ".$CI->db->escape($session_group)."");
foreach ($query->result() as $row)
{
$groups[] = $row->groupname;
}
}
return $groups;
}
function get_user_name()
{
$CI =& get_instance(); //Reference to the CodeIgniter class Library, so we can call it's API functions and helper, library functions.
$value=$CI->session->userdata('username');
return $value;
}
function get_session_user_status()
{
$CI =& get_instance(); //Reference to the CodeIgniter class Library, so we can call it's API functions and helper, library functions.
$status=$CI->session->userdata('authenticated');
return $status;
}
function get_session_user_id()
{
$CI =& get_instance(); //Reference to the CodeIgniter class Library, so we can call it's API functions and helper, library functions.
$status=$CI->session->userdata('userid');
return $status;
}
function get_session_groups()
{
$CI =& get_instance(); //Reference to the CodeIgniter class Library, so we can call it's API functions and helper, library functions.
$status=$CI->session->userdata('groups');
return $status;
}
function set_session_permissions()
{
$groups=explode("|",$this->get_session_groups()); //Create an array and split where it is this delimiter: '|'
$CI =& get_instance(); //Reference to the CodeIgniter class Library, so we can call it's API functions and helper, library functions.
ob_start();
$CI->load->library('session');
if(count($groups)!=1)
{
foreach($groups as $group)
{
/*Find all tables with the name convention FOOtable_permissions, and get their permissions.
This is for general permissions for modules and administration options.
It's an another table "user_module_permissions" that takes care of permissions on a per item basis.
For example a specific blog post could be changed by a specific user, and an another can not be changed.
*/
$query = $CI->db->query("SHOW TABLES LIKE '%permissions%' ");
for($i=0;$query->num_rows()>$i;$i++)
{
$tables=$query->row_array($i); //Get the permission tables.
foreach ($tables as $table_name) //Iterate through them.
{
/*Get the columns in the table '$table_name', those columns holds the function names and if a specific group can access it.*/
$query_for_columns=$CI->db->query("select column_name from information_schema.columns where table_name like ".$CI->db->escape($table_name)."");
/*why in the world $i2=2 ? Yeah, because we dont want to get the id and groupid column. (They are the 2 first ones)*/
for($i2=2;$query_for_columns->num_rows()>$i2;$i2++)
{
$permissions=$query_for_columns->row_array($i2);
foreach($permissions as $permission)
{
$values = $CI->db->query("SELECT * FROM $table_name WHERE `group` like ".$CI->db->escape($group)."");
if($values->num_rows())
{
/*If a permission session variable value equals with zero, it equals no access for the user to the
corresponding function/table/etcetera.
*/
$is_set=$CI->session->userdata($table_name."@".$permission);
if($is_set!=0) //If the value is not zero then we dont need to do anything.
{
/*A permission is already set, that means, we shouldnt override it.
Otherwise it wouldnt work out to have different groups with differnet permissions.
IE, you are in a group responsible for articles about music, the permissions
for this groups gives the possibility to just post articles to the music category.
But one week the music group gets new responsibilities, posting articles about movies!
Otherwise they wouldnt have that priviligie. So the groups they belong to is now:
music|video. The permissions for group music states otherwise that they cannot
access video. But the video group overrides it.
*/
} else { //Okay, the permission is not set, lets do this!!!!!!
$CI->session->set_userdata($table_name."@".$permission, $values->row()->$permission);
/*This creates a session variable with the nameprefix $table_name@edit,
and append the value from the query result to it, wheter it be 0(no access) or 1(access)
For example, it could be permission for the blog module, then the session name will be
blog_permissions@edit or blog_permissions@delete etcetera..
*/
}
}
}
}
}
}
}
}
}
function login($user,$pass)
{
$CI =& get_instance(); //Reference to the CodeIgniter class Library, so we can call it's API functions and helper, library functions.
$CI->load->model('AuthModel');
$salt_str="6fc9205039ece914f0db009bb5fd321b"; //MD5 of my FOOBAR
$salt = sha1(md5($salt_str.$pass)); //Create a salt algorithm.
$pass = md5($pass.$salt); //Scramble it around...
$query = $CI->db->query("SELECT * FROM users WHERE username LIKE ".$CI->db->escape($user)." AND password LIKE ".$CI->db->escape($pass)." ");
if($query->num_rows() > 0)
{
if($query->row()->active==1) //Only login if the user account is activ(e)ated. Active equals 1, and inactive 0.
{
$CI->session->set_userdata('authenticated','true'); //Howdy Authy world. Houston here calling earth.
$CI->session->set_userdata('userid',$query->row()->id); //So we dont have to make db queries all the time to get that user id.
$CI->session->set_userdata('username',$query->row()->username); //Ahooy skipper, whats your name? Guybrush Threepwood?
$data=$this->get_groups(); //Store the group or groups an array.
$groups=null; //Initialize the groups array.
foreach($data as $item) //Iterate through the $data variable where the groups are stored.
{
$groups.=$item."|"; //Append group per group and add an tokenizer.
}
$CI->session->set_userdata('groups',$groups); //Set the session with the generated variable.
$this->set_session_permissions(); //Now when we have registered the users groups, we can set their respective permissions as session variables.
return "login-sucess";
} else { //Not active...right'e'o! Maybe a nasty user or he didnt yet activate his account from the email message :=)
return "not-active";
}
} else {
return "login-failed";
}
}
function logout()
{
$CI =& get_instance();
$CI->session->sess_destroy();
redirect("../../user/", "refresh");
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment