bakaphp /
phalcon-api
| 1 | <?php |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace Gewaer\Models; |
||
| 5 | |||
| 6 | use Gewaer\Traits\PermissionsTrait; |
||
| 7 | use Gewaer\Exception\ModelException; |
||
| 8 | |||
| 9 | class Users extends \Baka\Auth\Models\Users |
||
| 10 | {
|
||
| 11 | use PermissionsTrait; |
||
| 12 | |||
| 13 | public $roles_id; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Initialize method for model. |
||
| 17 | */ |
||
| 18 | 5 | public function initialize() |
|
| 19 | {
|
||
| 20 | 5 | parent::initialize(); |
|
| 21 | |||
| 22 | 5 | $this->setSource('users');
|
|
| 23 | |||
| 24 | 5 | $this->hasOne( |
|
| 25 | 5 | 'id', |
|
| 26 | 5 | 'Gewaer\Models\UserRoles', |
|
| 27 | 5 | 'users_id', |
|
| 28 | 5 | ['alias' => 'permission'] |
|
| 29 | ); |
||
| 30 | |||
| 31 | 5 | $this->hasMany( |
|
| 32 | 5 | 'id', |
|
| 33 | 5 | 'Gewaer\Models\UserRoles', |
|
| 34 | 5 | 'users_id', |
|
| 35 | 5 | ['alias' => 'permissions'] |
|
| 36 | ); |
||
| 37 | 5 | } |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Returns table name mapped in the model. |
||
| 41 | * |
||
| 42 | * @return string |
||
| 43 | */ |
||
| 44 | 4 | public function getSource(): string |
|
| 45 | {
|
||
| 46 | 4 | return 'users'; |
|
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get the User key for redis |
||
| 51 | * |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | public function getKey(): string |
||
| 55 | {
|
||
| 56 | return $this->id; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * What to do after the creation of a new users |
||
| 61 | * - Assign default role |
||
| 62 | * |
||
| 63 | * @return void |
||
| 64 | */ |
||
| 65 | public function afterCreate() |
||
| 66 | {
|
||
| 67 | parent::afterCreate(); |
||
| 68 | |||
| 69 | //Assign admin role to the system if we dont get a specify role |
||
| 70 | if (empty($this->roles_id)) {
|
||
| 71 | $role = Roles::findFirstByName('Admins');
|
||
| 72 | $this->roles_id = $role->getId(); |
||
| 73 | $this->update(); |
||
| 74 | |||
| 75 | $userRoles = new UserRoles(); |
||
| 76 | $userRoles->users_id = $this->getId(); |
||
| 77 | $userRoles->roles_id = $role->getId(); |
||
| 78 | $userRoles->apps_id = $this->di->getConfig()->app->id; |
||
| 79 | $userRoles->company_id = $this->default_company; |
||
| 80 | if (!$userRoles->save()) {
|
||
| 81 | throw new ModelException((string) current($userRoles->getMessages())); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 |