1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\Authentication\Controller; |
4
|
|
|
|
5
|
|
|
use BrainExe\Annotations\Annotations\Inject; |
6
|
|
|
use BrainExe\Core\Annotations\Controller; |
7
|
|
|
use BrainExe\Core\Annotations\Guest; |
8
|
|
|
use BrainExe\Core\Annotations\Route; |
9
|
|
|
use BrainExe\Core\Application\UserException; |
10
|
|
|
use BrainExe\Core\Authentication\DatabaseUserProvider; |
11
|
|
|
use BrainExe\Core\Authentication\UserVO; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @Controller |
16
|
|
|
*/ |
17
|
|
|
class UserController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var DatabaseUserProvider |
21
|
|
|
*/ |
22
|
|
|
private $userProvider; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @Inject("@DatabaseUserProvider") |
26
|
|
|
* @param DatabaseUserProvider $userProvider |
27
|
2 |
|
*/ |
28
|
|
|
public function __construct(DatabaseUserProvider $userProvider) |
29
|
2 |
|
{ |
30
|
2 |
|
$this->userProvider = $userProvider; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Request $request |
35
|
|
|
* @return UserVO |
36
|
|
|
* @Route("/user/", name="authenticate.current_user", methods="GET") |
37
|
|
|
* @Guest |
38
|
1 |
|
*/ |
39
|
|
|
public function getCurrentUser(Request $request) |
40
|
1 |
|
{ |
41
|
|
|
return $request->attributes->get('user'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string[] |
46
|
|
|
* @Route("/user/avatar/", name="authenticate.avatars", methods="GET") |
47
|
|
|
* @Guest |
48
|
|
|
*/ |
49
|
1 |
|
public function getAvatars() |
50
|
|
|
{ |
51
|
1 |
|
return UserVO::AVATARS; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Request $request |
56
|
|
|
* @param string $avatar |
57
|
|
|
* @return UserVO |
58
|
|
|
* @throws UserException |
59
|
|
|
* @Route("/user/avatar/{avatar}/", name="authenticate.setAvatar", methods="POST") |
60
|
|
|
* @Guest |
61
|
|
|
*/ |
62
|
|
|
public function setAvatars(Request $request, $avatar) |
63
|
|
|
{ |
64
|
|
|
if (!in_array($avatar, UserVO::AVATARS)) { |
65
|
|
|
throw new UserException(sprintf(_('Invalid avatar: %s'), $avatar)); |
66
|
|
|
} |
67
|
|
|
/** @var UserVO $user */ |
68
|
|
|
$user = $request->attributes->get('user'); |
69
|
|
|
$user->avatar = $avatar; |
70
|
|
|
$this->userProvider->setUserProperty($user, UserVO::PROPERTY_AVATAR); |
71
|
|
|
|
72
|
|
|
return $user; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Receives a list all all registered users. indexed by user-id |
77
|
|
|
* |
78
|
|
|
* @return string[] |
79
|
|
|
* @Route("/user/list/", name="authenticate.list_user", methods="GET") |
80
|
|
|
*/ |
81
|
|
|
public function getList() |
82
|
|
|
{ |
83
|
|
|
return array_flip($this->userProvider->getAllUserNames()); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|