Welcome Guest, Not a member yet? Register   Sign In
postgres users authentication
#1

[eluser]XeRGi0[/eluser]
Hello there CI loverss... I have a lil' problem... I'm trying to implement an tiny authentication system, with DBMS users (postgre) instead of tables...

I auto load the database library with a dbms superuser, 'cause i need it to make the validations, to do what i wanted i got to override default database configuration.

The thing is, I made all the validations and stuff... the problem i have is when i'm trying to create a new connection (the new logged user information, such as username and password), it seems that is not creating the connection based on the new configuration i pass throw...

I use a model, a controller and a view...

Model:
Code:
<?php
    Class Auth_model extends Model {
        
        function Auth_model (){
            parent::Model();
            $this->load->database(); // I tried removing database class autoload and calling only this time, but it doesn't work either
        }
        
        function checkUser($str) {
                  //some statements
        }
        
        function checkPass($user, $pass) {
                  //some statements
        }
        
        function login($user, $pass) {
            $this->db->close();
            
            $db['hostname'] = "localhost";
            $db['username'] = $user;
            $db['password'] = $pass;
            $db['database'] = "mydb";
            $db['dbdriver'] = "postgre";
            $db['dbprefix'] = "";
            $db['pconnect'] = TRUE;
            $db['db_debug'] = TRUE;
            $db['cache_on'] = FALSE;
            $db['cachedir'] = "";
            $db['char_set'] = "utf8";
            $db['dbcollat'] = "utf8_general_ci";
            
            $this->load->database($db);
        }
        
    }
?>

Controller:
Code:
<?php
    Class Login extends Controller {
        
        function Login() {
            parent::Controller();
            $this->load->model("auth_model");
        }
        
        function index() {
            $this->load->view('loginForm');
        }
        
        function _checkUser($str) {
                  //some statement
        }
        
        function _checkPass($user, $pass) {
                  //some statements
        }
        
        function do_login() {
            $user = $this->input->post('user', TRUE);
            $pass = $this->input->post('pass', TRUE);
            
                $this->auth_model->login($user, $pass);
                echo $this->db->get_user(); //a function that i added to postgre driver class
            
        }
        
    }
?>

Plz Help! Sad
#2

[eluser]XeRGi0[/eluser]
Well people... I was all night trying to make this work...

This is what i have so far... I have disabled the database autoload and make it all manually model by model, but I have another problem... I'll show you...

My auth model, called by the controller, no big deal here...
Code:
function login($user, $pass) {
            //I set the data for the DNS
            $dns = 'postgre://'.$user.':'.$pass.'@localhost/mydb_dev';
            
            //I store my data in the session so i can create further connections
            $values = array (
                        'userid'    =>    $user,
                        'password'    =>    $pass,
                        'dns'        =>    $dns,
                    );
            $this->session->set_userdata($values);
            
            //I create the new connection
            //$this->db->close();
            $this->load->database($dns, FALSE, TRUE);
        }

So far it's everything ok, but when i tried to access one of my controllers... I get this error message:
Quote:Fatal error: Call to a member function get() on a non-object in D:\Tools\apps\DESARROLLO\system\application\models\categorias_model.php on line 26

I only load the database class in the model, my controller only calls that function and show the data.. So that's way i don't understand why i get that error...

This is my model:
Code:
<?php
    Class Categorias_model extends Model {
        /*
         * Table: categorias;
         * Fields:
         *     cat_codcategoria
         *     cat_nombre
         */    
        var $cat_nombre;
        
        function Categorias_model () {
            parent::Model();
            // Create a new connection
            $dns = $this->session->userdata('dns');
            print_r($dns);
            $this->load->database($dns, FALSE, TRUE);
        }
        
        function getCategoria($id) {
            $this->db->where("cat_codcategoria", $id);
            $query = $this->db->get("categorias");
            return $query->row();
        }
        
        function getCategorias() {
            $query = $this->db->get("categorias");
            return $query->result_array();
        }
}
?>

Again.. Plz Help!
#3

[eluser]Pascal Kriete[/eluser]
I assume you read the section on connecting to multiple databases here.

When you use the normal loading call, the database is attached to the CI super object as $db.
Code:
$this->load->database();  // no third parameter

// Use it:
$this->db->whatever();

But when you pass in that third parameter, the database object is returned instead of being tied to $db. So you would do:
Code:
$DB = this->load->database($dns, NULL, TRUE);

// Use it:
$DB->whatever();

If you need it for more than one method, you will want it as a class variable. Try this:
Code:
Class Categorias_model extends Model {
        /*
         * Table: categorias;
         * Fields:
         *     cat_codcategoria
         *     cat_nombre
         */    
        var $cat_nombre;
        
        function Categorias_model () {
            parent::Model();
            // Create a new connection
            $dns = $this->session->userdata('dns');
            print_r($dns);
            $this->DB1 = $this->load->database($dns, FALSE, TRUE);
        }
        
        function getCategoria($id) {
            $this->DB1->where("cat_codcategoria", $id);
            $query = $this->DB1->get("categorias");
            return $query->row();
        }
        
        function getCategorias() {
            $query = $this->DB1->get("categorias");
            return $query->result_array();
        }
}

Hope that helps.
#4

[eluser]XeRGi0[/eluser]
It works, but it wasn't the third parameter, it was the second, the third is to set the active record to true... I set the second parameter to TRUE and make the changes you told me and works...

I didn't get the whole idea of using multiples databases, 'cause i wasn't using multiple databases, i just wanted to change the connection information (user and password).

Know... If I assign a new connection for every model I have... won't overload my dbms? All the other connections will be open?

I think CI is not that flexible to work with this type of authentication... I think is better in case you want to prevent user to access to certain tables, you make all the validations in the dbms, and let him do the job for you...

Going back to my first question on this thread... Is there a way to override the database configuration? To avoid the calling to the database class for every single model?

Thanx for your response




Theme © iAndrew 2016 - Forum software by © MyBB