1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Gewaer\Models; |
5
|
|
|
|
6
|
|
|
use Gewaer\Traits\PermissionsTrait; |
7
|
|
|
use Phalcon\Cashier\Billable; |
8
|
|
|
use Gewaer\Exception\UnprocessableEntityHttpException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Users |
12
|
|
|
* |
13
|
|
|
* @package Gewaer\Models |
14
|
|
|
* |
15
|
|
|
* @property Users $user |
16
|
|
|
* @property Config $config |
17
|
|
|
* @property Apps $app |
18
|
|
|
* @property Companies $defaultCompany |
19
|
|
|
* @property \Phalcon\Di $di |
20
|
|
|
*/ |
21
|
|
|
class Users extends \Baka\Auth\Models\Users |
22
|
|
|
{ |
23
|
|
|
use PermissionsTrait; |
24
|
|
|
use Billable; |
25
|
|
|
|
26
|
|
|
public $default_company_branch; |
27
|
|
|
public $roles_id; |
28
|
|
|
public $stripe_id; |
29
|
|
|
public $card_last_four; |
30
|
|
|
public $card_brand; |
31
|
|
|
public $trial_ends_at; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Provide the app plan id |
35
|
|
|
* if the user is signing up a new company |
36
|
|
|
* |
37
|
|
|
* @var integer |
38
|
|
|
*/ |
39
|
|
|
public $appPlanId = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Initialize method for model. |
43
|
|
|
*/ |
44
|
18 |
|
public function initialize() |
45
|
|
|
{ |
46
|
18 |
|
parent::initialize(); |
47
|
|
|
|
48
|
18 |
|
$this->setSource('users'); |
49
|
|
|
|
50
|
18 |
|
$this->hasOne( |
51
|
18 |
|
'id', |
52
|
18 |
|
'Gewaer\Models\UserRoles', |
53
|
18 |
|
'users_id', |
54
|
18 |
|
['alias' => 'permission'] |
55
|
|
|
); |
56
|
|
|
|
57
|
18 |
|
$this->hasMany( |
58
|
18 |
|
'id', |
59
|
18 |
|
'Gewaer\Models\UserRoles', |
60
|
18 |
|
'users_id', |
61
|
18 |
|
['alias' => 'permissions'] |
62
|
|
|
); |
63
|
|
|
|
64
|
18 |
|
$this->hasManyToMany( |
65
|
18 |
|
'id', |
66
|
18 |
|
'Gewaer\Models\UserRoles', |
67
|
18 |
|
'users_id', |
68
|
18 |
|
'roles_id', |
69
|
18 |
|
'Gewaer\Models\Roles', |
70
|
18 |
|
'id', |
71
|
|
|
[ |
72
|
18 |
|
'alias' => 'roles', |
73
|
|
|
'params' => [ |
74
|
18 |
|
'limit' => 1, |
75
|
18 |
|
'conditions' => 'Gewaer\Models\UserRoles.apps_id = ' . $this->di->getConfig()->app->id, |
76
|
|
|
] |
77
|
|
|
] |
78
|
|
|
); |
79
|
18 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns table name mapped in the model. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
12 |
|
public function getSource(): string |
87
|
|
|
{ |
88
|
12 |
|
return 'users'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the User key for redis |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getKey(): string |
97
|
|
|
{ |
98
|
|
|
return $this->id; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get all of the subscriptions for the user. |
103
|
|
|
*/ |
104
|
|
|
public function subscriptions() |
105
|
|
|
{ |
106
|
|
|
$this->hasMany( |
107
|
|
|
'id', |
108
|
|
|
Subscription::class, |
109
|
|
|
'user_id', |
110
|
|
|
[ |
111
|
|
|
'alias' => 'subscriptions', |
112
|
|
|
'params' => [ |
113
|
|
|
'conditions' => 'apps_id = ?0 and company_id = ?1', |
114
|
|
|
'bind' => [$this->di->getApp()->getId(), $this->default_company], |
115
|
|
|
'order' => 'id DESC' |
116
|
|
|
] |
117
|
|
|
] |
118
|
|
|
); |
119
|
|
|
return $this->getRelated('subscriptions'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Before create |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
2 |
|
public function beforeCreate() |
128
|
|
|
{ |
129
|
2 |
|
parent::beforeCreate(); |
130
|
|
|
|
131
|
|
|
//Assign admin role to the system if we dont get a specify role |
132
|
2 |
|
if (empty($this->roles_id)) { |
133
|
1 |
|
$role = Roles::findFirstByName('Admins'); |
134
|
1 |
|
$this->roles_id = $role->getId(); |
135
|
|
|
} |
136
|
2 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* What to do after the creation of a new users |
140
|
|
|
* - Assign default role |
141
|
|
|
* |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
2 |
|
public function afterCreate() |
145
|
|
|
{ |
146
|
2 |
|
if (empty($this->default_company)) { |
147
|
1 |
|
parent::afterCreate(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
//Create new company associated company |
151
|
2 |
|
$newUserAssocCompany = new UsersAssociatedCompany(); |
152
|
2 |
|
$newUserAssocCompany->users_id = $this->id; |
153
|
2 |
|
$newUserAssocCompany->company_id = $this->default_company; |
154
|
2 |
|
$newUserAssocCompany->identify_id = 1; |
155
|
2 |
|
$newUserAssocCompany->user_active = 1; |
156
|
2 |
|
$newUserAssocCompany->user_role = $this->roles_id == 1 ? 'admins' : 'users'; |
157
|
|
|
|
158
|
2 |
|
if (!$newUserAssocCompany->save()) { |
159
|
|
|
throw new UnprocessableEntityHttpException((string) current($newUserAssocCompany->getMessages())); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
//Insert record into user_roles |
163
|
2 |
|
$userRole = new UserRoles(); |
164
|
2 |
|
$userRole->users_id = $this->id; |
165
|
2 |
|
$userRole->roles_id = $this->roles_id; |
166
|
2 |
|
$userRole->apps_id = $this->di->getApp()->getId(); |
167
|
2 |
|
$userRole->company_id = $this->default_company; |
168
|
|
|
|
169
|
2 |
|
if (!$userRole->save()) { |
170
|
|
|
throw new UnprocessableEntityHttpException((string) current($userRole->getMessages())); |
171
|
|
|
} |
172
|
2 |
|
} |
173
|
|
|
} |
174
|
|
|
|