This repository has been archived by the owner on Mar 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Services.php
124 lines (113 loc) · 4.57 KB
/
Services.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
<?php
namespace Synapse\Application;
use Synapse\Application;
/**
* Define services
*/
class Services implements ServicesInterface
{
/**
* {@inheritDoc}
*/
public function register(Application $app)
{
// Register log and error providers first to catch any exceptions thrown in the others
$app->register(new \Synapse\Log\LogServiceProvider);
$app->register(new ErrorServiceProvider);
// Register security component before other providers attempt to extend $app['security.firewalls']
$app->register(new \Silex\Provider\SecurityServiceProvider);
$this->registerSecurityFirewalls($app);
$this->registerServiceProviders($app);
}
/**
* Register service providers
*
* @param Application $app
*/
protected function registerServiceProviders(Application $app)
{
$app->register(new \Synapse\Command\CommandServiceProvider);
$app->register(new \Synapse\Db\DbServiceProvider);
$app->register(new \Synapse\OAuth2\ServerServiceProvider);
$app->register(new \Synapse\OAuth2\SecurityServiceProvider);
$app->register(new \Synapse\Resque\ResqueServiceProvider);
$app->register(new \Synapse\Controller\ControllerServiceProvider);
$app->register(new \Synapse\Email\EmailServiceProvider);
$app->register(new \Synapse\User\UserServiceProvider);
$app->register(new \Synapse\Migration\MigrationServiceProvider);
$app->register(new \Synapse\Install\InstallServiceProvider);
$app->register(new \Synapse\Security\SecurityServiceProvider);
$app->register(new \Synapse\Session\SessionServiceProvider);
$app->register(new \Synapse\SocialLogin\SocialLoginServiceProvider);
$app->register(new \Synapse\Time\TimeServiceProvider);
$app->register(new \Synapse\Validator\ValidatorServiceProvider);
$app->register(new \Synapse\View\ViewServiceProvider, [
'mustache.paths' => array(
APPDIR.'/templates'
),
'mustache.options' => [
'cache' => TMPDIR,
],
]);
$app->register(new \Silex\Provider\ValidatorServiceProvider);
$app->register(new \Silex\Provider\UrlGeneratorServiceProvider);
// Register the CORS middleware
$app->register(new \JDesrosiers\Silex\Provider\CorsServiceProvider);
$app->after($app['cors']);
}
/**
* Register the security firewalls for use with the Security Context in SecurityServiceProvider
*
* How to add application-specific firewalls:
*
* $app->extend('security.firewalls', function ($firewalls, $app) {
* $newFirewalls = [...];
*
* return array_merge($newFirewalls, $firewalls);
* });
*
* It's important to return an array with $firewalls at the end, as in the example,
* so that the catch-all 'base.api' firewall does not preclude more specific firewalls.
*
* Application-specific firewalls should only be needed to allow passthrough
* for public endpoints, since 'base.api' requires authentication.
*
* Firewalls available include:
* - oauth
* - Requires the user to be logged in
* - oauth-optional
* - Does not require the user to be logged in
* - If the user is logged in, sets their token on the security context so that their info can be accessed
* - anonymous
* - Does not require the user to be logged in
* - Does not attempt to retrieve user's information if Authentication header is sent
*
* The same can be done with security.access_rules, which are used to restrict
* sections of the application based on a user's role:
*
* $app->extend('security.access_rules', function ($rules, $app) {
* $newRules = [...];
*
* return array_merge($newRules, $rules);
* });
*
* @link http://silex.sensiolabs.org/doc/providers/security.html#defining-more-than-one-firewall
* @link http://silex.sensiolabs.org/doc/providers/security.html#defining-access-rules
*
* @param Application $app
*/
public function registerSecurityFirewalls(Application $app)
{
$app['security.firewalls'] = $app->share(function () {
return [
'base.api' => [
'pattern' => '^/',
'oauth' => true,
],
];
});
$app['security.access_rules'] = $app->share(function () {
return [];
});
}
}