bakaphp /
phalcon-api
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace Gewaer\Models; |
||
| 5 | |||
| 6 | use Gewaer\Traits\PermissionsTrait; |
||
| 7 | use Gewaer\Exception\ModelException; |
||
| 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 \Phalcon\Di $di |
||
| 20 | */ |
||
| 21 | class Users extends \Baka\Auth\Models\Users |
||
| 22 | { |
||
| 23 | use PermissionsTrait; |
||
|
1 ignored issue
–
show
introduced
by
Loading history...
|
|||
| 24 | use Billable; |
||
|
1 ignored issue
–
show
|
|||
| 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 | * What to do after the creation of a new users |
||
| 124 | * - Assign default role |
||
| 125 | * |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | 1 | public function afterCreate() |
|
| 129 | { |
||
| 130 | 1 | if (empty($this->default_company)) { |
|
| 131 | 1 | parent::afterCreate(); |
|
| 132 | } |
||
| 133 | |||
| 134 | //Assign admin role to the system if we dont get a specify role |
||
| 135 | if (empty($this->roles_id)) { |
||
| 136 | $role = Roles::findFirstByName('Admins'); |
||
| 137 | $this->roles_id = $role->getId(); |
||
| 138 | $this->update(); |
||
| 139 | |||
| 140 | $userRoles = new UserRoles(); |
||
| 141 | $userRoles->users_id = $this->getId(); |
||
| 142 | $userRoles->roles_id = $role->getId(); |
||
| 143 | $userRoles->apps_id = $this->di->getConfig()->app->id; |
||
| 144 | $userRoles->company_id = $this->default_company; |
||
| 145 | if (!$userRoles->save()) { |
||
| 146 | throw new ModelException((string) current($userRoles->getMessages())); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Function that executes after saving a new User |
||
| 153 | */ |
||
| 154 | 1 | public function afterSave() |
|
| 155 | { |
||
| 156 | //Create new company associated company |
||
| 157 | 1 | $newUserAssocCompany = new UsersAssociatedCompany(); |
|
| 158 | 1 | $newUserAssocCompany->users_id = $this->id; |
|
| 159 | 1 | $newUserAssocCompany->company_id = $this->default_company; |
|
| 160 | 1 | $newUserAssocCompany->identify_id = 1; |
|
| 161 | 1 | $newUserAssocCompany->user_active = 1; |
|
| 162 | 1 | $newUserAssocCompany->user_role = $this->roles_id == 1 ? 'admins' : 'users'; |
|
| 163 | 1 | $newUserAssocCompany->created_at = date('Y-m-d H:m:s'); |
|
| 164 | |||
| 165 | 1 | if (!$newUserAssocCompany->save()) { |
|
| 166 | throw new UnprocessableEntityHttpException((string) current($newUserAssocCompany->getMessages())); |
||
| 167 | } |
||
| 168 | //Insert record into user_roles |
||
| 169 | 1 | $userRole = new UserRoles(); |
|
| 170 | 1 | $userRole->users_id = $this->id; |
|
| 171 | 1 | $userRole->roles_id = $this->roles_id; |
|
| 172 | 1 | $userRole->apps_id = $this->di->getApp()->getId(); |
|
| 173 | 1 | $userRole->company_id = $this->default_company; |
|
| 174 | 1 | $userRole->created_at = date('Y-m-d H:m:s'); |
|
| 175 | 1 | $userRole->is_deleted = 0; |
|
| 176 | |||
| 177 | 1 | if (!$userRole->save()) { |
|
| 178 | 1 | throw new UnprocessableEntityHttpException((string) current($userRole->getMessages())); |
|
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 |