Blame view

app/library/App/Controllers/UserController.php 3.42 KB
15479603   Alex Savenko   initialize
1
2
3
4
  <?php
  
  namespace App\Controllers;
  
9d682170   Alex Savenko   user registration
5
  use App\Model\User;
06ecc69d   Alex Savenko   email validation
6
7
  use Phalcon\Validation;
  use Phalcon\Validation\Validator\Email;
15479603   Alex Savenko   initialize
8
  use PhalconRest\Mvc\Controllers\CrudResourceController;
06ecc69d   Alex Savenko   email validation
9
10
  use PhalconApi\Exception;
  use PhalconApi\Constants\ErrorCodes;
15479603   Alex Savenko   initialize
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  
  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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  
          $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
49
50
51
  
      public function registration() {
  
a07c0bce   Alex Savenko   test
52
53
54
55
          $this->beforeHandle();
          $this->beforeHandleWrite();
          $this->beforeHandleCreate();
  
e8cab5b7   Alex Savenko   test
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
          $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);
  
761fa362   Alex Savenko   test
72
73
74
75
76
77
          $newItem = new User();
          $map = $newItem->columnMap();
          foreach ($data as $key => $value) {
              if (in_array($key, $map))
              $newItem->$key = $value;
          }
ad0d0178   Alex Savenko   test
78
79
  
          if (!$newItem) {
cf5145f7   Alex Savenko   test
80
              return $this->onCreateFailed($newItem, $data);
ad0d0178   Alex Savenko   test
81
82
          }
  
c9f9c602   Alex Savenko   test
83
          $newItem->save();
fae1e3db   Alex Savenko   test
84
  
c9f9c602   Alex Savenko   test
85
86
          $last_id = $newItem->getWriteConnection()->lastInsertId();
  
c9f9c602   Alex Savenko   test
87
          $responseData = $this->getFindData($last_id);
ad0d0178   Alex Savenko   test
88
89
          $response = $this->getCreateResponse($responseData, $data);
  
a07c0bce   Alex Savenko   test
90
91
92
93
          $this->afterHandleCreate($newItem, $data, $response);
          $this->afterHandleWrite();
          $this->afterHandle();
  
ad0d0178   Alex Savenko   test
94
          return $response;
e95cb5df   Alex Savenko   user registration
95
  
e95cb5df   Alex Savenko   user registration
96
      }
852ac6bc   Alex Savenko   user registration
97
  
95622a8c   Alex Savenko   user registration
98
99
      protected function transformPostDataValue($key, $value, $data)
      {
93189714   Alex Savenko   test
100
          if ($key == 'pass') {
95622a8c   Alex Savenko   user registration
101
102
103
104
105
106
              return $this->security->hash($value);
          }
          else {
              return $value;
          }
      }
1a3a0f1d   Alex Savenko   user registration
107
  
06ecc69d   Alex Savenko   email validation
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
      protected function beforeHandleWrite()
      {
          $email_field = 'email';
  
          $validation = new Validation();
  
          $validation->add(
              $email_field,
              new Email(
                  [
                      "message" => "The e-mail is not valid",
                  ]
              )
          );
  
          $data = $this->getPostedData();
  
          if (!isset($data[$email_field])) {
  
2c0b6e23   Alex Savenko   email validation
127
              $message = $validation->validate($data['email']);
06ecc69d   Alex Savenko   email validation
128
129
130
131
132
133
134
135
136
137
138
139
              if (count($message)) {
                  throw new Exception(ErrorCodes::DATA_FAILED, 'Unable to create item', [
                      'messages' => $message,
                      'data' => $data[$email_field]
                  ]);
              }
  
          }
  
  
      }
  
15479603   Alex Savenko   initialize
140
  }