Passed
Push — master ( 51edae...d29a9b )
by Curtis
11:52 queued 05:54
created

LoginController::loggableUser()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 45
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 45
rs 9.4888
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
namespace App\Http\Controllers\Auth;
22
23
use App\Events\enso\core\Login;
24
use App\Http\Controllers\Controller;
25
use App\Models\User;
26
use Illuminate\Foundation\Auth\AuthenticatesUsers;
27
use Illuminate\Http\Request;
28
use Illuminate\Support\Facades\Auth;
29
use Illuminate\Validation\ValidationException;
30
use LaravelEnso\Multitenancy\Enums\Connections;
31
use LaravelEnso\Multitenancy\Services\Tenant;
32
33
class LoginController extends Controller
34
{
35
    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...
36
37
    protected $redirectTo = '/';
38
39
    public function __construct()
40
    {
41
        $this->middleware('guest')->except('logout');
42
43
        $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...
44
    }
45
46
    public function logout(Request $request)
47
    {
48
        $this->guard()->logout();
49
50
        $request->session()->invalidate();
51
    }
52
53
    protected function attemptLogin(Request $request)
54
    {
55
        $user = $this->loggableUser($request);
56
57
        if (! $user) {
58
            return false;
59
        }
60
61
        Auth::login($user, $request->input('remember'));
62
63
        Login::dispatch($user, $request->ip(), $request->header('User-Agent'));
64
65
        return true;
66
    }
67
68
    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

68
    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

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