我想使用身份验证插件分隔模型时怎么办?

时间:2020-08-06 11:43:14

标签: cakephp cakephp-4.x

我目前正在CakePHP4中实现登录功能。 因此,我想将其分为普通用户模型和管理用户模型。

我根本不知道该怎么做,我整日都在寻找实现它的方法,但我做不到。

我想在“ routes.php”中设置身份验证,但是在“ Application.php”中的“ implementing AuthenticationServiceProviderInterface”中遇到问题。我该怎么办?

// routes.php

$routes->scope('/', function (RouteBuilder $builder) {
    /......./

    // ログイン
    //$builder->connect('mgt-account/*', ['controller' => 'MgtAccount', 'action' => 'login', 'prefix' => 'Admin']);
    $builder->connect('users/*', ['controller' => 'Users', 'action' => 'login', 'prefix' => 'Normal']);
  
    /......./
});

$routes->prefix('Normal', function (RouteBuilder $routes) {

    $loginUrl = Router::url('/normal/users/login');
    $fields   = [
        'username' => 'mail',
        'password' => 'password',
    ];
    $service = new AuthenticationService([
        'unauthenticatedRedirect' => $loginUrl,
        'queryParam' => 'redirect',
        ]);
    $service->loadAuthenticator('Authentication.Session');
 
    $service->loadAuthenticator('Authentication.Form', [
        'fields' => $fields,
        'loginUrl' => $loginUrl,
    ]);

    $service->loadIdentifier('Authentication.Password', compact('fields'));

    $routes->registerMiddleware(
        'auth',
        new \Authentication\Middleware\AuthenticationMiddleware($service)
    );
    $routes->applyMiddleware('auth');

    //$routes->connect('/:controller');
    $routes->fallbacks(DashedRoute::class);
});

$routes->prefix('Admin', function (RouteBuilder $routes) {

    $loginUrl = Router::url('/admin/mgt-account/login');
    $fields   = [
        'username' => 'mail',
        'password' => 'password',
    ];
    $service = new AuthenticationService([
        'unauthenticatedRedirect' => $loginUrl,
        'queryParam' => 'redirect',
        ]);
    $service->loadAuthenticator('Authentication.Session');

    $service->loadAuthenticator('Authentication.Form', [
        'fields' => $fields,
        'loginUrl' => $loginUrl,
    ]);

    $service->loadIdentifier('Authentication.Password', compact('fields'));

    $routes->registerMiddleware(
        'auth',
        new \Authentication\Middleware\AuthenticationMiddleware($service)
    );
    $routes->applyMiddleware('auth');

    //$routes->connect('/:controller');
    $routes->fallbacks(DashedRoute::class);
});

<?php
// src/Application.php

declare(strict_types=1);

namespace App;

use Cake\Core\Configure;
use Cake\Core\Exception\MissingPluginException;
use Cake\Error\Middleware\ErrorHandlerMiddleware;
use Cake\Http\BaseApplication;
use Cake\Http\MiddlewareQueue;
use Cake\Routing\Middleware\AssetMiddleware;
use Cake\Routing\Middleware\RoutingMiddleware;

use Authentication\AuthenticationService;
use Authentication\AuthenticationServiceInterface;
use Authentication\AuthenticationServiceProviderInterface;
use Authentication\Middleware\AuthenticationMiddleware;
use Psr\Http\Message\ServerRequestInterface;

/**
 * Application setup class.
 *
 * This defines the bootstrapping logic and middleware layers you
 * want to use in your application.
 */
class Application extends BaseApplication
// I really want to comment out and delete this bottom.
implements AuthenticationServiceProviderInterface
{
    /**
     * Load all the application configuration and bootstrap logic.
     *
     * @return void
     */
    public function bootstrap(): void
    {
        // Call parent to load bootstrap from files.
        parent::bootstrap();

        if (PHP_SAPI === 'cli') {
            $this->bootstrapCli();
        }

        /*
         * Only try to load DebugKit in development mode
         * Debug Kit should not be installed on a production system
         */
        if (Configure::read('debug')) {
            $this->addPlugin('DebugKit');
        }

        // Load more plugins here
    }

    /**
     * Setup the middleware queue your application will use.
     *
     * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup.
     * @return \Cake\Http\MiddlewareQueue The updated middleware queue.
     */
    public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
    {
        $middlewareQueue
            // Catch any exceptions in the lower layers,
            // and make an error page/response
            ->add(new ErrorHandlerMiddleware(Configure::read('Error')))

            // Handle plugin/theme assets like CakePHP normally does.
            ->add(new AssetMiddleware([
                'cacheTime' => Configure::read('Asset.cacheTime'),
            ]))

            // Add routing middleware.
            // If you have a large number of routes connected, turning on routes
            // caching in production could improve performance. For that when
            // creating the middleware instance specify the cache config name by
            // using it's second constructor argument:
            // `new RoutingMiddleware($this, '_cake_routes_')`
            ->add(new RoutingMiddleware($this))

            // I really want to comment out and delete this bottom.
            ->add(new AuthenticationMiddleware($this));

        return $middlewareQueue;
    }

    /**
     * Bootrapping for CLI application.
     *
     * That is when running commands.
     *
     * @return void
     */
    protected function bootstrapCli(): void
    {
        try {
            $this->addPlugin('Bake');
        } catch (MissingPluginException $e) {
            // Do not halt if the plugin is missing
        }

        $this->addPlugin('Migrations');

        // Load more plugins here
    }

    // I really want to comment out and delete this bottom.
    public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
    {
        $authenticationService = new AuthenticationService([
            'unauthenticatedRedirect' => '/normal/users/login',
            'queryParam' => 'redirect',
        ]);

 
        $authenticationService->loadIdentifier('Authentication.Password', [
            'fields' => [
                'username' => 'mail',
                'password' => 'password',
            ]
        ]);


        $authenticationService->loadAuthenticator('Authentication.Session');

        $authenticationService->loadAuthenticator('Authentication.Form', [
            'fields' => [
                'username' => 'mail',
                'password' => 'password',
            ],
            'loginUrl' => '/normal/users/login',
        ]);

        return $authenticationService;
    }
}

因为这是我第一次使用“ stackoverflow”,所以我不确定如何问一个好问题。如果您能帮助我,我将不胜感激。

如果您能向我指出,我将不胜感激。

谢谢。

1 个答案:

答案 0 :(得分:0)

我相信答案是根据当前请求(管理员或普通用户)加载正确的中间件身份验证器。但是我还没有多重身份验证,因此可能不正确。在Application.php方法getAuthenticationService()中,在其中调用loadIdentifier()的情况下,根据当前请求URL(或区分管理员url)指定一个“解析器”或另一个。

CakePHP 4.x文档中有关于多种身份验证方案的部分。我相信您可以使用两个不同的表。 Configuring Multiple Authentication Setups

此论坛项目可能有您需要的答案(问题下方的答案):Multiple Authentication Setups