Blame view

app/library/App/Model/User.php 3.58 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()
      {
0b8d4ffc   Alex Savenko   Add Roles.
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
          $this->hasMany(
              'id',
              Project::class,
              'user_id',
              [
                  'alias' => 'Projects',
              ]
          );
  
          $this->hasManyToMany(
              'id',
              UserProject::class,
              'user_id',
              'project_id',
              Project::class,
              'id',
              array('alias' => 'projects')
          );
983789a9   Alex Savenko   registration
55
56
      }
  
0b8d4ffc   Alex Savenko   Add Roles.
57
58
59
60
61
62
63
64
      /** ------- Getters and Setters ------- **/
  
          /** validation: unique, non-empty, 4+ letters *
           * @param $username
           * @throws Exception
           */
          public function setUsername($username)
          {
86777356   Alex Savenko   кастомные фильды ...
65
  
86777356   Alex Savenko   кастомные фильды ...
66
67
68
69
70
71
72
73
74
              $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`';
0b8d4ffc   Alex Savenko   Add Roles.
75
                  throw new Exception(ErrorCodes::POST_DATA_INVALID, $msg, ['username' => $username]);
86777356   Alex Savenko   кастомные фильды ...
76
77
78
79
80
81
              }
              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]);
              }
86777356   Alex Savenko   кастомные фильды ...
82
  
0b8d4ffc   Alex Savenko   Add Roles.
83
84
              $this->username = $username;
          }
86777356   Alex Savenko   кастомные фильды ...
85
  
0b8d4ffc   Alex Savenko   Add Roles.
86
87
88
89
90
91
          /** validation: FILTER_VALIDATE_EMAIL *
           * @param $email
           * @throws Exception
           */
          public function setEmail($email)
          {
86777356   Alex Savenko   кастомные фильды ...
92
93
94
95
96
              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]);
              }
86777356   Alex Savenko   кастомные фильды ...
97
  
0b8d4ffc   Alex Savenko   Add Roles.
98
99
              $this->email = $email;
          }
86777356   Alex Savenko   кастомные фильды ...
100
  
0b8d4ffc   Alex Savenko   Add Roles.
101
102
103
104
105
106
          /** validation: constant value *
           * @param $role
           * @throws Exception
           */
          public function setRole($role)
          {
84125667   Alex Savenko   изменение пользов...
107
              if (!in_array($role, AclRoles::ALL_REAL_ROLES))
86777356   Alex Savenko   кастомные фильды ...
108
109
              {
                  $msg = 'Post-data is invalid, bad `role` value';
84125667   Alex Savenko   изменение пользов...
110
111
112
113
114
                  throw new Exception(
                      ErrorCodes::POST_DATA_INVALID,
                      $msg,
                      ['role' => $role, 'valid values' => AclRoles::ALL_REAL_ROLES]
                  );
86777356   Alex Savenko   кастомные фильды ...
115
              }
86777356   Alex Savenko   кастомные фильды ...
116
  
0b8d4ffc   Alex Savenko   Add Roles.
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
              $this->role = $role;
          }
  
  
          public function getUsername()
          {
              return $this->username;
          }
  
          public function getEmail()
          {
              return $this->email;
          }
  
          public function getRole()
          {
              $service = new Service();
              return $service->getRole();
          }
  
      /** ----------------------------------- **/
86777356   Alex Savenko   кастомные фильды ...
138
  
15479603   Alex Savenko   initialize
139
  }