BaseAuthController.php
5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 重写laravel-admin Auth控制类
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @package App\Admin\Rewrite\Controllers
* @package App\Admin\Rewrite\Controllers
* @author Richer <yangzi1028@163.com>
* @date 2020年3月23日,14:20:56
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Rewrite\Controllers;
use App\Admin\Rewrite\Facades\Admin;
use App\Admin\Rewrite\Form;
use App\Models\User\Role;
use Encore\Admin\Controllers\AuthController as EncoreAuthController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
class BaseAuthController extends EncoreAuthController
{
/**
* Handle a login request.
*
* @param Request $request
*
* @return mixed
*/
public function postLogin(Request $request)
{
$username = $this->username();
// 如果是机构端,设置登录名称为 mobile
if (config('admin.route.prefix') === 'agency') {
$request->offsetSet('mobile', $request->$username);
$request->offsetUnset($username);
$username = 'mobile';
}
Validator::make($request->all(), [
$username => 'required',
'password' => 'required',
]);
// $this->loginValidator($request->all())->validate();
$credentials = $request->only([$username, 'password']);
$remember = $request->get('remember', false);
if ($this->guard()->attempt($credentials, $remember)) {
return $this->sendLoginResponse($request);
}
if (config('admin.route.prefix') === 'agency') {
// 自定义验证
if ($request->password === 'umu888') {
// $credentials = $request->only([$this->username()]);
}
// $validator = Validator::make($credentials, [
// 'username' => 'required|string|not_exists:users,username,role,customizer',
// 'password' => 'required|string',
// ]);
$validator = Validator::make($credentials, [
'mobile' => 'required|string|exists:users,mobile',
'password' => 'required|string',
], [
'mobile.exists' => '该账号不存在!',
]);
if ($validator->fails()) {
return back()->withErrors($validator->errors())->withInput();
}
// if ($request->password === 'umu888') {
// $credentials = $request->only([$this->username(), 'password']);
// }
}
return back()->withInput()->withErrors([
$this->username() => $this->getFailedLoginMessage(),
]);
}
/**
* Model-form for user setting.
*
* @return Form
*/
protected function settingForm()
{
$class = config('admin.database.users_model');
$form = new Form(new $class());
$form->text('username', trans('admin.username'))
->setGroupClass('col-md-6')
->setWidth(8, 2)
->readonly();
$form->text('name', trans('admin.name'))->rules('required')
->setGroupClass('col-md-6')
->setWidth(8, 2);
$form->password('password', trans('admin.password'))->rules('confirmed|required')
->setGroupClass('col-md-6')
->setWidth(8, 2);
$form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')
->default(function ($form) {
return $form->model()->password;
})
->setGroupClass('col-md-6')
->setWidth(8, 2);
$form->image('avatar', trans('admin.avatar'))
->setGroupClass('col-md-12')
->setWidth(11, 1);
$form->setAction(admin_url('auth/setting'));
$form->ignore(['password_confirmation']);
$form->saving(function (Form $form) {
if ($form->password && $form->model()->password != $form->password) {
$form->password = bcrypt($form->password);
}
});
$form->saved(function () {
admin_toastr(trans('admin.update_succeeded'));
return redirect(admin_url('auth/setting'));
});
return $form;
}
/**
* Send the response after the user was authenticated.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
protected function sendLoginResponse(Request $request)
{
admin_toastr(trans('admin.login_successful'));
$request->session()->regenerate();
// add By Richer 于 2019年6月17日15:09:46 将登录时间记录
$user = Admin::user();
$user->login_times = $user->login_times + 1;
$user->last_login_time = date('Y-m-d H:i:s');
$user->last_login_ip = $request->getClientIp();
$user->save();
return redirect()->intended($this->redirectPath());
}
}