Here is the code I was playing with for you to compare:
[code]
require_once 'Zend/Acl.php';
$acl=new Zend_Acl();
require_once('Zend/Acl/Role.php');
$acl->addRole(new Zend_Acl_Role('guest'))
->addRole(new Zend_Acl_Role('member'));
require_once('Zend/Acl/Resource.php');
$acl->add(new Zend_Acl_Resource('test'));
$acl->deny('guest', 'test');
$acl->allow('member', 'test');
echo $acl->isAllowed('guest', 'test') ? 'allowed' : 'denied';
//require_once 'Test.php';
//--- embeded for one file testing
require_once 'Zend/Acl/Resource/Interface.php';
class test implements Zend_Acl_Resource_Interface {
public function __construct(Zend_Acl $acl) {
$this->_acl = $acl;
}
public function getResourceId()
{
return 'test';
}
public function echoHello(){
if (!$this->_acl->isAllowed('guest', __CLASS__,
__FUNCTION__)) {
throw new Exception('ACCESS DENIED!');
}
return 'hello';
}
}
//----
// create test class
$test = new test($acl);
// catch exceptions
try {
echo $test->echoHello();
} catch (Exception $e) {
// do something with the triggered exception
echo 'an unexpected error occured.';
echo '<h2>Unexpected Exception: ' . $e->getMessage() . '</h2><br
/><pre>';
echo $e->getTraceAsString();
}
[/code]
Your not passing any type of current user role to this class, it will always
return the exception.
Something like this :
public function echoHello($myRole = 'guest'){
if (!$this->_acl->isAllowed(myRole , __CLASS__,
__FUNCTION__)) {
throw new Exception('ACCESS DENIED!');
}
return 'hello';
}
Then in the function call pass the role to be used...
// whats this current user/page load group
// Should match a roles defined in the acl
$thisUsersRole = 'member' ; //? Member or guest or etc...
echo $test->echoHello($thisUsersRole);
Only problem is if the role doesn't exists, I think it will throw an
error... Just incase, might need to put in a hasRole check before the
isAllowed. I think this page has the info on the hasRole, or its in there
somewhere, http://framework.zend.com/manual/en/zend.acl.html
Also, just out of curiosity, is this part really needed for this?
---
require_once 'Zend/Acl/Resource/Interface.php';
class Test implements Zend_Acl_Resource_Interface {
--
Since the acl is passed as an object var to the class I don't see that it's
used.
This should work with less overhead.
--
Class Test () {
--
Hope that all makes sence...
Terre
-----Original Message-----
From: maxarbos [mailto:[EMAIL PROTECTED]
Sent: Thursday, July 10, 2008 3:36 PM
To: [email protected]
Subject: RE: [fw-general] Is it possible to use Zend_ACL without MVC
still getting the same error:
denied
Fatal error: Uncaught exception 'Zend_Acl_Exception' with message 'Resource
'Test' not found'
in /xxx/Zend/Acl.php:297
Stack trace:
#0 /xxx/Zend/Acl.php(691): Zend_Acl->get('Test')
#1 /xxxx/admin/Test.php(24):
Zend_Acl->isAllowed('guest', 'Test', 'echoHello')
#2 /xxx/admin/index.php(50): Test->echoHello()
#3 {main} thrown in /xxx/Zend/Acl.php on line 297
vRandom wrote:
>
> Move this
>
> require_once 'Test.php';
> $test = new Test($acl);
>
> under
>
> $acl->allow('member', 'test');
>
> Terre
>
>
--
View this message in context:
http://www.nabble.com/Is-it-possible-to-use-Zend_ACL-without-MVC-tp18385583p
18390278.html
Sent from the Zend Framework mailing list archive at Nabble.com.