Blame view

app/library/App/Controllers/UserController.php 2.63 KB
15479603   Alex Savenko   initialize
1
2
3
4
5
  <?php
  
  namespace App\Controllers;
  
  use PhalconRest\Mvc\Controllers\CrudResourceController;
852ac6bc   Alex Savenko   user registration
6
  use Phalcon\Mvc\Model;
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
38
39
40
41
42
43
44
  
  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'
          ];
      }
e95cb5df   Alex Savenko   user registration
45
46
47
  
      public function registration() {
  
e95cb5df   Alex Savenko   user registration
48
49
          $data = $this->getPostedData();
  
e95cb5df   Alex Savenko   user registration
50
51
52
53
54
55
56
57
58
59
60
61
          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();
          }
  
e95cb5df   Alex Savenko   user registration
62
63
          $data = $this->transformPostData($data);
  
e95cb5df   Alex Savenko   user registration
64
65
66
67
68
69
70
71
          $item = $this->createModelInstance();
  
          $newItem = $this->createItem($item, $data);
  
          if (!$newItem) {
              return $this->onCreateFailed($item, $data);
          }
  
a2604481   Alex Savenko   user registration
72
73
74
  
          return $data;
  
e95cb5df   Alex Savenko   user registration
75
76
77
78
79
80
81
82
83
84
85
86
          $primaryKey = $this->getModelPrimaryKey();
          $responseData = $this->getFindData($newItem->$primaryKey);
  
          $response = $this->getCreateResponse($responseData, $data);
  
          $this->afterHandleCreate($newItem, $data, $response);
          $this->afterHandleWrite();
          $this->afterHandle();
  
          return $response;
  
      }
852ac6bc   Alex Savenko   user registration
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  
      protected function createItem(Model $item, $data)
      {
          $this->beforeAssignData($item, $data);
          $item->assign($data, null, $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
107
  }