1414 */
1515namespace App ;
1616
17+ use Authentication \AuthenticationService ;
18+ use Authentication \AuthenticationServiceInterface ;
19+ use Authentication \AuthenticationServiceProviderInterface ;
20+ use Authentication \Middleware \AuthenticationMiddleware ;
21+ use Authorization \AuthorizationService ;
22+ use Authorization \AuthorizationServiceInterface ;
23+ use Authorization \AuthorizationServiceProviderInterface ;
24+ use Authorization \Middleware \AuthorizationMiddleware ;
25+ use Authorization \Policy \OrmResolver ;
26+ use Psr \Http \Message \ServerRequestInterface ;
1727use Cake \Core \Configure ;
1828use Cake \Error \Middleware \ErrorHandlerMiddleware ;
1929use Cake \Http \BaseApplication ;
2737 * want to use in your application.
2838 */
2939class Application extends BaseApplication
40+ implements AuthenticationServiceProviderInterface,
41+ AuthorizationServiceProviderInterface
3042{
43+ /**
44+ * Load all the application configuration and bootstrap logic.
45+ *
46+ * @return void
47+ */
48+ public function bootstrap (): void
49+ {
50+ // Call parent to load bootstrap from files.
51+ parent ::bootstrap ();
52+
53+ if (PHP_SAPI === 'cli ' ) {
54+ $ this ->bootstrapCli ();
55+ }
56+
57+ /*
58+ * Only try to load DebugKit in development mode
59+ * Debug Kit should not be installed on a production system
60+ */
61+ if (Configure ::read ('debug ' )) {
62+ $ this ->addPlugin ('DebugKit ' );
63+ }
64+ $ this ->addPlugin ('Authentication ' );
65+ $ this ->addPlugin ('Authorization ' );
66+
67+ // Load more plugins here
68+ }
69+
70+ /**
71+ * Bootrapping for CLI application.
72+ *
73+ * That is when running commands.
74+ *
75+ * @return void
76+ */
77+ protected function bootstrapCli (): void
78+ {
79+ try {
80+ $ this ->addPlugin ('Bake ' );
81+ } catch (MissingPluginException $ e ) {
82+ // Do not halt if the plugin is missing
83+ }
84+
85+ $ this ->addPlugin ('Migrations ' );
86+
87+ // Load more plugins here
88+ }
89+
3190 /**
3291 * Setup the middleware queue your application will use.
3392 *
@@ -48,14 +107,15 @@ public function middleware($middlewareQueue): \Cake\Http\MiddlewareQueue
48107 ->add (new RoutingMiddleware ($ this ))
49108
50109 // add Authentication after RoutingMiddleware
51- ->add (new \ Authentication \ Middleware \ AuthenticationMiddleware ($ this -> configAuth ()));
110+ ->add (new AuthenticationMiddleware ($ this ))
52111
53- return $ middlewareQueue ;
112+ // Add authorization **after** authentication
113+ ->add (new AuthorizationMiddleware ($ this ));
54114
55115 return $ middlewareQueue ;
56116 }
57117
58- protected function configAuth ( ): AuthenticationService
118+ public function getAuthenticationService ( ServerRequestInterface $ request ): AuthenticationServiceInterface
59119 {
60120 $ authenticationService = new AuthenticationService ([
61121 'unauthenticatedRedirect ' => '/users/login ' ,
@@ -83,4 +143,11 @@ protected function configAuth(): AuthenticationService
83143
84144 return $ authenticationService ;
85145 }
146+
147+ public function getAuthorizationService (ServerRequestInterface $ request ): AuthorizationServiceInterface
148+ {
149+ $ resolver = new OrmResolver ();
150+
151+ return new AuthorizationService ($ resolver );
152+ }
86153}
0 commit comments