This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
UsersController.php
146 lines (110 loc) · 3.8 KB
/
UsersController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace App\Controller\Rest\V1;
use App\Exception\AccessDeniedException;
use App\Exception\Exception;
use App\Exception\FormInvalidException;
use App\Exception\InvalidArgumentException;
use App\Model\User;
use App\Service\Mail;
use Symfony\Component\HttpFoundation\Request;
/**
* @see https://docs.symlex.org/en/latest/framework/rest/
*/
class UsersController extends EntityControllerAbstract
{
protected $modelName = 'User';
protected $searchFormName = 'User\Search';
protected $createFormName = 'User\Create';
protected $editFormName = 'User\Edit';
protected $mail;
public function setMailService(Mail $mail)
{
$this->mail = $mail;
}
public function getMailService(): Mail
{
if(!$this->mail) {
throw new Exception('Mail service not set');
}
return $this->mail;
}
public function cgetAction(Request $request)
{
if (!$this->session->isAdmin()) {
throw new AccessDeniedException("You don't have permission to query this service");
}
$form = $this->createForm($this->searchFormName)->setDefinedWritableValues($request->query->all())->validate();
return $this->search($form, $request);
}
public function optionsAction($id, Request $request)
{
if (!$this->session->isAdmin()) {
throw new AccessDeniedException("You don't have permission to query this service");
}
return parent::optionsAction($id, $request);
}
public function deleteAction($id)
{
if (!$this->session->isAdmin()) {
throw new AccessDeniedException('Users can not delete users');
}
parent::deleteAction($id);
}
public function putAction($id, Request $request)
{
if (!$this->session->isAdmin()) {
throw new AccessDeniedException('User ID does not match');
}
return parent::putAction($id, $request);
}
public function postAction(Request $request)
{
if (!$this->session->isAdmin()) {
throw new AccessDeniedException('Users can not create new users');
}
$result = parent::postAction($request);
/** @var User $model */
$model = $this->model;
$this->getMailService()->newUser($model);
return $result;
}
public function cputPasswordAction($id, Request $request)
{
if ($id != $this->session->getUserId()) {
throw new AccessDeniedException('User ID does not match');
}
/** @var User $model */
$model = $this->model;
$model->find($id);
$password = $request->get('password');
$new_password = $request->get('new_password');
if (!$model->passwordIsValid($password)) {
throw new InvalidArgumentException('Old password is invalid');
}
$model->updatePassword($new_password);
}
public function coptionsProfileAction($id)
{
if ($id != $this->session->getUserId()) {
throw new AccessDeniedException('User ID does not match');
}
$form = $this->createForm('User\Profile');
$this->model->find($id);
$form->setDefinedValues($this->model->getValues());
return $form->getAsArray();
}
public function postProfileAction($id, Request $request)
{
if ($id != $this->session->getUserId()) {
throw new AccessDeniedException('User ID does not match');
}
$this->model->find($id);
$form = $this->createForm('User\Profile');
$form->setDefinedWritableValues($request->request->all())->validate();
if ($form->hasErrors()) {
throw new FormInvalidException($form->getFirstError());
}
$this->model->update($form->getValues());
return $this->model->getValues();
}
}