You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
3.2 KiB
91 lines
3.2 KiB
namespace App\Admin\Controllers;
|
|
|
|
use Encore\Admin\Auth\Database\Administrator; // 引入管理员模型
|
|
use Encore\Admin\Controllers\AuthController as BaseAuthController; // 继承 Encore\Admin 的 AuthController
|
|
use Encore\Admin\Facades\Admin; // 引入 Admin 门面
|
|
use Encore\Admin\Form; // 引入表单处理类
|
|
use Encore\Admin\Grid; // 引入 Grid 类
|
|
use Encore\Admin\Layout\Content; // 引入布局类
|
|
use Illuminate\Http\Request; // 引入请求类
|
|
use Illuminate\Support\Facades\Validator; // 引入验证器
|
|
|
|
class AuthController extends BaseAuthController
|
|
{
|
|
/**
|
|
* 覆盖默认的登录方法
|
|
*
|
|
* @param Request $request
|
|
* @return mixed
|
|
*/
|
|
public function postLogin(Request $request)
|
|
{
|
|
// 获取用户名和密码
|
|
$credentials = $request->only([$this->username(), 'password']);
|
|
|
|
/** @var \Illuminate\Validation\Validator $validator */
|
|
// 对输入的凭证进行验证
|
|
$validator = Validator::make($credentials, [
|
|
$this->username() => 'required', // 用户名为必填
|
|
'password' => 'required', // 密码为必填
|
|
]);
|
|
|
|
// 如果验证失败,返回错误信息
|
|
if ($validator->fails()) {
|
|
return back()->withInput()->withErrors($validator);
|
|
}
|
|
|
|
// 如果验证通过,尝试进行身份验证
|
|
if ($this->guard()->attempt($credentials)) {
|
|
// 登录成功后,执行身份验证后的操作(比如记录 IP 地址)
|
|
$this->authenticated($this->guard()->user());
|
|
|
|
// 记录登录日期并返回成功响应
|
|
return $this->sendLoginResponse($request);
|
|
}
|
|
|
|
// 如果身份验证失败,返回失败信息
|
|
return back()->withInput()->withErrors([
|
|
$this->username() => $this->getFailedLoginMessage(), // 获取失败的登录信息
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 登录之后提示 IP 地址
|
|
*
|
|
* @param Administrator $user
|
|
*/
|
|
protected function authenticated(Administrator $user)
|
|
{
|
|
$ip = request()->getClientIp(); // 获取当前登录用户的 IP 地址
|
|
|
|
// 如果当前登录 IP 与上次登录的 IP 不一致,提示风险
|
|
if (! is_null($user->login_ip) && $ip != $user->login_ip) {
|
|
admin_info('上一次登录的地址与本次不同,如果不是本人操作,建议及时修改密码');
|
|
}
|
|
|
|
// 更新用户的登录 IP 地址
|
|
$user->login_ip = $ip;
|
|
$user->save(); // 保存更新后的 IP 地址
|
|
}
|
|
|
|
/**
|
|
* 设置更新操作
|
|
*/
|
|
public function putSetting()
|
|
{
|
|
$form = $this->settingForm(); // 获取设置表单
|
|
|
|
// 表单提交时的处理逻辑
|
|
$form->submitted(function (Form $form) {
|
|
// 如果是开发环境,则禁止操作
|
|
if (app()->environment('dev')) {
|
|
admin_toastr('开发环境不允许操作', 'error'); // 显示错误提示
|
|
return back()->withInput(); // 返回上一页面并保留输入
|
|
}
|
|
});
|
|
|
|
// 执行设置更新
|
|
return $form->update(Admin::user()->id); // 更新当前登录用户的设置
|
|
}
|
|
}
|