235 lines
5.8 KiB
PHP
235 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Tool\Google;
|
|
use Encore\Admin\Auth\Database\Administrator;
|
|
use Encore\Admin\Controllers\AuthController as BaseAuthController;
|
|
use Encore\Admin\Facades\Admin;
|
|
use Encore\Admin\Form;
|
|
use Encore\Admin\Layout\Content;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Lang;
|
|
use Illuminate\Support\Facades\Redirect;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
use Earnp\GoogleAuthenticator\GoogleAuthenticator;
|
|
use App\System as SystemMysql;
|
|
|
|
class AuthController extends BaseAuthController
|
|
{
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $loginView = 'admin::login';
|
|
|
|
/**
|
|
* Show the login page.
|
|
*
|
|
* @return \Illuminate\Contracts\View\Factory|Redirect|\Illuminate\View\View
|
|
*/
|
|
public function getLogin()
|
|
{
|
|
if ($this->guard()->check()) {
|
|
return redirect($this->redirectPath());
|
|
}
|
|
|
|
return view($this->loginView);
|
|
}
|
|
|
|
/**
|
|
* Handle a login request.
|
|
*
|
|
* @param Request $request
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function postLogin(Request $request)
|
|
{
|
|
$this->loginValidator($request->all())->validate();
|
|
|
|
$secret = $request->input('secret');
|
|
|
|
$credentials = $request->only([$this->username(), 'password']);
|
|
$remember = $request->get('remember', false);
|
|
|
|
// 系统设置
|
|
$admin = Administrator::where(array("username" => $request->input('username')))->first();
|
|
|
|
$ga = new Google();
|
|
|
|
//if($ga->verifyCode($admin->secret, $secret,8)){
|
|
if ($this->guard()->attempt($credentials, $remember)) {
|
|
return $this->sendLoginResponse($request);
|
|
}
|
|
// }
|
|
|
|
return back()->withInput()->withErrors([
|
|
$this->username() => $this->getFailedLoginMessage(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get a validator for an incoming login request.
|
|
*
|
|
* @param array $data
|
|
*
|
|
* @return \Illuminate\Contracts\Validation\Validator
|
|
*/
|
|
protected function loginValidator(array $data)
|
|
{
|
|
return Validator::make($data, [
|
|
$this->username() => 'required',
|
|
'password' => 'required',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* User logout.
|
|
*
|
|
* @return Redirect
|
|
*/
|
|
public function getLogout(Request $request)
|
|
{
|
|
$this->guard()->logout();
|
|
|
|
$request->session()->invalidate();
|
|
|
|
return redirect(config('admin.route.prefix'));
|
|
}
|
|
|
|
/**
|
|
* User setting page.
|
|
*
|
|
* @param Content $content
|
|
*
|
|
* @return Content
|
|
*/
|
|
public function getSetting(Content $content)
|
|
{
|
|
$form = $this->settingForm();
|
|
$form->tools(
|
|
function (Form\Tools $tools) {
|
|
$tools->disableList();
|
|
$tools->disableDelete();
|
|
$tools->disableView();
|
|
}
|
|
);
|
|
|
|
return $content
|
|
->title(trans('admin.user_setting'))
|
|
->body($form->edit(Admin::user()->id));
|
|
}
|
|
|
|
/**
|
|
* Update user setting.
|
|
*
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
*/
|
|
public function putSetting()
|
|
{
|
|
return $this->settingForm()->update(Admin::user()->id);
|
|
}
|
|
|
|
/**
|
|
* Model-form for user setting.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function settingForm()
|
|
{
|
|
$class = config('admin.database.users_model');
|
|
|
|
$form = new Form(new $class());
|
|
|
|
$form->display('username', trans('admin.username'));
|
|
$form->text('name', trans('admin.name'))->rules('required');
|
|
$form->image('avatar', trans('admin.avatar'));
|
|
$form->password('password', trans('admin.password'))->rules('confirmed|required');
|
|
$form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')
|
|
->default(function ($form) {
|
|
return $form->model()->password;
|
|
});
|
|
|
|
$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 = Hash::make($form->password);
|
|
}
|
|
});
|
|
|
|
$form->saved(function () {
|
|
admin_toastr(trans('admin.update_succeeded'));
|
|
|
|
return redirect(admin_url('auth/setting'));
|
|
});
|
|
|
|
return $form;
|
|
}
|
|
|
|
/**
|
|
* @return string|\Symfony\Component\Translation\TranslatorInterface
|
|
*/
|
|
protected function getFailedLoginMessage()
|
|
{
|
|
return Lang::has('auth.failed')
|
|
? trans('auth.failed')
|
|
: 'These credentials do not match our records.';
|
|
}
|
|
|
|
/**
|
|
* Get the post login redirect path.
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function redirectPath()
|
|
{
|
|
if (method_exists($this, 'redirectTo')) {
|
|
return $this->redirectTo();
|
|
}
|
|
|
|
return property_exists($this, 'redirectTo') ? $this->redirectTo : config('admin.route.prefix');
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
|
|
return redirect()->intended($this->redirectPath());
|
|
}
|
|
|
|
/**
|
|
* Get the login username to be used by the controller.
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function username()
|
|
{
|
|
return 'username';
|
|
}
|
|
|
|
/**
|
|
* Get the guard to be used during authentication.
|
|
*
|
|
* @return \Illuminate\Contracts\Auth\StatefulGuard
|
|
*/
|
|
protected function guard()
|
|
{
|
|
return Admin::guard();
|
|
}
|
|
}
|