| Total Complexity | 10 |
| Total Lines | 207 |
| Duplicated Lines | 0 % |
| Coverage | 61.9% |
| Changes | 0 | ||
| 1 | <?php |
||
| 17 | class Roles extends AbstractModel |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * |
||
| 21 | * @var integer |
||
| 22 | */ |
||
| 23 | public $id; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | public $name; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | public $description; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * |
||
| 39 | * @var integer |
||
| 40 | */ |
||
| 41 | public $scope; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * |
||
| 45 | * @var integer |
||
| 46 | */ |
||
| 47 | public $companies_id; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | public $apps_id; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | public $created_at; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | public $updated_at; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * |
||
| 69 | * @var integer |
||
| 70 | */ |
||
| 71 | public $is_deleted; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Default ACL company |
||
| 75 | * |
||
| 76 | */ |
||
| 77 | const DEFAULT_ACL_COMPANY_ID = 0; |
||
| 78 | const DEFAULT_ACL_APP_ID = 0; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Initialize method for model. |
||
| 82 | */ |
||
| 83 | 20 | public function initialize() |
|
| 84 | { |
||
| 85 | 20 | $this->setSource('roles'); |
|
| 86 | |||
| 87 | 20 | $this->hasMany( |
|
| 88 | 20 | 'id', |
|
| 89 | 20 | 'Gewaer\Models\AccessList', |
|
| 90 | 20 | 'roles_id', |
|
| 91 | 20 | ['alias' => 'accesList'] |
|
| 92 | ); |
||
| 93 | 20 | } |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Validations and business logic |
||
| 97 | */ |
||
| 98 | 3 | public function validation() |
|
| 99 | { |
||
| 100 | 3 | $validator = new Validation(); |
|
| 101 | |||
| 102 | 3 | $validator->add( |
|
| 103 | 3 | 'name', |
|
| 104 | 3 | new PresenceOf([ |
|
| 105 | 3 | 'field' => 'name', |
|
| 106 | 'required' => true, |
||
| 107 | ]) |
||
| 108 | ); |
||
| 109 | |||
| 110 | 3 | $validator->add( |
|
| 111 | 3 | 'description', |
|
| 112 | 3 | new PresenceOf([ |
|
| 113 | 3 | 'field' => 'description', |
|
| 114 | 'required' => true, |
||
| 115 | ]) |
||
| 116 | ); |
||
| 117 | |||
| 118 | 3 | $validator->add( |
|
| 119 | 3 | 'name', |
|
| 120 | 3 | new StringLength([ |
|
| 121 | 3 | 'max' => 32, |
|
| 122 | 3 | 'messageMinimum' => _('Role Name. Maxium 32 characters.'), |
|
| 123 | ]) |
||
| 124 | ); |
||
| 125 | |||
| 126 | 3 | return $this->validate($validator); |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Returns table name mapped in the model. |
||
| 131 | * |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | 19 | public function getSource(): string |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get the entity by its name |
||
| 141 | * |
||
| 142 | * @param string $name |
||
| 143 | * @return void |
||
| 144 | */ |
||
| 145 | public static function getByName(string $name) |
||
| 146 | { |
||
| 147 | return self::findFirst([ |
||
|
1 ignored issue
–
show
|
|||
| 148 | 'conditions' => 'name = ?0 AND companies_id = ?1 AND apps_id = ?2 AND is_deleted = 0', |
||
| 149 | 'bind' => [$name, Di::getDefault()->getUserData()->default_company, Di::getDefault()->getApp()->getId()] |
||
| 150 | ]); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Get the entity by its name |
||
| 155 | * |
||
| 156 | * @param string $name |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | 1 | public static function getById(int $id) |
|
| 160 | { |
||
| 161 | 1 | return self::findFirst([ |
|
|
1 ignored issue
–
show
|
|||
| 162 | 1 | 'conditions' => 'id = ?0 AND companies_id in (?1, ?2) AND apps_id in (?3, ?4) AND is_deleted = 0', |
|
| 163 | 1 | 'bind' => [$id, Di::getDefault()->getUserData()->default_company, Apps::GEWAER_DEFAULT_APP_ID, Di::getDefault()->getApp()->getId(), Apps::GEWAER_DEFAULT_APP_ID] |
|
| 164 | ]); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get the Role by it app name |
||
| 169 | * |
||
| 170 | * @param string $role |
||
| 171 | * @return Roles |
||
| 172 | */ |
||
| 173 | 7 | public static function getByAppName(string $role, Companies $company): Roles |
|
| 174 | { |
||
| 175 | //echeck if we have a dot , taht means we are sending the specific app to use |
||
| 176 | 7 | if (strpos($role, '.') === false) { |
|
| 177 | throw new ServerErrorHttpException('ACL - We are expecting the app for this role'); |
||
| 178 | } |
||
| 179 | |||
| 180 | 7 | $appRole = explode('.', $role); |
|
| 181 | 7 | $role = $appRole[1]; |
|
|
1 ignored issue
–
show
|
|||
| 182 | 7 | $appName = $appRole[0]; |
|
| 183 | |||
| 184 | //look for the app and set it |
||
| 185 | 7 | if (!$app = Apps::getACLApp($appName)) { |
|
| 186 | throw new ServerErrorHttpException('ACL - No app found for this role'); |
||
| 187 | } |
||
| 188 | |||
| 189 | 7 | return self::findFirst([ |
|
|
1 ignored issue
–
show
|
|||
| 190 | 7 | 'conditions' => 'apps_id in (?0, ?1) AND companies_id in (?2 , ?3)', |
|
| 191 | 7 | 'bind' => [$app->getId(), self::DEFAULT_ACL_APP_ID, $company->getId(), self::DEFAULT_ACL_COMPANY_ID] |
|
| 192 | ]); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Duplicate a role with it access list |
||
| 197 | * |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | public function copy(): Roles |
||
| 224 | } |
||
| 225 | } |
||
| 226 |