• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Admin Class

#1
[eluser]GI-Joe[/eluser]
I have to create a Admin class which should be accessible to admin only.
I have maintain same DB table for admin and other users containing username and passwords and level(say 0 for User, 1 for Admin).

Once the user login system will check his level and create session for it.

How should I create the session as I am new to CodeIgniter please help me out. I have separate controller and model for users. Login verification is done in user model itself.

Where and how should I create session for level of user.

Please let me know if I am going wrong

Can I do something like this

Code:
Class Admin extends CI_Controller
{

function __construct()
{
  /* Can I check the level through session here so that only admin can access this class.
If user is not admin he will be redirected to user home page if he tried to access the methods in this controller class via URL */

}

}


Please do reply and help me as I am new to this framework.
If you have better solution please do post it.


#2
[eluser]InsiteFX[/eluser]
I would check it in the controller that is sending you to your admin backend.

#3
[eluser]GI-Joe[/eluser]
[quote author="InsiteFX" date="1339170657"]I would check it in the controller that is sending you to your admin backend.
[/quote]

You mean in login validation method.
But user can access admin methods via urls as is_looged_in will be true if he is a user.

if you can give skeleton of code would be great

#4
[eluser]InsiteFX[/eluser]
IF you place a underscore _ at the front of a CodeIgniter function it makes it private and can not be accessed from the url. But you can access it from your code.

#5
[eluser]PhilTem[/eluser]
Code:
class Admin_Controller extends CI_Controller {

function __construct()
{
  if ( ! $this->your_auth_class->user_is_admin() )
  {
    redirect('/* to standard user page /*');
  }
}

function display_users()
{
  // Do something to display your users. This page is only accessible if the user is an authenticated admin
}

}

And your function inside your auth class to check if the user has admin-level

Code:
function user_is_admin()
{
  if ( $this->session->userdata('is_admin') !== FALSE )
  {
    return TRUE;
  }
  
  return FALSE;
}

The session will be set by you once the user's credentials are validated successfully.


That should be enough skeleton for you to learn how to do it properly. If you don't fail on your own feet you will never learn it right Wink

#6
[eluser]GI-Joe[/eluser]
[quote author="PhilTem" date="1339174660"]
Code:
class Admin_Controller extends CI_Controller {

function __construct()
{
  if ( ! $this->your_auth_class->user_is_admin() )
  {
    redirect('/* to standard user page /*');
  }
}

function display_users()
{
  // Do something to display your users. This page is only accessible if the user is an authenticated admin
}

}

And your function inside your auth class to check if the user has admin-level

Code:
function user_is_admin()
{
  if ( $this->session->userdata('is_admin') !== FALSE )
  {
    return TRUE;
  }
  
  return FALSE;
}

The session will be set by you once the user's credentials are validated successfully.


That should be enough skeleton for you to learn how to do it properly. If you don't fail on your own feet you will never learn it right Wink[/quote]


Thanks a lot. You have replied as I was expected(even more than that).

#7
[eluser]GI-Joe[/eluser]
[quote author="PhilTem" date="1339174660"]
Code:
class Admin_Controller extends CI_Controller {

function __construct()
{
  if ( ! $this->your_auth_class->user_is_admin() )
  {
    redirect('/* to standard user page /*');
  }
}

function display_users()
{
  // Do something to display your users. This page is only accessible if the user is an authenticated admin
}

}

And your function inside your auth class to check if the user has admin-level

Code:
function user_is_admin()
{
  if ( $this->session->userdata('is_admin') !== FALSE )
  {
    return TRUE;
  }
  
  return FALSE;
}

The session will be set by you once the user's credentials are validated successfully.


That should be enough skeleton for you to learn how to do it properly. If you don't fail on your own feet you will never learn it right Wink[/quote]


Code:
$this->your_auth_class->user_is_admin()

is not working I have autoloaded the session library and also have added session key in config file.

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Admin::$session

Filename: controllers/admin.php

Line Number: 19

Fatal error: Call to a member function userdata() on a non-object in F:\xampp\htdocs\web\application\controllers\admin.php on line 19

Getting above error


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.