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\UserProvider; |
11
|
|
|
use BrainExe\Core\Authentication\UserVO; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @Controller |
16
|
|
|
*/ |
17
|
|
|
class UserController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var UserProvider |
21
|
|
|
*/ |
22
|
|
|
private $userProvider; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @Inject("@Core.Authentication.UserProvider") |
26
|
|
|
* @param UserProvider $userProvider |
27
|
|
|
*/ |
28
|
6 |
|
public function __construct(UserProvider $userProvider) |
29
|
|
|
{ |
30
|
6 |
|
$this->userProvider = $userProvider; |
31
|
6 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Request $request |
35
|
|
|
* @return UserVO |
36
|
|
|
* @Route("/user/", name="authenticate.current_user", methods="GET") |
37
|
|
|
* @Guest |
38
|
|
|
*/ |
39
|
1 |
|
public function getCurrentUser(Request $request) : UserVO |
40
|
|
|
{ |
41
|
1 |
|
return $request->attributes->get('user'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string[] |
46
|
|
|
* @Route("/user/avatar/", name="authenticate.avatars", methods="GET", options={"cache":10800}) |
47
|
|
|
*/ |
48
|
1 |
|
public function getAvatars() : array |
49
|
|
|
{ |
50
|
1 |
|
return UserVO::AVATARS; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Request $request |
55
|
|
|
* @param string $avatar |
56
|
|
|
* @return UserVO |
57
|
|
|
* @throws UserException |
58
|
|
|
* @Route("/user/avatar/{avatar}/", name="authenticate.setAvatar", methods="POST") |
59
|
|
|
*/ |
60
|
2 |
|
public function setAvatars(Request $request, $avatar) : UserVO |
61
|
|
|
{ |
62
|
2 |
|
if (!in_array($avatar, UserVO::AVATARS)) { |
63
|
1 |
|
throw new UserException(sprintf(_('Invalid avatar: %s'), $avatar)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** @var UserVO $user */ |
67
|
1 |
|
$user = $request->attributes->get('user'); |
68
|
1 |
|
$user->avatar = $avatar; |
69
|
1 |
|
$this->userProvider->setUserProperty($user, UserVO::PROPERTY_AVATAR); |
70
|
|
|
|
71
|
1 |
|
return $user; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Request $request |
76
|
|
|
* @return UserVO |
77
|
|
|
* @throws UserException |
78
|
|
|
* @Route("/user/change_email/", name="authenticate.setEmail", methods="POST") |
79
|
|
|
* @Guest |
80
|
|
|
*/ |
81
|
1 |
|
public function setEmail(Request $request) : UserVO |
82
|
|
|
{ |
83
|
|
|
/** @var UserVO $user */ |
84
|
1 |
|
$user = $request->attributes->get('user'); |
85
|
1 |
|
$user->email = $request->request->get('email'); |
86
|
1 |
|
$this->userProvider->setUserProperty($user, UserVO::PROPERTY_EMAIL); |
87
|
|
|
|
88
|
1 |
|
return $user; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Receives a list of all registered user names. indexed by user-id |
93
|
|
|
* |
94
|
|
|
* @return string[] |
95
|
|
|
* @Route("/user/list/", name="authenticate.list_user", methods="GET", options={"cache":30}) |
96
|
|
|
*/ |
97
|
1 |
|
public function getList() : array |
98
|
|
|
{ |
99
|
1 |
|
return array_flip($this->userProvider->getAllUserNames()); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|