为什么Authorize在AuthComponent中要设置成Controller?

Why Authorize is to set to Controller in AuthComponent?

为什么Authentication是在AppController中进行,而Authentication是在Controller中设置的?

喜欢:我在Blog example做的时候得到了它,但没有得到它的详细解释

$this->loadComponent('Auth', [
        'authorize' => ['Controller']
    ]);

我读了 Authorize Section 但无法理解。那么有人可以帮助我理解吗?

这本书描述了您将如何在控制器级别控制您自己的授权。 身份验证标识有效用户。如果任何登录用户可以访问您应用程序的任何部分,那么您不需要实施任何进一步的授权。但是,如果您希望限制对某些控制器的访问,例如基于 role,您可以按照描述在 Auth 配置中设置 'authorize' => ['Controller'],然后在每个控制器中定义您自己的isAuthorized() 方法,基于用户的角色。

例如,如果在您的 InvoicesController 中您只想让角色为 'Accounting' 的用户访问这些方法,您可以在 InvoicesController 的 isAuthorized() 方法中包含一个测试:

// src/Controller/InvoicesController.php
class InvoicesController extends AppController
{
    public function isAuthorized($user)
    {
        if ($user['role'] === 'Accounting'){
            return true;
        }
        return parent::isAuthorized($user);
    }

    // other methods
}