1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Canvas\Models; |
5
|
|
|
|
6
|
|
|
use Canvas\Traits\PermissionsTrait; |
7
|
|
|
use Canvas\Traits\SubscriptionPlanLimitTrait; |
8
|
|
|
use Phalcon\Cashier\Billable; |
9
|
|
|
use Carbon\Carbon; |
10
|
|
|
use Phalcon\Validation; |
11
|
|
|
use Phalcon\Validation\Validator\Email; |
12
|
|
|
use Phalcon\Validation\Validator\PresenceOf; |
13
|
|
|
use Phalcon\Validation\Validator\Regex; |
14
|
|
|
use Phalcon\Validation\Validator\Uniqueness; |
15
|
|
|
use Canvas\Traits\FileSystemModelTrait; |
16
|
|
|
use Phalcon\Security\Random; |
17
|
|
|
use Baka\Database\Contracts\HashTableTrait; |
18
|
|
|
use Canvas\Contracts\Notifications\NotifiableTrait; |
19
|
|
|
use Canvas\Traits\EventManagerAwareTrait; |
20
|
|
|
use Phalcon\Di; |
21
|
|
|
use Canvas\Auth\App as AppAuth; |
22
|
|
|
use Exception; |
23
|
|
|
use Canvas\Validations\PasswordValidation; |
24
|
|
|
use Baka\Auth\Models\Users as BakUser; |
25
|
|
|
use Canvas\Hashing\Password; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Users. |
29
|
|
|
* |
30
|
|
|
* @package Canvas\Models |
31
|
|
|
* |
32
|
|
|
* @property Users $user |
33
|
|
|
* @property Config $config |
34
|
|
|
* @property Apps $app |
35
|
|
|
* @property Companies $defaultCompany |
36
|
|
|
* @property \Phalcon\Di $di |
37
|
|
|
*/ |
38
|
|
|
class Users extends \Baka\Auth\Models\Users |
39
|
|
|
{ |
40
|
|
|
use PermissionsTrait; |
41
|
|
|
use Billable; |
42
|
|
|
use SubscriptionPlanLimitTrait; |
43
|
|
|
use FileSystemModelTrait; |
44
|
|
|
use HashTableTrait; |
45
|
|
|
use NotifiableTrait; |
46
|
|
|
use EventManagerAwareTrait; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Default Company Branch. |
50
|
|
|
* |
51
|
|
|
* @var integer |
52
|
|
|
*/ |
53
|
|
|
public $default_company_branch; |
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Roles id. |
57
|
|
|
* |
58
|
|
|
* @var integer |
59
|
|
|
*/ |
60
|
|
|
public $roles_id; |
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Stripe id. |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
public $stripe_id; |
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Card last four numbers. |
71
|
|
|
* |
72
|
|
|
* @var integer |
73
|
|
|
*/ |
74
|
|
|
public $card_last_four; |
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Card Brand. |
78
|
|
|
* |
79
|
|
|
* @var integer |
80
|
|
|
*/ |
81
|
|
|
public $card_brand; |
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Trial end date. |
85
|
|
|
* |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
public $trial_ends_at; |
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Provide the app plan id |
92
|
|
|
* if the user is signing up a new company. |
93
|
|
|
* |
94
|
|
|
* @var integer |
95
|
|
|
*/ |
96
|
|
|
public $appPlanId = null; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Active subscription id.Not an actual table field, used temporarily. |
100
|
|
|
* @var string |
101
|
|
|
*/ |
102
|
|
|
public $active_subscription_id; |
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* System Module Id. |
106
|
|
|
* @var integer |
107
|
|
|
*/ |
108
|
|
|
public $system_modules_id = 2; |
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* User email activation code. |
112
|
|
|
* |
113
|
|
|
* @var string |
114
|
|
|
*/ |
115
|
|
|
public $user_activation_email; |
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Initialize method for model. |
119
|
|
|
*/ |
120
|
|
|
public function initialize() |
121
|
|
|
{ |
122
|
|
|
$this->setSource('users'); |
123
|
|
|
|
124
|
|
|
//overwrite parent relationships |
125
|
|
|
$this->hasOne('id', 'Baka\Auth\Models\Sessions', 'users_id', ['alias' => 'session']); |
126
|
|
|
$this->hasMany('id', 'Baka\Auth\Models\Sessions', 'users_id', ['alias' => 'sessions']); |
127
|
|
|
$this->hasMany('id', 'Baka\Auth\Models\SessionKeys', 'users_id', ['alias' => 'sessionKeys']); |
128
|
|
|
$this->hasMany('id', 'Baka\Auth\Models\Banlist', 'users_id', ['alias' => 'bans']); |
129
|
|
|
$this->hasMany('id', 'Baka\Auth\Models\Sessions', 'users_id', ['alias' => 'sessions']); |
130
|
|
|
$this->hasMany('id', 'Canvas\Models\UserConfig', 'users_id', ['alias' => 'config']); |
131
|
|
|
$this->hasMany('id', 'Canvas\Models\UserLinkedSources', 'users_id', ['alias' => 'sources']); |
132
|
|
|
|
133
|
|
|
$this->hasOne( |
134
|
|
|
'default_company', |
135
|
|
|
'Canvas\Models\Companies', |
136
|
|
|
'id', |
137
|
|
|
['alias' => 'defaultCompany'] |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$this->hasOne( |
141
|
|
|
'default_company', |
142
|
|
|
'Canvas\Models\Companies', |
143
|
|
|
'id', |
144
|
|
|
['alias' => 'currentCompany'] |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
$this->hasMany( |
148
|
|
|
'id', |
149
|
|
|
'Canvas\Models\Subscription', |
150
|
|
|
'user_id', |
151
|
|
|
[ |
152
|
|
|
'alias' => 'allSubscriptions', |
153
|
|
|
'params' => [ |
154
|
|
|
'conditions' => 'apps_id = ?0', |
155
|
|
|
'bind' => [$this->di->getApp()->getId()], |
156
|
|
|
'order' => 'id DESC' |
157
|
|
|
] |
158
|
|
|
] |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
$this->hasMany( |
162
|
|
|
'id', |
163
|
|
|
'Canvas\Models\UsersAssociatedApps', |
164
|
|
|
'users_id', |
165
|
|
|
[ |
166
|
|
|
'alias' => 'companies', |
167
|
|
|
'params' => [ |
168
|
|
|
'conditions' => 'apps_id = ?0', |
169
|
|
|
'bind' => [$this->di->getApp()->getId()], |
170
|
|
|
] |
171
|
|
|
] |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
$this->hasMany( |
175
|
|
|
'id', |
176
|
|
|
'Canvas\Models\UsersAssociatedApps', |
177
|
|
|
'users_id', |
178
|
|
|
[ |
179
|
|
|
'alias' => 'apps', |
180
|
|
|
] |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
$this->hasOne( |
184
|
|
|
'id', |
185
|
|
|
'Canvas\Models\UsersAssociatedApps', |
186
|
|
|
'users_id', |
187
|
|
|
[ |
188
|
|
|
'alias' => 'app', |
189
|
|
|
'params' => [ |
190
|
|
|
'conditions' => 'apps_id = ?0', |
191
|
|
|
'bind' => [Di::getDefault()->getApp()->getId()] |
192
|
|
|
] |
193
|
|
|
] |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
$this->hasMany( |
197
|
|
|
'id', |
198
|
|
|
'Canvas\Models\UserWebhooks', |
199
|
|
|
'users_id', |
200
|
|
|
['alias' => 'userWebhook'] |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
$systemModule = SystemModules::getSystemModuleByModelName(self::class); |
|
|
|
|
204
|
|
|
$this->hasOne( |
205
|
|
|
'id', |
206
|
|
|
'Canvas\Models\FileSystemEntities', |
207
|
|
|
'entity_id', |
208
|
|
|
[ |
209
|
|
|
'alias' => 'files', |
210
|
|
|
'params' => [ |
211
|
|
|
'conditions' => 'system_modules_id = ?0', |
212
|
|
|
'bind' => [$systemModule->getId()] |
213
|
|
|
] |
214
|
|
|
] |
215
|
|
|
); |
216
|
|
|
|
217
|
|
|
$this->hasOne( |
218
|
|
|
'id', |
219
|
|
|
'Canvas\Models\FileSystemEntities', |
220
|
|
|
'entity_id', |
221
|
|
|
[ |
222
|
|
|
'alias' => 'photo', |
223
|
|
|
'params' => [ |
224
|
|
|
'conditions' => 'system_modules_id = ?0', |
225
|
|
|
'bind' => [$systemModule->getId()] |
226
|
|
|
] |
227
|
|
|
] |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Initialize relationshit after fetch |
233
|
|
|
* since we need company id info. |
234
|
|
|
* |
235
|
|
|
* @return void |
236
|
|
|
*/ |
237
|
|
|
public function afterFetch() |
238
|
|
|
{ |
239
|
|
|
$this->hasManyToMany( |
240
|
|
|
'id', |
241
|
|
|
'Canvas\Models\UserRoles', |
242
|
|
|
'users_id', |
243
|
|
|
'roles_id', |
244
|
|
|
'Canvas\Models\Roles', |
245
|
|
|
'id', |
246
|
|
|
[ |
247
|
|
|
'alias' => 'roles', |
248
|
|
|
'params' => [ |
249
|
|
|
'limit' => 1, |
250
|
|
|
'conditions' => 'Canvas\Models\UserRoles.apps_id = ' . $this->di->getApp()->getId() . ' AND Canvas\Models\UserRoles.companies_id = ' . $this->currentCompanyId(), |
251
|
|
|
'order' => 'Canvas\Models\UserRoles.apps_id desc', |
252
|
|
|
] |
253
|
|
|
] |
254
|
|
|
); |
255
|
|
|
|
256
|
|
|
$this->hasOne( |
257
|
|
|
'id', |
258
|
|
|
'Canvas\Models\UserRoles', |
259
|
|
|
'users_id', |
260
|
|
|
[ |
261
|
|
|
'alias' => 'userRole', |
262
|
|
|
'params' => [ |
263
|
|
|
'limit' => 1, |
264
|
|
|
'conditions' => 'Canvas\Models\UserRoles.apps_id in (?0, ?1) AND Canvas\Models\UserRoles.companies_id = ' . $this->currentCompanyId(), |
265
|
|
|
'bind' => [$this->di->getApp()->getId(), Roles::DEFAULT_ACL_APP_ID], |
266
|
|
|
'order' => 'apps_id desc', |
267
|
|
|
] |
268
|
|
|
] |
269
|
|
|
); |
270
|
|
|
|
271
|
|
|
$this->hasMany( |
272
|
|
|
'id', |
273
|
|
|
'Canvas\Models\UserRoles', |
274
|
|
|
'users_id', |
275
|
|
|
[ |
276
|
|
|
'alias' => 'permissions', |
277
|
|
|
'params' => [ |
278
|
|
|
'conditions' => 'Canvas\Models\UserRoles.apps_id = ' . $this->di->getApp()->getId() . ' AND Canvas\Models\UserRoles.companies_id = ' . $this->currentCompanyId(), |
279
|
|
|
] |
280
|
|
|
] |
281
|
|
|
); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Validations and business logic. |
287
|
|
|
*/ |
288
|
|
|
public function validation() |
|
|
|
|
289
|
|
|
{ |
290
|
|
|
$validator = new Validation(); |
291
|
|
|
$validator->add( |
292
|
|
|
'email', |
293
|
|
|
new Email([ |
294
|
|
|
'field' => 'email', |
295
|
|
|
'required' => true, |
296
|
|
|
]) |
297
|
|
|
); |
298
|
|
|
|
299
|
|
|
$validator->add( |
300
|
|
|
'displayname', |
301
|
|
|
new PresenceOf([ |
302
|
|
|
'field' => 'displayname', |
303
|
|
|
'required' => true, |
304
|
|
|
]) |
305
|
|
|
); |
306
|
|
|
|
307
|
|
|
// Unique values |
308
|
|
|
$validator->add( |
309
|
|
|
'email', |
310
|
|
|
new Uniqueness([ |
311
|
|
|
'field' => 'email', |
312
|
|
|
'message' => _('This email already has an account.'), |
313
|
|
|
]) |
314
|
|
|
); |
315
|
|
|
|
316
|
|
|
return $this->validate($validator); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Returns table name mapped in the model. |
321
|
|
|
* |
322
|
|
|
* @return string |
323
|
|
|
*/ |
324
|
|
|
public function getSource() : string |
325
|
|
|
{ |
326
|
|
|
return 'users'; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Set hashtable settings table, userConfig ;). |
331
|
|
|
* |
332
|
|
|
* @return void |
333
|
|
|
*/ |
334
|
|
|
private function createSettingsModel(): void |
|
|
|
|
335
|
|
|
{ |
336
|
|
|
$this->settingsModel = new UserConfig(); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Get the User key for redis. |
341
|
|
|
* |
342
|
|
|
* @return string |
|
|
|
|
343
|
|
|
*/ |
344
|
|
|
public function getKey() : int |
345
|
|
|
{ |
346
|
|
|
return $this->id; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* A company owner is the first person that register this company |
351
|
|
|
* This only ocurres when signing up the first time, after that all users invites |
352
|
|
|
* come with a default_company id attached. |
353
|
|
|
* |
354
|
|
|
* @return boolean |
355
|
|
|
*/ |
356
|
|
|
public function isFirstSignup(): bool |
357
|
|
|
{ |
358
|
|
|
return empty($this->default_company); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Does the user have a role assign to him? |
363
|
|
|
* |
364
|
|
|
* @return boolean |
365
|
|
|
*/ |
366
|
|
|
public function hasRole(): bool |
367
|
|
|
{ |
368
|
|
|
return !empty($this->roles_id); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Get all of the subscriptions for the user. |
373
|
|
|
*/ |
374
|
|
|
public function subscriptions() |
|
|
|
|
375
|
|
|
{ |
376
|
|
|
$this->hasMany( |
377
|
|
|
'id', |
378
|
|
|
'Canvas\Models\Subscription', |
379
|
|
|
'user_id', |
380
|
|
|
[ |
381
|
|
|
'alias' => 'subscriptions', |
382
|
|
|
'params' => [ |
383
|
|
|
'conditions' => 'apps_id = ?0 and companies_id = ?1', |
384
|
|
|
'bind' => [$this->di->getApp()->getId(), $this->default_company], |
385
|
|
|
'order' => 'id DESC' |
386
|
|
|
] |
387
|
|
|
] |
388
|
|
|
); |
389
|
|
|
|
390
|
|
|
return $this->getRelated('subscriptions'); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Strat a free trial. |
395
|
|
|
* |
396
|
|
|
* @param Users $user |
|
|
|
|
397
|
|
|
* @return Subscription |
398
|
|
|
*/ |
399
|
|
|
public function startFreeTrial() : Subscription |
400
|
|
|
{ |
401
|
|
|
$defaultPlan = AppsPlans::getDefaultPlan(); |
402
|
|
|
$trialEndsAt = Carbon::now()->addDays($this->di->getApp()->plan->free_trial_dates); |
403
|
|
|
|
404
|
|
|
$subscription = new Subscription(); |
405
|
|
|
$subscription->user_id = $this->getId(); |
406
|
|
|
$subscription->companies_id = $this->default_company; |
407
|
|
|
$subscription->apps_id = $this->di->getApp()->getId(); |
408
|
|
|
$subscription->apps_plans_id = $this->di->getApp()->default_apps_plan_id; |
409
|
|
|
$subscription->name = $defaultPlan->name; |
410
|
|
|
$subscription->stripe_id = $defaultPlan->stripe_id; |
411
|
|
|
$subscription->stripe_plan = $defaultPlan->stripe_plan; |
412
|
|
|
$subscription->quantity = 1; |
413
|
|
|
$subscription->trial_ends_at = $trialEndsAt->toDateTimeString(); |
414
|
|
|
$subscription->trial_ends_days = $trialEndsAt->diffInDays(Carbon::now()); |
415
|
|
|
$subscription->is_freetrial = 1; |
416
|
|
|
$subscription->is_active = 1; |
417
|
|
|
$subscription->saveOrFail(); |
418
|
|
|
|
419
|
|
|
$this->trial_ends_at = $subscription->trial_ends_at; |
420
|
|
|
$this->updateOrFail(); |
421
|
|
|
|
422
|
|
|
return $subscription; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Before create. |
427
|
|
|
* |
428
|
|
|
* @return void |
429
|
|
|
*/ |
430
|
|
|
public function beforeCreate() |
431
|
|
|
{ |
432
|
|
|
parent::beforeCreate(); |
433
|
|
|
$random = new Random(); |
434
|
|
|
$this->user_activation_email = $random->uuid(); |
435
|
|
|
|
436
|
|
|
//this is only empty when creating a new user |
437
|
|
|
if (!$this->isFirstSignup()) { |
438
|
|
|
//confirm if the app reach its limit |
439
|
|
|
$this->isAtLimit(); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
//Assign admin role to the system if we dont get a specify role |
443
|
|
|
if (!$this->hasRole()) { |
444
|
|
|
$role = Roles::getByName('Admins'); |
445
|
|
|
$this->roles_id = $role->getId(); |
446
|
|
|
} |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* What the current company the users is logged in with |
451
|
|
|
* in this current session? |
452
|
|
|
* |
453
|
|
|
* @return integer |
454
|
|
|
*/ |
455
|
|
|
public function currentCompanyId(): int |
456
|
|
|
{ |
457
|
|
|
$defaultCompanyId = $this->get(Companies::cacheKey()); |
|
|
|
|
458
|
|
|
return !is_null($defaultCompanyId) ? (int) $defaultCompanyId : (int) $this->default_company; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* Overwrite the user relationship. |
463
|
|
|
* use Phalcon Registry to assure we mantian the same instance accross the request. |
464
|
|
|
*/ |
465
|
|
|
public function getDefaultCompany(): Companies |
466
|
|
|
{ |
467
|
|
|
$registry = Di::getDefault()->getRegistry(); |
468
|
|
|
$key = 'company_' . Di::getDefault()->getApp()->getId() . '_' . $this->getId(); |
469
|
|
|
if (!isset($registry[$key])) { |
470
|
|
|
$registry[$key] = Companies::findFirstOrFail($this->currentCompanyId()); |
471
|
|
|
} |
472
|
|
|
return $registry[$key]; |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* What the current company brach the users is logged in with |
477
|
|
|
* in this current session? |
478
|
|
|
* |
479
|
|
|
* @return integer |
480
|
|
|
*/ |
481
|
|
|
public function currentCompanyBranchId(): int |
482
|
|
|
{ |
483
|
|
|
return (int) $this->default_company_branch; |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* What to do after the creation of a new users |
488
|
|
|
* - Assign default role. |
489
|
|
|
* |
490
|
|
|
* @return void |
491
|
|
|
*/ |
492
|
|
|
public function afterCreate() |
493
|
|
|
{ |
494
|
|
|
//need to run it here, since we overwirte the default_company id and null this function objective |
495
|
|
|
$isFirstSignup = $this->isFirstSignup(); |
496
|
|
|
|
497
|
|
|
/** |
498
|
|
|
* if we dont find the userdata di lets create it. |
499
|
|
|
* @todo this is not ideal lets fix it later |
500
|
|
|
*/ |
501
|
|
|
if (!$this->di->has('userData')) { |
502
|
|
|
$this->di->setShared('userData', $this); |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
$this->fire('user:afterSignup', $this, $isFirstSignup); |
506
|
|
|
|
507
|
|
|
//update user activity when its not a empty user |
508
|
|
|
if (!$isFirstSignup) { |
509
|
|
|
$this->updateAppActivityLimit(); |
510
|
|
|
} |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* Upload Files. |
515
|
|
|
* |
516
|
|
|
* @todo move this to the baka class |
517
|
|
|
* |
518
|
|
|
* @return void |
519
|
|
|
*/ |
520
|
|
|
public function afterSave() |
521
|
|
|
{ |
522
|
|
|
$this->associateFileSystem(); |
523
|
|
|
//$this->updatePermissionRoles(); |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* update user role for the specific app. |
528
|
|
|
* |
529
|
|
|
* @return void |
|
|
|
|
530
|
|
|
*/ |
531
|
|
|
protected function updatePermissionRoles(): bool |
|
|
|
|
532
|
|
|
{ |
533
|
|
|
if ($permission = $this->getPermission()) { |
534
|
|
|
$permission->roles_id = $this->roles_id; |
535
|
|
|
return $permission->updateOrFail(); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
return false; |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* Overwrite the permission relationship to force the user of company id. |
543
|
|
|
* |
544
|
|
|
* @return UserRoles |
545
|
|
|
*/ |
546
|
|
|
public function getPermission() |
547
|
|
|
{ |
548
|
|
|
return $this->getUserRole(); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
/** |
552
|
|
|
* Get the list of all the associated apps this users has. |
553
|
|
|
*:w. |
554
|
|
|
* @return array |
555
|
|
|
*/ |
556
|
|
|
public function getAssociatedApps(): array |
557
|
|
|
{ |
558
|
|
|
$apps = $this->getApps(['columns' => 'apps_id', 'group' => 'apps_id']); |
559
|
|
|
|
560
|
|
|
if ($apps->count()) { |
561
|
|
|
return array_map(function ($apps) { |
562
|
|
|
return $apps['apps_id']; |
563
|
|
|
}, $apps->toArray()); |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
return [0]; |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
/** |
570
|
|
|
* Get an array of the associates companies Ids. |
571
|
|
|
* |
572
|
|
|
* @return array |
573
|
|
|
*/ |
574
|
|
|
public function getAssociatedCompanies(): array |
575
|
|
|
{ |
576
|
|
|
$companies = $this->getCompanies(['columns' => 'companies_id']); |
577
|
|
|
|
578
|
|
|
if ($companies->count()) { |
579
|
|
|
return array_map(function ($company) { |
580
|
|
|
return $company['companies_id']; |
581
|
|
|
}, $companies->toArray()); |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
return [0]; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* Get user by key. |
589
|
|
|
* @param string $userActivationEmail |
590
|
|
|
* @return Users |
591
|
|
|
*/ |
592
|
|
|
public static function getByUserActivationEmail(string $userActivationEmail): Users |
593
|
|
|
{ |
594
|
|
|
return self::findFirst([ |
595
|
|
|
'conditions' => 'user_activation_email = ?0 and user_active =?1 and is_deleted = 0', |
596
|
|
|
'bind' => [$userActivationEmail, 1], |
597
|
|
|
]); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* Overwrite the relationship. |
602
|
|
|
* |
603
|
|
|
* @return void |
|
|
|
|
604
|
|
|
*/ |
605
|
|
|
public function getPhoto() |
606
|
|
|
{ |
607
|
|
|
return $this->getFileByName('photo'); |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* Update the user current default company. |
612
|
|
|
* |
613
|
|
|
* @param integer $companyId |
|
|
|
|
614
|
|
|
* @return void |
615
|
|
|
*/ |
616
|
|
|
public function switchDefaultCompanyByBranch(int $branchId): void |
617
|
|
|
{ |
618
|
|
|
if ($branch = CompaniesBranches::findFirst($branchId)) { |
619
|
|
|
if ($branch->company) { |
620
|
|
|
if ($branch->company->userAssociatedToCompany($this)) { |
621
|
|
|
$this->default_company = $branch->company->getId(); |
622
|
|
|
$this->default_company_branch = $branch->getId(); |
623
|
|
|
//set the default company id per the specific app , we do this so we can have multip default companies per diff apps |
624
|
|
|
$this->set(Companies::cacheKey(), $this->default_company); |
625
|
|
|
} |
626
|
|
|
} |
627
|
|
|
} |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
/** |
631
|
|
|
* Update the password for a current user. |
632
|
|
|
* |
633
|
|
|
* @param string $newPassword |
634
|
|
|
* @return boolean |
635
|
|
|
*/ |
636
|
|
|
public function updatePassword(string $currentPassword, string $newPassword, string $verifyPassword) : bool |
|
|
|
|
637
|
|
|
{ |
638
|
|
|
$currentPassword = trim($currentPassword); |
|
|
|
|
639
|
|
|
$newPassword = trim($newPassword); |
|
|
|
|
640
|
|
|
$verifyPassword = trim($verifyPassword); |
|
|
|
|
641
|
|
|
|
642
|
|
|
$app = Di::getDefault()->getApp(); |
643
|
|
|
|
644
|
|
|
if (!$app->ecosystemAuth()) { |
645
|
|
|
$userAppData = $this->getApp([ |
646
|
|
|
'conditions' => 'companies_id = :id:', |
647
|
|
|
'bind' => [ |
648
|
|
|
'id' => $this->currentCompanyId() |
649
|
|
|
] |
650
|
|
|
]); |
651
|
|
|
|
652
|
|
|
$password = $userAppData->password; |
653
|
|
|
} else { |
654
|
|
|
$password = $this->password; |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
// First off check that the current password matches the current password |
658
|
|
|
if (Password::check($currentPassword, $password)) { |
659
|
|
|
PasswordValidation::validate($newPassword, $verifyPassword); |
660
|
|
|
|
661
|
|
|
return $this->resetPassword($newPassword); |
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
throw new Exception(_(' Your current password is incorrect .')); |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
/** |
668
|
|
|
* Reset the user passwrod. |
669
|
|
|
* |
670
|
|
|
* @param string $newPassword |
671
|
|
|
* @return bool |
672
|
|
|
*/ |
673
|
|
|
public function resetPassword(string $newPassword): bool |
|
|
|
|
674
|
|
|
{ |
675
|
|
|
$app = Di::getDefault()->getApp(); |
676
|
|
|
|
677
|
|
|
if (!$app->ecosystemAuth()) { |
678
|
|
|
//update all companies password for the current user app |
679
|
|
|
AppAuth::updatePassword($this, Password::make($newPassword)); |
680
|
|
|
} else { |
681
|
|
|
$this->password = Password::make($newPassword); |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
return true; |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* user signup to the service. |
689
|
|
|
* |
690
|
|
|
* did we find the email? |
691
|
|
|
* does it have access to this app? |
692
|
|
|
* no? |
693
|
|
|
* ok lets register / associate to this app |
694
|
|
|
* yes? |
695
|
|
|
* it meas he was invites so get the fuck out? |
696
|
|
|
* |
697
|
|
|
* @return Users |
698
|
|
|
*/ |
699
|
|
|
public function signUp() : BakUser |
700
|
|
|
{ |
701
|
|
|
$app = Di::getDefault()->getApp(); |
702
|
|
|
|
703
|
|
|
if (!$app->ecosystemAuth()) { |
704
|
|
|
try { |
705
|
|
|
$user = self::getByEmail($this->email); |
706
|
|
|
|
707
|
|
|
$userAppData = $user->countApps('apps_id = ' . $this->getDI()->getDefault()->getApp()->getId()); |
708
|
|
|
|
709
|
|
|
if ($userAppData > 0) { |
710
|
|
|
throw new Exception('This email already has an account.'); |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
//assign user role for the current app |
714
|
|
|
$user->roles_id = Roles::getByName(Roles::DEFAULT)->getId(); |
715
|
|
|
|
716
|
|
|
$this->fire('user:afterSignup', $user, true); |
717
|
|
|
|
718
|
|
|
//update the passwords for the current app |
719
|
|
|
AppAuth::updatePassword($user, Password::make($this->password)); |
720
|
|
|
|
721
|
|
|
//overwrite the current user object |
722
|
|
|
$this->id = $user->getId(); |
723
|
|
|
$this->email = $user->getEmail(); |
724
|
|
|
} catch (Exception $e) { |
725
|
|
|
//if we cant find the user normal signup |
726
|
|
|
$user = parent::signUp(); |
727
|
|
|
|
728
|
|
|
//update all the password for the apps |
729
|
|
|
AppAuth::updatePassword($user, $this->password); |
730
|
|
|
} |
731
|
|
|
} else { |
732
|
|
|
$user = parent::signUp(); |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
return $user; |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
/** |
739
|
|
|
* Generate new forgot password hash. |
740
|
|
|
* |
741
|
|
|
* @return string |
742
|
|
|
*/ |
743
|
|
|
public function generateForgotHash(): string |
744
|
|
|
{ |
745
|
|
|
$this->user_activation_forgot = $this->generateActivationKey(); |
746
|
|
|
$this->updateOrFail(); |
747
|
|
|
|
748
|
|
|
return $this->user_activation_forgot; |
749
|
|
|
} |
750
|
|
|
} |
751
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.