Welcome Guest, Not a member yet? Register   Sign In
SEAuth - Session Extension for Authorization - Please take a quick look!
#1

[eluser]Costa Rica SEO[/eluser]
Hey everybody. This my my first bit of code to share, so please go easy on me Wink I was looking for something REALLY simple to do user validation / authorization. I didn't see it. So I made an extension to the Session class.

It works like this:
Code:
$this->session->login($username,$password);
$this->session->logout();
$this->session->auth($privilege_type_needed);

Login checks the user name and password, then loads the privileges in to the cookie.

Logout removes all privileges

Auth checks to see if the user has that privilege.

It has a config file called auth.php that needs the following:
Quote:$user_table - The name of the database table to use for user lookup.

$user_name - This is the record where the user name is stored.

$password - This is the record where the user password is stored.

$session_auth - This is the variable saved in the cookie that validates the user is logged in.

$privilege - This is an array of boolean records to indicate if the user has permission to access various types of content.

Here is a SUPER simple login page:
Code:
function login(){
      $data = array();
      $data['user'] = $this->input->post('user');
      $data['password'] = $this->input->post('password');

      if ($data['user']) {
          $secure = $this->session->login($data['user'],$data['password']);
          if ($secure) {redirect('/authclients/admin');}
          else {show_error('Bad User Name or Password');}
        } else {$this->load->view('view_login');}
      }

Here is a SUPER simple logout page:
Code:
function logout(){
        $this->session->logout();
      echo "logged out";
      }

Here is a SUPER simple admin page (with session authorization):
Code:
function admin(){
        $user_is_admin = $this->session->auth('user_admin');
      if ($user_is_admin) {
        $this->load->view('view_admin');
      }
      else {
        show_error('Permission Denied');
      }
      }

I threw together the program quickly over the past two hours or so and tested it. It works great so far. I'm planning on making a page for it and adding it to the Wiki, but I want some input first. The program is less than 100 lines with documentation. Anyone care to give it a look over and make suggestions before I post it? I've attached the code as a ZIP.

I like it because it doesn't add too much extra code. Just make sure the session library is loaded and you're done. I didn't feel it was significant enough to need another library (though it could be made in to one in minutes).

- Paul


Messages In This Thread
SEAuth - Session Extension for Authorization - Please take a quick look! - by El Forum - 09-22-2008, 03:44 PM



Theme © iAndrew 2016 - Forum software by © MyBB