0

my doubt is: is there any way to do something like this in codeigniter

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

class Users extends CI_Controller {

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

  //This method only user with admin permission can access this
  public function onlyAdmin(){
  }
  //This method all user  can access this
  public function allUser(){
  }
}

Something like that or more dynamic.

Thanx for you time.

josemm1790
  • 831
  • 2
  • 11
  • 19

2 Answers2

0

The easiest way to do this is use an authentication library since you'll obviously need authentication at some point in your app. There are a large number available for CodeIgniter each with their own merits and weaknesses in different situations.

Have a look at this post for a good overview of a few and see if there's one that'll suit your needs.

You'll probably want one that supports different user groups. That way when a user lands on your page you can simply check their group (admin / user / not-logged-in) and then direct them or render the page accordingly. To do this you could use a case statement that runs a different function for each group. These functions should render the page however you need to with the specific customisations for each user group.

Community
  • 1
  • 1
Daniel Waghorn
  • 2,997
  • 2
  • 20
  • 33
0

There are many ways you could do that, but one of basic is Phill Sturgeon's suggestion about having core classes for any of app using level or make something like this (MY_Controller class) in your APPPATH . 'core' directory:

class MY_Controller extends CI_Controller // prefix MY_ can be found in APPPATH . 'config/config.php' or you can set your own
{
    public __construct()
    {
        parent::__construct();
    }
}

class Admin_controller extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        //pseudo code
        if ($this->session->userdata('level') != 'admin')
        {
            exit("You need admin's privileges to get in here.");
        }
    }
}

class Public_controller extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
}

Than in controllers you would make files/classes that would extend appropriate core class:

class Blog extends Public_controller
{
    public function __construct()
    {
        parent::__construct();
    }
}

or something like:

class Dashboard extends Admin_controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function some_admin_method()
    {
        //
    }
}

Alternativelly, you can check this link too.

Tpojka
  • 6,996
  • 2
  • 29
  • 39