Advertisement
Guest User

Untitled

a guest
Apr 19th, 2020
167
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None
  1. <?php
  2. declare(strict_types=1);
  3.  
  4. /**
  5. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  13. * @link https://cakephp.org CakePHP(tm) Project
  14. * @since 3.3.0
  15. * @license https://opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace App;
  18.  
  19. use Cake\Core\Configure;
  20. use Cake\Core\Exception\MissingPluginException;
  21. use Cake\Error\Middleware\ErrorHandlerMiddleware;
  22. use Cake\Http\BaseApplication;
  23. use Cake\Http\MiddlewareQueue;
  24. use Cake\Routing\Middleware\AssetMiddleware;
  25. use Cake\Routing\Middleware\RoutingMiddleware;
  26. use Authentication\Middleware\AuthenticationMiddleware;
  27.  
  28. use Authorization\AuthorizationService;
  29. use Authorization\AuthorizationServiceInterface;
  30. use Authorization\AuthorizationServiceProviderInterface;
  31. use Authorization\Middleware\AuthorizationMiddleware;
  32. use Authorization\Policy\OrmResolver;
  33. use Psr\Http\Message\ResponseInterface;
  34. use Psr\Http\Message\ServerRequestInterface;
  35.  
  36. /**
  37. * Application setup class.
  38. *
  39. * This defines the bootstrapping logic and middleware layers you
  40. * want to use in your application.
  41. */
  42. class Application extends BaseApplication
  43. {
  44. /**
  45. * Load all the application configuration and bootstrap logic.
  46. *
  47. * @return void
  48. */
  49. public function bootstrap(): void
  50. {
  51. // Call parent to load bootstrap from files.
  52. parent::bootstrap();
  53.  
  54. if (PHP_SAPI === 'cli') {
  55. $this->bootstrapCli();
  56. }
  57.  
  58. /*
  59. * Only try to load DebugKit in development mode
  60. * Debug Kit should not be installed on a production system
  61. */
  62. if (Configure::read('debug')) {
  63. $this->addPlugin('DebugKit');
  64. }
  65. $this->addPlugin('Authentication');
  66. // Load more plugins here
  67. }
  68.  
  69. /**
  70. * Setup the middleware queue your application will use.
  71. *
  72. * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup.
  73. * @return \Cake\Http\MiddlewareQueue The updated middleware queue.
  74. */
  75. public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
  76. {
  77. $middlewareQueue
  78. ->add(new ErrorHandlerMiddleware(Configure::read('Error')))
  79. ->add(new AssetMiddleware(['cacheTime' => Configure::read('Asset.cacheTime')]))
  80. ->add(new RoutingMiddleware($this))
  81. ->add(new AuthenticationMiddleware($this->configAuth()));
  82. return $middlewareQueue;
  83. }
  84.  
  85. protected function configAuth(): \Authentication\AuthenticationService
  86. {
  87. $authenticationService = new \Authentication\AuthenticationService([
  88. 'unauthenticatedRedirect' => '/users/login',
  89. 'queryParam' => 'redirect',
  90. ]);
  91.  
  92. // Load identifiers, ensure we check email and password fields
  93. $authenticationService->loadIdentifier('Authentication.Password', [
  94. 'fields' => [
  95. 'username' => 'email',
  96. 'password' => 'password',
  97. ]
  98. ]);
  99.  
  100. // Load the authenticators, you want session first
  101. $authenticationService->loadAuthenticator('Authentication.Session');
  102. // Configure form data check to pick email and password
  103. $authenticationService->loadAuthenticator('Authentication.Form', [
  104. 'fields' => [
  105. 'username' => 'email',
  106. 'password' => 'password',
  107. ],
  108. 'loginUrl' => '/users/login',
  109. ]);
  110.  
  111. return $authenticationService;
  112. }
  113.  
  114. /**
  115. * Bootrapping for CLI application.
  116. *
  117. * That is when running commands.
  118. *
  119. * @return void
  120. */
  121. protected function bootstrapCli(): void
  122. {
  123. try {
  124. $this->addPlugin('Bake');
  125. } catch (MissingPluginException $e) {
  126. // Do not halt if the plugin is missing
  127. }
  128.  
  129. $this->addPlugin('Migrations');
  130. $this->addPlugin('Authentication');
  131. // Load more plugins here
  132. }
  133. }
Advertisement
RAW Paste Data Copied
Advertisement