bakaphp /
phalcon-api
| 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\UnprocessableEntityHttpException; |
||||||
| 10 | |||||||
| 11 | /** |
||||||
| 12 | * Class Users |
||||||
| 13 | * |
||||||
| 14 | * @package Gewaer\Models |
||||||
| 15 | * |
||||||
| 16 | * @property Users $user |
||||||
| 17 | * @property Config $config |
||||||
| 18 | * @property Apps $app |
||||||
| 19 | * @property Companies $defaultCompany |
||||||
| 20 | * @property \Phalcon\Di $di |
||||||
| 21 | */ |
||||||
| 22 | class Users extends \Baka\Auth\Models\Users |
||||||
| 23 | { |
||||||
| 24 | use PermissionsTrait; |
||||||
| 25 | use Billable; |
||||||
| 26 | use SubscriptionPlanLimitTrait; |
||||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||||
| 27 | |||||||
| 28 | public $default_company_branch; |
||||||
| 29 | public $roles_id; |
||||||
| 30 | public $stripe_id; |
||||||
| 31 | public $card_last_four; |
||||||
| 32 | public $card_brand; |
||||||
| 33 | public $trial_ends_at; |
||||||
| 34 | |||||||
| 35 | /** |
||||||
| 36 | * Provide the app plan id |
||||||
| 37 | * if the user is signing up a new company |
||||||
| 38 | * |
||||||
| 39 | * @var integer |
||||||
| 40 | */ |
||||||
| 41 | public $appPlanId = null; |
||||||
| 42 | |||||||
| 43 | /** |
||||||
| 44 | * Initialize method for model. |
||||||
| 45 | */ |
||||||
| 46 | 24 | public function initialize() |
|||||
| 47 | { |
||||||
| 48 | 24 | $this->setSource('users'); |
|||||
| 49 | |||||||
| 50 | //overwrite parent relationships |
||||||
| 51 | 24 | $this->hasOne('id', 'Baka\Auth\Models\Sessions', 'users_id', ['alias' => 'session']); |
|||||
| 52 | 24 | $this->hasMany('id', 'Baka\Auth\Models\Sessions', 'users_id', ['alias' => 'sessions']); |
|||||
| 53 | 24 | $this->hasMany('id', 'Baka\Auth\Models\SessionKeys', 'users_id', ['alias' => 'sessionKeys']); |
|||||
| 54 | 24 | $this->hasMany('id', 'Baka\Auth\Models\Banlist', 'users_id', ['alias' => 'bans']); |
|||||
| 55 | 24 | $this->hasMany('id', 'Baka\Auth\Models\Sessions', 'users_id', ['alias' => 'sessions']); |
|||||
| 56 | 24 | $this->hasMany('id', 'Gewaer\Models\UserConfig', 'users_id', ['alias' => 'config']); |
|||||
| 57 | 24 | $this->hasMany('id', 'Gewaer\Models\UserLinkedSources', 'users_id', ['alias' => 'sources']); |
|||||
| 58 | 24 | $this->hasMany('id', 'Baka\Auth\Models\UsersAssociatedCompany', 'users_id', ['alias' => 'companies']); |
|||||
| 59 | 24 | $this->hasOne('default_company', 'Gewaer\Models\Companies', 'id', ['alias' => 'defaultCompany']); |
|||||
| 60 | |||||||
| 61 | 24 | $this->hasOne( |
|||||
| 62 | 24 | 'id', |
|||||
| 63 | 24 | 'Gewaer\Models\UserRoles', |
|||||
| 64 | 24 | 'users_id', |
|||||
| 65 | 24 | ['alias' => 'permission'] |
|||||
| 66 | ); |
||||||
| 67 | |||||||
| 68 | 24 | $this->hasMany( |
|||||
| 69 | 24 | 'id', |
|||||
| 70 | 24 | 'Gewaer\Models\UserRoles', |
|||||
| 71 | 24 | 'users_id', |
|||||
| 72 | 24 | ['alias' => 'permissions'] |
|||||
| 73 | ); |
||||||
| 74 | |||||||
| 75 | 24 | $this->hasManyToMany( |
|||||
| 76 | 24 | 'id', |
|||||
| 77 | 24 | 'Gewaer\Models\UserRoles', |
|||||
| 78 | 24 | 'users_id', |
|||||
| 79 | 24 | 'roles_id', |
|||||
| 80 | 24 | 'Gewaer\Models\Roles', |
|||||
| 81 | 24 | 'id', |
|||||
| 82 | [ |
||||||
| 83 | 24 | 'alias' => 'roles', |
|||||
| 84 | 'params' => [ |
||||||
| 85 | 24 | 'limit' => 1, |
|||||
| 86 | 24 | 'conditions' => 'Gewaer\Models\UserRoles.apps_id = ' . $this->di->getConfig()->app->id, |
|||||
| 87 | ] |
||||||
| 88 | ] |
||||||
| 89 | ); |
||||||
| 90 | 24 | } |
|||||
| 91 | |||||||
| 92 | /** |
||||||
| 93 | * Returns table name mapped in the model. |
||||||
| 94 | * |
||||||
| 95 | * @return string |
||||||
| 96 | */ |
||||||
| 97 | 19 | public function getSource() : string |
|||||
| 98 | { |
||||||
| 99 | 19 | return 'users'; |
|||||
| 100 | } |
||||||
| 101 | |||||||
| 102 | /** |
||||||
| 103 | * Get the User key for redis |
||||||
| 104 | * |
||||||
| 105 | * @return string |
||||||
| 106 | */ |
||||||
| 107 | public function getKey() : string |
||||||
| 108 | { |
||||||
| 109 | return $this->id; |
||||||
| 110 | } |
||||||
| 111 | |||||||
| 112 | /** |
||||||
| 113 | * Get all of the subscriptions for the user. |
||||||
| 114 | */ |
||||||
| 115 | public function subscriptions() |
||||||
| 116 | { |
||||||
| 117 | $this->hasMany( |
||||||
| 118 | 'id', |
||||||
| 119 | Subscription::class, |
||||||
| 120 | 'user_id', |
||||||
| 121 | [ |
||||||
| 122 | 'alias' => 'subscriptions', |
||||||
| 123 | 'params' => [ |
||||||
| 124 | 'conditions' => 'apps_id = ?0 and company_id = ?1', |
||||||
| 125 | 'bind' => [$this->di->getApp()->getId(), $this->default_company], |
||||||
| 126 | 'order' => 'id DESC' |
||||||
| 127 | ] |
||||||
| 128 | ] |
||||||
| 129 | ); |
||||||
| 130 | return $this->getRelated('subscriptions'); |
||||||
| 131 | } |
||||||
| 132 | |||||||
| 133 | /** |
||||||
| 134 | * Before create |
||||||
| 135 | * |
||||||
| 136 | * @return void |
||||||
| 137 | */ |
||||||
| 138 | 1 | public function beforeCreate() |
|||||
| 139 | { |
||||||
| 140 | 1 | parent::beforeCreate(); |
|||||
| 141 | |||||||
| 142 | //confirm if the app reach its limit |
||||||
| 143 | |||||||
| 144 | 1 | $this->isAtLimit(); |
|||||
| 145 | |||||||
| 146 | //Assign admin role to the system if we dont get a specify role |
||||||
| 147 | 1 | if (empty($this->roles_id)) { |
|||||
| 148 | 1 | $role = Roles::findFirstByName('Admins'); |
|||||
| 149 | 1 | $this->roles_id = $role->getId(); |
|||||
| 150 | } |
||||||
| 151 | 1 | } |
|||||
| 152 | |||||||
| 153 | /** |
||||||
| 154 | * What to do after the creation of a new users |
||||||
| 155 | * - Assign default role |
||||||
| 156 | * |
||||||
| 157 | * @return void |
||||||
| 158 | */ |
||||||
| 159 | 1 | public function afterCreate() |
|||||
| 160 | { |
||||||
| 161 | 1 | if (empty($this->default_company)) { |
|||||
| 162 | //create company |
||||||
| 163 | 1 | $company = new Companies(); |
|||||
| 164 | 1 | $company->name = $this->defaultCompanyName; |
|||||
| 165 | 1 | $company->users_id = $this->getId(); |
|||||
| 166 | |||||||
| 167 | 1 | if (!$company->save()) { |
|||||
| 168 | throw new Exception(current($company->getMessages())); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 169 | } |
||||||
| 170 | |||||||
| 171 | 1 | $this->default_company = $company->getId(); |
|||||
| 172 | |||||||
| 173 | 1 | if (!$this->update()) { |
|||||
| 174 | 1 | throw new Exception(current($this->getMessages())); |
|||||
| 175 | } |
||||||
| 176 | } else { |
||||||
| 177 | //we have the company id |
||||||
| 178 | if (empty($this->default_company_branch)) { |
||||||
| 179 | $this->default_company_branch = $this->defaultCompany->branch->getId(); |
||||||
|
1 ignored issue
–
show
The method
getId() does not exist on Phalcon\Mvc\Model\Resultset.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
The property
branch does not exist on Gewaer\Models\Companies. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||||
| 180 | } |
||||||
| 181 | } |
||||||
| 182 | |||||||
| 183 | //Create new company associated company |
||||||
| 184 | 1 | $newUserAssocCompany = new UsersAssociatedCompany(); |
|||||
| 185 | 1 | $newUserAssocCompany->users_id = $this->id; |
|||||
| 186 | 1 | $newUserAssocCompany->company_id = $this->default_company; |
|||||
| 187 | 1 | $newUserAssocCompany->identify_id = 1; |
|||||
| 188 | 1 | $newUserAssocCompany->user_active = 1; |
|||||
| 189 | 1 | $newUserAssocCompany->user_role = $this->roles_id == 1 ? 'admins' : 'users'; |
|||||
| 190 | |||||||
| 191 | 1 | if (!$newUserAssocCompany->save()) { |
|||||
| 192 | throw new UnprocessableEntityHttpException((string)current($newUserAssocCompany->getMessages())); |
||||||
| 193 | } |
||||||
| 194 | |||||||
| 195 | //Insert record into user_roles |
||||||
| 196 | 1 | $userRole = new UserRoles(); |
|||||
| 197 | 1 | $userRole->users_id = $this->id; |
|||||
| 198 | 1 | $userRole->roles_id = $this->roles_id; |
|||||
| 199 | 1 | $userRole->apps_id = $this->di->getApp()->getId(); |
|||||
| 200 | 1 | $userRole->company_id = $this->default_company; |
|||||
| 201 | |||||||
| 202 | 1 | if (!$userRole->save()) { |
|||||
| 203 | throw new UnprocessableEntityHttpException((string)current($userRole->getMessages())); |
||||||
| 204 | } |
||||||
| 205 | |||||||
| 206 | //update model total activity |
||||||
| 207 | 1 | $this->updateAppActivityLimit(); |
|||||
| 208 | 1 | } |
|||||
| 209 | } |
||||||
| 210 |