Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
12 / 12 |
CRAP | |
100.00% |
1 / 1 |
| PolicyGen | |
100.00% |
17 / 17 |
|
100.00% |
12 / 12 |
17 | |
100.00% |
1 / 1 |
| getPolicyName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getUsesPolicies | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getControllerPolicy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPolicyReadArgs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPolicyReadRules | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPolicyWriteArgs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPolicyWriteRules | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPolicyRules | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
| getPolicyArgs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getParentAuthorization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getModelAuthorization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPolicyAuthorization | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace San\Crud\Generators; |
| 4 | |
| 5 | use San\Crud\Utils\NameUtils; |
| 6 | use San\Crud\Utils\SchemaUtils; |
| 7 | |
| 8 | class PolicyGen extends BaseGen { |
| 9 | |
| 10 | public function getPolicyName() { |
| 11 | return NameUtils::getPolicyName($this->mainTable()); |
| 12 | } |
| 13 | |
| 14 | public function getUsesPolicies() { |
| 15 | return sprintf('use %s;', NameUtils::getPolicyName((array) $this->tables)); |
| 16 | } |
| 17 | |
| 18 | public function getControllerPolicy() { |
| 19 | return sprintf("public function __construct() {\n\t\t\$this->authorizeResource(%s::class, '%s');\n\t}", NameUtils::getModelName((array) $this->mainTable()), NameUtils::getVariableName($this->mainTable())); |
| 20 | } |
| 21 | |
| 22 | public function getPolicyReadArgs() { |
| 23 | return $this->getPolicyArgs([]); |
| 24 | } |
| 25 | |
| 26 | public function getPolicyReadRules() { |
| 27 | return $this->getPolicyRules([]); |
| 28 | } |
| 29 | |
| 30 | public function getPolicyWriteArgs() { |
| 31 | return $this->getPolicyArgs($this->mainTable()); |
| 32 | } |
| 33 | |
| 34 | public function getPolicyWriteRules() { |
| 35 | return $this->getPolicyRules($this->mainTable()); |
| 36 | } |
| 37 | |
| 38 | public function getPolicyRules($tables) { |
| 39 | foreach ((array) $tables as $table) { |
| 40 | if (SchemaUtils::getUserIdField($table)) { |
| 41 | $checks[] = sprintf('($%s->user_id == $user->id)', NameUtils::getVariableName($table)); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return empty($checks) ? "return true;" : sprintf('return ($user->id > 0) && %s;', join(' && ', $checks)); |
| 46 | } |
| 47 | |
| 48 | public function getPolicyArgs($tables) { |
| 49 | return join(', ', array_map(fn($table) => sprintf('%s $%s', NameUtils::getModelName((array) $table), NameUtils::getVariableName($table)), array_merge(['User'], (array) $tables))); |
| 50 | } |
| 51 | |
| 52 | public function getParentAuthorization() { |
| 53 | return $this->getPolicyAuthorization($this->parentTables(), 'view'); |
| 54 | } |
| 55 | |
| 56 | public function getModelAuthorization() { |
| 57 | return $this->getPolicyAuthorization($this->mainTable(), 'delete'); |
| 58 | } |
| 59 | |
| 60 | public function getPolicyAuthorization($tables, $fn) { |
| 61 | foreach ((array) $tables as $table) { |
| 62 | $checks[] = sprintf("\$this->authorize('%s', [%s::class, \$%s]);", $fn, NameUtils::getModelName($table), NameUtils::getVariableName($table)); |
| 63 | } |
| 64 | |
| 65 | return empty($checks) ? "" : join("\n\t\t", $checks); |
| 66 | } |
| 67 | } |