Direct access

Contact

tomcode.com

0.4367 sec

Google tracked

Direct Access

Introduction

This model allows to override implemented Authorization / Identification schemes.

This little app demonstrates it's basic usage. You can

  1. Create a direct access URL.
  2. Access a protected page one time with it.

You can use anything which CI considers as email to create an access. Create

The model code

<?php

/**
 * Non-intrusive Direct Access Model
 *
 * This model creates and controls direct access tokens
 * which can be used for features like forgotten password.
 *
 * @package    CodeIgniter
 * @subpackage    Models
 * @category    Authorization
 * @author        Thomas Traub
 * @link        http://www.tomcode.com/inside/codeigniter/direct_access
 */
class Direct_access_model extends Model {

    var 
$db_table 'direct_access';
    var 
$hash_field 'hash';
    
    
/** 
     * Creates, stores and returns the direct
     * access token for the passed condition. 
     * Overwrites an exiting old entry.
     * 
     * @param array associative the database query condition
     * @return string the access URL param
     */
    
function set_access($condition)
    {
        
// create hash
        
$data[$this->hash_field] = str_shuffle(uniqid('' .rand(032768)));

        
// look for an existing entry
        
$query $this->db->getwhere($this->db_table$condition10);
        
$row $query->row();
        
        
//  update or insert
        
if ($query->num_rows == 0
        {
            
$condition array_merge($condition$data);
            
            
$this->db->insert($this->db_table$condition); 
        }
        else 
$this->db->update($this->db_table$data$condition);
        
        
// return only in case of success
        
if($this->db->affected_rows() == 1) return array_merge($condition$data);
    }
    
    
/**     
     * Returns the ident data for a passed condition (hash token),
     * and, by default, deletes the found entry.
     * 
     * @param array associative the database query condition
     * @param boolean shall the entry be deleted
     * @return mixed boolean:false or object: the query row
     */
    
function get_access($condition$delete_condition true)
    {
        
$query $this->db->getwhere($this->db_table$condition10);
        
        if (
$query->num_rows != 1) return FALSE;
        
        if(
$delete_condition$this->delete_access($condition);
        
        return 
$query->row();
    }
    
    
/**     
     * Deletes an entry based on the passed query condition
     * 
     * @param array associative the database query condition
     */
    
function delete_access($condition)
    {
        
$this->db->delete($this->db_table$condition); 
    }
}