Part of the EllisLab Network
   
1 of 10
1
ErkanaAuth: A non-invasive user authentication library
Posted: 23 October 2007 08:58 PM   [ Ignore ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2280
Joined  07-30-2007

I don’t have the time right now to create a proper post or a Wiki article (feel free to do so for me, if you’d like) so here’s a copy-paste from my blog and a link to the full article:

First of all, let me be blunt about this: this is my authorization library. Of course, I will be taking user suggestions and bug fixes into account but ultimately, if it doesn’t fit within the scope of my needs, it won’t make it into the library.

User Authentication is something that many CodeIgniter developers face every single day - there are tons of libraries out there to help in doing this as well. In my opinion though, most of them are to bloated for my use.

My goal with this library was to create a small set of methods and helpers that would prove useful for a variety of user authentication while not hijacking the framework and forcing you to adopt the practices that library dictates.

What I came up with is Erkana Auth - a library of 3 methods and a helper with 2 functions. Erkana Auth supports user login (maintaining this login via a Session), logout, and a basic role system. The role system is merely the definition of roles, the actual implementation of roles is still left up to you - the developer.

http://www.michaelwales.com/2007/10/erkana-codeigniter-authorization-library/

 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

Profile
 
 
Posted: 24 October 2007 02:28 AM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  92
Joined  09-20-2007

Very Nice Gratz

Small 4kbs
FLEXIBLE
WORKS

maybe addRole function

 Signature 

PHP Rocks!

Profile
 
 
Posted: 24 October 2007 02:35 AM   [ Ignore ]   [ # 2 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2280
Joined  07-30-2007

Nice suggestion - I’ll see about adding that soon.

Although, it would be changeRole($newrole integer) - since this library only supports a single role.

Nonetheless, good idea.

 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

Profile
 
 
Posted: 24 October 2007 03:46 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  501
Joined  02-08-2007

Nicely done chief… I am sure this will become another commonly used tool in the stable.

 Signature 

you’ve got that sexy Canadian thing working for you… - Derek Allard


Pancake Payments | http://DearIE6.com
http://twitter.com/thatleeguy

Profile
 
 
Posted: 24 October 2007 08:41 AM   [ Ignore ]   [ # 4 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

I will check if it’s php4 compatible as soon as i can.

I have one suggestion. Maybe the tablenames should be defined in a config array/file for more flexibility. It’s not always possible to rename the tables.

It’s the first authentication library i see i will try out, keep up the good work

Profile
 
 
Posted: 24 October 2007 12:17 PM   [ Ignore ]   [ # 5 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2280
Joined  07-30-2007

Thanks for the compliment and the suggestion xwero - that’s something I had already considered, but I hate the fact of adding another file that you have to open up and change to use the library.

I think I’ll make a class variable to store the tables, and then a method to change those from the defaults if you wish…

 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

Profile
 
 
Posted: 24 October 2007 01:22 PM   [ Ignore ]   [ # 6 ]  
Grad Student
Avatar
Rank
Total Posts:  92
Joined  09-20-2007

Default use users and roles, but check the 2 parameter on the constructor

$tables= array(‘usersTable’ = ‘users’, ‘rolesTable’ => ‘roles’);
$this->load->library(‘Erkanaauth’,$tables);

 Signature 

PHP Rocks!

Profile
 
 
Posted: 24 October 2007 03:51 PM   [ Ignore ]   [ # 7 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

My first impression

- The try login snippet needs array() around the array content

- what is the point of the helper functions? They are just wrappers for the library methods. Do they work when the library isn’t loaded, i think not but maybe i’m wrong. So the only reason why there are helper functions is to lessen the typing?

- using php4 calling the try_login method gives following error

Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: database/DB_active_rec.php
Line Number: 62

 
I build the query and i discovered removing the limit en offset from the getwhere method did the trick.

- in the getRole method why do you use and explicit join

$this->CI->db->select('roles.name');
$this->CI->db->JOIN('roles''users.role_id = roles.id');
$query $this->CI->db->getwhere('users', array('users.id'=>$this->CI->session->userdata('user_id')), 10); 

Don’t other databases know implicit joins?

$this->CI->db->select('roles.name');
$query $this->CI->db->getwhere('users,roles', array('users.role_id' => 'roles.id','users.id'=>$this->CI->session->userdata('user_id')), 10); 

- I’m wondering how the class would handle content that is visible for different roles

<? if (getRole() == 'admin' || getRole() == 'test' || getRole() == 'test2'{ ?>
    
<li><?anchor('admin/users''Users'); ?></li>
  
<? } ?> 

This can get messy.

Maybe i’m coming on a bit too strong but i think this is good stuff so it can accept a little criticism smile

Profile
 
 
Posted: 24 October 2007 05:54 PM   [ Ignore ]   [ # 8 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2280
Joined  07-30-2007

Yeah there is definitely some room for improvement - I’ll come back to all of these and make a few changes in short-order. The files that I uploaded were posted as soon as I got it working, without any refactoring or really analyzing the code prior to release.

 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

Profile
 
 
Posted: 24 October 2007 06:27 PM   [ Ignore ]   [ # 9 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2280
Joined  07-30-2007

The try login snippet needs array() around the array content

I assume you are referring to this line:

$query $this->CI->db->getwhere('users'$condition10); 

It doesn’t need to be surrounded by array() because condition is an array. Your suggestion would make it an array, that contained an array, that included the conditions (in other words SELECT * FROM users WHERE Array() LIMIT 1,0).

The helpers are there so getRole() and getField() can be used in a view - saves on typing a little.

The foreach() error I am unsure about… if removing the limit and offset works, cool. Odd error nonetheless.

The explicit join is just because I suck at joins and have to refer to the MySQL documentation every time. I thought about giving the implicit you mention a try but didn’t test it. If it works properly, I’ll probably go that route in the future.

The multiple role situation is one I am aware of and I intend to correct that in the future (it will be a new method that accepts a string (or an array, I haven’t decided) of user roles. If the user has one of those roles it will return TRUE.

 Signature 

Follow me on twitter here.
MichaelWales.com | MichaelWales.info

Profile
 
 
Posted: 24 October 2007 07:25 PM   [ Ignore ]   [ # 10 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006
walesmd - 24 October 2007 10:27 PM

The try login snippet needs array() around the array content

I assume you are referring to this line:

$query $this->CI->db->getwhere('users'$condition10); 

No i was refering to your blog post where you added an example of the try_login method

try_login('username'=>'test','password'=>'hello'

I read documentation smile

walesmd - 24 October 2007 10:27 PM

The helpers are there so getRole() and getField() can be used in a view - saves on typing a little.

Ok i wasn’t sure about that. I will not use the helper i rather type than debug.

walesmd - 24 October 2007 10:27 PM

The multiple role situation is one I am aware of and I intend to correct that in the future (it will be a new method that accepts a string (or an array, I haven’t decided) of user roles. If the user has one of those roles it will return TRUE.

The way i do this is creating an action/pagepart entry and attach the roles to it then compare the actions roles with the current role. It requires extra tables.
view_object : id,name
view_object_roles : view_object_id, roles_id

function enableViewObject($view_object_id)
{
    $query 
$this->db->getwhere('view_object_roles', array('view_object_id' => $view_object_id'roles_id' => $this->getRole()));
    return (
$query->num_rows() > 0)?TRUE:FALSE;
Profile
 
 
   
1 of 10
1