UserController.php
3.48 KB
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
<?php
namespace App\Controllers;
use App\Model\User;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Email;
use PhalconRest\Mvc\Controllers\CrudResourceController;
use PhalconApi\Exception;
use PhalconApi\Constants\ErrorCodes;
class UserController extends CrudResourceController
{
public function me()
{
return $this->createResourceResponse($this->userService->getDetails());
}
public function authenticate()
{
$username = $this->request->getUsername();
$password = $this->request->getPassword();
$session = $this->authManager->loginWithUsernamePassword(\App\Auth\UsernameAccountType::NAME, $username,
$password);
$transformer = new \App\Transformers\UserTransformer;
$transformer->setModelClass('App\Model\User');
$user = $this->createItemResponse(\App\Model\User::findFirst($session->getIdentity()), $transformer);
$response = [
'token' => $session->getToken(),
'expires' => $session->getExpirationTime(),
'user' => $user
];
return $this->createArrayResponse($response, 'data');
}
public function whitelist()
{
return [
'firstName',
'lastName',
'password'
];
}
public function registration() {
$this->beforeHandle();
$this->beforeHandleWrite();
$this->beforeHandleCreate();
$data = $this->getPostedData();
if (!$data || count($data) == 0) {
return $this->onNoDataProvided();
}
if (!$this->postDataValid($data, false)) {
return $this->onDataInvalid($data);
}
if (!$this->saveAllowed($data) || !$this->createAllowed($data)) {
return $this->onNotAllowed();
}
$data = $this->transformPostData($data);
$newItem = new User();
$map = $newItem->columnMap();
foreach ($data as $key => $value) {
if (in_array($key, $map))
$newItem->$key = $value;
}
if (!$newItem) {
return $this->onCreateFailed($newItem, $data);
}
$newItem->save();
$last_id = $newItem->getWriteConnection()->lastInsertId();
$responseData = $this->getFindData($last_id);
$response = $this->getCreateResponse($responseData, $data);
$this->afterHandleCreate($newItem, $data, $response);
$this->afterHandleWrite();
$this->afterHandle();
return $response;
}
protected function transformPostDataValue($key, $value, $data)
{
if ($key == 'pass') {
return $this->security->hash($value);
}
else {
return $value;
}
}
protected function beforeHandleWrite()
{
throw new Exception('777', "test exception");
$email_field = 'email';
$validation = new Validation();
$validation->add(
$email_field,
new Email(
[
"message" => "The e-mail is not valid",
]
)
);
$data = $this->getPostedData();
if (!isset($data[$email_field])) {
$message = $validation->validate($data[$email_field]);
if (count($message)) {
throw new Exception(ErrorCodes::DATA_FAILED, 'Unable to create item', [
'messages' => $message,
'data' => $data[$email_field]
]);
}
}
}
}