18
18
use Symfony \Component \HttpFoundation \Response ;
19
19
use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
20
20
use Symfony \Component \Routing \Exception \RouteNotFoundException ;
21
+ use Symfony \Component \Security \Core \Authorization \AuthorizationCheckerInterface ;
22
+ use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
21
23
use JMS \Serializer \SerializerInterface ;
22
24
use JMS \Serializer \SerializationContext ;
23
25
24
26
class ResourceController
25
27
{
28
+ const ROLE_RESOURCE_READ = 'CMF_RESOURCE_READ ' ;
29
+ const ROLE_RESOURCE_WRITE = 'CMF_RESOURCE_WRITE ' ;
30
+
26
31
/**
27
32
* @var RepositoryRegistryInterface
28
33
*/
@@ -33,14 +38,20 @@ class ResourceController
33
38
*/
34
39
private $ serializer ;
35
40
41
+ /**
42
+ * @var AuthorizationCheckerInterface|null
43
+ */
44
+ private $ authorizationChecker ;
45
+
36
46
/**
37
47
* @param SerializerInterface $serializer
38
48
* @param RepositoryRegistryInterface $registry
39
49
*/
40
- public function __construct (SerializerInterface $ serializer , RepositoryRegistryInterface $ registry )
50
+ public function __construct (SerializerInterface $ serializer , RepositoryRegistryInterface $ registry, AuthorizationCheckerInterface $ authorizationChecker = null )
41
51
{
42
52
$ this ->serializer = $ serializer ;
43
53
$ this ->registry = $ registry ;
54
+ $ this ->authorizationChecker = $ authorizationChecker ;
44
55
}
45
56
46
57
/**
@@ -51,9 +62,15 @@ public function __construct(SerializerInterface $serializer, RepositoryRegistryI
51
62
*/
52
63
public function getResourceAction ($ repositoryName , $ path )
53
64
{
65
+ $ path = '/ ' .ltrim ($ path , '/ ' );
66
+
54
67
try {
55
68
$ repository = $ this ->registry ->get ($ repositoryName );
56
- $ resource = $ repository ->get ('/ ' .$ path );
69
+
70
+ $ fullPath = method_exists ($ repository , 'resolvePath ' ) ? $ repository ->resolvePath ($ path ) : $ path ;
71
+ $ this ->guardAccess ('read ' , $ repositoryName , $ fullPath );
72
+
73
+ $ resource = $ repository ->get ($ path );
57
74
58
75
return $ this ->createResponse ($ resource );
59
76
} catch (ResourceNotFoundException $ e ) {
@@ -86,9 +103,12 @@ public function getResourceAction($repositoryName, $path)
86
103
*/
87
104
public function patchResourceAction ($ repositoryName , $ path , Request $ request )
88
105
{
106
+ $ path = '/ ' .ltrim ($ path , '/ ' );
89
107
$ repository = $ this ->registry ->get ($ repositoryName );
90
108
91
- $ path = '/ ' .ltrim ($ path , '/ ' );
109
+ $ fullPath = method_exists ($ repository , 'resolvePath ' ) ? $ repository ->resolvePath ($ path ) : $ path ;
110
+ $ this ->guardAccess ('write ' , $ repositoryName , $ fullPath );
111
+
92
112
93
113
$ requestContent = json_decode ($ request ->getContent (), true );
94
114
if (!$ requestContent ) {
@@ -124,9 +144,11 @@ public function patchResourceAction($repositoryName, $path, Request $request)
124
144
*/
125
145
public function deleteResourceAction ($ repositoryName , $ path )
126
146
{
147
+ $ path = '/ ' .ltrim ($ path , '/ ' );
127
148
$ repository = $ this ->registry ->get ($ repositoryName );
128
149
129
- $ path = '/ ' .ltrim ($ path , '/ ' );
150
+ $ fullPath = method_exists ($ repository , 'resolvePath ' ) ? $ repository ->resolvePath ($ path ) : $ path ;
151
+ $ this ->guardAccess ('write ' , $ repositoryName , $ fullPath );
130
152
131
153
$ repository ->remove ($ path );
132
154
@@ -143,6 +165,18 @@ private function badRequestResponse($message)
143
165
return $ this ->createResponse (['message ' => $ message ], Response::HTTP_BAD_REQUEST );
144
166
}
145
167
168
+ private function guardAccess ($ attribute , $ repository , $ path )
169
+ {
170
+ if (null !== $ this ->authorizationChecker
171
+ && !$ this ->authorizationChecker ->isGranted (
172
+ 'CMF_RESOURCE_ ' .strtoupper ($ attribute ),
173
+ ['repository_name ' => $ repository , 'path ' => $ path ]
174
+ )
175
+ ) {
176
+ throw new AccessDeniedException (sprintf ('%s access denied for "%s". ' , ucfirst ($ attribute ), $ path ));
177
+ }
178
+ }
179
+
146
180
/**
147
181
* @param mixed $resource
148
182
* @param int $httpStatusCode
0 commit comments