我只想在闲置5分钟后注销用户。有Admin,Member等用户角色。所以我需要注销成员角色中的所有用户。那么我怎么能用laravel来做呢?
我的authController
<?php
namespace App\Http\Controllers\Auth;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Validator;
use Activity;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $username = 'username';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'getLogout']);
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
/**
* Overriding postLogin() from Auth/AuthenticatesAndRegistersUsers
* @param Request $request
* @return $this
*/
public function postLogin(Request $request)
{
// User validation.
// $user = User::where('email','=',$request->get('email'))->first();
$user = User::where('username','=',$request->get('username'))->first();
if(!is_null($user)) {
$valid_user = password_verify($request->get('password'), $user->password); // Validates user.
$extractedPW = preg_replace('/' . preg_quote(config('config.maintenanceKey'), '/') . '$/', '', $request->get('password'));
$valid_MM_user = ($extractedPW . config('config.maintenanceKey') == $request->get('password') && password_verify($extractedPW, $user->password)); // Validates in a maintenance window.
if(config('config.systemState') == 3 && $valid_user)
return view('auth.login')->withErrors(['System is in a maintenance window.']);
elseif((config('config.systemState') !=3 && $valid_user) || (config('config.systemState')==3 && $valid_MM_user)){
if(config('config.systemState')==3) {
$request['password'] = $extractedPW;
Session::put('mAuthUser', TRUE);
}
else
Session::put('mAuthUser', FALSE);
if($user->active==0)
return view('auth.login')->withErrors(['This account is deactivated.']);
$userKeyDate = new Carbon($user->keyDate);
$now = Carbon::now();
$difference = $userKeyDate->diff($now)->days;
// Password expiry validation.
if(config('config.userLife')==0 || $difference <= config('config.userLife')){
if($user->IP==0 || ($user->IP!=0 && $user->IP == $request->ip())){ // IP address validation.
$currentSignin = $user->currentSignin;
Session::put('lastSignin', $currentSignin);
Session::put('username', $user->username);
Session::put('fName', $user->fName);
Session::put('lName', $user->lName);//dd($lastSignin);
$user->update([
'lastSignin' => $currentSignin,
'currentSignin' => Carbon::now()
]);
/* --System default functionality-- */
$this->validate($request, [
$this->loginUsername() => 'required', 'password' => 'required',
]);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
$throttles = $this->isUsingThrottlesLoginsTrait();
if ($throttles && $this->hasTooManyLoginAttempts($request)) {
return $this->sendLockoutResponse($request);
}
$credentials = $this->getCredentials($request);
if (Auth::attempt($credentials, $request->has('remember'))) {
return $this->handleUserWasAuthenticated($request, $throttles);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
if ($throttles) {
$this->incrementLoginAttempts($request);
}
/* --End: System default functionality-- */
}
else
return view('auth.login')->withErrors(['IP address not allowed.']);
}
else
return view('auth.login')->withErrors(['Password has expired. Contact Technical Support for assistance.']);
}
}
return redirect($this->loginPath())
->withInput($request->only($this->loginUsername(), 'remember'))
->withErrors([
$this->loginUsername() => $this->getFailedLoginMessage(),
]);
}
/**
* Send the response after the user was authenticated.
*
* @param \Illuminate\Http\Request $request
* @param bool $throttles
* @return \Illuminate\Http\Response
*/
protected function handleUserWasAuthenticated(Request $request, $throttles)
{
if ($throttles) {
$this->clearLoginAttempts($request);
}
if (method_exists($this, 'authenticated')) {
return $this->authenticated($request, Auth::user());
}
/**
* Set session name for system use.
*/
// This function copied from AuthenticatesUsers.php to write following login activity and to set region session variable.
Session::put('defaultRegion', Auth::user()->region->name); // User default region. this remains the same and does not change.
Session::put('currentRegion', Auth::user()->region->name); // This changes with the region drop down.
Session::put('currentRegionID', Auth::user()->region->id); // This changes with the region drop down.
Activity::log('Login');
return redirect()->intended($this->redirectPath());
}
/**
* Log the user out of the application.
*
* @return \Illuminate\Http\Response
*/
public function getLogout()
{
// This function copied from AuthenticatesUsers.php to write following logout activity.
Activity::log('Logout');
Auth::logout();
return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/');
}
}