Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.3.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- 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\Middleware\AuthenticationMiddleware;
- use Authorization\AuthorizationService;
- use Authorization\AuthorizationServiceInterface;
- use Authorization\AuthorizationServiceProviderInterface;
- use Authorization\Middleware\AuthorizationMiddleware;
- use Authorization\Policy\OrmResolver;
- use Psr\Http\Message\ResponseInterface;
- 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
- {
- /**
- * 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');
- }
- $this->addPlugin('Authentication');
- // 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
- ->add(new ErrorHandlerMiddleware(Configure::read('Error')))
- ->add(new AssetMiddleware(['cacheTime' => Configure::read('Asset.cacheTime')]))
- ->add(new RoutingMiddleware($this))
- ->add(new AuthenticationMiddleware($this->configAuth()));
- return $middlewareQueue;
- }
- protected function configAuth(): \Authentication\AuthenticationService
- {
- $authenticationService = new \Authentication\AuthenticationService([
- 'unauthenticatedRedirect' => '/users/login',
- 'queryParam' => 'redirect',
- ]);
- // Load identifiers, ensure we check email and password fields
- $authenticationService->loadIdentifier('Authentication.Password', [
- 'fields' => [
- 'username' => 'email',
- 'password' => 'password',
- ]
- ]);
- // Load the authenticators, you want session first
- $authenticationService->loadAuthenticator('Authentication.Session');
- // Configure form data check to pick email and password
- $authenticationService->loadAuthenticator('Authentication.Form', [
- 'fields' => [
- 'username' => 'email',
- 'password' => 'password',
- ],
- 'loginUrl' => '/users/login',
- ]);
- return $authenticationService;
- }
- /**
- * 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');
- $this->addPlugin('Authentication');
- // Load more plugins here
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement