Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
84.62% |
66 / 78 |
|
91.67% |
11 / 12 |
CRAP | |
0.00% |
0 / 1 |
| User | |
84.62% |
66 / 78 |
|
91.67% |
11 / 12 |
45.83 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| readWorkstation | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
8 | |||
| testWorkstationAssigend | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
72 | |||
| testWorkstationAccessRights | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
5 | |||
| testWorkstationAssignedRights | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| checkRights | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| checkDepartments | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| checkDepartment | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
5 | |||
| hasRights | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| hasXApiKey | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| testWorkstationIsOveraged | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| testReadDepartmentByOrganisation | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Zmsapi\Helper; |
| 4 | |
| 5 | use BO\Slim\Render; |
| 6 | use BO\Zmsdb\Useraccount; |
| 7 | use BO\Zmsdb\Workstation; |
| 8 | use BO\Zmsapi\Helper\UserAuth; |
| 9 | use BO\Zmsentities\Collection\DepartmentList; |
| 10 | |
| 11 | /** |
| 12 | * |
| 13 | * @SuppressWarnings(CouplingBetweenObjects) |
| 14 | */ |
| 15 | class User |
| 16 | { |
| 17 | public static $workstation = null; |
| 18 | public static $workstationResolved = null; |
| 19 | |
| 20 | public static $assignedWorkstation = null; |
| 21 | |
| 22 | public static $request = null; |
| 23 | |
| 24 | public function __construct($request, $resolveReferences = 0) |
| 25 | { |
| 26 | static::$request = $request; |
| 27 | static::readWorkstation($resolveReferences); |
| 28 | } |
| 29 | |
| 30 | public static function readWorkstation($resolveReferences = 0) |
| 31 | { |
| 32 | $request = (static::$request) ? static::$request : Render::$request; |
| 33 | if (! static::$workstation) { |
| 34 | $useraccount = UserAuth::getUseraccountByAuthMethod($request); |
| 35 | if ($useraccount && $useraccount->hasId()) { |
| 36 | static::$workstation = (new Workstation())->readEntity($useraccount->id, $resolveReferences); |
| 37 | if ($resolveReferences < 1) { |
| 38 | static::$workstation->useraccount = $useraccount; |
| 39 | } |
| 40 | static::$workstationResolved = $resolveReferences; |
| 41 | } else { |
| 42 | static::$workstation = new \BO\Zmsentities\Workstation(); |
| 43 | } |
| 44 | } |
| 45 | if ($resolveReferences > static::$workstationResolved && static::$workstation->hasId()) { |
| 46 | static::$workstation = (new Workstation()) |
| 47 | ->readResolvedReferences(static::$workstation, $resolveReferences); |
| 48 | } |
| 49 | return static::$workstation; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @throws \BO\Zmsapi\Exception\Workstation\WorkstationAlreadyAssigned |
| 54 | * |
| 55 | */ |
| 56 | public static function testWorkstationAssigend(\BO\Zmsentities\Workstation $entity, $resolveReferences = 0) |
| 57 | { |
| 58 | if (! static::$assignedWorkstation && $entity->name) { |
| 59 | static::$assignedWorkstation = (new Workstation())->readWorkstationByScopeAndName( |
| 60 | $entity->scope['id'], |
| 61 | $entity->name, |
| 62 | $resolveReferences |
| 63 | ); |
| 64 | } |
| 65 | if ( |
| 66 | static::$assignedWorkstation && |
| 67 | static::$assignedWorkstation->id != $entity->id && |
| 68 | static::$assignedWorkstation->name == $entity->name && |
| 69 | static::$assignedWorkstation->scope['id'] == $entity->scope['id'] && |
| 70 | ! static::$assignedWorkstation->getUseraccount()->isOveraged(\App::$now) |
| 71 | ) { |
| 72 | throw new \BO\Zmsapi\Exception\Workstation\WorkstationAlreadyAssigned(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @throws \BO\Zmsentities\Exception\UserAccountAccessRightsFailed() |
| 78 | * |
| 79 | */ |
| 80 | public static function testWorkstationAccessRights($useraccount) |
| 81 | { |
| 82 | if ( |
| 83 | ( |
| 84 | ! static::$workstation->getUseraccount()->isSuperUser() && |
| 85 | ! static::$workstation->hasAccessToUseraccount($useraccount) |
| 86 | ) || |
| 87 | ( |
| 88 | ! static::$workstation->getUseraccount()->isSuperUser() && |
| 89 | $useraccount->isSuperUser() |
| 90 | ) |
| 91 | ) { |
| 92 | throw new \BO\Zmsentities\Exception\UserAccountAccessRightsFailed(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @throws \BO\Zmsentities\Exception\UserAccountMissingRights() |
| 98 | * \BO\Zmsentities\Exception\UserAccountMissingLogin() |
| 99 | * |
| 100 | */ |
| 101 | public static function testWorkstationAssignedRights($useraccount) |
| 102 | { |
| 103 | static::$workstation |
| 104 | ->getUseraccount() |
| 105 | ->testRights( |
| 106 | array_keys( |
| 107 | array_filter($useraccount->rights, function ($right) { |
| 108 | return (1 == $right); |
| 109 | }) |
| 110 | ) |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @return \BO\Zmsentities\Workstation |
| 116 | * |
| 117 | */ |
| 118 | public static function checkRights() |
| 119 | { |
| 120 | $workstation = static::readWorkstation(); |
| 121 | if (\App::RIGHTSCHECK_ENABLED) { |
| 122 | $workstation->getUseraccount()->testRights(func_get_args()); |
| 123 | } |
| 124 | return $workstation; |
| 125 | } |
| 126 | |
| 127 | public static function checkDepartments($departmentIds) |
| 128 | { |
| 129 | $departments = new DepartmentList(); |
| 130 | |
| 131 | foreach ($departmentIds as $departmentId) { |
| 132 | $departments->addEntity(self::checkDepartment($departmentId)); |
| 133 | } |
| 134 | |
| 135 | return $departments; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @return \BO\Zmsentities\Department |
| 140 | * |
| 141 | */ |
| 142 | public static function checkDepartment($departmentId) |
| 143 | { |
| 144 | $workstation = static::readWorkstation(2); |
| 145 | $userAccount = $workstation->getUseraccount(); |
| 146 | if (! $userAccount->hasId()) { |
| 147 | throw new \BO\Zmsentities\Exception\UseraccountMissingLogin(); |
| 148 | } |
| 149 | if ($userAccount->isSuperUser()) { |
| 150 | $department = (new \BO\Zmsdb\Department())->readEntity($departmentId); |
| 151 | } elseif ($userAccount->hasRights(['department'])) { |
| 152 | $department = self::testReadDepartmentByOrganisation($departmentId, $userAccount); |
| 153 | } else { |
| 154 | $department = $userAccount->testDepartmentById($departmentId); |
| 155 | } |
| 156 | if (! $department) { |
| 157 | throw new \BO\Zmsentities\Exception\UserAccountMissingDepartment( |
| 158 | "No access to department " . htmlspecialchars($departmentId) |
| 159 | ); |
| 160 | } |
| 161 | return $department; |
| 162 | } |
| 163 | |
| 164 | public static function hasRights() |
| 165 | { |
| 166 | $userAccount = static::readWorkstation()->getUseraccount(); |
| 167 | return $userAccount->hasId(); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get X-Api-Key from header |
| 172 | * |
| 173 | */ |
| 174 | public static function hasXApiKey($request) |
| 175 | { |
| 176 | $xApiKeyEntity = null; |
| 177 | $xApiKey = $request->getHeaderLine('x-api-key'); |
| 178 | if ($xApiKey) { |
| 179 | $xApiKeyEntity = (new \BO\Zmsdb\Apikey())->readEntity($xApiKey); |
| 180 | } |
| 181 | return ($xApiKeyEntity && $xApiKeyEntity->hasId()); |
| 182 | } |
| 183 | |
| 184 | public static function testWorkstationIsOveraged($workstation) |
| 185 | { |
| 186 | if ($workstation->hasId() && $workstation->getUseraccount()->isOveraged(\App::$now)) { |
| 187 | $exception = new \BO\Zmsapi\Exception\Useraccount\AuthKeyFound(); |
| 188 | $exception->data = $workstation; |
| 189 | throw $exception; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | protected static function testReadDepartmentByOrganisation($departmentId, $userAccount) |
| 194 | { |
| 195 | $organisation = (new \BO\Zmsdb\Organisation())->readByDepartmentId($departmentId, 1); |
| 196 | $organisation->departments = $organisation->getDepartmentList()->withAccess($userAccount); |
| 197 | $department = $organisation->departments->getEntity($departmentId); |
| 198 | return $department; |
| 199 | } |
| 200 | } |