-
Notifications
You must be signed in to change notification settings - Fork 0
/
RouteServiceProvider.php
207 lines (186 loc) · 6.81 KB
/
RouteServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Foundation\Providers;
use CachetHQ\Cachet\Http\Middleware\Acceptable;
use CachetHQ\Cachet\Http\Middleware\Authenticate;
use CachetHQ\Cachet\Http\Middleware\RemoteUserAuthenticate;
use CachetHQ\Cachet\Http\Middleware\Timezone;
use CachetHQ\Cachet\Http\Middleware\VerifyCsrfToken;
use CachetHQ\Cachet\Http\Routes\ApiSystemRoutes;
use CachetHQ\Cachet\Http\Routes\AuthRoutes;
use CachetHQ\Cachet\Http\Routes\Setup\ApiRoutes as ApiSetupRoutes;
use CachetHQ\Cachet\Http\Routes\SetupRoutes;
use CachetHQ\Cachet\Http\Routes\SignupRoutes;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Routing\Router;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;
/**
* This is the route service provider.
*
* @author James Brooks <[email protected]>
* @author Joseph Cohen <[email protected]>
* @author Graham Campbell <[email protected]>
*/
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'CachetHQ\Cachet\Http\Controllers';
/**
* These are the route files that should always be available anonymously.
*
* When applying the always_authenticate feature, these routes will be skipped.
*
* @var string[]
*/
protected $whitelistedAuthRoutes = [
AuthRoutes::class,
SetupRoutes::class,
SignupRoutes::class,
ApiSystemRoutes::class,
ApiSetupRoutes::class,
];
/**
* Define the route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
parent::boot();
$this->app->call([$this, 'bind']);
}
/**
* Define the bindings for the application.
*
* @param \Illuminate\Routing\Router $router
*
* @return void
*/
public function bind(Router $router)
{
$router->model('component', 'CachetHQ\Cachet\Models\Component');
$router->model('component_group', 'CachetHQ\Cachet\Models\ComponentGroup');
$router->model('incident', 'CachetHQ\Cachet\Models\Incident');
$router->model('incident_template', 'CachetHQ\Cachet\Models\IncidentTemplate');
$router->model('incident_update', 'CachetHQ\Cachet\Models\IncidentUpdate');
$router->model('metric', 'CachetHQ\Cachet\Models\Metric');
$router->model('metric_point', 'CachetHQ\Cachet\Models\MetricPoint');
$router->model('schedule', 'CachetHQ\Cachet\Models\Schedule');
$router->model('setting', 'CachetHQ\Cachet\Models\Setting');
$router->model('subscriber', 'CachetHQ\Cachet\Models\Subscriber');
$router->model('subscription', 'CachetHQ\Cachet\Models\Subscription');
$router->model('tag', 'CachetHQ\Cachet\Models\Tag');
$router->model('user', 'CachetHQ\Cachet\Models\User');
}
/**
* Define the routes for the application.
*
* @param \Illuminate\Routing\Router $router
*
* @return void
*/
public function map(Router $router)
{
$router->group(['namespace' => $this->namespace, 'as' => 'core::'], function (Router $router) {
$path = app_path('Http/Routes');
$applyAlwaysAuthenticate = $this->app['config']->get('setting.always_authenticate', false);
$AllFileIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
$PhpFileIterator = new \RegexIterator($AllFileIterator, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH);
foreach ($PhpFileIterator as $file => $object) {
$class = substr($file, strlen($path));
$class = str_replace('/', '\\', $class);
$class = substr($class, 0, -4);
$routes = $this->app->make("CachetHQ\\Cachet\\Http\\Routes${class}");
if ($routes::$browser) {
$this->mapForBrowser($router, $routes, $applyAlwaysAuthenticate);
} else {
$this->mapOtherwise($router, $routes, $applyAlwaysAuthenticate);
}
}
});
}
/**
* Wrap the routes in the browser specific middleware.
*
* @param \Illuminate\Routing\Router $router
* @param object $routes
* @param bool $applyAlwaysAuthenticate
*
* @return void
*/
protected function mapForBrowser(Router $router, $routes, $applyAlwaysAuthenticate)
{
$middleware = [
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
];
if ($applyAlwaysAuthenticate && !$this->isWhiteListedAuthRoute($routes)) {
$middleware[] = RemoteUserAuthenticate::class;
$middleware[] = Authenticate::class;
}
$router->group(['middleware' => $middleware], function (Router $router) use ($routes) {
$routes->map($router);
});
}
/**
* Wrap the routes in the basic middleware.
*
* @param \Illuminate\Routing\Router $router
* @param object $routes
* @param bool $applyAlwaysAuthenticate
*
* @return void
*/
protected function mapOtherwise(Router $router, $routes, $applyAlwaysAuthenticate)
{
$middleware = [
SubstituteBindings::class,
Acceptable::class,
Timezone::class,
];
if ($applyAlwaysAuthenticate && !$this->isWhiteListedAuthRoute($routes)) {
$middleware[] = 'auth.api:true';
}
$router->group(['middleware' => $middleware], function (Router $router) use ($routes) {
$routes->map($router);
});
}
/**
* Validates if the route object is an instance of the whitelisted routes.
* A small workaround since we cant use multiple classes in a `instanceof` comparison.
*
* @param object $routes
*
* @return bool
*/
private function isWhiteListedAuthRoute($routes)
{
foreach ($this->whitelistedAuthRoutes as $whitelistedRoute) {
if (is_a($routes, $whitelistedRoute)) {
return true;
}
}
return false;
}
}