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
/
Copy pathUserServiceProvider.php
168 lines (139 loc) · 5.53 KB
/
UserServiceProvider.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
<?php
namespace Synapse\User;
use Synapse\User\TokenEntity;
use Synapse\User\TokenMapper;
use Synapse\View\Email\VerifyRegistration as VerifyRegistrationView;
use Synapse\View\Email\ResetPassword as ResetPasswordView;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Symfony\Component\HttpFoundation\RequestMatcher;
/**
* Service provider for user related services
*/
class UserServiceProvider implements ServiceProviderInterface
{
/**
* Register services related to Users
*
* @param Application $app
*/
public function register(Application $app)
{
$app['user.mapper'] = $app->share(function ($app) {
return new UserMapper($app['db'], new UserEntity);
});
$app['user-token.mapper'] = $app->share(function ($app) {
return new TokenMapper($app['db'], new TokenEntity);
});
$app['user-role-pivot.mapper'] = $app->share(function ($app) {
return new UserRolePivotMapper($app['db']);
});
$app['user.service'] = $app->share(function ($app) {
$verifyRegistrationView = new VerifyRegistrationView($app['mustache']);
$verifyRegistrationView->setUrlGenerator($app['url_generator']);
$service = new UserService;
$service->setUserMapper($app['user.mapper'])
->setTokenMapper($app['user-token.mapper'])
->setEmailService($app['email.service'])
->setVerifyRegistrationView($verifyRegistrationView);
return $service;
});
$app['role.service'] = $app->share(function ($app) {
return new RoleService(
$app['user-role-pivot.mapper']
);
});
$app['user.validator'] = $app->share(function ($app) {
return new UserValidator($app['validator']);
});
$app['user-registration.validator'] = $app->share(function ($app) {
return new UserRegistrationValidator($app['validator']);
});
$app['user.controller'] = $app->share(function ($app) {
$controller = new UserController();
$controller
->setUserService($app['user.service'])
->setUserValidator($app['user.validator'])
->setUserRegistrationValidator($app['user-registration.validator']);
return $controller;
});
$app['user.converter'] = $app->share(function ($app) {
return new UserConverter($app['user.mapper']);
});
$app['verify-registration.controller'] = $app->share(function ($app) {
$controller = new VerifyRegistrationController();
$controller->setUserService($app['user.service']);
return $controller;
});
$app['reset-password.controller'] = $app->share(function ($app) {
$resetPasswordView = new ResetPasswordView($app['mustache']);
$resetPasswordView->setUrlGenerator($app['url_generator']);
return new ResetPasswordController(
$app['user.service'],
$app['email.service'],
$resetPasswordView
);
});
$app->match('/users', 'user.controller:rest')
->method('HEAD|POST')
->bind('user-collection');
$app->match('/user', 'user.controller:rest')
->method('GET|PUT')
->bind('user-entity-self');
$app->match('/users/{user}', 'user.controller:rest')
->method('GET')
->bind('user-entity')
->assert('user', '\d+')
->convert('user', 'user.converter:getUser');
$app->match('/users/{id}/verify-registration', 'verify-registration.controller:rest')
->method('POST')
->bind('verify-registration');
$app->match('/user/reset-password', 'reset-password.controller:rest')
->method('POST|PUT')
->bind('reset-password');
$this->setFirewallsAndAccessRules($app);
}
/**
* Perform chores on boot. (None required here.)
*
* @param Application $app
*/
public function boot(Application $app)
{
// Noop
}
/**
* Set user related firewalls
*
* @param Application $app
*/
protected function setFirewallsAndAccessRules(Application $app)
{
$app->extend('security.firewalls', function ($firewalls, $app) {
$createUser = new RequestMatcher('^/users$', null, ['POST']);
$verifyRegistration = new RequestMatcher('^/users/[0-9]+/verify-registration$', null, ['POST']);
$userFirewalls = [
'create-users' => [
'pattern' => $createUser, // User registration endpoint is public
'anonymous' => true,
],
'verify-registration' => [
'pattern' => $verifyRegistration, // User registration endpoint is public
'anonymous' => true,
],
'reset-password' => [
'pattern' => '^/user/reset-password$',
'anonymous' => true,
],
];
return array_merge($userFirewalls, $firewalls);
});
$app->extend('security.access_rules', function ($rules, $app) {
$usersAdminFunctionRequestMatcher = new RequestMatcher('^/users/\d+$', null, ['GET']);
$newRules = [
[$usersAdminFunctionRequestMatcher, 'ROLE_ADMIN']
];
return array_merge($newRules, $rules);
});
}
}