1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Gewaer\Models; |
5
|
|
|
|
6
|
|
|
use Gewaer\Traits\PermissionsTrait; |
7
|
|
|
use Gewaer\Traits\SubscriptionPlanLimitTrait; |
8
|
|
|
use Phalcon\Cashier\Billable; |
9
|
|
|
use Gewaer\Exception\ServerErrorHttpException; |
10
|
|
|
use Exception; |
11
|
|
|
use Carbon\Carbon; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Users |
15
|
|
|
* |
16
|
|
|
* @package Gewaer\Models |
17
|
|
|
* |
18
|
|
|
* @property Users $user |
19
|
|
|
* @property Config $config |
20
|
|
|
* @property Apps $app |
21
|
|
|
* @property Companies $defaultCompany |
22
|
|
|
* @property \Phalcon\Di $di |
23
|
|
|
*/ |
24
|
|
|
class Users extends \Baka\Auth\Models\Users |
25
|
|
|
{ |
26
|
|
|
use PermissionsTrait; |
27
|
|
|
use Billable; |
|
|
|
|
28
|
|
|
use SubscriptionPlanLimitTrait; |
29
|
|
|
|
30
|
|
|
public $default_company_branch; |
31
|
|
|
public $roles_id; |
32
|
|
|
public $stripe_id; |
33
|
|
|
public $card_last_four; |
34
|
|
|
public $card_brand; |
35
|
|
|
public $trial_ends_at; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Provide the app plan id |
39
|
|
|
* if the user is signing up a new company |
40
|
|
|
* |
41
|
|
|
* @var integer |
42
|
|
|
*/ |
43
|
|
|
public $appPlanId = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Initialize method for model. |
47
|
|
|
*/ |
48
|
33 |
|
public function initialize() |
49
|
|
|
{ |
50
|
33 |
|
$this->setSource('users'); |
51
|
|
|
|
52
|
|
|
//overwrite parent relationships |
53
|
33 |
|
$this->hasOne('id', 'Baka\Auth\Models\Sessions', 'users_id', ['alias' => 'session']); |
54
|
33 |
|
$this->hasMany('id', 'Baka\Auth\Models\Sessions', 'users_id', ['alias' => 'sessions']); |
55
|
33 |
|
$this->hasMany('id', 'Baka\Auth\Models\SessionKeys', 'users_id', ['alias' => 'sessionKeys']); |
56
|
33 |
|
$this->hasMany('id', 'Baka\Auth\Models\Banlist', 'users_id', ['alias' => 'bans']); |
57
|
33 |
|
$this->hasMany('id', 'Baka\Auth\Models\Sessions', 'users_id', ['alias' => 'sessions']); |
58
|
33 |
|
$this->hasMany('id', 'Gewaer\Models\UserConfig', 'users_id', ['alias' => 'config']); |
59
|
33 |
|
$this->hasMany('id', 'Gewaer\Models\UserLinkedSources', 'users_id', ['alias' => 'sources']); |
60
|
33 |
|
$this->hasMany('id', 'Baka\Auth\Models\UsersAssociatedCompany', 'users_id', ['alias' => 'companies']); |
61
|
33 |
|
$this->hasOne('default_company', 'Gewaer\Models\Companies', 'id', ['alias' => 'defaultCompany']); |
62
|
|
|
|
63
|
33 |
|
$this->hasOne( |
64
|
33 |
|
'id', |
65
|
33 |
|
'Gewaer\Models\UserRoles', |
66
|
33 |
|
'users_id', |
67
|
33 |
|
['alias' => 'permission'] |
68
|
|
|
); |
69
|
|
|
|
70
|
33 |
|
$this->hasMany( |
71
|
33 |
|
'id', |
72
|
33 |
|
'Gewaer\Models\UserRoles', |
73
|
33 |
|
'users_id', |
74
|
33 |
|
['alias' => 'permissions'] |
75
|
|
|
); |
76
|
|
|
|
77
|
33 |
|
$this->hasManyToMany( |
78
|
33 |
|
'id', |
79
|
33 |
|
'Gewaer\Models\UserRoles', |
80
|
33 |
|
'users_id', |
81
|
33 |
|
'roles_id', |
82
|
33 |
|
'Gewaer\Models\Roles', |
83
|
33 |
|
'id', |
84
|
|
|
[ |
85
|
33 |
|
'alias' => 'roles', |
86
|
|
|
'params' => [ |
87
|
33 |
|
'limit' => 1, |
88
|
33 |
|
'conditions' => 'Gewaer\Models\UserRoles.apps_id = ' . $this->di->getConfig()->app->id, |
89
|
|
|
] |
90
|
|
|
] |
91
|
|
|
); |
92
|
|
|
|
93
|
33 |
|
$this->hasMany( |
94
|
33 |
|
'id', |
95
|
33 |
|
'Gewaer\Models\Subscription', |
96
|
33 |
|
'user_id', |
97
|
|
|
[ |
98
|
33 |
|
'alias' => 'allSubscriptions', |
99
|
|
|
'params' => [ |
100
|
33 |
|
'conditions' => 'apps_id = ?0', |
101
|
33 |
|
'bind' => [$this->di->getApp()->getId()], |
102
|
33 |
|
'order' => 'id DESC' |
103
|
|
|
] |
104
|
|
|
] |
105
|
|
|
); |
106
|
33 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns table name mapped in the model. |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
26 |
|
public function getSource() : string |
114
|
|
|
{ |
115
|
26 |
|
return 'users'; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the User key for redis |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getKey() : string |
124
|
|
|
{ |
125
|
|
|
return $this->id; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* A company owner is the first person that register this company |
130
|
|
|
* This only ocurres when signing up the first time, after that all users invites |
131
|
|
|
* come with a default_company id attached |
132
|
|
|
* |
133
|
|
|
* @return boolean |
134
|
|
|
*/ |
135
|
4 |
|
public function isFirstSignup(): bool |
136
|
|
|
{ |
137
|
4 |
|
return empty($this->default_company) ? true : false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Does the user have a role assign to him? |
142
|
|
|
* |
143
|
|
|
* @return boolean |
144
|
|
|
*/ |
145
|
4 |
|
public function hasRole(): bool |
146
|
|
|
{ |
147
|
4 |
|
return !empty($this->roles_id) ? true : false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get all of the subscriptions for the user. |
152
|
|
|
*/ |
153
|
3 |
|
public function subscriptions() |
154
|
|
|
{ |
155
|
3 |
|
$this->hasMany( |
156
|
3 |
|
'id', |
157
|
3 |
|
'Gewaer\Models\Subscription', |
158
|
3 |
|
'user_id', |
159
|
|
|
[ |
160
|
3 |
|
'alias' => 'subscriptions', |
161
|
|
|
'params' => [ |
162
|
3 |
|
'conditions' => 'apps_id = ?0 and company_id = ?1', |
163
|
3 |
|
'bind' => [$this->di->getApp()->getId(), $this->default_company], |
164
|
3 |
|
'order' => 'id DESC' |
165
|
|
|
] |
166
|
|
|
] |
167
|
|
|
); |
168
|
|
|
|
169
|
3 |
|
return $this->getRelated('subscriptions'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Strat a free trial |
174
|
|
|
* |
175
|
|
|
* @param Users $user |
176
|
|
|
* @return Subscription |
177
|
|
|
*/ |
178
|
1 |
|
public function startFreeTrial() : Subscription |
179
|
|
|
{ |
180
|
1 |
|
$defaultPlan = AppsPlans::getDefaultPlan(); |
181
|
|
|
|
182
|
1 |
|
$subscription = new Subscription(); |
183
|
1 |
|
$subscription->user_id = $this->getId(); |
184
|
1 |
|
$subscription->company_id = $this->default_company; |
185
|
1 |
|
$subscription->apps_id = $this->di->getApp()->getId(); |
186
|
1 |
|
$subscription->apps_plans_id = $this->di->getApp()->default_apps_plan_id; |
187
|
1 |
|
$subscription->name = $defaultPlan->name; |
188
|
1 |
|
$subscription->stripe_id = $defaultPlan->stripe_id; |
189
|
1 |
|
$subscription->stripe_plan = $defaultPlan->stripe_plan; |
190
|
1 |
|
$subscription->quantity = 1; |
191
|
1 |
|
$subscription->trial_ends_at = Carbon::now()->addDays($this->di->getApp()->plan->free_trial_dates)->toDateTimeString(); |
192
|
|
|
|
193
|
1 |
|
if (!$subscription->save()) { |
194
|
|
|
throw new ServerErrorHttpException((string)current($this->getMessages())); |
195
|
|
|
} |
196
|
|
|
|
197
|
1 |
|
$this->trial_ends_at = $subscription->trial_ends_at; |
198
|
1 |
|
$this->update(); |
199
|
|
|
|
200
|
1 |
|
return $subscription; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Before create |
205
|
|
|
* |
206
|
|
|
* @return void |
207
|
|
|
*/ |
208
|
4 |
|
public function beforeCreate() |
209
|
|
|
{ |
210
|
4 |
|
parent::beforeCreate(); |
211
|
|
|
|
212
|
|
|
//this is only empty when creating a new user |
213
|
4 |
|
if (!$this->isFirstSignup()) { |
214
|
|
|
//confirm if the app reach its limit |
215
|
3 |
|
$this->isAtLimit(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
//Assign admin role to the system if we dont get a specify role |
219
|
3 |
|
if (!$this->hasRole()) { |
220
|
1 |
|
$role = Roles::findFirstByName('Admins'); |
221
|
1 |
|
$this->roles_id = $role->getId(); |
222
|
|
|
} |
223
|
3 |
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* What to do after the creation of a new users |
227
|
|
|
* - Assign default role |
228
|
|
|
* |
229
|
|
|
* @return void |
230
|
|
|
*/ |
231
|
3 |
|
public function afterCreate() |
232
|
|
|
{ |
233
|
|
|
//need to run it here, since we overwirte the default_company id and null this function objective |
234
|
3 |
|
$isFirstSignup = $this->isFirstSignup(); |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* User signing up for a new app / plan |
238
|
|
|
* How do we know? well he doesnt have a default_company |
239
|
|
|
*/ |
240
|
3 |
|
if ($isFirstSignup) { |
241
|
1 |
|
$company = new Companies(); |
242
|
1 |
|
$company->name = $this->defaultCompanyName; |
243
|
1 |
|
$company->users_id = $this->getId(); |
244
|
|
|
|
245
|
1 |
|
if (!$company->save()) { |
246
|
|
|
throw new Exception(current($company->getMessages())); |
247
|
|
|
} |
248
|
|
|
|
249
|
1 |
|
$this->default_company = $company->getId(); |
250
|
|
|
|
251
|
1 |
|
if (!$this->update()) { |
252
|
|
|
throw new ServerErrorHttpException((string) current($this->getMessages())); |
253
|
|
|
} |
254
|
|
|
|
255
|
1 |
|
$this->default_company_branch = $this->defaultCompany->branch->getId(); |
256
|
1 |
|
$this->update(); |
257
|
|
|
|
258
|
|
|
//update default subscription free trial |
259
|
1 |
|
$company->app->subscriptions_id = $this->startFreeTrial()->getId(); |
260
|
1 |
|
$company->update(); |
261
|
|
|
} else { |
262
|
|
|
//we have the company id |
263
|
2 |
|
if (empty($this->default_company_branch)) { |
264
|
|
|
$this->default_company_branch = $this->defaultCompany->branch->getId(); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
//Create new company associated company |
269
|
3 |
|
$newUserAssocCompany = new UsersAssociatedCompany(); |
270
|
3 |
|
$newUserAssocCompany->users_id = $this->id; |
271
|
3 |
|
$newUserAssocCompany->company_id = $this->default_company; |
272
|
3 |
|
$newUserAssocCompany->identify_id = 1; |
273
|
3 |
|
$newUserAssocCompany->user_active = 1; |
274
|
3 |
|
$newUserAssocCompany->user_role = $this->roles_id == 1 ? 'admins' : 'users'; |
275
|
|
|
|
276
|
3 |
|
if (!$newUserAssocCompany->save()) { |
277
|
|
|
throw new ServerErrorHttpException((string)current($newUserAssocCompany->getMessages())); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
//Insert record into user_roles |
281
|
3 |
|
$userRole = new UserRoles(); |
282
|
3 |
|
$userRole->users_id = $this->id; |
283
|
3 |
|
$userRole->roles_id = $this->roles_id; |
284
|
3 |
|
$userRole->apps_id = $this->di->getApp()->getId(); |
285
|
3 |
|
$userRole->company_id = $this->default_company; |
286
|
|
|
|
287
|
3 |
|
if (!$userRole->save()) { |
288
|
|
|
throw new ServerErrorHttpException((string)current($userRole->getMessages())); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
//update user activity when its not a empty user |
292
|
3 |
|
if (!$isFirstSignup) { |
293
|
2 |
|
$this->updateAppActivityLimit(); |
294
|
|
|
} |
295
|
3 |
|
} |
296
|
|
|
} |
297
|
|
|
|