Kevin
Kevin

Reputation: 653

How to separate admin and user in codeigniter?

I have two pages for admin and user. When admin logged in it will redirect to http://localhost/ci3/admin/Adminhomepage and when user logged in it will redirect to http://localhost/ci3/user/Userhomepage. After logged in as user if I changed url like thishttp://localhost/ci3/admin/Adminhomepage it will directly go to admin home page. I want like it will go to admin login page http://localhost/ci3/admin/Adminlogin. I have userId and permissions for admin like view,edit,add and deleteRole. In this anyone is set to 1 then he is admin otherwise he is user. I want to write that logic in Admin_controller.

Admin_controller

<?php
class Admin_controller extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        $this->load->model("Adminmodel","",true);   
        
        $userId =  $this->session->userdata('cp_adminid');
        
        $toCheck = $this->Adminmodel->tocheckadmin($userId);            
    }
}
?>

Admin_model

public function tocheckadmin($userId){
        $sql = "SELECT * FROM users u
            INNER JOIN roles r
            ON r.roleId = u.roleId
            INNER JOIN permissions p
            ON p.roleId = r.roleId
            INNER JOIN module m
            ON m.moduleId = p.moduleId
            WHERE userId= $userId
            AND (p.view == 1 or p.add == 1 or p.edit == 1 or p.deleteRole == 1)";
}

Upvotes: 1

Views: 2480

Answers (2)

Jethro Hazelhurst
Jethro Hazelhurst

Reputation: 3285

I solve this by setting the users permissions in a session variable when logging them in.

$data = ['is_admin'     => true];
$this->session->set_userdata($data);

Then in the controllers for the admin area I do a check in the __constructor() method

    /**
     * login protect
     */
    public function __construct()
    {
        if (!$this->session->userdata('is_admin')) {
            redirect('admin/account/login');
        }
    }

Here is the update code for your example with comments

/**
 * tocheckadmin
 */
public function tocheckadmin($admin_id)
{
    // get the admin
    $sql = "SELECT * FROM users u
            INNER JOIN roles r
            ON r.roleId = u.roleId
            INNER JOIN permissions p
            ON p.roleId = r.roleId
            INNER JOIN module m
            ON m.moduleId = p.moduleId
            WHERE userId= $userId
            AND (p.view == 1 or p.add == 1 or p.edit == 1 or p.deleteRole == 1)";
    $query = $this->db->query($sql);
    return $query->row(); // use row() to return a single object
}

/**
 *
 */
class Admin_controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model("Adminmodel","",true);

        $userId =  $this->session->userdata('cp_adminid');

        $toCheck = $this->Adminmodel->tocheckadmin($userId);

        if ($toCheck === false) {

            // then the user is not an admin
            // redirect to the admin login

        }
    }
}

Upvotes: 0

sandip kakade
sandip kakade

Reputation: 1356

So, in the main index.php file change the application path, i.e. change this:

$application_folder = 'application';

To:

`$application_folder = 'application/frontend';`

For the admin application create a directory, let's say backend, put a copy of the index.php file there and make it point to the admin app, so:

$application_folder = 'application/admin';

To complete the operation put a copy of the .htaccess file inside the backend directory and change it to use this directory as base of the rewrites:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /backend/index.php/$1 [L]

Upvotes: 1

Related Questions