Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
43.75% |
21 / 48 |
|
20.00% |
2 / 10 |
CRAP | |
0.00% |
0 / 1 |
| ExchangeAccessFilter | |
43.75% |
21 / 48 |
|
20.00% |
2 / 10 |
202.04 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| getFilteredEntity | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
6.05 | |||
| getFilteredEntityByUseraccountPermissions | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getFilteredEntityByUseraccountSuperuser | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getFilteredEntityByScope | |
50.00% |
2 / 4 |
|
0.00% |
0 / 1 |
4.12 | |||
| getFilteredEntityByDepartment | |
50.00% |
2 / 4 |
|
0.00% |
0 / 1 |
4.12 | |||
| getFilteredEntityByOrganisation | |
33.33% |
2 / 6 |
|
0.00% |
0 / 1 |
8.74 | |||
| allowedScopeIds | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| allowedDepartmentIds | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getOrganisationListByDepartments | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Zmsbackend\Helper; |
| 4 | |
| 5 | use BO\Slim\Render; |
| 6 | |
| 7 | /** |
| 8 | * |
| 9 | * @SuppressWarnings(CouplingBetweenObjects) |
| 10 | */ |
| 11 | class ExchangeAccessFilter |
| 12 | { |
| 13 | protected static $filterList = [ |
| 14 | 'scope.id' => 'getFilteredEntityByScope', |
| 15 | 'department.id' => 'getFilteredEntityByDepartment', |
| 16 | 'organisation.id' => 'getFilteredEntityByOrganisation', |
| 17 | 'useraccount.permissions' => 'getFilteredEntityByUseraccountPermissions', |
| 18 | 'useraccount.permissions.superuser' => 'getFilteredEntityByUseraccountSuperuser', |
| 19 | ]; |
| 20 | |
| 21 | protected static $exchangeEntity = null; |
| 22 | |
| 23 | protected static $filteredEntity = null; |
| 24 | |
| 25 | protected static $workstation = null; |
| 26 | |
| 27 | protected static $organisationList = null; |
| 28 | |
| 29 | /** @var array<string, true>|null */ |
| 30 | protected static $allowedScopeIds = null; |
| 31 | |
| 32 | /** @var array<string, true>|null */ |
| 33 | protected static $allowedDepartmentIds = null; |
| 34 | |
| 35 | public function __construct($exchangeEntity, $workstation) |
| 36 | { |
| 37 | static::$exchangeEntity = $exchangeEntity; |
| 38 | static::$workstation = $workstation; |
| 39 | static::$organisationList = null; |
| 40 | static::$allowedScopeIds = null; |
| 41 | static::$allowedDepartmentIds = null; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @return \BO\Zmsentities\Exchange |
| 46 | * |
| 47 | */ |
| 48 | public function getFilteredEntity() |
| 49 | { |
| 50 | static::$filteredEntity = clone static::$exchangeEntity; |
| 51 | if (static::$workstation->getUseraccount()->isSuperUser()) { |
| 52 | return static::$filteredEntity; |
| 53 | } |
| 54 | |
| 55 | foreach (static::$exchangeEntity->dictionary as $entry) { |
| 56 | if ($entry['reference'] && isset(static::$filterList[$entry['reference']])) { |
| 57 | $filterMethod = self::$filterList[$entry['reference']]; |
| 58 | foreach (static::$filteredEntity->data as $key => $data) { |
| 59 | static::$filterMethod($data[$entry['position']], $key); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return static::$filteredEntity; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @psalm-api |
| 69 | */ |
| 70 | protected static function getFilteredEntityByUseraccountPermissions($permission, $filteredKey): void |
| 71 | { |
| 72 | if (! static::$workstation->getUseraccount()->hasPermissions([$permission])) { |
| 73 | unset(static::$filteredEntity->data[$filteredKey]); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @SuppressWarnings(UnusedFormalParameter) |
| 79 | * |
| 80 | * @psalm-api |
| 81 | */ |
| 82 | protected static function getFilteredEntityByUseraccountSuperuser($unused, $filteredKey): void |
| 83 | { |
| 84 | if (! static::$workstation->getUseraccount()->isSuperUser()) { |
| 85 | unset(static::$filteredEntity->data[$filteredKey]); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @psalm-api |
| 91 | */ |
| 92 | protected static function getFilteredEntityByScope($entityId, $filteredKey): void |
| 93 | { |
| 94 | if (! static::$workstation->getUseraccount()->hasPermissions(['scope'])) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | if (! isset(static::allowedScopeIds()[(string) $entityId])) { |
| 99 | unset(static::$filteredEntity->data[$filteredKey]); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @psalm-api |
| 105 | */ |
| 106 | protected static function getFilteredEntityByDepartment($entityId, $filteredKey): void |
| 107 | { |
| 108 | if (! static::$workstation->getUseraccount()->hasPermissions(['department'])) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | if (! isset(static::allowedDepartmentIds()[(string) $entityId])) { |
| 113 | unset(static::$filteredEntity->data[$filteredKey]); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @psalm-api |
| 119 | */ |
| 120 | protected static function getFilteredEntityByOrganisation($entityId, $filteredKey): void |
| 121 | { |
| 122 | if (! static::$workstation->getUseraccount()->hasPermissions(['organisation'])) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | if (static::$organisationList === null) { |
| 127 | static::$organisationList = static::getOrganisationListByDepartments(); |
| 128 | } |
| 129 | |
| 130 | if (! static::$organisationList->hasEntity($entityId)) { |
| 131 | unset(static::$filteredEntity->data[$filteredKey]); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @return array<string, true> |
| 137 | */ |
| 138 | protected static function allowedScopeIds(): array |
| 139 | { |
| 140 | if (static::$allowedScopeIds === null) { |
| 141 | static::$allowedScopeIds = []; |
| 142 | foreach (static::$workstation->getScopeListFromAssignedDepartments() as $scope) { |
| 143 | static::$allowedScopeIds[(string) $scope->id] = true; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return static::$allowedScopeIds; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @return array<string, true> |
| 152 | */ |
| 153 | protected static function allowedDepartmentIds(): array |
| 154 | { |
| 155 | if (static::$allowedDepartmentIds === null) { |
| 156 | static::$allowedDepartmentIds = []; |
| 157 | foreach (static::$workstation->getDepartmentList() as $department) { |
| 158 | static::$allowedDepartmentIds[(string) $department->id] = true; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return static::$allowedDepartmentIds; |
| 163 | } |
| 164 | |
| 165 | protected static function getOrganisationListByDepartments(): \BO\Zmsentities\Collection\OrganisationList |
| 166 | { |
| 167 | $organisationList = new \BO\Zmsentities\Collection\OrganisationList(); |
| 168 | foreach (static::$workstation->getDepartmentList() as $department) { |
| 169 | $organisation = (new \BO\Zmsbackend\Organisation\Service\Organisation())->readByDepartmentId($department->id); |
| 170 | if ($organisation && $organisation instanceof \BO\Zmsentities\Organisation) { |
| 171 | $organisationList->addEntity($organisation); |
| 172 | } |
| 173 | } |
| 174 | return $organisationList; |
| 175 | } |
| 176 | } |