Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Ajax Jquery dispatcher getAcl

Hello! I need to set up a Acl for ajax queries from JQuery.

  $controller = $dispatcher->getControllerName();
  $action = $dispatcher->getActionName();

    $acl = $this->getAcl();

    if (!$acl->isResource($controller)) {
        $dispatcher->forward([
            'controller' => 'errors',
            'action'     => 'show404'
        ]);

         return false;
      }

return ajax error. Help:)



3.0k
edited Apr '17

return ajax error.

What do you mean?

Have you set up an Acl object with roles, resources, and access controlls?



1.1k
edited Apr '17

return ajax error.

What do you mean?

Have you set up an Acl object with roles, resources, and access controlls?

Everything works only if I comment on the controller verification code ( if (!$acl->isResource($controller)) ). Otherwise, the error 404 (or rather, instead of success, ajax returns an error). Probably the $controller does not display the real controller (indicated in the path ajax). Independently did not understand.

What does your $acl->addResource() look like?

You can create custom names for each resource, and have to use the same values in isResource



1.1k
edited Apr '17

What does your $acl->addResource() look like?

You can create custom names for each resource, and have to use the same values in isResource

        Private area resources
        $privateResources = array(
                            'offers'      => array('index'),
                       //.......
        );
        foreach ($privateResources as $resource => $actions) {
            $acl->addResource(new Resource($resource), $actions);
        }

        //Public area resources
        $publicResources = array(
            'index'      => array('index'),
               //............            

            'errors'     => array('show401', 'show404', 'show500'),
            'authorization' => array('index', 'start', 'end'),
            'contacts'    => array('index', 'send')
        );
        foreach ($publicResources as $resource => $actions) {
            $acl->addResource(new Resource($resource), $actions);
        }
        //Grant access to public areas to both users and guests
        foreach ($roles as $role) {
            foreach ($publicResources as $resource => $actions) {
                foreach ($actions as $action){
                    $acl->allow($role->getName(), $resource, $action);
                }
            }
        }


1.1k

There is an assumption that the problem is related specifically to the interaction of Ajax (Jquery) and the answer Acl.

edited Apr '17

No, acl doesn't interact anyhow with ajax. I have ACL working without any problem with angular.



1.1k
Accepted
answer
edited Apr '17

I'm sorry - I found an error in the code. The topic is closed. Thank you all for your help.

ajax: /ProjectQuestions/index but security plugin: projectquestion

ajax must be: /projectquestions/index too