bakaphp /
phalcon-api
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace Gewaer\Models; |
||
| 5 | |||
| 6 | use Gewaer\Exception\ServerErrorHttpException; |
||
| 7 | use Baka\Auth\Models\Companies as BakaCompanies; |
||
| 8 | use Phalcon\Di; |
||
| 9 | use Phalcon\Validation; |
||
| 10 | use Phalcon\Validation\Validator\PresenceOf; |
||
| 11 | use Phalcon\Validation\Validator\StringLength; |
||
| 12 | |||
| 13 | class Roles extends AbstractModel |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * |
||
| 17 | * @var integer |
||
| 18 | */ |
||
| 19 | public $id; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * |
||
| 23 | * @var integer |
||
| 24 | */ |
||
| 25 | public $name; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * |
||
| 29 | * @var integer |
||
| 30 | */ |
||
| 31 | public $description; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * |
||
| 35 | * @var integer |
||
| 36 | */ |
||
| 37 | public $scope; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * |
||
| 41 | * @var integer |
||
| 42 | */ |
||
| 43 | public $company_id; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | public $apps_id; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | public $created_at; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | public $updated_at; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * |
||
| 65 | * @var integer |
||
| 66 | */ |
||
| 67 | public $is_deleted; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Default ACL company |
||
| 71 | * |
||
| 72 | */ |
||
| 73 | const DEFAULT_ACL_COMPANY_ID = 0; |
||
| 74 | const DEFAULT_ACL_APP_ID = 0; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Initialize method for model. |
||
| 78 | */ |
||
| 79 | 18 | public function initialize() |
|
| 80 | { |
||
| 81 | 18 | $this->setSource('roles'); |
|
| 82 | |||
| 83 | 18 | $this->hasMany( |
|
| 84 | 18 | 'roles_name', |
|
| 85 | 18 | 'Gewaer\Models\Roles', |
|
| 86 | 18 | 'name', |
|
| 87 | 18 | ['alias' => 'role'] |
|
| 88 | ); |
||
| 89 | 18 | } |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Validations and business logic |
||
| 93 | */ |
||
| 94 | 2 | public function validation() |
|
| 95 | { |
||
| 96 | 2 | $validator = new Validation(); |
|
| 97 | |||
| 98 | 2 | $validator->add( |
|
| 99 | 2 | 'name', |
|
| 100 | 2 | new PresenceOf([ |
|
| 101 | 2 | 'field' => 'name', |
|
| 102 | 'required' => true, |
||
| 103 | ]) |
||
| 104 | ); |
||
| 105 | |||
| 106 | 2 | $validator->add( |
|
| 107 | 2 | 'description', |
|
| 108 | 2 | new PresenceOf([ |
|
| 109 | 2 | 'field' => 'description', |
|
| 110 | 'required' => true, |
||
| 111 | ]) |
||
| 112 | ); |
||
| 113 | |||
| 114 | 2 | $validator->add( |
|
| 115 | 2 | 'name', |
|
| 116 | 2 | new StringLength([ |
|
| 117 | 2 | 'max' => 32, |
|
| 118 | 2 | 'messageMinimum' => _('Role Name. Maxium 32 characters.'), |
|
| 119 | ]) |
||
| 120 | ); |
||
| 121 | |||
| 122 | 2 | return $this->validate($validator); |
|
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Returns table name mapped in the model. |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | 17 | public function getSource(): string |
|
| 131 | { |
||
| 132 | 17 | return 'roles'; |
|
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get the entity by its name |
||
| 137 | * |
||
| 138 | * @param string $name |
||
| 139 | * @return void |
||
| 140 | */ |
||
| 141 | public static function getByName(string $name) |
||
| 142 | { |
||
| 143 | return self::findFirst([ |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 144 | 'conditions' => 'name = ?0 AND company_id = ?1 AND apps_id = ?2 AND is_delete = 0', |
||
| 145 | 'bind' => [$name, Di::getDefault()->getUserData()->default_company, Di::getDefault()->getApp()->getId()] |
||
| 146 | ]); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get the Role by it app name |
||
| 151 | * |
||
| 152 | * @param string $role |
||
| 153 | * @return Roles |
||
| 154 | */ |
||
| 155 | 6 | public static function getByAppName(string $role, BakaCompanies $company): Roles |
|
| 156 | { |
||
| 157 | //echeck if we have a dot , taht means we are sending the specific app to use |
||
| 158 | 6 | if (strpos($role, '.') === false) { |
|
| 159 | throw new ServerErrorHttpException('ACL - We are expecting the app for this role'); |
||
| 160 | } |
||
| 161 | |||
| 162 | 6 | $appRole = explode('.', $role); |
|
| 163 | 6 | $role = $appRole[1]; |
|
| 164 | 6 | $appName = $appRole[0]; |
|
| 165 | |||
| 166 | //look for the app and set it |
||
| 167 | 6 | if (!$app = Apps::getACLApp($appName)) { |
|
| 168 | throw new ServerErrorHttpException('ACL - No app found for this role'); |
||
| 169 | } |
||
| 170 | |||
| 171 | 6 | return self::findFirst([ |
|
| 172 | 6 | 'conditions' => 'apps_id in (?0, ?1) AND company_id in (?2 , ?3)', |
|
| 173 | 6 | 'bind' => [$app->getId(), self::DEFAULT_ACL_APP_ID, $company->getId(), self::DEFAULT_ACL_COMPANY_ID] |
|
| 174 | ]); |
||
| 175 | } |
||
| 176 | } |
||
| 177 |