简体   繁体   中英

make dynamic switch to different database based on logged in user's credentials?

Database.php

<?php
class Database extends CI_Controller {
    public $branch_db;

    function __construct($company_name, $branch_name) {
        parent::__construct();
        $branch_db = $this->load->database($company_name.'_db_'.$branch_name);
    }
}
?>

Account_model.php

protected function verifyLogin($username, $password) {
    $this->db->trans_start();

    $sql = "SELECT password, company_id, branch_id
                FROM account
                WHERE username = {$username}"; //Prepare statement

    $query = $this->db->query($sql);
    $result_num = $query->num_rows();

    if ($result_num == 1) {
        $first_row = $query->row();
        $stored_password = $first_row->password;

        if (password_verify($password, $stored_password)) {
            //Successful login
            //Get company name
            //Get branch name
            //Set database
            //Pass company_name, branch_name
            //All models must be able to access the dynamically loaded database
        }
    }
    $this->db->trans_complete();
}

There are multiple databases that can be accessed. For each branch there is a dynamically created database. Each branch has an account. When an account is logged in, the database related to that account will be accessed. That database will then be used for the entire session until the user logs out.

I have a main database where a master list of the valid username/password combinations are stored. I had that declared in my database.php in the config folder. But after the user logs in, I will need to switch to the database related to that user account so they can access their data.

How can I use this controller for my models so they all access that same database (without having to redo $this->load->database() over and over)?

In Your database.php Application->config->database.php I am expecting that you create a session and store admin_type variable as it logged in.

$admin_type = $this->session->userdata['admin_type'];
$active_group = 'default';
$query_builder = TRUE;
 if ($admin_type==1) {
   $db['default'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => 'root',
        'database' => 'name_db',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => (ENVIRONMENT !== 'production'),
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
    );
}else{
    $db['default'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => 'root',
        'database' => 'name_db2',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => (ENVIRONMENT !== 'production'),
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
    );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM