Blame view

app/library/App/Controllers/UserController.php 3.45 KB
15479603   Alex Savenko   initialize
1
2
3
4
  <?php
  
  namespace App\Controllers;
  
15479603   Alex Savenko   initialize
5
6
7
8
  use PhalconRest\Mvc\Controllers\CrudResourceController;
  
  class UserController extends CrudResourceController
  {
84125667   Alex Savenko   изменение пользов...
9
10
11
12
13
      /**
       * Accessible fields
       *
       * @return array
       */
a85cc509   Alex Savenko   registration
14
15
16
17
18
      public function whitelist()
      {
          return [
              'username',
              'password',
84125667   Alex Savenko   изменение пользов...
19
20
              'email',
              'role'
a85cc509   Alex Savenko   registration
21
22
23
          ];
      }
  
84125667   Alex Savenko   изменение пользов...
24
25
26
27
28
      /**
       * Возвращает текущего залогиненного пользователя
       *
       * @return mixed
       */
c4e1ecec   Alex Savenko   delete action for...
29
      public function meAction()
15479603   Alex Savenko   initialize
30
31
32
33
      {
          return $this->createResourceResponse($this->userService->getDetails());
      }
  
84125667   Alex Savenko   изменение пользов...
34
35
36
37
38
      /**
       * Авторизация пользователя через BasicAuth и возвращает токен доступа
       *
       * @return mixed
       */
c4e1ecec   Alex Savenko   delete action for...
39
      public function authenticateAction()
15479603   Alex Savenko   initialize
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
      {
          $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');
      }
  
84125667   Alex Savenko   изменение пользов...
61
62
63
64
65
      /**
       * Регистрация нового пользователя
       *
       * @return mixed
       */
c4e1ecec   Alex Savenko   delete action for...
66
      public function registerAction() {
95d1b70c   Alex Savenko   registration
67
  
3754fbeb   Alex Savenko   registration
68
69
70
71
72
73
74
75
76
          $this->beforeHandle();
          $this->beforeHandleWrite();
          $this->beforeHandleCreate();
  
          $data = $this->getPostedData();
  
          if (!$data || count($data) == 0) {
              return $this->onNoDataProvided();
          }
3754fbeb   Alex Savenko   registration
77
78
79
80
81
82
83
84
85
86
87
          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
88
  
3754fbeb   Alex Savenko   registration
89
          $newItem = $this->createItem($item, $data);
3754fbeb   Alex Savenko   registration
90
91
92
93
94
  
          if (!$newItem) {
              return $this->onCreateFailed($item, $data);
          }
  
3412fb9f   Alex Savenko   registration
95
96
          $last_id = $newItem->getWriteConnection()->lastInsertId();
          $responseData = $this->getFindData($last_id);
c23be67b   Alex Savenko   registration
97
  
3754fbeb   Alex Savenko   registration
98
99
100
101
102
103
104
105
          $response = $this->getCreateResponse($responseData, $data);
  
          $this->afterHandleCreate($newItem, $data, $response);
          $this->afterHandleWrite();
          $this->afterHandle();
  
          return $response;
  
3582f649   Alex Savenko   registration
106
107
      }
  
84125667   Alex Savenko   изменение пользов...
108
109
110
111
112
113
      /**
       * Переопределение входных данных
       *
       * @param $data
       * @return array
       */
6c48a95f   Alex Savenko   registration
114
115
116
117
118
119
120
121
      protected function transformPostData($data)
      {
          $result = [];
  
          foreach ($data as $key => $value) {
              $result[$key] = $this->transformPostDataValue($key, $value, $data);
          }
  
6c48a95f   Alex Savenko   registration
122
123
124
          return $result;
      }
  
84125667   Alex Savenko   изменение пользов...
125
126
127
128
129
130
131
132
      /**
       * Хеширование пароля
       *
       * @param $key
       * @param $value
       * @param $data
       * @return string
       */
11e41ecb   Alex Savenko   registration
133
134
135
136
137
138
139
140
141
      protected function transformPostDataValue($key, $value, $data)
      {
          if ($key == 'password') {
              return $this->security->hash($value);
          } else {
              return $value;
          }
      }
  
b15debd3   Alex Savenko   user errors response
142
  }