2010年3月10日星期三

[fw-auth] Zend's Acl Role not clear

Hi,

I am totally new with Zend Framework, Php and also stepping new in the proffessional life, so don't mind if my questions seems silly to you.

I am following the Zend Tutorials on youtube by alexander;

http://www.youtube.com/watch?v=6vhxo2oL70E


This is the error i'm getting on my page for the ROLE variable when i'm running my code:

Notice: Trying to get property of non-object in C:\wamp\www\ZCF_Documents\application\plugins\AccessCheck.php  on line 18

As far as I know, this error is when Zend doesn't find the mentioned variable in the code or it is not transferred to the calling code properly. But what I want to inquire is that why are you not getting this error and I am. I have followed your tutorial, still the ROLE in my code doesn't seem to transfer at all. I inquire the reason and want to know what is it that I am missing??? may be my concepts with Zend variables in ACL coding are not clear enough...Help needed please.


----------<<<<<<<<>>>>>>>>>--------------
this is my AccessCheck.php code:
----------<<<<<<<<>>>>>>>>>--------------

<?php
class Plugin_AccessCheck extends Zend_Controller_Plugin_Abstract {

    private $_acl= null;
    private $_auth = null;

    public function __construct(Zend_Acl $acl, Zend_Auth $auth) {
        $this->_acl=$acl;
        $this->_auth=$auth;
    }

    public function preDispatch(Zend_Controller_Request_Abstract $request) {

        $resource = $request->getControllerName();
        $action = $request->getActionName();

        $auth_all_items_stored = $this->_auth->getStorage()->read();
        $role = $auth_all_items_stored->role;

        if(!$this->_acl->isAllowed($role, $resource, $action)) {

            $request->setControllerName('authentication')
                ->setActionName('login');
        }
    }
}



----------<<<<<<<<>>>>>>>>>--------------
My Model code:
----------<<<<<<<<>>>>>>>>>--------------

<?php

class Model_Authentication_User extends Zend_Acl {
    protected $_name = 'user';

    public function __construct() {
        $this->add(new Zend_Acl_Resource('index'));
        $this->add(new Zend_Acl_Resource('authentication'));

        $this->add(new Zend_Acl_Resource('documents'));
        $this->add(new Zend_Acl_Resource('edit'),'documents');
        $this->add(new Zend_Acl_Resource('add'),'documents');
        $this->add(new Zend_Acl_Resource('list'),'documents');

//        $this->add(new Zend_Acl_Resource('docs'));
//        $this->add(new Zend_Acl_Resource('list'),'docs');

        $this->addRole(new Zend_Acl_Role('user'));
        $this->addRole(new Zend_Acl_Role('admin'),'user');

        $this->allow('user','index');
        $this->allow('user','documents');
        $this->allow('user','authentication');
        $this->allow('user','documents','list');
        $this->allow('admin','documents','list');
        $this->allow('admin','documents','edit');
        $this->allow('admin','documents','add');
    }
}

----------<<<<<<<<>>>>>>>>>--------------
this is my Controller code:
----------<<<<<<<<>>>>>>>>>--------------

class AuthenticationController extends Zend_Controller_Action {

public function loginAction() {

        if(Zend_Auth::getInstance()->hasIdentity()) {
            echo 'asg';
            $this->_redirect('authentication/index');
        }
        $req = $this->getRequest();

        $form = new Form_LoginPage();
       
        if($req->isPost()) {
            if($form->isValid($this->_request->getPost())) {

                $AuthentAdapter = $this->getAuthAdapter();

                $user_name = $form->getValue('username');
                $user_password = $form->getValue('userpassword');

                $AuthentAdapter ->setIdentity($user_name)
                    ->setCredential($user_password);

                $auth=Zend_Auth::getInstance();

                $result = $auth->authenticate($AuthentAdapter);

                if($result->isValid()) {
                    $identity=$AuthentAdapter->getResultRowObject();
                    $authStorage = $auth->getStorage();
                    $authStorage->write($identity);

                    $this->_redirect('Documents/list');
                }
                else
                    $this->view->errorMessage = 'Invalid User name or Password provided, try again';
            }
        }
        $this->view->form = $form;
}



   private function getAuthAdapter() {


        $AuthentAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());

        $AuthentAdapter->setTableName('user')
            ->setIdentityColumn('user_name')
            ->setCredentialColumn('user_password');
        return $AuthentAdapter;
      }
}

没有评论: