Blame view

app/library/App/Controllers/UserController.php 2.96 KB
15479603   Alex Savenko   initialize
1
2
3
4
  <?php
  
  namespace App\Controllers;
  
9d682170   Alex Savenko   user registration
5
  use App\Model\User;
15479603   Alex Savenko   initialize
6
  use PhalconRest\Mvc\Controllers\CrudResourceController;
9d682170   Alex Savenko   user registration
7
  use Phalcon\Mvc\Model;
15479603   Alex Savenko   initialize
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  
  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);
15479603   Alex Savenko   initialize
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  
          $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'
          ];
      }
e95cb5df   Alex Savenko   user registration
46
47
48
  
      public function registration() {
  
cdd122fc   Alex Savenko   user registration
49
50
51
52
53
54
          $this->beforeHandle();
          $this->beforeHandleWrite();
          $this->beforeHandleCreate();
  
          $data = $this->getPostedData();
  
cdd122fc   Alex Savenko   user registration
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
          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();
cdd122fc   Alex Savenko   user registration
70
  
aba75087   Alex Savenko   user registration
71
          $newItem = $this->createItem($item, $data);
7a2fdcb0   Alex Savenko   user registration
72
  
cdd122fc   Alex Savenko   user registration
73
74
75
76
          if (!$newItem) {
              return $this->onCreateFailed($item, $data);
          }
  
e1949eee   Alex Savenko   user registration
77
  
cdd122fc   Alex Savenko   user registration
78
          $primaryKey = $this->getModelPrimaryKey();
cdd122fc   Alex Savenko   user registration
79
80
          $responseData = $this->getFindData($newItem->$primaryKey);
  
cdd122fc   Alex Savenko   user registration
81
82
          $response = $this->getCreateResponse($responseData, $data);
  
cdd122fc   Alex Savenko   user registration
83
84
85
86
          $this->afterHandleCreate($newItem, $data, $response);
          $this->afterHandleWrite();
          $this->afterHandle();
  
cdd122fc   Alex Savenko   user registration
87
          return $response;
e95cb5df   Alex Savenko   user registration
88
89
  
      }
852ac6bc   Alex Savenko   user registration
90
  
95622a8c   Alex Savenko   user registration
91
92
93
94
95
96
97
98
99
      protected function transformPostDataValue($key, $value, $data)
      {
          if ($key == 'password') {
              return $this->security->hash($value);
          }
          else {
              return $value;
          }
      }
1a3a0f1d   Alex Savenko   user registration
100
  
9d682170   Alex Savenko   user registration
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
      protected function createItem(User $item, $data)
      {
          $this->beforeAssignData($item, $data);
          $item->assign($data, $item->columnMap(), $this->whitelistCreate());
          $this->afterAssignData($item, $data);
  
          $this->beforeSave($item);
          $this->beforeCreate($item);
  
          $success = $item->create();
  
          if ($success) {
  
              $this->afterCreate($item);
              $this->afterSave($item);
          }
  
          return $success ? $item : null;
      }
  
15479603   Alex Savenko   initialize
121
  }