GrafanaOauth::checkOrg()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 3 Features 1
Metric Value
cc 2
eloc 15
c 5
b 3
f 1
nc 3
nop 1
dl 0
loc 21
rs 9.7666
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Support\Str;
7
8
use Illuminate\Http\Request;
9
10
class GrafanaOauth extends Controller
11
{
12
13
    /**
14
     * check the user if exist in the Grafana
15
     *
16
     * @param Request $request
17
     * @return void
18
     */
19
    public function checkOrg(Request $request)
20
    {
21
        $userLogin = $request->user()->email;
22
        $header = [
23
            'Accept' => 'application/json',
24
            'Content-Type' => 'application/json',
25
        ];
26
27
        $client = new Client([
28
            'base_uri' => env('GRAFANA_URL'),
29
            'auth' => [env('GRAFANA_USER'), env('GRAFANA_PASSWORD')],
30
            'headers' =>  $header
31
        ]);
32
        try {
33
            $response = $client->get("/api/users/lookup?loginOrEmail={$userLogin}", [])->getBody();
34
            $data = json_decode($response, true);
35
        } catch (\Throwable $th) {
36
            $this->createGrafanaUser($request);
37
            $data = $this->checkOrg($request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as $this->checkOrg($request) targeting App\Http\Controllers\GrafanaOauth::checkOrg() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
38
        }
39
        return $data;
40
    }
41
42
    /**
43
     * update user in Grafana Org 
44
     *
45
     * @param Request $request
46
     * @param [type] $data
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
47
     * @return void
48
     */
49
    public function updateUserOrg(Request $request, $data)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

49
    public function updateUserOrg(/** @scrutinizer ignore-unused */ Request $request, $data)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
    {
51
        $header = [
52
            'Accept' => 'application/json',
53
            'Content-Type' => 'application/json',
54
        ];
55
        $client = new Client([
56
            'base_uri' => env('GRAFANA_URL'),
57
            'auth' => [env('GRAFANA_USER'), env('GRAFANA_PASSWORD')],
58
            'headers' =>  $header
59
        ]);
60
        try {
61
            $client->post("/api/orgs/{$data['orgId']}/users", [
62
                'json' => [
63
                    'role' => 'Viewer',
64
                    'loginOrEmail' => $data['user']['login']
65
                ]
66
            ]);
67
        } catch (\Throwable $th) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
68
        }
69
    }
70
71
    /**
72
     * Create the user in Grafana
73
     *
74
     * @param Request $request
75
     * @return void
76
     */
77
    public function createGrafanaUser(Request $request)
78
    {
79
        try {
80
            $header = [
81
                'Accept' => 'application/json',
82
                'Content-Type' => 'application/json',
83
            ];
84
            $client = new Client([
85
                'base_uri' => env('GRAFANA_URL'),
86
                'auth' => [env('GRAFANA_USER'), env('GRAFANA_PASSWORD')],
87
                'headers' =>  $header,
88
            ]);
89
            $response = $client->request('POST', '/api/admin/users', [
90
                'json' => [
91
                    'name' => $request->user()->last_name,
92
                    'login' => $request->user()->username,
93
                    'email' => $request->user()->email ? $request->user()->email : $request->user()->username.'@sis.moe.gov.lk',
94
                    'password' =>  Str::random(8),
95
                ]
96
            ])->getBody();
97
            $data = json_decode($response, true);
98
            return $data;
99
        } catch (\Throwable $th) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
100
        }
101
    }
102
103
    /**
104
     * Remove user form main Org
105
     *
106
     * @param [type] $data
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
107
     * @return void
108
     */
109
    public function removeUserMainOrg($data)
110
    {
111
        try {
112
            $header = [
113
                'Accept' => 'application/json',
114
                'Content-Type' => 'application/json',
115
            ];
116
            $client = new Client([
117
                'base_uri' => env('GRAFANA_URL'),
118
                'auth' => [env('GRAFANA_USER'), env('GRAFANA_PASSWORD')],
119
                'headers' =>  $header,
120
            ]);
121
            $client->delete("/api/orgs/1/users/{$data['user']['id']}");
122
        } catch (\Throwable $th) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
123
        }
124
    }
125
126
    /**
127
     *  get the user org for according to the Role in SIS
128
     *
129
     * @param Request $request
130
     * @return void
131
     */
132
    public function getUserOrg(Request $request)
133
    {
134
        if ($request->user()->super_admin) {
135
            $data = [
136
                [
137
                    "orgId" => 2,
138
                    "userId" => $request->user()->id,
139
                    "role" => 'Admin',
140
                    "name" => 'Schools',
141
                    "email" => $request->user()->username,
142
                    "login" => 'Schools',
143
                ],
144
                [
145
                    "orgId" => 4,
146
                    "userId" => $request->user()->id,
147
                    "role" => 'Admin',
148
                    "name" => 'Zones',
149
                    "email" => $request->user()->username,
150
                    "login" => 'Zones',
151
                ],
152
                [
153
                    "orgId" => 5,
154
                    "userId" => $request->user()->id,
155
                    "role" => 'Admin',
156
                    "name" => 'Provinces',
157
                    "email" => $request->user()->username,
158
                    "login" => 'Provinces',
159
                ]
160
            ];
161
        } elseif ($request->user() &&  (!($request->user()->principal->isEmpty()))  && !is_null($request->user()->principal) && ($request->user()->principal[0]->roles->code == 'PRINCIPAL')) {
162
            $data = $this->checkOrg($request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as $this->checkOrg($request) targeting App\Http\Controllers\GrafanaOauth::checkOrg() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
163
            if (empty($data) || ((!empty($data['orgId']) && $data['orgId'] !== 2))) {
164
                $request['data'] = $data;
165
                $data['user'] = $data;
166
                $data['orgId'] = 2;
167
                $this->updateUserOrg($request, $data);
168
                $this->removeUserMainOrg($data);
169
            }
170
            $data = [
171
                [
172
                    "orgId" => 2,
173
                    "userId" => $request->user()->id,
174
                    "role" => 'Viewer',
175
                    "name" => 'Schools',
176
                    "email" => $request->user()->username,
177
                    "login" => 'Schools',
178
                ]
179
            ];
180
        } elseif ($request->user()  &&  (!($request->user()->zonal_cordinator->isEmpty())) && !is_null($request->user()->zonal_cordinator) && ($request->user()->zonal_cordinator[0]->roles->code == 'ZONAL_COORDINATOR')) {
181
            $data = $this->checkOrg($request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as $this->checkOrg($request) targeting App\Http\Controllers\GrafanaOauth::checkOrg() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
182
            if (empty($data) || ((!empty($data['orgId']) && $data['orgId'] !== 4))) {
183
                $request['data'] = $data;
184
                $data['user'] = $data;
185
                $data['orgId'] = 4;
186
                $this->updateUserOrg($request, $data);
187
                $this->removeUserMainOrg($data);
188
            }
189
            $data = [
190
                [
191
                    "orgId" => 4,
192
                    "userId" => $request->user()->id,
193
                    "role" => 'Viewer',
194
                    "name" => 'Zones',
195
                    "email" => $request->user()->username,
196
                    "login" => 'Zones',
197
                ]
198
            ];
199
        } elseif ($request->user() &&  (!($request->user()->provincial_cordinator->isEmpty())) && !is_null($request->user()->provincial_cordinator) && ($request->user()->provincial_cordinator[0]->roles->code == 'PROVINCIAL_COORDINATOR')) {
200
            $data = $this->checkOrg($request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as $this->checkOrg($request) targeting App\Http\Controllers\GrafanaOauth::checkOrg() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
201
            if (empty($data)  || ((!empty($data['orgId']) && $data['orgId'] !== 5))) {
202
                $request['data'] = $data;
203
                $data['user'] = $data;
204
                $data['orgId'] = 5;
205
                $this->updateUserOrg($request, $data);
206
                $this->removeUserMainOrg($data);
207
            }
208
            $data = [
209
                [
210
                    "orgId" => 5,
211
                    "userId" => $request->user()->id,
212
                    "role" => 'Viewer',
213
                    "name" => 'Provinces',
214
                    "email" => $request->user()->username,
215
                    "login" => 'Provinces',
216
                ]
217
            ];
218
        }
219
        return $data;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.
Loading history...
220
    }
221
}
222