1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RestTemplate\Rest; |
4
|
|
|
|
5
|
|
|
use Builder\Psr11; |
6
|
|
|
use RestTemplate\Model\User; |
7
|
|
|
|
8
|
|
|
class SampleProtected extends ServiceAbstractBase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Sample Ping Only Authenticated |
12
|
|
|
* |
13
|
|
|
* @SWG\Get( |
14
|
|
|
* path="/sampleprotected/ping", |
15
|
|
|
* tags={"sampleprotected"}, |
16
|
|
|
* security={{ |
17
|
|
|
* "jwt-token":{} |
18
|
|
|
* }}, |
19
|
|
|
* @SWG\Response( |
20
|
|
|
* response=200, |
21
|
|
|
* description="The object", |
22
|
|
|
* @SWG\Schema( |
23
|
|
|
* required={"result"}, |
24
|
|
|
* @SWG\Property(property="result", type="string") |
25
|
|
|
* ) |
26
|
|
|
* ), |
27
|
|
|
* @SWG\Response( |
28
|
|
|
* response=401, |
29
|
|
|
* description="Não autorizado", |
30
|
|
|
* @SWG\Schema(ref="#/definitions/error") |
31
|
|
|
* ) |
32
|
|
|
* ) |
33
|
|
|
* |
34
|
|
|
* @param \ByJG\RestServer\HttpResponse $response |
35
|
|
|
* @param \ByJG\RestServer\HttpRequest $request |
36
|
|
|
* @throws \ByJG\RestServer\Exception\Error401Exception |
37
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
38
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
39
|
|
|
*/ |
40
|
|
|
public function getPing($response, $request) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$this->requireAuthenticated(); |
43
|
|
|
|
44
|
|
|
$response->write([ |
45
|
|
|
'result' => 'pong' |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Sample Ping Only Admin |
51
|
|
|
* |
52
|
|
|
* @SWG\Get( |
53
|
|
|
* path="/sampleprotected/pingadm", |
54
|
|
|
* tags={"sampleprotected"}, |
55
|
|
|
* security={{ |
56
|
|
|
* "jwt-token":{} |
57
|
|
|
* }}, |
58
|
|
|
* @SWG\Response( |
59
|
|
|
* response=200, |
60
|
|
|
* description="The object", |
61
|
|
|
* @SWG\Schema( |
62
|
|
|
* required={"result"}, |
63
|
|
|
* @SWG\Property(property="result", type="string") |
64
|
|
|
* ) |
65
|
|
|
* ), |
66
|
|
|
* @SWG\Response( |
67
|
|
|
* response=401, |
68
|
|
|
* description="Não autorizado", |
69
|
|
|
* @SWG\Schema(ref="#/definitions/error") |
70
|
|
|
* ) |
71
|
|
|
* ) |
72
|
|
|
* |
73
|
|
|
* @param \ByJG\RestServer\HttpResponse $response |
74
|
|
|
* @param \ByJG\RestServer\HttpRequest $request |
75
|
|
|
* @throws \ByJG\RestServer\Exception\Error401Exception |
76
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
77
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
78
|
|
|
*/ |
79
|
|
|
public function getPingAdm($response, $request) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$this->requireRole('admin'); |
82
|
|
|
|
83
|
|
|
$response->write([ |
84
|
|
|
'result' => 'pongadm' |
85
|
|
|
]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Sample how to add an user; |
90
|
|
|
* |
91
|
|
|
* @SWG\Post( |
92
|
|
|
* path="/sampleprotected/adduser", |
93
|
|
|
* tags={"sampleprotected"}, |
94
|
|
|
* security={{ |
95
|
|
|
* "jwt-token":{} |
96
|
|
|
* }}, |
97
|
|
|
* @SWG\Parameter( |
98
|
|
|
* name="body", |
99
|
|
|
* in="body", |
100
|
|
|
* description="The login data", |
101
|
|
|
* required=true, |
102
|
|
|
* @SWG\Schema( |
103
|
|
|
* required={"username","password"}, |
104
|
|
|
* @SWG\Property(property="name", type="string", description="The Name"), |
105
|
|
|
* @SWG\Property(property="email", type="string", description="The Email"), |
106
|
|
|
* @SWG\Property(property="username", type="string", description="The username"), |
107
|
|
|
* @SWG\Property(property="password", type="string", description="The password"), |
108
|
|
|
* ) |
109
|
|
|
* ), |
110
|
|
|
* @SWG\Response( |
111
|
|
|
* response=200, |
112
|
|
|
* description="The object", |
113
|
|
|
* @SWG\Schema( |
114
|
|
|
* required={"result"}, |
115
|
|
|
* @SWG\Property(property="result", type="string") |
116
|
|
|
* ) |
117
|
|
|
* ), |
118
|
|
|
* @SWG\Response( |
119
|
|
|
* response=401, |
120
|
|
|
* description="Não autorizado", |
121
|
|
|
* @SWG\Schema(ref="#/definitions/error") |
122
|
|
|
* ) |
123
|
|
|
* ) |
124
|
|
|
* |
125
|
|
|
* @param \ByJG\RestServer\HttpResponse $response |
126
|
|
|
* @param \ByJG\RestServer\HttpRequest $request |
127
|
|
|
* @throws \ByJG\RestServer\Exception\Error401Exception |
128
|
|
|
* @throws \Psr\Container\ContainerExceptionInterface |
129
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
130
|
|
|
*/ |
131
|
|
|
public function postAddUser($response, $request) |
132
|
|
|
{ |
133
|
|
|
$this->requireRole('admin'); |
134
|
|
|
|
135
|
|
|
$data = json_decode($request->payload()); |
136
|
|
|
$user = new User($data->name, $data->email, $data->username, $data->password); |
137
|
|
|
$users = Psr11::container()->getClosure('LOGIN'); |
138
|
|
|
$users->save($user); |
139
|
|
|
|
140
|
|
|
$savedUser = $users->getByEmail($data->email); |
141
|
|
|
|
142
|
|
|
$updateField = $users->getUserDefinition()->getClosureForUpdate('userid'); |
143
|
|
|
$users->removeUserById($updateField($savedUser->getUserid())); |
144
|
|
|
|
145
|
|
|
$response->write([ |
146
|
|
|
'result' => 'pong' |
147
|
|
|
]); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.