15479603
Alex Savenko
initialize
|
1
2
3
4
|
<?php
namespace App\Controllers;
|
bb6147e1
Alex Savenko
registration
|
5
|
use App\Model\Project;
|
a6aec719
Alex Savenko
registration
|
6
|
use App\Model\User;
|
15479603
Alex Savenko
initialize
|
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
|
use PhalconRest\Mvc\Controllers\CrudResourceController;
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');
}
|
3582f649
Alex Savenko
registration
|
38
|
public function register() {
|
95d1b70c
Alex Savenko
registration
|
39
|
|
3754fbeb
Alex Savenko
registration
|
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
$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);
$item = $this->createModelInstance();
|
bb6147e1
Alex Savenko
registration
|
61
62
63
|
/** ****************************/
$user = new User();
|
ee392fef
Alex Savenko
registration
|
64
|
$user->username = 'test33233222';
|
2c27a20b
Alex Savenko
registration
|
65
|
$user->save();
|
3ef115a0
Alex Savenko
registration
|
66
|
|
bb6147e1
Alex Savenko
registration
|
67
68
69
70
71
72
|
echo $user->id;
$project = new Project();
$project->name = 'test33233222';
$project->save();
echo $project->id;
|
2c27a20b
Alex Savenko
registration
|
73
|
die();
|
e611d3a3
Alex Savenko
registration
|
74
|
|
bb6147e1
Alex Savenko
registration
|
75
76
|
/** **********************/
|
e611d3a3
Alex Savenko
registration
|
77
|
|
3754fbeb
Alex Savenko
registration
|
78
|
$newItem = $this->createItem($item, $data);
|
3754fbeb
Alex Savenko
registration
|
79
80
81
82
83
|
if (!$newItem) {
return $this->onCreateFailed($item, $data);
}
|
e611d3a3
Alex Savenko
registration
|
84
85
86
|
//$last_id = $newItem->getWriteConnection()->lastInsertId();
//$responseData = $this->getFindData($last_id);
|
c23be67b
Alex Savenko
registration
|
87
|
|
3754fbeb
Alex Savenko
registration
|
88
89
90
91
92
93
94
95
|
$response = $this->getCreateResponse($responseData, $data);
$this->afterHandleCreate($newItem, $data, $response);
$this->afterHandleWrite();
$this->afterHandle();
return $response;
|
3582f649
Alex Savenko
registration
|
96
97
|
}
|
175f1d0b
Alex Savenko
registration
|
98
99
100
101
102
103
104
105
|
// public function whitelist()
// {
// return [
// 'username',
// 'password',
// 'email'
// ];
// }
|
5a57cafe
Alex Savenko
registration
|
106
107
108
109
110
|
protected function getModelPrimaryKey()
{
return 'id';
}
|
ed3514b8
Alex Savenko
registration
|
111
|
|
11e41ecb
Alex Savenko
registration
|
112
113
114
115
116
117
118
119
120
|
protected function transformPostDataValue($key, $value, $data)
{
if ($key == 'password') {
return $this->security->hash($value);
} else {
return $value;
}
}
|
15479603
Alex Savenko
initialize
|
121
|
}
|