LoginController::postIndex()   B
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 43
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.0877

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 43
ccs 16
cts 18
cp 0.8889
rs 8.4444
c 0
b 0
f 0
cc 8
nc 6
nop 1
crap 8.0877
1
<?php namespace Distilleries\Expendable\Http\Controllers\Backend;
2
3
use Distilleries\Expendable\Contracts\LayoutManagerContract;
4
use Distilleries\Expendable\Events\UserEvent;
5
use Distilleries\Expendable\Formatter\Message;
6
use Distilleries\Expendable\Helpers\UserUtils;
7
use Distilleries\Expendable\Http\Controllers\Backend\Base\BaseController;
8
use Illuminate\Http\Request;
9
use Illuminate\Contracts\Auth\Guard;
10
use Illuminate\Foundation\Auth\ResetsPasswords;
11
use FormBuilder;
12
use Illuminate\Support\Facades\Password;
13
14
15
class LoginController extends BaseController
16
{
17
18
    use ResetsPasswords;
0 ignored issues
show
introduced by
The trait Illuminate\Foundation\Auth\ResetsPasswords requires some properties which are not provided by Distilleries\Expendable\...Backend\LoginController: $email, $redirectTo
Loading history...
19
20
    protected $layout = 'expendable::admin.layout.login';
21
22
23
    /**
24
     * Create a new password controller instance.
25
     *
26
     * @param  \Illuminate\Contracts\Auth\Guard $auth
27
     * @param  \Distilleries\Expendable\Contracts\LayoutManagerContract $layoutManager
28
     */
29 28
    public function __construct(Guard $auth, LayoutManagerContract $layoutManager)
30
    {
31 28
        parent::__construct($layoutManager);
32
33 28
        $this->auth = $auth;
0 ignored issues
show
Bug Best Practice introduced by
The property auth does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
35
    }
36
37
    // ------------------------------------------------------------------------------------------------
38
    // ------------------------------------------------------------------------------------------------
39
    // ------------------------------------------------------------------------------------------------
40
41 2
    public function getLoginRedirect()
42
    {
43 2
        return redirect()->action('\\'.self::class.'@getIndex');
44
    }
45
46 2
    public function getIndex()
47
    {
48
49 2
        $form = FormBuilder::create('Distilleries\Expendable\Http\Forms\Login\SignIn', [
50 1
            'class' => 'login-form'
51 1
        ]);
52
53 2
        $content = view('expendable::admin.login.signin', [
54 2
            'form' => $form
55
        ]);
56
57 2
        $this->layoutManager->add([
58 2
            'class_layout' => 'login',
59 2
            'content'      => $content,
60
        ]);
61
62 2
        return $this->layoutManager->render();
63
    }
64
65
    // ------------------------------------------------------------------------------------------------
66
67 6
    public function postIndex(Request $request)
68
    {
69
70 6
        $form = FormBuilder::create('Distilleries\Expendable\Http\Forms\Login\SignIn');
71
72 6
        if ($form->hasError())
73
        {
74 2
            return $form->validateAndRedirectBack();
75
        }
76
77 4
        $credential     = $request->only('email', 'password');
78 4
        $userCredential = app('Distilleries\Expendable\Contracts\LockableContract')->where('email', $credential['email'])->get()->last();
79
80 4
        if (UserUtils::securityCheckLockEnabled() && !empty($userCredential) && $userCredential->isLocked())
81
        {
82
            return redirect()->back()->with(Message::WARNING, [trans('expendable::login.locked')]);
83
        }
84
85 4
        if ($this->auth->attempt($credential, config('expendable.remember_me')))
86
        {
87 2
            $user = $this->auth->user();
88 2
            new UserEvent(UserEvent::LOGIN_EVENT, $user);
89
90 2
            $menu = config('expendable.menu');
91
92
93 2
            if (method_exists($user, 'getFirstRedirect'))
94
            {
95 2
                return redirect()->to($this->auth->user()->getFirstRedirect($menu['left']));
96
            }
97
98
            return redirect()->to('/');
99
100
        } else
101
        {
102
103 2
            if (UserUtils::securityCheckLockEnabled())
104
            {
105 2
                new UserEvent(UserEvent::SECURITY_EVENT, $credential['email']);
106
            }
107
108
109 2
            return redirect()->back()->with(Message::WARNING, [trans('expendable::login.credential')]);
110
        }
111
112
    }
113
114
115
    // ------------------------------------------------------------------------------------------------
116
    // ------------------------------------------------------------------------------------------------
117
    // ------------------------------------------------------------------------------------------------
118
119 2
    public function getRemind()
120
    {
121
122 2
        $form = FormBuilder::create('Distilleries\Expendable\Http\Forms\Login\Forgotten', [
123 1
            'class' => 'login-form'
124 1
        ]);
125
126 2
        $content = view('expendable::admin.login.forgot', [
127 2
            'form' => $form
128
        ]);
129
130 2
        $this->layoutManager->add([
131 2
            'class_layout' => 'login',
132 2
            'content'      => $content,
133
        ]);
134
135 2
        return $this->layoutManager->render();
136
137
    }
138
139
    // ------------------------------------------------------------------------------------------------
140
141 6
    public function postRemind(Request $request)
142
    {
143
144 6
        $form = FormBuilder::create('Distilleries\Expendable\Http\Forms\Login\Forgotten');
145 6
        if ($form->hasError())
146
        {
147 2
            return $form->validateAndRedirectBack();
148
        }
149
150 4
        $this->validate($request, ['email' => 'required|email']);
151
        
152 4
        $response = $this->broker()->sendResetLink($request->only('email'));
153
154
        switch ($response)
155
        {
156 4
            case \Password::RESET_LINK_SENT:
157 2
                return redirect()->back()->with(Message::MESSAGE, [trans($response)]);
158
            default:
159 2
                return redirect()->back()->with(Message::WARNING, [trans($response)]);
160
        }
161
162
    }
163
164
165 4
    public function getReset($token = null)
166
    {
167 4
        if (is_null($token))
168
        {
169 2
            abort(404);
170
        }
171
172
173 2
        $form = FormBuilder::create('Distilleries\Expendable\Http\Forms\Login\Reset', [
174 1
            'class' => 'login-form'
175 1
        ], [
176 2
            'token' => $token
177
        ]);
178
179 2
        $content = view('expendable::admin.login.reset', [
180 2
            'form' => $form
181
        ]);
182
183 2
        $this->layoutManager->add([
184 2
            'class_layout' => 'login',
185 2
            'content'      => $content,
186
        ]);
187
188 2
        return $this->layoutManager->render();
189
    }
190
191
    /**
192
     * Handle a POST request to reset a user's password.
193
     *
194
     * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
195
     */
196 4
    public function postReset(Request $request)
197
    {
198
199 4
        $form = FormBuilder::create('Distilleries\Expendable\Http\Forms\Login\Forgotten');
200 4
        if ($form->hasError())
201
        {
202 2
            return $form->validateAndRedirectBack();
203
        }
204
205 2
        $response = $this->broker()->reset(
206
            $this->credentials($request), function($user, $password) {
207
            $this->resetPassword($user, $password);
208 2
        });
209
210
        // If the password was successfully reset, we will redirect the user back to
211
        // the application's home authenticated view. If there is an error we can
212
        // redirect them back to where they came from with their error message.
213 2
        return $response == Password::PASSWORD_RESET
214
            ? redirect()->to(action('\\'.get_class($this).'@getIndex'))
215 2
            : redirect()->back()->with('error', trans($response));
216
217
    }
218
219
    // ------------------------------------------------------------------------------------------------
220
221 2
    public function getLogout()
222
    {
223
224 2
        new UserEvent(UserEvent::LOGOUT_EVENT, $this->auth->user());
225
226 2
        $this->auth->logout();
227
228 2
        return redirect()->route('login');
229
    }
230
231
232
233
234
    // ------------------------------------------------------------------------------------------------
235
    // ------------------------------------------------------------------------------------------------
236
    // ------------------------------------------------------------------------------------------------
237
238
239 28
    protected function initStaticPart()
240
    {
241 28
        $this->layoutManager->initStaticPart();
242
    }
243
244
}