thinkstudeo /
laravel-rakshak
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Thinkstudeo\Rakshak\Middleware; |
||
| 4 | |||
| 5 | use Closure; |
||
| 6 | use Illuminate\Support\Carbon; |
||
| 7 | use Illuminate\Support\Facades\Auth; |
||
| 8 | use Illuminate\Support\Facades\Cache; |
||
| 9 | use Thinkstudeo\Rakshak\Rakshak; |
||
| 10 | use Thinkstudeo\Rakshak\RakshakSetting; |
||
| 11 | |||
| 12 | class VerifyTwoFactorOtp |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Handle the incoming request. |
||
| 16 | * |
||
| 17 | * @param Request $request |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 18 | * @param Closure $next |
||
| 19 | * @return void |
||
| 20 | */ |
||
| 21 | public function handle($request, Closure $next) |
||
| 22 | { |
||
| 23 | if ($this->is2faEnabled()) { |
||
| 24 | $user = Auth::user(); |
||
| 25 | if ($user->otp_expiry > Carbon::now()) { |
||
|
0 ignored issues
–
show
|
|||
| 26 | return $next($request); |
||
| 27 | } |
||
| 28 | |||
| 29 | $user->otp_token = Rakshak::generateOtp(); |
||
|
0 ignored issues
–
show
|
|||
| 30 | $user->save(); |
||
| 31 | |||
| 32 | Rakshak::sendOtp($user); |
||
| 33 | |||
| 34 | return redirect(Rakshak::verifyOtpPath()); |
||
|
0 ignored issues
–
show
|
|||
| 35 | } |
||
| 36 | |||
| 37 | return $next($request); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Determine whether the 2fa is enabled for the current user. |
||
| 42 | * |
||
| 43 | * @return bool |
||
| 44 | */ |
||
| 45 | private function is2faEnabled() |
||
| 46 | { |
||
| 47 | $enabled = config('rakshak.enable_2fa'); |
||
| 48 | // $controlLevel = RakshakSetting::first()->control_level_2fa; |
||
| 49 | $controlLevel = Cache::get('rakshak.control_level_2fa'); |
||
| 50 | |||
| 51 | if ($enabled && $controlLevel === 'admin') { |
||
| 52 | return true; |
||
| 53 | } |
||
| 54 | |||
| 55 | if ($enabled && $controlLevel === 'user') { |
||
| 56 | return Auth::user()->enable_2fa; |
||
|
0 ignored issues
–
show
|
|||
| 57 | } |
||
| 58 | } |
||
| 59 | } |
||
| 60 |