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