Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
72.41% |
21 / 29 |
|
50.00% |
4 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ExchangeAccessFilter | |
72.41% |
21 / 29 |
|
50.00% |
4 / 8 |
34.11 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getFilteredEntity | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 | |||
| getFilteredEntityByUseraccountPermissions | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getFilteredEntityByUseraccountSuperuser | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getFilteredEntityByScope | |
33.33% |
1 / 3 |
|
0.00% |
0 / 1 |
5.67 | |||
| getFilteredEntityByDepartment | |
33.33% |
1 / 3 |
|
0.00% |
0 / 1 |
5.67 | |||
| getFilteredEntityByOrganisation | |
33.33% |
1 / 3 |
|
0.00% |
0 / 1 |
5.67 | |||
| getOrganisationListByDepartments | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| 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 | public function __construct($exchangeEntity, $workstation) |
| 30 | { |
| 31 | static::$exchangeEntity = $exchangeEntity; |
| 32 | static::$workstation = $workstation; |
| 33 | static::$organisationList = $this->getOrganisationListByDepartments(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @return \BO\Zmsentities\Exchange |
| 38 | * |
| 39 | */ |
| 40 | public function getFilteredEntity() |
| 41 | { |
| 42 | static::$filteredEntity = clone static::$exchangeEntity; |
| 43 | foreach (static::$exchangeEntity->dictionary as $entry) { |
| 44 | if ($entry['reference'] && isset(static::$filterList[$entry['reference']])) { |
| 45 | $filterMethod = self::$filterList[$entry['reference']]; |
| 46 | foreach (static::$filteredEntity->data as $key => $data) { |
| 47 | static::$filterMethod($data[$entry['position']], $key); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return static::$filteredEntity; |
| 53 | } |
| 54 | |
| 55 | protected static function getFilteredEntityByUseraccountPermissions($permission, $filteredKey) |
| 56 | { |
| 57 | if (! static::$workstation->getUseraccount()->hasPermissions([$permission])) { |
| 58 | unset(static::$filteredEntity->data[$filteredKey]); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @SuppressWarnings(UnusedFormalParameter) |
| 64 | */ |
| 65 | protected static function getFilteredEntityByUseraccountSuperuser($unused, $filteredKey) |
| 66 | { |
| 67 | if (! static::$workstation->getUseraccount()->isSuperUser()) { |
| 68 | unset(static::$filteredEntity->data[$filteredKey]); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | protected static function getFilteredEntityByScope($entityId, $filteredKey) |
| 73 | { |
| 74 | if (static::$workstation->getUseraccount()->hasPermissions(['scope'])) { |
| 75 | if (! static::$workstation->getScopeListFromAssignedDepartments()->hasEntity($entityId)) { |
| 76 | unset(static::$filteredEntity->data[$filteredKey]); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | protected static function getFilteredEntityByDepartment($entityId, $filteredKey) |
| 82 | { |
| 83 | if (static::$workstation->getUseraccount()->hasPermissions(['department'])) { |
| 84 | if (! static::$workstation->getDepartmentList()->hasEntity($entityId)) { |
| 85 | unset(static::$filteredEntity->data[$filteredKey]); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | protected static function getFilteredEntityByOrganisation($entityId, $filteredKey) |
| 91 | { |
| 92 | if (static::$workstation->getUseraccount()->hasPermissions(['organisation'])) { |
| 93 | if (! static::$organisationList->hasEntity($entityId)) { |
| 94 | unset(static::$filteredEntity->data[$filteredKey]); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | protected static function getOrganisationListByDepartments() |
| 100 | { |
| 101 | $organisationList = new \BO\Zmsentities\Collection\OrganisationList(); |
| 102 | foreach (static::$workstation->getDepartmentList() as $department) { |
| 103 | $organisation = (new \BO\Zmsbackend\Organisation\Service\Organisation())->readByDepartmentId($department->id); |
| 104 | if ($organisation && $organisation instanceof \BO\Zmsentities\Organisation) { |
| 105 | $organisationList->addEntity($organisation); |
| 106 | } |
| 107 | } |
| 108 | return $organisationList; |
| 109 | } |
| 110 | } |