Blame view

app/library/App/Controllers/UserController.php 2.29 KB
15479603   Alex Savenko   initialize
1
2
3
4
  <?php
  
  namespace App\Controllers;
  
a6aec719   Alex Savenko   registration
5
  use App\Model\User;
15479603   Alex Savenko   initialize
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
  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
37
      public function register() {
95d1b70c   Alex Savenko   registration
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  
          $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);
  
a77548f9   Alex Savenko   registration
55
          $item = $this->createModelInstance();
95d1b70c   Alex Savenko   registration
56
  
a77548f9   Alex Savenko   registration
57
          //$newItem = $this->createItem($item, $data);
596a708f   Alex Savenko   registration
58
              $this->beforeCreate($item);
a77548f9   Alex Savenko   registration
59
60
61
  
              $success = $item->create();
  
36a398b9   Alex Savenko   registration
62
63
64
65
66
67
              if ($success) {
  
                  $this->afterCreate($item);
                  $this->afterSave($item);
              }
  
a77548f9   Alex Savenko   registration
68
              $newItem = $success ? $item : null;
95d1b70c   Alex Savenko   registration
69
70
71
72
73
  
          if (!$newItem) {
              return $this->onCreateFailed($item, $data);
          }
  
34ce0f86   Alex Savenko   registration
74
          die(var_dump($newItem));
d0c4d979   Alex Savenko   registration
75
  
95d1b70c   Alex Savenko   registration
76
77
78
79
80
81
          $primaryKey = $this->getModelPrimaryKey();
          $responseData = $this->getFindData($newItem->$primaryKey);
  
          $response = $this->getCreateResponse($responseData, $data);
  
          return $response;
3582f649   Alex Savenko   registration
82
83
      }
  
15479603   Alex Savenko   initialize
84
85
86
87
88
89
90
91
92
      public function whitelist()
      {
          return [
              'firstName',
              'lastName',
              'password'
          ];
      }
  }