Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 9 |
CRAP | |
14.63% |
6 / 41 |
AbstractMicroServiceController | |
0.00% |
0 / 1 |
|
0.00% |
0 / 9 |
268.84 | |
14.63% |
6 / 41 |
__construct | |
0.00% |
0 / 1 |
3.03 | |
85.71% |
6 / 7 |
|||
GetResponseForCreatedOperation | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
checkOperationAssignment | |
0.00% |
0 / 1 |
30 | |
0.00% |
0 / 11 |
|||
assignedIndex | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 10 |
|||
getHttpCode | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 3 |
|||
externalIndex | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
superAdminIndex | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
userIndex | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
getCurrentUser | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 6 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\MicroService\Controllers; |
4 | |
5 | use App\Http\Controllers\Controller; |
6 | use App\User; |
7 | use Illuminate\Http\Request; |
8 | use Illuminate\Http\Response; |
9 | use Illuminate\Support\Facades\Log; |
10 | use Qmp\Laravel\MicroService\Client\Client; |
11 | use Qmp\Laravel\MicroService\Client\Tools\Request as ClientRequest; |
12 | use Qmp\Laravel\Acls\Client\Client as AclsClient; |
13 | use Qmp\Laravel\Acls\Exceptions\RolePermissionException; |
14 | use Qmp\Laravel\MicroService\Exceptions\OperationAssignmentException; |
15 | |
16 | |
17 | abstract class AbstractMicroServiceController extends Controller |
18 | { |
19 | protected $userId = null; |
20 | protected $authType = null; |
21 | protected $isSystemCall = false; |
22 | |
23 | protected $operationTypeId = null; |
24 | protected $superAdminRole = "super_admin"; |
25 | |
26 | protected $httpOrigin = null; |
27 | |
28 | |
29 | public function __construct(Request $request) |
30 | { |
31 | $this->userId = (int) $request->header(Client::KEYWORD_HEADER_REQUEST); |
32 | $this->authType = $request->header(Client::KEYWORD_HEADER_REQUEST_AUTH_TYPE); |
33 | $this->httpOrigin = $request->header(Client::KEYWORD_HEADER_REQUEST_ORIGIN); |
34 | if ($request->header(Client::KEYWORD_HEADER_REQUEST_SYSTEM_CALL)) { |
35 | $this->isSystemCall = true; |
36 | } |
37 | |
38 | if (strtolower($this->authType) !== 'external') { $this->authType = 'internal'; } |
39 | } |
40 | |
41 | protected function GetResponseForCreatedOperation($id) |
42 | { |
43 | return response()->json(['status' => 'ok', 'operation_id' => $id]); |
44 | } |
45 | |
46 | |
47 | /** |
48 | * Undocumented function |
49 | * |
50 | * @param [type] $operationId |
51 | * @return boolean |
52 | */ |
53 | protected function checkOperationAssignment($operationId) |
54 | { |
55 | if ($this->operationTypeId) { |
56 | try { |
57 | if(!$this->isSystemCall) { |
58 | AclsClient::role($this->userId, [$this->superAdminRole]); |
59 | } |
60 | } catch (RolePermissionException $e) { |
61 | $request = ClientRequest::createObject('service_campaigns', "campaign/get-user-operations/" . $this->operationTypeId . "/$this->userId"); |
62 | $cLientResponse = Client::systemSend('get', $request); |
63 | if (!in_array($operationId, $cLientResponse->content['datas']['ids'])) { |
64 | Log::debug("user #$this->userId try to access operation #$operationId without the right assignment !"); |
65 | throw new OperationAssignmentException('You are not assigned to this operation'); |
66 | }; |
67 | } |
68 | } else { |
69 | Log::debug('protected $operationTypeId is not setted !'); |
70 | } |
71 | } |
72 | |
73 | /** |
74 | * Undocumented function |
75 | * |
76 | * @return void |
77 | */ |
78 | protected function assignedIndex() |
79 | { |
80 | if (strtolower($this->authType) === 'external') { |
81 | return $this->externalIndex(); |
82 | } else { |
83 | try { |
84 | AclsClient::role($this->userId, [$this->superAdminRole]); |
85 | return $this->superAdminIndex(); |
86 | } catch (RolePermissionException $e) { |
87 | if ($this->operationTypeId) { |
88 | $request = ClientRequest::createObject('service_campaigns', 'campaign/get-user-operations/' . $this->operationTypeId . '/' . $this->userId); |
89 | $cLientResponse = Client::systemSend('get', $request); |
90 | return $this->userIndex($cLientResponse->content['datas']['ids']); |
91 | } |
92 | return response()->json(['status' => 'ko', 'message' => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY); |
93 | }; |
94 | } |
95 | } |
96 | |
97 | /** |
98 | * Undocumented function |
99 | * |
100 | * @param \Exception $e |
101 | * @return void |
102 | */ |
103 | protected function getHttpCode(\Exception $e): int |
104 | { |
105 | switch (get_class($e)) { |
106 | case OperationAssignmentException::class: |
107 | return Response::HTTP_UNAUTHORIZED; |
108 | break; |
109 | default: |
110 | return Response::HTTP_UNPROCESSABLE_ENTITY; |
111 | break; |
112 | } |
113 | } |
114 | |
115 | /** |
116 | * Undocumented function |
117 | * |
118 | * @return void |
119 | */ |
120 | protected function externalIndex() |
121 | { |
122 | return null; |
123 | } |
124 | |
125 | /** |
126 | * Undocumented function |
127 | * |
128 | * @return void |
129 | */ |
130 | protected function superAdminIndex() |
131 | { |
132 | return null; |
133 | } |
134 | |
135 | /** |
136 | * Undocumented function |
137 | * |
138 | * @param [type] $ids |
139 | * @return void |
140 | */ |
141 | protected function userIndex($ids) |
142 | { |
143 | return null; |
144 | } |
145 | |
146 | /** |
147 | * Undocumented function |
148 | * |
149 | * @return void |
150 | */ |
151 | protected function getCurrentUser() { |
152 | $user = User::findOrFail($this->userId); |
153 | [$firstname, $lastname] = array_map('ucfirst', explode('.', explode('@', $user->email)[0])); |
154 | |
155 | return [ |
156 | 'firstname' => $firstname, |
157 | 'lastname' => $lastname, |
158 | 'email' => $user->email, |
159 | 'user_id' => $user->id |
160 | ]; |
161 | |
162 | } |
163 | } |