Passed
Push — master ( 4c7ed3...b8c9dc )
by Curtis
05:38
created

LoginController::logout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
// namespace App\Http\Controllers\Auth;
4
5
// use App\Http\Controllers\Controller;
6
// use App\Providers\RouteServiceProvider;
7
// use Illuminate\Foundation\Auth\AuthenticatesUsers;
8
9
// class LoginController extends Controller
10
// {
11
//     use AuthenticatesUsers;
12
13
//     protected $redirectTo = RouteServiceProvider::HOME;
14
15
//     public function __construct()
16
//     {
17
//         $this->middleware('guest')->except('logout');
18
//     }
19
// }
20
21
22
namespace App\Http\Controllers\Auth;
23
24
use App\Http\Controllers\Controller;
25
use Illuminate\Foundation\Auth\AuthenticatesUsers;
26
use Illuminate\Http\Request;
27
use Illuminate\Support\Facades\Auth;
28
use Illuminate\Validation\ValidationException;
29
use App\Events\enso\core\Login;
30
use App\Models\User;
31
use LaravelEnso\Multitenancy\Services\Tenant;
32
use LaravelEnso\Multitenancy\Enums\Connections;
33
34
class LoginController extends Controller
35
{
36
    use AuthenticatesUsers;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Foundation\Auth\AuthenticatesUsers requires the property $decayMinutes which is not provided by App\Http\Controllers\Auth\LoginController.
Loading history...
37
38
    protected $redirectTo = '/';
39
40
    public function __construct()
41
    {
42
        $this->middleware('guest')->except('logout');
43
44
        $this->maxAttempts = config('enso.auth.maxLoginAttempts');
0 ignored issues
show
Bug Best Practice introduced by
The property maxAttempts does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
45
    }
46
47
    public function logout(Request $request)
48
    {
49
        $this->guard()->logout();
50
51
        $request->session()->invalidate();
52
    }
53
54
    protected function attemptLogin(Request $request)
55
    {
56
        $user = $this->loggableUser($request);
57
58
        if (! $user) {
59
            return false;
60
        }
61
62
        Auth::login($user, $request->input('remember'));
63
64
        Login::dispatch($user, $request->ip(), $request->header('User-Agent'));
65
66
        return true;
67
    }
68
69
    protected function authenticated(Request $request, $user)
0 ignored issues
show
Unused Code introduced by
The parameter $user 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

69
    protected function authenticated(Request $request, /** @scrutinizer ignore-unused */ $user)

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...
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

69
    protected function authenticated(/** @scrutinizer ignore-unused */ Request $request, $user)

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...
70
    {
71
        return response()->json([
72
            'auth' => Auth::check(),
73
            'csrfToken' => csrf_token(),
74
        ]);
75
    }
76
77
    private function loggableUser(Request $request)
78
    {
79
        $user = User::whereEmail($request->input('email'))->first();
80
        $company = $user->company();
81
        $tanent = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $tanent is dead and can be removed.
Loading history...
82
        if($company) {
83
            $tanent = true;
84
        }
85
86
        $value = env('DB_DATABASE');
87
        if (optional($company)->isTenant()) {
88
            // $key = 'database.default';
89
            // $value = Connections::Tenant;
90
            // config([$key => $value]);
91
92
            // Tenant::set($company);
93
            $value = Connections::Tenant.$company->id;
94
95
        }else{
96
            // $value = '';
97
98
        }
99
        $key = 'database.connections.mysql.database';
100
        config([$key => $value]);
101
102
        \DB::purge('mysql');
103
104
        \DB::reconnect('mysql');
105
106
        \Session::put('db', $value);
107
108
        if (! optional($user)->currentPasswordIs($request->input('password'))) {
109
            return;
110
        }
111
112
        if ($user->passwordExpired()) {
113
            throw ValidationException::withMessages([
114
                'email' => 'Password expired. Please set a new one.',
115
            ]);
116
        }
117
        if ($user->isInactive()) {
118
            throw ValidationException::withMessages([
119
                'email' => 'Account disabled. Please contact the administrator.',
120
            ]);
121
        }
122
        return $user;
123
    }
124
}
125