E
E
Eugene2019-05-16 14:06:56
CodeIgniter
Eugene, 2019-05-16 14:06:56

How to implement a function that should check if the user has client_id != 0, then redirect the authorization to the https://$client_id.domain.com subdomain?

each client who registers on the site www.domain.com receives its own subdomain $client_id.domain.com with its own isolated database, the user is recorded as a client in the main database www.domain.com and in the database of his subdomain as an administrator.
when authorizing a user on www.domain.com, the function should determine $client_id and if $client_id != 0 then redirect authorization to $client_id.domain.com
model

function authenticate($email, $password) {
        $this->db->select("id,user_type,client_id");
        $result = $this->db->get_where($this->table, array('email' => $email, 'password' => md5($password), 'status' => 'active', 'deleted' => 0, 'disable_login' => 0));
        if ($result->num_rows() == 1) {
            $user_info = $result->row();

            //check client login settings
            if ($user_info->user_type === "client" && get_setting("disable_client_login")) {
                return false;
            } else if ($user_info->user_type === "client") {
                //user can't be loged in if client has deleted
                $clients_table = $this->db->dbprefix('clients');

                $sql = "SELECT $clients_table.id
                        FROM $clients_table
                        WHERE $clients_table.id= $user_info->client_id AND $clients_table.deleted=0
                        ";
                $client_result = $this->db->query($sql);
                if (!$client_result->num_rows()) {
                    return false;
                }
            }

            $this->session->set_userdata('user_id', $user_info->id);
            return true;
        }
    }

    function login_user_id() {
        $login_user_id = $this->session->user_id;
        return $login_user_id ? $login_user_id : false;
    }

Controller
function index() {
        if ($this->Users_model->login_user_id()) {
            redirect('dashboard/view');
        } else {

            $view_data["redirect"] = "";
            if (isset($_REQUEST["redirect"])) {
                $view_data["redirect"] = $_REQUEST["redirect"];
            }


            //check if there reCaptcha is enabled
            //if reCaptcha is enabled, check the validation
            if (get_setting("re_captcha_secret_key")) {
                $this->form_validation->set_rules('g-recaptcha-response', '', 'callback_check_recaptcha');
            }


            $this->form_validation->set_rules('email', '', 'callback_authenticate'); //авторизация
            $this->form_validation->set_error_delimiters('<span>', '</span>');


            if ($this->form_validation->run() == FALSE) {

                $this->load->view('signin/index', $view_data);
            } else {

                if ($view_data["redirect"]) {
                    redirect($view_data["redirect"]);
                } else {
          
          redirect('dashboard/view');
          
                }
            }
        }
    }



 // check authentication
    function authenticate($email) {

        //don't check password if there is any error
        if (validation_errors()) {
            $this->form_validation->set_message('authenticate', "");
            return false;
        }


        $password = $this->input->post("password");
        if (!$this->Users_model->authenticate($email, $password)) {
            $this->form_validation->set_message('authenticate', lang("authentication_failed"));
            return false;
        }
        return true;
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question