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