-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nguyen Tan Dung
committed
Dec 12, 2023
1 parent
c42480e
commit cc3eeba
Showing
27 changed files
with
723 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\admin\ProfileRequest; | ||
use App\Http\Services\Admin\ProfileService; | ||
use Illuminate\Http\Request; | ||
use Illuminate\View\View; | ||
|
||
class ProfileController extends Controller | ||
{ | ||
protected ProfileService $profileService; | ||
|
||
public function __construct() | ||
{ | ||
$this->profileService = new ProfileService(); | ||
} | ||
|
||
public function index(): View | ||
{ | ||
$user = auth()->user(); | ||
return view("admin.profile", compact("user")); | ||
} | ||
|
||
public function save(ProfileRequest $request) | ||
{ | ||
$data = $request->validated(); | ||
|
||
$result = $this->profileService->save($data); | ||
|
||
if ($result) | ||
return redirect() | ||
->route("admin.index") | ||
->with("success", trans("Update Successfully")); | ||
$errors = session("errors"); | ||
$errors[] = "Update Failed"; | ||
return redirect()->back()->withErrors($errors); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Http\Repositories\Admin; | ||
|
||
|
||
use App\Models\User; | ||
use function Laravel\Prompts\password; | ||
|
||
class ProfileRepository | ||
{ | ||
protected User $user; | ||
|
||
public function __construct() | ||
{ | ||
$this->user = new User(); | ||
} | ||
|
||
public function save(array $data): bool | ||
{ | ||
try { | ||
$user = $this->user::query() | ||
->updateOrCreate([ | ||
"email" => auth()->user()->email, | ||
], $data); | ||
|
||
$user->save(); | ||
return true; | ||
} catch (\Exception $e) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\admin; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Support\Facades\Auth; | ||
use Lunaweb\RecaptchaV3\Facades\RecaptchaV3; | ||
|
||
class ProfileRequest extends FormRequest | ||
{ | ||
public function validationData(): array | ||
{ | ||
$original = parent::ValidationData(); | ||
|
||
if (isset($original["g-recaptcha-response"])){ | ||
$original["score"] = RecaptchaV3::verify($original["g-recaptcha-response"], 'register'); | ||
} | ||
|
||
return $original; | ||
} | ||
|
||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return Auth::check(); | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
"name" => ["required", "string", "min:5"], | ||
"phone" => ["required", "string", "min:5", "max:15"], | ||
"id_card" => ["required", "string", "min:5", "max:32"], | ||
"birthday" => ["nullable", "date",], | ||
"gender" => ["required", "integer", "min:0", "max:1"], | ||
"old_password" => ["nullable", "string", "min:3", "max:32"], | ||
"new_password" => ["nullable", "string", "min:3", "max:32", "different:old_password"], | ||
"g-recaptcha-response" => ["required"], | ||
"score" => ["required", "numeric", "min:0.5", "max:0.99"], | ||
]; | ||
} | ||
|
||
public function messages(): array | ||
{ | ||
return [ | ||
"string" => trans("Unknown error please try again"), | ||
"min:5" => trans("Fields must be more than 5 characters"), | ||
"phone.max:15" => trans("Fields must be less than 15 characters"), | ||
"max:32" => trans("Fields must be less than 32 characters"), | ||
"date" => trans("Fields must be a date"), | ||
"integer" => trans("Unknown error please try again"), | ||
"gender.min:0" => trans("Unknown error please try again"), | ||
"gender.max:1" => trans("Unknown error please try again"), | ||
"new_password.different" => trans("New password must be different from old password"), | ||
"score.required" => "Looks like you're spamming", | ||
"score.numeric" => "Looks like you are spamming", | ||
"score.min:0.5" => "Looks like you are spamming", | ||
"score.max:0.99" => "Looks like you are spamming", | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace App\Http\Services\Admin; | ||
|
||
use App\Http\Repositories\Admin\ProfileRepository; | ||
use Illuminate\Support\Facades\Hash; | ||
|
||
class ProfileService { | ||
protected ProfileRepository $profileRepository; | ||
|
||
public function __construct() | ||
{ | ||
$this->profileRepository = new ProfileRepository(); | ||
} | ||
|
||
public function save(array $data): bool | ||
{ | ||
try { | ||
if (isset($data['old_password']) && isset($data['new_password'])) | ||
{ | ||
if(! Hash::check($data['old_password'], auth()->user()->password)) | ||
{ | ||
session()->flash('errors', ['Old password is incorrect']); | ||
|
||
return false; | ||
} | ||
|
||
$data["password"] = Hash::make($data['new_password']); | ||
} | ||
|
||
$this->profileRepository->save($data); | ||
|
||
return true; | ||
} catch (\Exception $e) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
return [ | ||
'origin' => env('RECAPTCHAV3_ORIGIN', 'https://www.google.com/recaptcha'), | ||
'sitekey' => env('RECAPTCHAV3_SITEKEY', ''), | ||
'secret' => env('RECAPTCHAV3_SECRET', ''), | ||
'locale' => env('RECAPTCHAV3_LOCALE', '') | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.