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