1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Controllers; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Auth\Database\Administrator; |
6
|
|
|
use Encore\Admin\Facades\Admin; |
7
|
|
|
use Encore\Admin\Form; |
8
|
|
|
use Encore\Admin\Layout\Content; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Routing\Controller; |
11
|
|
|
use Illuminate\Support\Facades\Auth; |
12
|
|
|
use Illuminate\Support\Facades\Lang; |
13
|
|
|
use Illuminate\Support\Facades\Redirect; |
14
|
|
|
use Illuminate\Support\Facades\Validator; |
15
|
|
|
|
16
|
|
|
class AuthController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Show the login page. |
20
|
|
|
* |
21
|
|
|
* @return \Illuminate\Contracts\View\Factory|Redirect|\Illuminate\View\View |
22
|
|
|
*/ |
23
|
|
|
public function getLogin() |
24
|
|
|
{ |
25
|
|
|
if ($this->guard()->check()) { |
26
|
|
|
return redirect($this->redirectPath()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return view('admin::login'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Handle a login request. |
34
|
|
|
* |
35
|
|
|
* @param Request $request |
36
|
|
|
* |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public function postLogin(Request $request) |
40
|
|
|
{ |
41
|
|
|
$credentials = $request->only([$this->username(), 'password']); |
42
|
|
|
|
43
|
|
|
/** @var \Illuminate\Validation\Validator $validator */ |
44
|
|
|
$validator = Validator::make($credentials, [ |
45
|
|
|
$this->username() => 'required', |
46
|
|
|
'password' => 'required', |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
if ($validator->fails()) { |
50
|
|
|
return back()->withInput()->withErrors($validator); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($this->guard()->attempt($credentials)) { |
54
|
|
|
return $this->sendLoginResponse($request); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return back()->withInput()->withErrors([ |
58
|
|
|
$this->username() => $this->getFailedLoginMessage(), |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* User logout. |
64
|
|
|
* |
65
|
|
|
* @return Redirect |
66
|
|
|
*/ |
67
|
|
|
public function getLogout(Request $request) |
68
|
|
|
{ |
69
|
|
|
$this->guard()->logout(); |
70
|
|
|
|
71
|
|
|
$request->session()->invalidate(); |
72
|
|
|
|
73
|
|
|
return redirect(config('admin.route.prefix')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* User setting page. |
78
|
|
|
* |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
|
public function getSetting() |
82
|
|
|
{ |
83
|
|
|
return Admin::content(function (Content $content) { |
84
|
|
|
$content->header(trans('admin.user_setting')); |
85
|
|
|
$form = $this->settingForm(); |
86
|
|
|
$form->tools( |
87
|
|
|
function (Form\Tools $tools) { |
88
|
|
|
$tools->disableBackButton(); |
89
|
|
|
$tools->disableListButton(); |
90
|
|
|
} |
91
|
|
|
); |
92
|
|
|
$content->body($form->edit(Admin::user()->id)); |
|
|
|
|
93
|
|
|
}); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Update user setting. |
98
|
|
|
* |
99
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
100
|
|
|
*/ |
101
|
|
|
public function putSetting() |
102
|
|
|
{ |
103
|
|
|
return $this->settingForm()->update(Admin::user()->id); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Model-form for user setting. |
108
|
|
|
* |
109
|
|
|
* @return Form |
110
|
|
|
*/ |
111
|
|
|
protected function settingForm() |
112
|
|
|
{ |
113
|
|
|
return Administrator::form(function (Form $form) { |
114
|
|
|
$form->display('username', trans('admin.username')); |
115
|
|
|
$form->text('name', trans('admin.name'))->rules('required'); |
116
|
|
|
$form->image('avatar', trans('admin.avatar')); |
117
|
|
|
$form->password('password', trans('admin.password'))->rules('confirmed|required'); |
118
|
|
|
$form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required') |
119
|
|
|
->default(function ($form) { |
120
|
|
|
return $form->model()->password; |
121
|
|
|
}); |
122
|
|
|
|
123
|
|
|
$form->setAction(admin_base_path('auth/setting')); |
124
|
|
|
|
125
|
|
|
$form->ignore(['password_confirmation']); |
126
|
|
|
|
127
|
|
View Code Duplication |
$form->saving(function (Form $form) { |
|
|
|
|
128
|
|
|
if ($form->password && $form->model()->password != $form->password) { |
|
|
|
|
129
|
|
|
$form->password = bcrypt($form->password); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
}); |
132
|
|
|
|
133
|
|
|
$form->saved(function () { |
134
|
|
|
admin_toastr(trans('admin.update_succeeded')); |
135
|
|
|
|
136
|
|
|
return redirect(admin_base_path('auth/setting')); |
137
|
|
|
}); |
138
|
|
|
}); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return string|\Symfony\Component\Translation\TranslatorInterface |
143
|
|
|
*/ |
144
|
|
|
protected function getFailedLoginMessage() |
145
|
|
|
{ |
146
|
|
|
return Lang::has('auth.failed') |
147
|
|
|
? trans('auth.failed') |
148
|
|
|
: 'These credentials do not match our records.'; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the post login redirect path. |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
protected function redirectPath() |
157
|
|
|
{ |
158
|
|
|
if (method_exists($this, 'redirectTo')) { |
159
|
|
|
return $this->redirectTo(); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return property_exists($this, 'redirectTo') ? $this->redirectTo : config('admin.route.prefix'); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Send the response after the user was authenticated. |
167
|
|
|
* |
168
|
|
|
* @param \Illuminate\Http\Request $request |
169
|
|
|
* |
170
|
|
|
* @return \Illuminate\Http\Response |
171
|
|
|
*/ |
172
|
|
|
protected function sendLoginResponse(Request $request) |
173
|
|
|
{ |
174
|
|
|
admin_toastr(trans('admin.login_successful')); |
175
|
|
|
|
176
|
|
|
$request->session()->regenerate(); |
|
|
|
|
177
|
|
|
|
178
|
|
|
return redirect()->intended($this->redirectPath()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get the login username to be used by the controller. |
183
|
|
|
* |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
protected function username() |
187
|
|
|
{ |
188
|
|
|
return 'username'; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get the guard to be used during authentication. |
193
|
|
|
* |
194
|
|
|
* @return \Illuminate\Contracts\Auth\StatefulGuard |
195
|
|
|
*/ |
196
|
|
|
protected function guard() |
197
|
|
|
{ |
198
|
|
|
return Auth::guard('admin'); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: