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; |
||
1 ignored issue
–
show
Bug
introduced
by
![]() |
|||
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 | 1 | public function beforeCreate() |
|
128 | { |
||
129 | 1 | parent::beforeCreate(); |
|
130 | |||
131 | //Assign admin role to the system if we dont get a specify role |
||
132 | 1 | if (empty($this->roles_id)) { |
|
133 | 1 | $role = Roles::findFirstByName('Admins'); |
|
134 | 1 | $this->roles_id = $role->getId(); |
|
135 | } |
||
136 | 1 | } |
|
137 | |||
138 | /** |
||
139 | * What to do after the creation of a new users |
||
140 | * - Assign default role |
||
141 | * |
||
142 | * @return void |
||
143 | */ |
||
144 | 1 | public function afterCreate() |
|
145 | { |
||
146 | 1 | if (empty($this->default_company)) { |
|
147 | 1 | parent::afterCreate(); |
|
148 | } |
||
149 | |||
150 | //Create new company associated company |
||
151 | 1 | $newUserAssocCompany = new UsersAssociatedCompany(); |
|
152 | 1 | $newUserAssocCompany->users_id = $this->id; |
|
153 | 1 | $newUserAssocCompany->company_id = $this->default_company; |
|
154 | 1 | $newUserAssocCompany->identify_id = 1; |
|
155 | 1 | $newUserAssocCompany->user_active = 1; |
|
156 | 1 | $newUserAssocCompany->user_role = $this->roles_id == 1 ? 'admins' : 'users'; |
|
157 | |||
158 | 1 | if (!$newUserAssocCompany->save()) { |
|
159 | throw new UnprocessableEntityHttpException((string) current($newUserAssocCompany->getMessages())); |
||
160 | } |
||
161 | |||
162 | //Insert record into user_roles |
||
163 | 1 | $userRole = new UserRoles(); |
|
164 | 1 | $userRole->users_id = $this->id; |
|
165 | 1 | $userRole->roles_id = $this->roles_id; |
|
166 | 1 | $userRole->apps_id = $this->di->getApp()->getId(); |
|
167 | 1 | $userRole->company_id = $this->default_company; |
|
168 | |||
169 | 1 | if (!$userRole->save()) { |
|
170 | throw new UnprocessableEntityHttpException((string) current($userRole->getMessages())); |
||
171 | } |
||
172 | 1 | } |
|
173 | } |
||
174 |