Welcome Guest, Not a member yet? Register   Sign In
Multiple admin areas & user identification - best approach?
#1

I am completely new to CodeIgniter, so please bear with me. 

To better understand and learn how CodeIgniter works, I am currently building a sample job board application with a login area using Ion Auth. The idea here is to have a super administrator, who has complete access to edit and manage users, posts, settings etc. and a normal user group, that can post and edit only their own jobs and profile details.

With this in mind, I would like to show different admin-areas with different functions to the different user groups; one for the super-admin and one for the normal users.

My question now is, how to organize these two admin-dashboards.

I currently use a single controller "Dashboard.php" to basically load different menu-views depending on the users status (admin vs non-admin):


PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Dashboard extends MY_Controller
{
 
   function __construct()
 
   {
 
       parent::__construct();

 
       // Load authentication library
 
       $this->load->library('ion_auth');

 
       // Check user status
 
       if (!$this->ion_auth->logged_in())
 
       {
 
           redirect('auth/login');
 
       }
 
   }

 
   public function index()
 
   {
 
       if ($this->ion_auth->is_admin()) {
 
           $this->load->view('common/header');
 
           $this->load->view('dashboard/top_nav_admin');
 
           echo 'This is the administrators page';
 
           $this->load->view('common/footer');
 
       } else {
 
           $this->load->view('common/header');
 
           $this->load->view('dashboard/top_nav_user');
 
           $this->load->view('dashboard/index_view');
 
           $this->load->view('common/footer');
 
       }

 
   }

 

I am not sure if this is a good idea or if it is better to have a unique controller for every user group, also in order to define different methods for each group. I don't want to repeat much code regarding the views in the admin-area (i.e. basically same design but different menus).


Also, another question concerns how to identify a specific user once he or she is logged in to the admin-area in order to only show his posts / user details.

Is there a way to get the user ID and for instance save it in a session or what is the best approach here?

Thanks for your feedback.
Reply
#2

Your MY_Controller Class can contain other classes.

Save the below as application/core/MY_Controller.php

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * ------------------------------------------------------------------------
 * Editor   : PhpStorm 2016.2.2
 * Date     : 09/29/2016
 * Time     : 7:45 AM
 * Authors  : Raymond L King Sr.
 * ------------------------------------------------------------------------
 *
 * Class        MY_Controller
 *
 * @project     starter
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2016 Pro Covers FX, LLC.
 * @license     http://www.procoversfx.com/license
 * ------------------------------------------------------------------------
 */

class Base_Controller extends CI_Controller {

    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */

    /**
     * Hold's the data for the application views
     *
     * @var array $data
     */
    
public $data = array();


    
/**
     *  __construct ()
     * --------------------------------------------------------------------
     *
     * Class Constructor
     *
     */
    
public function __construct()
    {
        
parent::__construct();


        
log_message('debug''CI : Base_Controller class loaded');


    }
    
    
// -- Add more base methods below here
    
    
    
}    // End of Base_Controller Class.


class Admin_Controller extends Base_Controller
{
    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */


    /**
     *  __construct ()
     * --------------------------------------------------------------------
     *
     * Class    Constructor
     *
     */
    
public function __construct()
    {
        
parent::__construct();

    }


}    
// End of Admin_Controller Class.


class Public_Controller extends Base_Controller
{
    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */


    /**
     *  __construct ()
     * --------------------------------------------------------------------
     *
     * Class    Constructor
     *
     */
    
public function __construct()
    {
        
parent::__construct();

    }

}    
// End of Public_Controller Class.

/**
 * ------------------------------------------------------------------------
 * Filename: MY_Controller.php
 * Location: ./application/core/MY_Controller.php
 * ------------------------------------------------------------------------
 */ 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 10-30-2016, 12:51 PM by cartalot.)

so preface this with these are opinions, different developers will do it different ways.
my suggestion would be to have separate controllers. you take a few minutes in the beginning to set them up, and then you never have to worry about it again. a variation on this would be that the super admin has access to a controller with the shared tools - and they alone have access to a controller with the 'super admin only' tools.

keep your controllers 'thin' - put the real work in the models, so then both controllers can call the common functions and you are not repeating code. however you are going to do your logged in validation - put that code in a model - and call it from the constructor of your controller or your my controller. instead of just confirming this is someone or a super admin that is logged in - return an object (or array) that you can then use for all your methods. for example if you return $this->superadmin in your constructor, then $this->superadmin will be available for all your controller methods, models and views. another bonus is then you don't have to mess with session code at all except for the initial check in. finally make a template so you aren't having to call headers and footers in your controllers.
Reply
#4
Smile 
(This post was last modified: 10-31-2016, 05:31 AM by Neo.)

(10-30-2016, 12:47 PM)cartalot Wrote: so preface this with these are opinions, different developers will do it different ways.
my suggestion would be to have separate controllers. you take a few minutes in the beginning to set them up, and then you never have to worry about it again. a variation on this would be that the super admin has access to a controller with the shared tools - and they alone have access to a controller with the 'super admin only' tools.

keep your controllers 'thin' - put the real work in the models, so then both controllers can call the common functions and you are not repeating code. however you are going to do your logged in validation - put that code in a model - and call it from the constructor of your controller or your my controller. instead of just confirming this is someone or a super admin that is logged in - return an object (or array) that you can then use for all your methods. for example if you return $this->superadmin in your constructor, then $this->superadmin will be available for all your controller methods, models and views. another bonus is then you don't have to mess with session code at all except for the initial check in. finally make a template so you aren't having to call headers and footers in your controllers.

Thanks for your response, cartalot.

Ok, I decided to use 2 controllers for the 2 admin areas: User.php (for normal users) and Admin.php (for the super-admin).

I reworked my original controller a bit, which is now called User.php. As Ion Auth automatically saves the logged in user as a session variable, I decided to instantiate a user object in the constructor, which I can then use in the other methods. 

I am not sure about moving the login-verification into a separate model, as the Ion-Auth-class is already the model that handles all DB-queries etc. Will have to think about that...  Huh

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
User extends MY_Controller
{
 
   public $current_user;

 
   function __construct()
 
   {
 
       parent::__construct();

 
       // Load authentication library
 
       $this->load->library('ion_auth');

 
       // Check user status
 
       if ( ! $this->ion_auth->logged_in())
 
       {
 
           redirect('auth/login');
 
       }

        // Check for admin status
 
       if ($this->ion_auth->is_admin())
 
       {
 
           redirect('auth/login');
 
       }

 
       // Instantiate user object
 
       $this->current_user $this->ion_auth->user()->row();
 
   }

 
   public function index()
 
   {
 
       $data['userdetails'] = $this->current_user->first_name;

 
       $this->load->view('common/header');
 
       $this->load->view('common/top_nav');
 
       $this->load->view('common/test'$data);
 
       $this->load->view('common/footer');
 
   }



Finally, I will have to think about a template-engine like you suggested, but that is a topic for another day I guess...  Wink
Reply
#5

(This post was last modified: 10-31-2016, 06:39 AM by cartalot.)

(10-30-2016, 04:59 PM)Neo Wrote: I decided to instantiate a user object in the constructor, which I can then use in the other methods. 

cool - you will see that will save you so much time. generally in a controller there is something that needs to be referred to by every method. by doing $this->vip in the constructor or first method, it can save a bunch of time.

(10-30-2016, 04:59 PM)Neo Wrote: I am not sure about moving the login-verification into a separate model, as the Ion-Auth-class is already the model that handles all DB-queries etc. Will have to think about that...  Huh

Actually you are doing what i suggested - you are calling it from a model ! Wink
Reply




Theme © iAndrew 2016 - Forum software by © MyBB