Blame view

app/library/App/Model/User.php 2.91 KB
15479603   Alex Savenko   initialize
1
2
3
4
  <?php
  
  namespace App\Model;
  
86777356   Alex Savenko   кастомные фильды ...
5
6
7
8
9
10
11
  use App\Constants\AclRoles;
  use App\Mvc\DateTrackingModel;
  use App\User\Service;
  use PhalconApi\Exception;
  use PhalconApi\Constants\ErrorCodes;
  
  class User extends DateTrackingModel
15479603   Alex Savenko   initialize
12
13
14
  {
      public $id;
      public $role;
15479603   Alex Savenko   initialize
15
16
      public $username;
      public $password;
86777356   Alex Savenko   кастомные фильды ...
17
      public $email;
15479603   Alex Savenko   initialize
18
19
20
21
22
23
24
25
26
27
28
29
30
  
      public function getSource()
      {
          return 'user';
      }
  
      public function columnMap()
      {
          return parent::columnMap() + [
              'id' => 'id',
              'role' => 'role',
              'email' => 'email',
              'username' => 'username',
15479603   Alex Savenko   initialize
31
32
33
              'password' => 'password'
          ];
      }
983789a9   Alex Savenko   registration
34
35
36
  
      public function initialize()
      {
175f1d0b   Alex Savenko   registration
37
38
39
          $this->hasMany('id', Project::class, 'user_id', [
              'alias' => 'Projects',
          ]);
983789a9   Alex Savenko   registration
40
41
      }
  
86777356   Alex Savenko   кастомные фильды ...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
      public function getUsername()
      {
          return $this->username;
      }
  
      public function setUsername($username)
      {
          /** validation: unique, non-empty, 4+ letters **/
              $same_user = User::find(["username = '".$username."'"]);
              if (isset($same_user[0]) && !empty($same_user[0]->username))
              {
                  $msg = 'Post-data is invalid, trying to use non-unique value `'.$username.'` of `username`';
                  throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['username' => $username]);
              }
              elseif (empty($username))
              {
                  $msg = 'Post-data is invalid, trying to use empty value of `username`';
33813a62   Alex Savenko   функция проверки ...
59
                  throw new Exception(ErrorCodes::DATA_NOT_FOUND, $msg, ['username' => $username]);
86777356   Alex Savenko   кастомные фильды ...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
              }
              elseif (strlen($username) < 4)
              {
                  $msg = 'Post-data is invalid, value of `username` should be more than 4 letters';
                  throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['username' => $username]);
              }
          /** ---------------------------------------- **/
  
          $this->username = $username;
      }
  
      public function getEmail()
      {
          return $this->email;
      }
  
      public function setEmail($email)
      {
          /** validation: FILTER_VALIDATE_EMAIL **/
              if (!filter_var($email, FILTER_VALIDATE_EMAIL))
              {
                  $msg = 'Post-data is invalid, bad email value';
                  throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['email' => $email]);
              }
          /** ---------- */
  
          $this->email = $email;
      }
  
      public function getRole()
      {
          $service = new Service();
          return $service->getRole();
      }
  
      public function setRole($role)
      {
          /** validation: constant value **/
              if (!in_array($role, AclRoles::ALL_ROLES))
              {
                  $msg = 'Post-data is invalid, bad `role` value';
                  throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['role' => $role]);
              }
          /** -------------------------- **/
  
          $this->role = $role;
      }
  
15479603   Alex Savenko   initialize
108
  }