Auth: added additionalValidationCallback
| From: | Herman Sinte Maartensdijk | Date: | Mon, 15 Mar 2004 13:30:50 +0000 |
| Subject: | Auth: added additionalValidationCallback | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-26427@lists.php.net to get a copy of this message | ||
Hi,
I've started using the Auth package recently and although I like the package I needed run some
additional validation checks before deciding wether an authentication was valid (in my case I needed
to check wether the users IP was in a certain ip-range which is user-specific). This was not
possible with loginCallback or loginFailedCallback since these are both run after someone has logged
in, and I needed one that was run after the Auth had checked the user/pass, but before it had set
$login_ok.
Therefore I added the variable $additionalValidationCallback, the method
setAdditionalValidationCallback() and implemented these in the login() method.
Maybe it is an idea to work this patch into CVS as I can imagine it can be usefull to some other
people.
I've attached a diff file for this patch
Cheers,
Herman
--- Auth.php.173-PR2 Mon Mar 15 13:56:00 2004 +++ Auth.php.173-PR3 Mon Mar 15 14:29:44 2004 @@ -19,6 +19,7 @@ // $Id: Auth.php,v 1.73 2004/03/03 23:15:06 yavo Exp $ // PR1: Added setPostUsername & setPostPassword // PR2: Fixed bug in loginCallback & loginFailedCallback which send a copy of auth instead of a reference +// PR3: Added additionalValidationCallback // require_once 'PEAR.php'; @@ -123,6 +124,14 @@ var $password = ''; /** + * Additional validation callback function name + * + * @var string + * @see setAdditionalValidationCallback() + */ + var $additionalValidationCallback = ''; + + /** * Login callback function name * * @var string @@ -325,7 +334,14 @@ */ if (!empty($this->username)) { if (true === $this->storage->fetchData($this->username, $this->password)) { - $login_ok = true; + if (is_callable($this->additionalValidationCallback)) + { + $login_ok = call_user_func($this->additionalValidationCallback,$this->username, &$this); + } + else + { + $login_ok = true; + } } } @@ -459,6 +475,21 @@ /** * Register a callback function to be called on user login. + * This function can perform some additional validation checks. + * The function will receive two parameters, the username and a reference to the auth object + * and will return true or false + * + * @access public + * @param string callback function name + * @return bool additional validation was succesfull or not + */ + function setAdditionalValidationCallback($callback) + { + $this->additionalValidationCallback = $callback; + } + + /** + * Register a callback function to be called on user login. * The function will receive two parameters, the username and a reference to the auth object. * * @access public @@ -599,7 +630,7 @@ * Enable or disable the Advanced Security. * * @access public - * @param boolean + * @param boolean * @return void */ function setAdvancedSecurity($flag=true){
--- Auth.php.173-PR2 Mon Mar 15 13:56:00 2004 +++ Auth.php.173-PR3 Mon Mar 15 14:29:44 2004 @@ -19,6 +19,7 @@ // $Id: Auth.php,v 1.73 2004/03/03 23:15:06 yavo Exp $ // PR1: Added setPostUsername & setPostPassword // PR2: Fixed bug in loginCallback & loginFailedCallback which send a copy of auth instead of a reference +// PR3: Added additionalValidationCallback // require_once 'PEAR.php'; @@ -123,6 +124,14 @@ var $password = ''; /** + * Additional validation callback function name + * + * @var string + * @see setAdditionalValidationCallback() + */ + var $additionalValidationCallback = ''; + + /** * Login callback function name * * @var string @@ -325,7 +334,14 @@ */ if (!empty($this->username)) { if (true === $this->storage->fetchData($this->username, $this->password)) { - $login_ok = true; + if (is_callable($this->additionalValidationCallback)) + { + $login_ok = call_user_func($this->additionalValidationCallback,$this->username, &$this); + } + else + { + $login_ok = true; + } } } @@ -459,6 +475,21 @@ /** * Register a callback function to be called on user login. + * This function can perform some additional validation checks. + * The function will receive two parameters, the username and a reference to the auth object + * and will return true or false + * + * @access public + * @param string callback function name + * @return bool additional validation was succesfull or not + */ + function setAdditionalValidationCallback($callback) + { + $this->additionalValidationCallback = $callback; + } + + /** + * Register a callback function to be called on user login. * The function will receive two parameters, the username and a reference to the auth object. * * @access public @@ -599,7 +630,7 @@ * Enable or disable the Advanced Security. * * @access public - * @param boolean + * @param boolean * @return void */ function setAdvancedSecurity($flag=true){