2020-05-03 00:45:29 +01:00
< ? php
2020-10-10 16:30:26 +00:00
/*
* Copyright © 2020 Miguel Nogueira
*
* This file is part of Raspberry Staff Manager .
*
* Raspberry Staff Manager is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* Raspberry Staff Manager is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with Raspberry Staff Manager . If not , see < https :// www . gnu . org / licenses />.
*/
2020-05-03 00:45:29 +01:00
namespace App\Http\Controllers ;
2020-10-10 16:30:26 +00:00
use App\Ban ;
2022-10-24 01:01:10 +01:00
use App\Facades\IP ;
use App\Facades\Options ;
2020-10-10 16:30:26 +00:00
use App\Http\Requests\Add2FASecretRequest ;
2022-10-24 01:01:10 +01:00
use App\Http\Requests\AddDobRequest ;
use App\Http\Requests\BanUserRequest ;
2020-05-13 22:47:51 +01:00
use App\Http\Requests\ChangeEmailRequest ;
use App\Http\Requests\ChangePasswordRequest ;
2020-06-27 00:32:33 +01:00
use App\Http\Requests\DeleteUserRequest ;
2020-10-10 16:30:26 +00:00
use App\Http\Requests\FlushSessionsRequest ;
use App\Http\Requests\Remove2FASecretRequest ;
2022-10-24 01:01:10 +01:00
use App\Http\Requests\Reset2FASecretRequest ;
2020-06-27 00:32:33 +01:00
use App\Http\Requests\SearchPlayerRequest ;
2022-10-24 01:01:10 +01:00
use App\Http\Requests\SetNewPasswordRequest ;
2020-06-27 00:32:33 +01:00
use App\Http\Requests\UpdateUserRequest ;
2020-10-10 16:30:26 +00:00
use App\Notifications\ChangedPassword ;
use App\Notifications\EmailChanged ;
2022-10-24 01:01:10 +01:00
use App\Notifications\PasswordAdminResetNotification ;
use App\Notifications\TwoFactorResetNotification ;
use App\Services\AccountSuspensionService ;
use App\Services\DiscordService ;
2021-09-04 00:44:54 +01:00
use App\Traits\DisablesFeatures ;
2022-03-07 18:14:42 +00:00
use App\Traits\HandlesAccountDeletion ;
2020-10-10 16:30:26 +00:00
use App\Traits\ReceivesAccountTokens ;
2020-05-13 22:47:51 +01:00
use App\User ;
2020-10-10 16:30:26 +00:00
use Google2FA ;
2022-10-24 01:01:10 +01:00
use Illuminate\Contracts\Foundation\Application ;
use Illuminate\Http\Client\RequestException ;
use Illuminate\Http\RedirectResponse ;
2020-05-03 00:45:29 +01:00
use Illuminate\Http\Request ;
2022-10-24 01:01:10 +01:00
use Illuminate\Routing\Redirector ;
2020-05-13 22:47:51 +01:00
use Illuminate\Support\Facades\Auth ;
use Illuminate\Support\Facades\Hash ;
use Illuminate\Support\Facades\Log ;
2020-06-27 00:32:33 +01:00
use Spatie\Permission\Models\Role ;
2020-05-03 00:45:29 +01:00
class UserController extends Controller
{
2022-10-24 01:01:10 +01:00
use HandlesAccountDeletion , DisablesFeatures ;
2020-06-27 19:15:33 +01:00
2022-10-24 01:01:10 +01:00
/**
* Shows list of users
*
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\Contracts\View\View
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
2022-02-02 05:36:09 +00:00
public function showUsers ()
2020-05-03 00:45:29 +01:00
{
2020-06-27 19:15:33 +01:00
$this -> authorize ( 'viewPlayers' , User :: class );
2022-10-24 01:01:10 +01:00
return view ( 'dashboard.administration.users' )
2020-06-27 19:15:33 +01:00
-> with ([
2022-02-02 05:36:09 +00:00
'users' => User :: with ( 'roles' ) -> paginate ( '6' ),
'numUsers' => count ( User :: all ()),
2020-10-10 16:30:26 +00:00
'bannedUserCount' => Ban :: all () -> count (),
2020-06-27 19:15:33 +01:00
]);
2020-06-27 00:32:33 +01:00
}
2022-10-24 01:01:10 +01:00
/**
* Searches for a player with the given search query .
*
* @ deprecated Until Algolia implementation
* @ param SearchPlayerRequest $request
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse | \Illuminate\Routing\Redirector
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
2020-06-27 00:32:33 +01:00
public function showPlayersLike ( SearchPlayerRequest $request )
{
2020-06-27 19:15:33 +01:00
$this -> authorize ( 'viewPlayers' , User :: class );
2020-06-27 00:32:33 +01:00
2020-06-27 19:15:33 +01:00
$searchTerm = $request -> searchTerm ;
2020-06-27 00:32:33 +01:00
$matchingUsers = User :: query ()
-> where ( 'name' , 'LIKE' , " % { $searchTerm } % " )
-> orWhere ( 'email' , 'LIKE' , " % { $searchTerm } % " )
2022-03-06 12:56:41 +00:00
-> paginate ( 6 );
2020-06-27 00:32:33 +01:00
2020-10-10 16:30:26 +00:00
if ( ! $matchingUsers -> isEmpty ()) {
2021-07-20 10:32:43 +01:00
$request -> session () -> flash ( 'success' , __ ( 'There were :usersCount user(s) matching your search.' , [ 'usersCount' => $matchingUsers -> count ()]));
2020-06-27 00:32:33 +01:00
2022-10-24 01:01:10 +01:00
return view ( 'dashboard.administration.users' )
2020-06-27 00:32:33 +01:00
-> with ([
'users' => $matchingUsers ,
2022-02-22 19:46:18 +00:00
'numUsers' => count ( User :: all ()),
2020-10-10 16:30:26 +00:00
'bannedUserCount' => Ban :: all () -> count (),
2020-06-27 00:32:33 +01:00
]);
2020-10-10 16:30:26 +00:00
} else {
2021-07-20 10:32:43 +01:00
$request -> session () -> flash ( 'error' , __ ( 'Your search term did not return any results.' ));
2020-10-10 16:30:26 +00:00
2020-06-27 00:32:33 +01:00
return redirect ( route ( 'registeredPlayerList' ));
}
2020-05-03 00:45:29 +01:00
}
2020-05-13 22:47:51 +01:00
2022-10-24 01:01:10 +01:00
/**
* Shows the user account ' s settings page
*
* @ param Request $request
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\Contracts\View\View
* @ throws \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException
* @ throws \PragmaRX\Google2FA\Exceptions\InvalidCharactersException
* @ throws \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException
*/
2020-07-17 22:44:10 +01:00
public function showAccount ( Request $request )
2020-05-13 22:47:51 +01:00
{
2020-07-17 22:44:10 +01:00
$QRCode = null ;
2020-10-10 16:30:26 +00:00
if ( ! $request -> user () -> has2FA ()) {
if ( $request -> session () -> has ( 'twofaAttemptFailed' )) {
2020-07-17 22:44:10 +01:00
$twoFactorSecret = $request -> session () -> get ( 'current2FA' );
2020-10-10 16:30:26 +00:00
} else {
2020-07-17 22:44:10 +01:00
$twoFactorSecret = Google2FA :: generateSecretKey ( 32 , '' );
$request -> session () -> put ( 'current2FA' , $twoFactorSecret );
}
$QRCode = Google2FA :: getQRCodeInline (
config ( 'app.name' ),
$request -> user () -> email ,
$twoFactorSecret
);
}
2020-05-13 22:47:51 +01:00
return view ( 'dashboard.user.profile.useraccount' )
2020-07-17 22:44:10 +01:00
-> with ( 'ip' , request () -> ip ())
-> with ( 'twofaQRCode' , $QRCode );
2020-05-13 22:47:51 +01:00
}
2022-10-24 01:01:10 +01:00
/**
* Show account management screen
*
* @ param AccountSuspensionService $suspensionService
* @ param Request $request
* @ param User $user
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\View\Factory | \Illuminate\Contracts\View\View
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
public function showAcocuntManagement ( AccountSuspensionService $suspensionService , Request $request , User $user )
{
$this -> authorize ( 'adminEdit' , $user );
$systemRoles = Role :: all () -> pluck ( 'name' ) -> all ();
$userRoles = $user -> roles -> pluck ( 'name' ) -> all ();
$roleList = [];
foreach ( $systemRoles as $role ) {
if ( in_array ( $role , $userRoles )) {
$roleList [ $role ] = true ;
} else {
$roleList [ $role ] = false ;
}
}
return view ( 'dashboard.user.manage' )
-> with ([
'user' => $user ,
'roles' => $roleList ,
'isVerified' => $user -> isVerified (),
'isLocked' => $suspensionService -> isLocked ( $user ),
'isSuspended' => $suspensionService -> isSuspended ( $user ),
'hasDiscord' => $user -> hasDiscordConnection (),
'hasPassword' => $user -> hasPassword (),
'requireLicense' => Options :: getOption ( 'requireGameLicense' ),
'suspensionReason' => $suspensionService -> getSuspensionReason ( $user ),
'suspensionDuration' => $suspensionService -> getSuspensionDuration ( $user ),
'has2FA' => $user -> has2FA (),
'applications' => $user -> applications () -> get ()
]);
}
/**
* Log out other sessions for the current user
*
* @ param FlushSessionsRequest $request
* @ return \Illuminate\Http\RedirectResponse
* @ throws \Illuminate\Auth\AuthenticationException
*/
2020-05-13 22:47:51 +01:00
public function flushSessions ( FlushSessionsRequest $request )
{
// TODO: Move all log calls to a listener, which binds to an event fired by each significant event, such as this one
// This will allow for other actions to be performed on certain events (like login failed event)
Auth :: logoutOtherDevices ( $request -> currentPasswordFlush );
2020-10-10 16:30:26 +00:00
Log :: notice ( 'User ' . Auth :: user () -> name . ' has logged out other devices in their account' ,
2020-05-13 22:47:51 +01:00
[
'originIPAddress' => $request -> ip (),
'userID' => Auth :: user () -> id ,
2020-10-10 16:30:26 +00:00
'timestamp' => now (),
2020-05-13 22:47:51 +01:00
]);
2021-07-20 10:32:43 +01:00
$request -> session () -> flash ( 'success' , __ ( 'Successfully logged out other devices. Remember to change your password if you think you\'ve been compromised.' ));
2020-10-10 16:30:26 +00:00
2020-05-13 22:47:51 +01:00
return redirect () -> back ();
}
2022-10-24 01:01:10 +01:00
/**
* Change the current user ' s password
*
* @ param ChangePasswordRequest $request
* @ return \Illuminate\Http\RedirectResponse | void
*/
2020-05-13 22:47:51 +01:00
public function changePassword ( ChangePasswordRequest $request )
{
2021-09-04 00:44:54 +01:00
if ( config ( 'demo.is_enabled' )) {
return redirect ()
-> back ()
2022-03-31 16:54:39 +01:00
-> with ( 'error' , __ ( 'This feature is disabled' ));
2021-09-04 00:44:54 +01:00
}
2020-05-13 22:47:51 +01:00
$user = User :: find ( Auth :: user () -> id );
2020-10-10 16:30:26 +00:00
if ( ! is_null ( $user )) {
2020-05-13 22:47:51 +01:00
$user -> password = Hash :: make ( $request -> newPassword );
2021-01-06 05:03:38 +00:00
$user -> password_last_updated = now ();
2021-07-20 00:35:03 +01:00
2020-05-13 22:47:51 +01:00
$user -> save ();
2020-10-10 16:30:26 +00:00
Log :: info ( 'User ' . $user -> name . ' has changed their password' , [
2020-05-13 22:47:51 +01:00
'originIPAddress' => $request -> ip (),
'userID' => $user -> id ,
2020-10-10 16:30:26 +00:00
'timestamp' => now (),
2020-05-13 22:47:51 +01:00
]);
2020-06-27 00:32:33 +01:00
$user -> notify ( new ChangedPassword ());
2020-05-13 22:47:51 +01:00
2020-06-27 00:32:33 +01:00
Auth :: logout ();
2020-10-10 16:30:26 +00:00
2020-05-13 22:47:51 +01:00
return redirect () -> back ();
}
}
2022-10-24 01:01:10 +01:00
/**
* Sets a new password for the user .
*
* @ param SetNewPasswordRequest $request
* @ return Application | RedirectResponse | Redirector
*/
public function setPassword ( SetNewPasswordRequest $request ) {
if ( ! Auth :: user () -> hasPassword ()) {
Auth :: user () -> password = Hash :: make ( $request -> newpass );
Auth :: user () -> save ();
Auth :: logout ();
$request -> session () -> invalidate ();
$request -> session () -> regenerateToken ();
return redirect ( route ( 'login' ));
}
return redirect ()
-> back ()
-> with ( 'error' , __ ( 'Your account already has a password.' ));
}
/**
* Sets a user ' s password and removes their discord information from storage
*
* @ param User $user
* @ param SetNewPasswordRequest $request
* @ return \Illuminate\Contracts\Foundation\Application | \Illuminate\Http\RedirectResponse | \Illuminate\Routing\Redirector
*/
public function unlinkDiscordAccount ( Request $request , DiscordService $discordService )
2020-05-13 22:47:51 +01:00
{
2022-10-24 01:01:10 +01:00
if ( $request -> user () -> hasPassword ()) {
try {
$discordService -> revokeAccountTokens ( Auth :: user ());
Log :: warning ( 'Revoking social account tokens, user initiated' , [
'user' => Auth :: user () -> email
]);
} catch ( RequestException $requestException ) {
if ( $requestException -> getCode () == 401 ) {
return redirect ( route ( 'discordRedirect' ));
}
Log :: error ( 'Error while trying to revoke Discord credentials' , [ $requestException -> getMessage ()]);
return redirect ()
-> back ()
-> with ( 'error' , __ ( 'An unknown error ocurred. Please try again later.' ));
}
$request -> session () -> flash ( 'success' , __ ( 'Discord account unlinked successfully. Link it again by re-authorizing the app with the same account in the login screen, or through your account settings.' ));
return redirect () -> back ();
2021-09-04 00:44:54 +01:00
}
2022-10-24 01:01:10 +01:00
return redirect ()
-> back ()
-> with ( 'error' , __ ( 'Please set a password for your account first before trying to unlink Discord.' ));
}
/**
* Change the current user ' s email address
*
* @ param ChangeEmailRequest $request
* @ return \Illuminate\Http\RedirectResponse
*/
public function changeEmail ( ChangeEmailRequest $request )
{
$this -> disable ();
2020-05-13 22:47:51 +01:00
$user = User :: find ( Auth :: user () -> id );
2020-10-10 16:30:26 +00:00
if ( ! is_null ( $user )) {
2020-05-13 22:47:51 +01:00
$user -> email = $request -> newEmail ;
$user -> save ();
2020-10-10 16:30:26 +00:00
Log :: notice ( 'User ' . $user -> name . ' has just changed their contact email address' , [
2020-05-13 22:47:51 +01:00
'originIPAddress' => $request -> ip (),
'userID' => $user -> id ,
2020-10-10 16:30:26 +00:00
'timestamp' => now (),
2020-05-13 22:47:51 +01:00
]);
2020-06-27 00:32:33 +01:00
$user -> notify ( new EmailChanged ());
2020-05-13 22:47:51 +01:00
2021-07-20 00:35:03 +01:00
$request -> session () -> flash ( 'success' , __ ( 'Your email address has been changed!' ));
2020-10-10 16:30:26 +00:00
} else {
2021-07-20 00:35:03 +01:00
$request -> session () -> flash ( 'error' , __ ( 'There has been an error whilst trying to update your account. Please contact administrators.' ));
2020-05-13 22:47:51 +01:00
}
return redirect () -> back ();
}
2020-06-27 00:32:33 +01:00
2022-10-24 01:01:10 +01:00
/**
* Removes the user ' s password and notifies them .
*
* @ param User $user The user to remove the password for
* @ return \Illuminate\Http\RedirectResponse
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
public function forcePasswordReset ( User $user ) {
$this -> authorize ( 'adminEdit' , $user );
if ( $user -> hasPassword ()) {
$user -> notify ( new PasswordAdminResetNotification ());
$user -> password = null ;
$user -> save ();
Log :: alert ( " Removed account password " , [
'target' => $user ,
'actor' => Auth :: user ()
]);
2021-09-04 00:44:54 +01:00
return redirect ()
-> back ()
2022-10-24 01:01:10 +01:00
-> with ( 'success' , __ ( 'Account password removed.' ));
2021-09-04 00:44:54 +01:00
}
2022-10-24 01:01:10 +01:00
return redirect ()
-> back ()
-> with ( 'error' , __ ( 'This user doesn\'t have a password to reset.' ));
}
/**
* Adds a user 's date of birth if they don' t have one .
*
* @ param AddDobRequest $request
* @ return RedirectResponse
*/
public function addDob ( AddDobRequest $request ) {
Auth :: user () -> dob = $request -> dob ;
Auth :: user () -> save ();
return redirect ()
-> back ();
}
/**
* Delete the given user ' s account
*
* @ param DeleteUserRequest $request
* @ param User $user
* @ return \Illuminate\Http\RedirectResponse
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
public function delete ( DeleteUserRequest $request , User $user )
{
$this -> disable ();
2020-07-16 21:21:28 +01:00
$this -> authorize ( 'delete' , $user );
2020-10-10 16:30:26 +00:00
if ( $request -> confirmPrompt == 'DELETE ACCOUNT' ) {
2021-10-25 02:08:32 +01:00
$user -> delete ();
2021-07-20 00:35:03 +01:00
$request -> session () -> flash ( 'success' , __ ( 'User deleted successfully.' ));
2020-10-10 16:30:26 +00:00
} else {
2021-07-20 00:35:03 +01:00
$request -> session () -> flash ( 'error' , __ ( 'Wrong confirmation text! Try again.' ));
2020-06-27 00:32:33 +01:00
}
return redirect () -> route ( 'registeredPlayerList' );
}
2022-10-24 01:01:10 +01:00
/**
* Update a given user ' s details
*
* @ param UpdateUserRequest $request
* @ param User $user
* @ return \Illuminate\Http\RedirectResponse
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
2020-06-27 00:32:33 +01:00
public function update ( UpdateUserRequest $request , User $user )
{
2020-10-10 16:30:26 +00:00
$this -> authorize ( 'adminEdit' , $user );
2022-10-24 01:01:10 +01:00
$this -> disable ();
2020-06-27 00:32:33 +01:00
2020-10-10 16:30:26 +00:00
// Mass update would not be possible here without extra code, making route model binding useless
$user -> email = $request -> email ;
$user -> name = $request -> name ;
$user -> uuid = $request -> uuid ;
2020-06-27 00:32:33 +01:00
2020-10-10 16:30:26 +00:00
$existingRoles = Role :: all ()
2021-07-20 10:32:43 +01:00
-> pluck ( 'name' )
-> all ();
2020-06-27 00:32:33 +01:00
2020-10-10 16:30:26 +00:00
$roleDiff = array_diff ( $existingRoles , $request -> roles );
2020-06-27 00:32:33 +01:00
2020-10-10 16:30:26 +00:00
// Adds roles that were selected. Removes roles that aren't selected if the user has them.
foreach ( $roleDiff as $deselectedRole ) {
if ( $user -> hasRole ( $deselectedRole ) && $deselectedRole !== 'user' ) {
$user -> removeRole ( $deselectedRole );
}
2020-06-27 00:32:33 +01:00
}
2020-10-10 16:30:26 +00:00
foreach ( $request -> roles as $role ) {
if ( ! $user -> hasRole ( $role )) {
$user -> assignRole ( $role );
}
2020-06-27 00:32:33 +01:00
}
2020-10-10 16:30:26 +00:00
$user -> save ();
2021-07-20 10:32:43 +01:00
$request -> session () -> flash ( 'success' , __ ( 'User updated successfully!' ));
2020-06-27 00:32:33 +01:00
2020-10-10 16:30:26 +00:00
return redirect () -> back ();
2020-06-27 00:32:33 +01:00
}
2022-10-24 01:01:10 +01:00
/**
* Generate and add a 2 FA secret for the current user
*
* @ param Add2FASecretRequest $request
* @ return \Illuminate\Http\RedirectResponse
* @ throws \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException
* @ throws \PragmaRX\Google2FA\Exceptions\InvalidCharactersException
* @ throws \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException
*/
2020-07-17 22:44:10 +01:00
public function add2FASecret ( Add2FASecretRequest $request )
{
2021-09-04 00:44:54 +01:00
if ( config ( 'demo.is_enabled' )) {
return redirect ()
-> back ()
2022-03-31 16:54:39 +01:00
-> with ( 'error' , __ ( 'This feature is disabled' ));
2021-09-04 00:44:54 +01:00
}
2020-07-17 22:44:10 +01:00
$currentSecret = $request -> session () -> get ( 'current2FA' );
$isValid = Google2FA :: verifyKey ( $currentSecret , $request -> otp );
2020-10-10 16:30:26 +00:00
if ( $isValid ) {
$request -> user () -> twofa_secret = $currentSecret ;
$request -> user () -> save ();
2020-07-17 22:44:10 +01:00
2020-10-10 16:30:26 +00:00
Log :: warning ( 'SECURITY: User activated two-factor authentication' , [
'initiator' => $request -> user () -> email ,
'ip' => $request -> ip (),
]);
2020-07-17 22:44:10 +01:00
2020-10-10 16:30:26 +00:00
Google2FA :: login ();
2020-07-17 22:44:10 +01:00
2020-10-10 16:30:26 +00:00
Log :: warning ( 'SECURITY: Started two factor session automatically' , [
'initiator' => $request -> user () -> email ,
'ip' => $request -> ip (),
]);
2020-07-17 22:44:10 +01:00
2020-10-10 16:30:26 +00:00
$request -> session () -> forget ( 'current2FA' );
2020-07-17 22:44:10 +01:00
2020-10-10 16:30:26 +00:00
if ( $request -> session () -> has ( 'twofaAttemptFailed' )) {
$request -> session () -> forget ( 'twofaAttemptFailed' );
}
2020-07-17 22:44:10 +01:00
2021-07-20 00:35:03 +01:00
$request -> session () -> flash ( 'success' , __ ( '2FA succesfully enabled! You\'ll now be prompted for an OTP each time you log in.' ));
2020-10-10 16:30:26 +00:00
} else {
2021-07-20 00:35:03 +01:00
$request -> session () -> flash ( 'error' , __ ( 'Incorrect code. Please reopen the 2FA settings panel and try again.' ));
2020-10-10 16:30:26 +00:00
$request -> session () -> put ( 'twofaAttemptFailed' , true );
2020-07-17 22:44:10 +01:00
}
return redirect () -> back ();
}
2022-10-24 01:01:10 +01:00
/**
* Remove the current user ' s two factor secret key
*
* @ param Remove2FASecretRequest $request
* @ return \Illuminate\Http\RedirectResponse
*/
2020-07-17 22:44:10 +01:00
public function remove2FASecret ( Remove2FASecretRequest $request )
{
Log :: warning ( 'SECURITY: Disabling two factor authentication (user initiated)' , [
2020-10-10 16:30:26 +00:00
'initiator' => $request -> user () -> email ,
'ip' => $request -> ip (),
2020-07-17 22:44:10 +01:00
]);
$request -> user () -> twofa_secret = null ;
$request -> user () -> save ();
2021-07-20 00:35:03 +01:00
$request -> session () -> flash ( 'success' , __ ( 'Two-factor authentication disabled.' ));
2020-10-10 16:30:26 +00:00
2020-07-17 22:44:10 +01:00
return redirect () -> back ();
}
2022-10-24 01:01:10 +01:00
/**
* Remove the given user ' s two factor secret key
*
* @ param Reset2FASecretRequest $request
* @ param User $user
* @ return \Illuminate\Http\RedirectResponse
*/
public function reset2FASecret ( Reset2FASecretRequest $request , User $user ) {
// note: could invalidate other sessions for increased security
if ( $user -> has2FA ()) {
Log :: warning ( 'SECURITY: Disabling two factor authentication (admin initiated)' , [
'initiator' => $request -> user () -> email ,
'target' => $user -> email ,
'ip' => $request -> ip (),
]);
$user -> twofa_secret = null ;
$user -> password = null ;
$user -> save ();
$user -> notify ( new TwoFactorResetNotification ());
2021-09-04 00:44:54 +01:00
return redirect ()
-> back ()
2022-10-24 01:01:10 +01:00
-> with ( 'success' , __ ( 'Two factor removed & user notified.' ));
2021-09-04 00:44:54 +01:00
}
2020-06-27 00:32:33 +01:00
2022-10-24 01:01:10 +01:00
return redirect ()
-> back ()
-> with ( 'error' , 'This user does not have two-factor authentication enabled.' );
}
2020-10-10 16:30:26 +00:00
2022-10-24 01:01:10 +01:00
/**
* Suspend the given user
*
* @ param AccountSuspensionService $suspensionService
* @ param BanUserRequest $request
* @ param User $user
* @ return \Illuminate\Http\RedirectResponse
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
public function suspend ( AccountSuspensionService $suspensionService , BanUserRequest $request , User $user )
{
$this -> authorize ( 'create' , [ Ban :: class , $user ]);
$this -> disable ();
2020-06-27 00:32:33 +01:00
2022-10-24 01:01:10 +01:00
if ( $suspensionService -> isSuspended ( $user ))
{
return redirect ()
-> back ()
-> with ( 'error' , __ ( 'Account already suspended.' ));
}
2020-06-27 00:32:33 +01:00
2022-10-24 01:01:10 +01:00
if ( $request -> suspensionType = " on " ) {
$suspensionService -> suspend ( $user , $request -> reason , $request -> duration );
}
else {
$suspensionService -> suspend ( $user , $request -> reason );
2020-06-27 00:32:33 +01:00
}
2022-10-24 01:01:10 +01:00
return redirect () -> back ();
}
/**
* Unsuspend the given user
*
* @ param AccountSuspensionService $suspensionService
* @ param Request $request
* @ param User $user
* @ return \Illuminate\Http\RedirectResponse
* @ throws \Illuminate\Auth\Access\AuthorizationException
*/
public function unsuspend ( AccountSuspensionService $suspensionService , Request $request , User $user )
{
$this -> authorize ( 'delete' , $user -> bans );
$this -> disable ();
if ( $suspensionService -> isSuspended ( $user )) {
$suspensionService -> unsuspend ( $user );
$request -> session () -> flash ( 'success' , __ ( 'Account unsuspended successfully!' ));
} else {
$request -> session () -> flash ( 'error' , __ ( 'This account isn\'t suspended!' ));
}
2020-06-27 00:32:33 +01:00
return redirect () -> back ();
}
2022-10-24 01:01:10 +01:00
2020-05-03 00:45:29 +01:00
}