|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Controller\Infrastructure\RestController; |
|
6
|
|
|
use AppBundle\Entity\Repository\UserRepository; |
|
7
|
|
|
use AppBundle\Entity\User; |
|
8
|
|
|
use AppBundle\Response\ApiError; |
|
9
|
|
|
use AppBundle\Response\ApiResponse; |
|
10
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
|
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
12
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
13
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Actions with users |
|
18
|
|
|
* @author Vehsamrak |
|
19
|
|
|
* @Route("user") |
|
20
|
|
|
*/ |
|
21
|
|
|
class UserController extends RestController |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* View single user by login |
|
26
|
|
|
* @Route("/{login}", name="user_view") |
|
27
|
|
|
* @Method("GET") |
|
28
|
|
|
* @ApiDoc( |
|
29
|
|
|
* section="User", |
|
30
|
|
|
* statusCodes={ |
|
31
|
|
|
* 200="User was found", |
|
32
|
|
|
* 404="User with given login was not found", |
|
33
|
|
|
* } |
|
34
|
|
|
* ) |
|
35
|
|
|
* @param string $login user login |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function viewAction(string $login): Response |
|
38
|
|
|
{ |
|
39
|
2 |
|
return $this->viewEntity($this->get('rockparade.user_repository'), $login); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* View current user |
|
44
|
|
|
* @Route("", name="user_view_current") |
|
45
|
|
|
* @Method("GET") |
|
46
|
|
|
* @Security("has_role('ROLE_USER')") |
|
47
|
|
|
* @ApiDoc( |
|
48
|
|
|
* section="User", |
|
49
|
|
|
* statusCodes={ |
|
50
|
|
|
* 200="Current user is logged in", |
|
51
|
|
|
* 401="Authentication required", |
|
52
|
|
|
* } |
|
53
|
|
|
* ) |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function viewCurrentAction(): Response |
|
56
|
|
|
{ |
|
57
|
1 |
|
$user = $this->getUser(); |
|
58
|
|
|
|
|
59
|
1 |
|
if ($user) { |
|
60
|
1 |
|
$response = new ApiResponse($user, Response::HTTP_OK); |
|
61
|
|
|
} else { |
|
62
|
|
|
$response = $this->createUserNotLoggedInErrorResult(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
return $this->respond($response); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function createUserNotLoggedInErrorResult(): ApiError |
|
69
|
|
|
{ |
|
70
|
|
|
return new ApiError('You are not logged in.', Response::HTTP_UNAUTHORIZED); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|