Welcome Guest, Not a member yet? Register   Sign In
Another User Management Auth ACL Fancy Stuff Something Something library
#1

[eluser]davidbehler[/eluser]
Hey,
it's been quite a while since I've updated my auth library and today at work I had some idle time and started thinking about a new one.

There have been many discussions on what an auth/acl/whatever library should provide and wether we should distinguish between authentication, permission checking, registration, general user management and so on or put it into one library that handles everything.

I, for my part like having a library that handles everything that's needed to authenticate a user (e.g. login/logout) and check his permissions (e.g. ACL). The other stuff (registration, activation,...) is mostly done by one of my controller in combination with a model. There is no real reason why I've seperated these parts, propably because of my projects that use to differ widely in that part.

The other topic that's often discussed is how customizable a library should be and what parts should follow certain standards that would allow easy exchange of libraries (see here for a discussion on that topic started by xwero).

Anyway, after not as much thinking as I would like to have put into this, I came up with a first draft of my new auth library:
Code:
<?php
    class Fancy_class_name
     {
        var $config = array();
        var $adapter = FALSE;
        var $_default_adapater = 'Memory';
    
        function __construct($config = FALSE)
        {
            if(is_array($config))
            {
                $this->config = $config;
                $this->init();
            }
            else
            {
                $this->_set_adapater($this->_default_adapter);
            }
        }
        
        function init()
        {
            if(isset($this->config['adapater']))
            {
                $this->_set_adapater($this->config['adapater']);
            }
            else
            {
                $this->_set_adapater($this->_default_adapter);
            }
        }
        
        function _set_adapter($adapter)
        {
            if(file_exists(APPPATH.'libraries/auth/'.$adapter.'_adapter.php')
            {
                include(APPPATH.'libraries/auth/'.$adapter.'_adapter.php');
                $class_name = $adapter.'_adapter';
                $this->adapter = new $class_name($this->_raw_config[strtolower($adapter)]);
            }
            else
            {
                return FALSE;
            }
        }
        
        function login($identifier, $password)
        {
            return $this->_call_adapter_method('login', array($identifier, $password));
        }
        
        function logout()
        {
            return $this->_call_adapter_method('logout');
        }
        
        function register()
        {
            return $this->_call_adapter_method('register');
        }
        
        function check_permission($controller = FALSE, $function = FALSE, $right = FALSE)
        {
            return $this->_call_adapter_method('check_permission');
        }
        
        function _call_adapter_method($method, $arguments = array())
        {
            return call_user_func_array(array($this->adapater, method), $arguments);
        }
     }
<?

There isn't much functionality yet, but I guess you can see where this is headed: Easily customizable/extendable library that covers authentication/acl stuff/user management stuff.
Using adapters (I "stole" this term from Zend) it's possible to allow for as many ways of authentication as you want. Database access, LDAP, predefined sets of username and password, Facebook, OpenID, OAuth? Everything is possible, as long as you got a fitting adapter Smile If you carry the idea a bit further, you could add other adapters for session storage (database, cookie, php session, ...), registration and every other process you can think of.

Sure, one might overdo things by using adapters for every aspect but I'm only saying it's possible Wink

There is still alot of work to do to make really makes this library work and that's exactly the reason for me to post here now, before I've even really started working on it:
I want to hear your opinion on this! What do you think of this approach? Do you think it's worth the time?

I would not do this for me, altruistic as I am, but rather for the community! Nearly every day there is someone asking for an auth library. Wouldn't it be nice to have one library that fits all their needs and if it doesn't can be easily extended?

Tell me what you think! I'm looking forward to your comments!

Greetz,
waldmeister
#2

[eluser]Milos Dakic[/eluser]
Code:
return call_user_func_array(array($this->adapater, method), arguments);

I think that should be:
Code:
return call_user_func_array(array($this->adapater, $method), $arguments);

Other then that, I'd say the permissions stuff can be put into another library, so your left with "login, logout & register". Maybe add a "deactivate" one as well, which can be used for removing/deleting/deactivation user accounts.
#3

[eluser]davidbehler[/eluser]
Did you read the text surrounding the code tags? Wink

I put all that stuff in there on purpose! I'm aiming at a library that does everything to avoid having multiple libraries that all deal with user authentication, permission checking and so on.
#4

[eluser]Milos Dakic[/eluser]
"Auth" seems to be more specific to authentication (login,logout etc). And yes I did read the text, I just thought I'd mention it =] I do like the code structure though, as I have been trying to do something like this for a while. Hopefully one day I'll get around to finishing it off.
#5

[eluser]davidbehler[/eluser]
I see. I changed the class name, maybe that helps Wink
#6

[eluser]darkhouse[/eluser]
I like this, I just finished building an authorization system with roles/resources, but I did not incorporate the ability to run other "adapters", I'm thinking down the road I should convert what I've done into an adapter, and use this to manage everything.

Thanks!
#7

[eluser]darkhouse[/eluser]
I just wanted to mention that I finally used this class the other day, and it worked great. I used it for applying different shipping adapters to a store that I'm working on. It made life so much easier, at first I wasn't sure how I'd tackle it, but I remembered this class, and that was the solution.

There are a few syntax and spelling errors, but they're minor, and once they're fixed, it all works great. One of those issues was this line:
Code:
$this->adapter = new $class_name($this->_raw_config[strtolower($adapter)]);
Since _raw_config isn't set, I wasn't sure what it was supposed to be referencing, but changing it to the following works fine.
Code:
$this->adapter = new $class_name();

I was able to load config files for each of my shipping adapters as they all have different settings and requirements, and I was able to keep those config files in a shipping folder within the config folder, just for organization.

Again, thanks for this great work, I don't know what I would've done without it.




Theme © iAndrew 2016 - Forum software by © MyBB