Welcome Guest, Not a member yet? Register   Sign In
ErkanaAuth: A non-invasive user authentication library
#61

[eluser]tomcode[/eluser]
I understand that point. I just have this shortcut in my head : CodeIgniter ? data base ? => model.
#62

[eluser]BrandonDurham[/eluser]
So do you make your above code a library and autoload it?
#63

[eluser]Michael Wales[/eluser]
Quote:So do you make your above code a library and autoload it?
Yes.
#64

[eluser]flosaurus[/eluser]
[quote author="Michael Wales" date="1200265545"]

get_user() is a bit different than what we've seen in the past. I usually extend my Controller class, having all of my classes assign the return value to a variable I can us within my view (for echoing out user info, as needed). For any controllers needing Authorization - I make a valid get_user() return value a requirement.

application/libraries/MY_Controller.php
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Public_Controller extends CI_Controller {
    function Public_Controller() {
        parent::Controller();
        $this->data->user = get_user($this->session->userdata('user_id'));
    }
}

class Admin_Controller extends Public_Controller {
    function Admin_Controller() {
        parent::Controller();
        if ($this->data->user === FALSE) {
            redirect('user/login');
            return;
        }
    }
}
[/quote]

I'm quite new to CI and would like to know how to use your librarie MY_Controller.php to secure a controler. Could you provide some example ?

thanks a lot
#65

[eluser]fakeempire[/eluser]
http://www.michaelwales.com -- seems to be down and all download links point to that. Does someone have it mirrored somewhere?

Thanks
#66

[eluser]Michael Wales[/eluser]
fakeempire - it will be back up shortly (Dreamhost had a billing issue which made my account look way overdue).

You can look at this post and just copy-paste the code into /application/libraries/auth.php and it will work fine.
#67

[eluser]Michael Wales[/eluser]
Quote:I’m quite new to CI and would like to know how to use your librarie MY_Controller.php to secure a controler. Could you provide some example ?

Sure thing:

/application/libraries/MY_Controller.php
This code will create two classes that extends the Controller class.

The first class is Public_Controller and allows anyone to access your site. The currently logged in user information is available in your controller as $this->data->user and within your view as $user (if you pass the $this->data object to your view).

The second class is Admin_Controller and only allows a logged in user to access it. It will redirect to the default controller if a user is not logged in.
Code:
class Public_Controller extends Controller {
  function Public_Controller() {
    parent::Controller();
    $this->data->user = $this->auth->get_user($this->session->userdata('user_id'));
  }
}

class Admin_Controller extends Controller {
  function Admin_Controller() {
    parent::Controller();
    $this->data->user = $this->auth->get_user($this->session->userdata('user_id'));
    if ($this->data->user === FALSE) {
      redirect();
      return;
    }
  }
}

Example Controller
Code:
// Only a logged in user can access the methods within this class
class Admin extends Admin_Controller {

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

  function index() {
    $this->load->view('admin/cpanel', $this->data);
    return;
  }

}

Code:
// Anyone can access the methods within this class, but you still have access to user information.
class Home extends Public_Controller {

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

  function index() {
    // Concatenate and upper-case the user's name
    $this->data->display_name = ucwords($this->data->user->first_name . ' ' . $this->data->user->last_name);
    $this->load->view('home/index', $this->data);
    return;
  }
}

Finally - our view: showing off our user's display name:
Code:
Welcome, <?= $user->display_name; ?>.
#68

[eluser]tomcode[/eluser]
@Michel:

Doesn't he need to include the Public_Controller/Admin_Controller ?

Genre :
Code:
include 'Admin_Controller' .EXT;

// Only a logged in user can access the methods within this class
class Admin extends Admin_Controller {
#69

[eluser]Kemik[/eluser]
Hello,

What's the benefit of putting that code in MY_Controller? I usually make a function in the controller (e.g. function security, function got_permission, etc) and then call that.

I've checked the userguide as I think I've seen MY_ stuff used before but the page below doesn't go in to much detail.

http://ellislab.com/codeigniter/user-gui...aries.html

Edit: This question may be better for a separate thread.
#70

[eluser]tomcode[/eluser]
Quote:What’s the benefit of putting that code ...
It becomes reusable for several controllers




Theme © iAndrew 2016 - Forum software by © MyBB