The FOSRestBundle is working perfectly in my project but without authentification. Now, my goal is to make my requests with auth.
To do so, I added this firewall in security.yml
firewalls:
# ...
rest_api:
pattern: ^/api/
stateless: true
http_basic:
provider: fos_userbundle
# ...
access_control:
# ...
- { path: ^/api/, role: IS_AUTHENTICATED_FULLY }
To check this, I used this shell command:
curl -i http://localhost/tuto/web/app_dev.php/api/test/1
The result is:
HTTP/1.1 302 Found
Date: Fri, 11 Apr 2014 13:56:08 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.4.9-4ubuntu2.4
Set-Cookie: PHPSESSID=4dtr168vmj1eg523a07kbkjkh1; path=/
Cache-Control: no-cache
Location: http://localhost/tuto/web/app_dev.php/login
Vary: Accept-Language
X-Debug-Token: 220df7
Transfer-Encoding: chunked
Content-Type: application/json
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="1;url=http://localhost/tuto/web/app_dev.php/login" />
<title>Redirecting to http://localhost/tuto/web/app_dev.php/login</title>
</head>
<body>
Redirecting to <a href="http://localhost/tuto/web/app_dev.php/login">http://localhost/tuto/web/app_dev.php/login</a>.
</body>
</html>
As you can see, the returned code is 302 FOUND since it is URL is redirected to http://localhost/tuto/web/app_dev.php/login
as I am using FOSUserBundle.
This is strange since I defined my action() method as follows:
/**
* @Rest\View
* @Rest\Get("/api/test/{id}",
* requirements={"id" = "\d+"},
* defaults={"id" = 1}
* )
*/
public function getAction($id) {
$user = $this->get('security.context')->getToken()->getUser();
if(!($user instanceof \Minn\UserBundle\Entity\User)){
throw new \Symfony\Component\Security\Core\Exception\AccessDeniedException();
}
$repo = $this->getDoctrine()->
getManager()->
getRepository("MinnAdsAPIBundle:Test");
$entity = $repo->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find test entity!');
}
return array('test' => $entity);
}
So, is there any idea to fix my action() method?
Thanks.
By the way, what is the shell command to check my action with authentification?
I tried this command curl -i http://localhost/tuto/web/app_dev.php/api/test/1 --user user:password
but still I have 302 FOUND.