Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFR]: can we add controller filter like cakephp4 #14902

Closed
wcj343169893 opened this issue Mar 10, 2020 · 3 comments
Closed

[NFR]: can we add controller filter like cakephp4 #14902

wcj343169893 opened this issue Mar 10, 2020 · 3 comments
Labels
new feature request Planned Feature or New Feature Request

Comments

@wcj343169893
Copy link

If we add permission verification, we must add if judgment in each action.
CakePHP has an advantage. In basecontroller, beforefilter can take precedence over action execution. Here, you can judge permissions and define the global variable $allowedactions = [], The action in it does not need permission. Others need to judge whether the role has access to this action. If the user has permission, beforefilter can return true or null. If there is no permission, beforefilter returns response object in the general process, such as: abstractdispatcher's handler - > initialize();
https://github.com/phalcon/cphalcon/blob/c4a804a70c9b89728f010b23935f450c79690bd7/phalcon/Dispatcher/AbstractDispatcher.zep
In the same way, judge the return value of beforefilter. If it is response, jump or display the content directly。
Although exceptions can be thrown here to intercept permissions, exceptions will be written into the error log, which increases the difficulty of troubleshooting other problems。
thanks every body

@wcj343169893 wcj343169893 added the new feature request Planned Feature or New Feature Request label Mar 10, 2020
@wcj343169893
Copy link
Author

I find https://github.com/phalcon/cphalcon/issues/13790 and i try,it work,it`s ok?

public function beforeExecuteRoute($obj){
        $session = $this->getDI()->get("session");
        $role = $session->get("role");
        if($role == "admin"){
           return true;
       }
        $this->response->redirect('/admin/login/index' );
       //echo json_encode(["code"=>401]);
       // $this->response->setContentType("application/json");
        return false;
    }

@Jeckerson
Copy link
Member

Jeckerson commented Mar 10, 2020

In Phalcon you have events manager, which can be called during some specific part of the code execution.

And here is the full list of events - https://docs.phalcon.io/4.0/en/events#list-of-events

And your code example is correct.

@niden
Copy link
Member

niden commented Apr 10, 2020

Tracked in #14608

@niden niden closed this as completed Apr 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature request Planned Feature or New Feature Request
Projects
None yet
Development

No branches or pull requests

3 participants