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
|
|
|
use Phalcon\Validation; |
13
|
|
|
use Phalcon\Validation\Validator\Email; |
14
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
15
|
|
|
use Phalcon\Validation\Validator\Regex; |
16
|
|
|
use Phalcon\Validation\Validator\Uniqueness; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Users |
20
|
|
|
* |
21
|
|
|
* @package Gewaer\Models |
22
|
|
|
* |
23
|
|
|
* @property Users $user |
24
|
|
|
* @property Config $config |
25
|
|
|
* @property Apps $app |
26
|
|
|
* @property Companies $defaultCompany |
27
|
|
|
* @property \Phalcon\Di $di |
28
|
|
|
*/ |
29
|
|
|
class Users extends \Baka\Auth\Models\Users |
30
|
|
|
{ |
31
|
|
|
use PermissionsTrait; |
32
|
|
|
use Billable; |
33
|
|
|
use SubscriptionPlanLimitTrait; |
34
|
|
|
|
35
|
|
|
public $default_company_branch; |
36
|
|
|
public $roles_id; |
37
|
|
|
public $stripe_id; |
38
|
|
|
public $card_last_four; |
39
|
|
|
public $card_brand; |
40
|
|
|
public $trial_ends_at; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Provide the app plan id |
44
|
|
|
* if the user is signing up a new company |
45
|
|
|
* |
46
|
|
|
* @var integer |
47
|
|
|
*/ |
48
|
|
|
public $appPlanId = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Initialize method for model. |
52
|
|
|
*/ |
53
|
59 |
|
public function initialize() |
54
|
|
|
{ |
55
|
59 |
|
$this->setSource('users'); |
56
|
|
|
|
57
|
|
|
//overwrite parent relationships |
58
|
59 |
|
$this->hasOne('id', 'Baka\Auth\Models\Sessions', 'users_id', ['alias' => 'session']); |
59
|
59 |
|
$this->hasMany('id', 'Baka\Auth\Models\Sessions', 'users_id', ['alias' => 'sessions']); |
60
|
59 |
|
$this->hasMany('id', 'Baka\Auth\Models\SessionKeys', 'users_id', ['alias' => 'sessionKeys']); |
61
|
59 |
|
$this->hasMany('id', 'Baka\Auth\Models\Banlist', 'users_id', ['alias' => 'bans']); |
62
|
59 |
|
$this->hasMany('id', 'Baka\Auth\Models\Sessions', 'users_id', ['alias' => 'sessions']); |
63
|
59 |
|
$this->hasMany('id', 'Gewaer\Models\UserConfig', 'users_id', ['alias' => 'config']); |
64
|
59 |
|
$this->hasMany('id', 'Gewaer\Models\UserLinkedSources', 'users_id', ['alias' => 'sources']); |
65
|
59 |
|
$this->hasMany('id', 'Baka\Auth\Models\UsersAssociatedCompany', 'users_id', ['alias' => 'companies']); |
66
|
59 |
|
$this->hasOne('default_company', 'Gewaer\Models\Companies', 'id', ['alias' => 'defaultCompany']); |
67
|
59 |
|
$this->hasOne('default_company', 'Gewaer\Models\Companies', 'id', ['alias' => 'currentCompany']); |
68
|
|
|
|
69
|
59 |
|
$this->hasOne( |
70
|
59 |
|
'id', |
71
|
59 |
|
'Gewaer\Models\UserRoles', |
72
|
59 |
|
'users_id', |
73
|
59 |
|
['alias' => 'permission'] |
74
|
|
|
); |
75
|
|
|
|
76
|
59 |
|
$this->hasMany( |
77
|
59 |
|
'id', |
78
|
59 |
|
'Gewaer\Models\UserRoles', |
79
|
59 |
|
'users_id', |
80
|
59 |
|
['alias' => 'permissions'] |
81
|
|
|
); |
82
|
|
|
|
83
|
59 |
|
$this->hasManyToMany( |
84
|
59 |
|
'id', |
85
|
59 |
|
'Gewaer\Models\UserRoles', |
86
|
59 |
|
'users_id', |
87
|
59 |
|
'roles_id', |
88
|
59 |
|
'Gewaer\Models\Roles', |
89
|
59 |
|
'id', |
90
|
|
|
[ |
91
|
59 |
|
'alias' => 'roles', |
92
|
|
|
'params' => [ |
93
|
59 |
|
'limit' => 1, |
94
|
59 |
|
'conditions' => 'Gewaer\Models\UserRoles.apps_id = ' . $this->di->getConfig()->app->id, |
95
|
|
|
] |
96
|
|
|
] |
97
|
|
|
); |
98
|
|
|
|
99
|
59 |
|
$this->hasMany( |
100
|
59 |
|
'id', |
101
|
59 |
|
'Gewaer\Models\Subscription', |
102
|
59 |
|
'user_id', |
103
|
|
|
[ |
104
|
59 |
|
'alias' => 'allSubscriptions', |
105
|
|
|
'params' => [ |
106
|
59 |
|
'conditions' => 'apps_id = ?0', |
107
|
59 |
|
'bind' => [$this->di->getApp()->getId()], |
108
|
59 |
|
'order' => 'id DESC' |
109
|
|
|
] |
110
|
|
|
] |
111
|
|
|
); |
112
|
|
|
|
113
|
59 |
|
$this->hasMany( |
114
|
59 |
|
'id', |
115
|
59 |
|
'Gewaer\Models\UsersAssociatedCompany', |
116
|
59 |
|
'users_id', |
117
|
|
|
[ |
118
|
59 |
|
'alias' => 'companies', |
119
|
|
|
] |
120
|
|
|
); |
121
|
|
|
|
122
|
59 |
|
$this->hasMany( |
123
|
59 |
|
'id', |
124
|
59 |
|
'Gewaer\Models\UserWebhooks', |
125
|
59 |
|
'users_id', |
126
|
59 |
|
['alias' => 'userWebhook'] |
127
|
|
|
); |
128
|
|
|
|
129
|
59 |
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
130
|
59 |
|
$this->hasMany( |
131
|
59 |
|
'id', |
132
|
59 |
|
'Gewaer\Models\FileSystem', |
133
|
59 |
|
'entity_id', |
134
|
|
|
[ |
135
|
59 |
|
'alias' => 'filesystem', |
136
|
59 |
|
'conditions' => 'system_modules_id = ?0', |
137
|
59 |
|
'bind' => [$systemModule->getId()] |
138
|
|
|
] |
139
|
|
|
); |
140
|
59 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Validations and business logic |
144
|
|
|
*/ |
145
|
3 |
|
public function validation() |
146
|
|
|
{ |
147
|
3 |
|
$validator = new Validation(); |
148
|
3 |
|
$validator->add( |
149
|
3 |
|
'email', |
150
|
3 |
|
new Email([ |
151
|
3 |
|
'field' => 'email', |
152
|
|
|
'required' => true, |
153
|
|
|
]) |
154
|
|
|
); |
155
|
|
|
|
156
|
3 |
|
$validator->add( |
157
|
3 |
|
'displayname', |
158
|
3 |
|
new PresenceOf([ |
159
|
3 |
|
'field' => 'displayname', |
160
|
|
|
'required' => true, |
161
|
|
|
]) |
162
|
|
|
); |
163
|
|
|
|
164
|
3 |
|
$validator->add( |
165
|
3 |
|
'displayname', |
166
|
3 |
|
new Regex([ |
167
|
3 |
|
'field' => 'displayname', |
168
|
3 |
|
'message' => _('Please use alphanumerics only.'), |
169
|
3 |
|
'pattern' => '/^[A-Za-z0-9_-]{1,32}$/', |
170
|
|
|
]) |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
// Unique values |
174
|
3 |
|
$validator->add( |
175
|
3 |
|
'email', |
176
|
3 |
|
new Uniqueness([ |
177
|
3 |
|
'field' => 'email', |
178
|
3 |
|
'message' => _('This email already has an account.'), |
179
|
|
|
]) |
180
|
|
|
); |
181
|
|
|
|
182
|
3 |
|
return $this->validate($validator); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Returns table name mapped in the model. |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
45 |
|
public function getSource() : string |
191
|
|
|
{ |
192
|
45 |
|
return 'users'; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get the User key for redis |
197
|
|
|
* |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
public function getKey() : string |
201
|
|
|
{ |
202
|
|
|
return $this->id; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* A company owner is the first person that register this company |
207
|
|
|
* This only ocurres when signing up the first time, after that all users invites |
208
|
|
|
* come with a default_company id attached |
209
|
|
|
* |
210
|
|
|
* @return boolean |
211
|
|
|
*/ |
212
|
3 |
|
public function isFirstSignup(): bool |
213
|
|
|
{ |
214
|
3 |
|
return empty($this->default_company) ? true : false; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Does the user have a role assign to him? |
219
|
|
|
* |
220
|
|
|
* @return boolean |
221
|
|
|
*/ |
222
|
7 |
|
public function hasRole(): bool |
223
|
|
|
{ |
224
|
7 |
|
return !empty($this->roles_id) ? true : false; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get all of the subscriptions for the user. |
229
|
|
|
*/ |
230
|
3 |
|
public function subscriptions() |
231
|
|
|
{ |
232
|
3 |
|
$this->hasMany( |
233
|
3 |
|
'id', |
234
|
3 |
|
'Gewaer\Models\Subscription', |
235
|
3 |
|
'user_id', |
236
|
|
|
[ |
237
|
3 |
|
'alias' => 'subscriptions', |
238
|
|
|
'params' => [ |
239
|
3 |
|
'conditions' => 'apps_id = ?0 and companies_id = ?1', |
240
|
3 |
|
'bind' => [$this->di->getApp()->getId(), $this->default_company], |
241
|
3 |
|
'order' => 'id DESC' |
242
|
|
|
] |
243
|
|
|
] |
244
|
|
|
); |
245
|
|
|
|
246
|
3 |
|
return $this->getRelated('subscriptions'); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Strat a free trial |
251
|
|
|
* |
252
|
|
|
* @param Users $user |
253
|
|
|
* @return Subscription |
254
|
|
|
*/ |
255
|
1 |
|
public function startFreeTrial() : Subscription |
256
|
|
|
{ |
257
|
1 |
|
$defaultPlan = AppsPlans::getDefaultPlan(); |
258
|
|
|
|
259
|
1 |
|
$subscription = new Subscription(); |
260
|
1 |
|
$subscription->user_id = $this->getId(); |
261
|
1 |
|
$subscription->companies_id = $this->default_company; |
262
|
1 |
|
$subscription->apps_id = $this->di->getApp()->getId(); |
263
|
1 |
|
$subscription->apps_plans_id = $this->di->getApp()->default_apps_plan_id; |
264
|
1 |
|
$subscription->name = $defaultPlan->name; |
265
|
1 |
|
$subscription->stripe_id = $defaultPlan->stripe_id; |
266
|
1 |
|
$subscription->stripe_plan = $defaultPlan->stripe_plan; |
267
|
1 |
|
$subscription->quantity = 1; |
268
|
1 |
|
$subscription->trial_ends_at = Carbon::now()->addDays($this->di->getApp()->plan->free_trial_dates)->toDateTimeString(); |
269
|
|
|
|
270
|
1 |
|
if (!$subscription->save()) { |
271
|
|
|
throw new ServerErrorHttpException((string)current($this->getMessages())); |
272
|
|
|
} |
273
|
|
|
|
274
|
1 |
|
$this->trial_ends_at = $subscription->trial_ends_at; |
275
|
1 |
|
$this->update(); |
276
|
|
|
|
277
|
1 |
|
return $subscription; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Before create |
282
|
|
|
* |
283
|
|
|
* @return void |
284
|
|
|
*/ |
285
|
3 |
|
public function beforeCreate() |
286
|
|
|
{ |
287
|
3 |
|
parent::beforeCreate(); |
288
|
|
|
|
289
|
|
|
//this is only empty when creating a new user |
290
|
3 |
|
if (!$this->isFirstSignup()) { |
291
|
|
|
//confirm if the app reach its limit |
292
|
2 |
|
$this->isAtLimit(); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
//Assign admin role to the system if we dont get a specify role |
296
|
3 |
|
if (!$this->hasRole()) { |
297
|
1 |
|
$role = Roles::findFirstByName('Admins'); |
298
|
1 |
|
$this->roles_id = $role->getId(); |
299
|
|
|
} |
300
|
3 |
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Get an array of the associates companies Ids |
304
|
|
|
* |
305
|
|
|
* @return array |
306
|
|
|
*/ |
307
|
6 |
|
public function getAssociatedCompanies(): array |
308
|
|
|
{ |
309
|
|
|
return array_map(function ($company) { |
310
|
6 |
|
return $company['companies_id']; |
311
|
6 |
|
}, $this->getCompanies(['columns' => 'companies_id'])->toArray()); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* What the current company the users is logged in with |
316
|
|
|
* in this current session? |
317
|
|
|
* |
318
|
|
|
* @return integer |
319
|
|
|
*/ |
320
|
33 |
|
public function currentCompanyId(): int |
321
|
|
|
{ |
322
|
33 |
|
return (int) $this->default_company; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* What the current company brach the users is logged in with |
327
|
|
|
* in this current session? |
328
|
|
|
* |
329
|
|
|
* @return integer |
330
|
|
|
*/ |
331
|
1 |
|
public function currentCompanyBranchId(): int |
332
|
|
|
{ |
333
|
1 |
|
return (int) $this->default_company_branch; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* What to do after the creation of a new users |
338
|
|
|
* - Assign default role |
339
|
|
|
* |
340
|
|
|
* @return void |
341
|
|
|
*/ |
342
|
3 |
|
public function afterCreate() |
343
|
|
|
{ |
344
|
|
|
//need to run it here, since we overwirte the default_company id and null this function objective |
345
|
3 |
|
$isFirstSignup = $this->isFirstSignup(); |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* User signing up for a new app / plan |
349
|
|
|
* How do we know? well he doesnt have a default_company |
350
|
|
|
*/ |
351
|
3 |
|
if ($isFirstSignup) { |
352
|
1 |
|
$company = new Companies(); |
353
|
1 |
|
$company->name = $this->defaultCompanyName; |
354
|
1 |
|
$company->users_id = $this->getId(); |
355
|
|
|
|
356
|
1 |
|
if (!$company->save()) { |
357
|
|
|
throw new Exception(current($company->getMessages())); |
358
|
|
|
} |
359
|
|
|
|
360
|
1 |
|
$this->default_company = $company->getId(); |
361
|
|
|
|
362
|
1 |
|
if (!$this->update()) { |
363
|
|
|
throw new ServerErrorHttpException((string) current($this->getMessages())); |
364
|
|
|
} |
365
|
|
|
|
366
|
1 |
|
$this->default_company_branch = $this->defaultCompany->branch->getId(); |
367
|
1 |
|
$this->update(); |
368
|
|
|
|
369
|
|
|
//update default subscription free trial |
370
|
1 |
|
$company->app->subscriptions_id = $this->startFreeTrial()->getId(); |
371
|
1 |
|
$company->update(); |
372
|
|
|
} else { |
373
|
|
|
//we have the company id |
374
|
2 |
|
if (empty($this->default_company_branch)) { |
375
|
|
|
$this->default_company_branch = $this->defaultCompany->branch->getId(); |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
//Create new company associated company |
380
|
3 |
|
$newUserAssocCompany = new UsersAssociatedCompany(); |
381
|
3 |
|
$newUserAssocCompany->users_id = $this->id; |
382
|
3 |
|
$newUserAssocCompany->companies_id = $this->default_company; |
383
|
3 |
|
$newUserAssocCompany->identify_id = 1; |
384
|
3 |
|
$newUserAssocCompany->user_active = 1; |
385
|
3 |
|
$newUserAssocCompany->user_role = $this->roles_id == 1 ? 'admins' : 'users'; |
386
|
|
|
|
387
|
3 |
|
if (!$newUserAssocCompany->save()) { |
388
|
|
|
throw new ServerErrorHttpException((string)current($newUserAssocCompany->getMessages())); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
//Insert record into user_roles |
392
|
3 |
|
$userRole = new UserRoles(); |
393
|
3 |
|
$userRole->users_id = $this->id; |
394
|
3 |
|
$userRole->roles_id = $this->roles_id; |
395
|
3 |
|
$userRole->apps_id = $this->di->getApp()->getId(); |
396
|
3 |
|
$userRole->companies_id = $this->default_company; |
397
|
|
|
|
398
|
3 |
|
if (!$userRole->save()) { |
399
|
|
|
throw new ServerErrorHttpException((string)current($userRole->getMessages())); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
//update user activity when its not a empty user |
403
|
3 |
|
if (!$isFirstSignup) { |
404
|
2 |
|
$this->updateAppActivityLimit(); |
405
|
|
|
} |
406
|
3 |
|
} |
407
|
|
|
} |
408
|
|
|
|