Passed
Push — master ( 403ced...b7cceb )
by Curtis
11:20 queued 05:48
created

LoginController::loggableUser()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 16
nop 1
dl 0
loc 32
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Events\enso\core\Login;
6
use App\Http\Controllers\Controller;
7
use App\Models\User;
8
use Illuminate\Foundation\Auth\AuthenticatesUsers;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Auth;
11
use Illuminate\Validation\ValidationException;
12
use LaravelEnso\Multitenancy\Enums\Connections;
13
use LaravelEnso\Multitenancy\Services\Tenant;
14
use App\Traits\ConnectionTrait;
15
16
class LoginController extends Controller
17
{
18
    use AuthenticatesUsers, ConnectionTrait;
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...
19
20
    protected $redirectTo = '/';
21
22
    public function __construct()
23
    {
24
        $this->middleware('guest')->except('logout');
25
26
        $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...
27
    }
28
29
    public function logout(Request $request)
30
    {
31
        $this->guard()->logout();
32
33
        $request->session()->invalidate();
34
    }
35
36
    protected function attemptLogin(Request $request)
37
    {
38
        $user = $this->loggableUser($request);
39
40
        if (! $user) {
41
            return false;
42
        }
43
44
        Auth::login($user, $request->input('remember'));
45
46
        Login::dispatch($user, $request->ip(), $request->header('User-Agent'));
47
48
        return true;
49
    }
50
51
    protected function authenticated(Request $request, $user)
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

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

51
    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...
52
    {
53
        return response()->json([
54
            'auth' => Auth::check(),
55
            'csrfToken' => csrf_token(),
56
        ]);
57
    }
58
59
    private function loggableUser(Request $request)
60
    {
61
        $user = User::whereEmail($request->input('email'))->first();
62
        $company = $user->company();
63
        $tanent = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $tanent is dead and can be removed.
Loading history...
64
        if ($company) {
65
            $tanent = true;
66
        }
67
        // set company id as default
68
        $main_company = $user->person->company();
69
        if($main_company !== null && !($user->isAdmin())) {
70
            $c_id = $main_company->id;
71
            $db = Connections::Tenant.$c_id;
72
            $this->setConnection(Connections::Tenant, $db);
73
        }
74
75
        if (! optional($user)->currentPasswordIs($request->input('password'))) {
76
            return;
77
        }
78
79
        if ($user->passwordExpired()) {
80
            throw ValidationException::withMessages([
81
                'email' => 'Password expired. Please set a new one.',
82
            ]);
83
        }
84
        if ($user->isInactive()) {
85
            throw ValidationException::withMessages([
86
                'email' => 'Account disabled. Please contact the administrator.',
87
            ]);
88
        }
89
90
        return $user;
91
    }
92
}
93