HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //var/www/admin.javaapp.co.uk/app/Services/ResetPasswordService.php
<?php

namespace App\Services;

use App\Exceptions\ResetPasswordAndPldPasswordShouldNotSame;
use App\Models\Admin;
use App\Models\Cafe;
use App\Models\PasswordReset;
use App\Models\User;
use Error;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class ResetPasswordService
{
    public function showResetForm($request)
    {
        $token = $request->token;
        $url = '';
        $where = [
            'token' => $token,
            'user_type' => $request['user-type'],
        ];
        $result = PasswordReset::where($where)->first();
        if (empty($result)) {
            session()->flash('error_message', __('auth.resetPasswordLinkExpired'));
        } else {
            $time = Carbon::now()->timestamp;
            if ($result->expired_at >= $time) {
                $url = route('reset.resetPassword', ['token' => $token, 'user_type' => $request['user-type']]);
            } else {
                session()->flash('error_message', __('auth.resetPasswordLinkExpired'));
            }
        }
        return view('custom_auth.reset-password')->with(
            ['url' => $url]
        );
    }

    private function checkPasswordShouldNotSame($resetPassword, $oldPassword)
    {
        if (Hash::check($resetPassword, $oldPassword)) {
            // throw new Error(__('common.resetPasswordOldPasswordShouldNotSame'));
            throw new ResetPasswordAndPldPasswordShouldNotSame(__('common.resetPasswordOldPasswordShouldNotSame'));
        }
    }

    public function resetPassword($request, $token, $user_type)
    {
        DB::beginTransaction();
        try {
            $where = [
                'token' => $token,
                'user_type' => $user_type,
            ];
            $result = PasswordReset::where($where)->first();
            if (empty($result)) {
                DB::rollback();
                session()->flash('error_message', __('common.resetPasswordOldPasswordShouldNotSame'));
                return redirect()->back();
            } else {
                $password = bcrypt($request->password);
                switch ($user_type) {
                    case config('constants.admin'):
                        $admin = Admin::where('email', $result->email)->first();
                        $this->checkPasswordShouldNotSame($request->password, $admin->password);
                        $resetPasswordResult = $admin->update(['password' => $password]);
                        break;

                    case config('constants.cafe'):
                        $cafe = Cafe::where('email', $result->email)->where('is_active', 1)->where('approved', 1)->where('deleted_at', 0)->first();
                        $this->checkPasswordShouldNotSame($request->password, $cafe->password);
                        $resetPasswordResult = $cafe->update(['password' => $password]);
                        break;

                    case config('constants.user'):
                        $user = User::where('email', $result->email)->where('is_active', 1)->where('is_verified', 1)->where('is_deleted', 0)->first();
                        $this->checkPasswordShouldNotSame($request->password, $user->password);
                        $resetPasswordResult = $user->update(['password' => $password]);
                        break;

                    default:
                        $resetPasswordResult = null;
                        break;
                }
                if (empty($resetPasswordResult)) {
                    DB::rollback();
                    session()->flash('error_message', __('common.somethingWentWrong'));
                    return redirect()->back();
                } else {
                    $result->delete();
                    switch ($user_type) {
                        case config('constants.admin'):
                            DB::commit();
                            session()->flash('success_message', __('auth.passwordResetSuccess'));
                            return redirect()->route('admin.login');
                            break;

                        case config('constants.cafe'):
                            DB::commit();
                            session()->flash('success_message', __('auth.passwordResetSuccess'));
                            return redirect()->route('cafe.login');
                            break;

                        case config('constants.user'):
                            DB::commit();
                            return redirect()->route('password-reset-success');
                            break;

                        default:
                            DB::rollback();
                            session()->flash('error_message', __('common.somethingWentWrong'));
                            return redirect()->back();
                            break;
                    }
                }
            }
        } catch (ResetPasswordAndPldPasswordShouldNotSame $e) {
            DB::rollback();
            session()->flash('error_message', $e->getMessage());
            return redirect()->back();
        } catch (\Throwable $th) {
            DB::rollback();
            // if ($th->getMessage() == __('common.resetPasswordOldPasswordShouldNotSame')) {
            //     session()->flash('error_message', __('common.resetPasswordOldPasswordShouldNotSame'));
            // } else {
                session()->flash('error_message', __('common.somethingWentWrong'));
            // }
            return redirect()->back();
        }
    }
}