Update source into github
This commit is contained in:
@@ -0,0 +1,408 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\frontend;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Facade\SignRSA;
|
||||
use App\Http\Helpers\Helper;
|
||||
use App\Http\Requests\User\UserRegisterRequest;
|
||||
use App\Http\Requests\User\UserStoreInformation;
|
||||
use App\Jobs\Mail\CustomerRegister;
|
||||
use App\Jobs\Mail\PasswordRecoverJob;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Mail;
|
||||
use App\Models\ModuleSnapshotLimit;
|
||||
use App\Repositories\Customer\CustomerLogRepositoryInterface;
|
||||
use App\Repositories\Customer\CustomerRepositoryInterface;
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cookie;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
public CustomerRepositoryInterface $customerRepository,
|
||||
public CustomerLogRepositoryInterface $customerLogRepository
|
||||
)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function Login()
|
||||
{
|
||||
return view("frontend.auth.main");
|
||||
}
|
||||
|
||||
public function LoginStore(Request $request)
|
||||
{
|
||||
$request->validate(
|
||||
[
|
||||
'email' => 'required',
|
||||
'password' => 'required'
|
||||
],
|
||||
[
|
||||
'email' => [
|
||||
'required' => 'Vui lòng nhập Email'
|
||||
],
|
||||
'password' => [
|
||||
'required' => 'Vui lòng nhập Mật Khẩu'
|
||||
]
|
||||
]
|
||||
);
|
||||
$customer = Customer::where('email', $request->email);
|
||||
|
||||
if (!$customer) {
|
||||
return back()->withInput()->withErrors('Địa chỉ email không tồn tại');
|
||||
}
|
||||
|
||||
if (
|
||||
auth('users')->attempt(
|
||||
['email' => $request->email, 'password' => $request->password],
|
||||
$request->remember
|
||||
)
|
||||
) {
|
||||
|
||||
if (auth('users')->user()->status == 2) {
|
||||
|
||||
auth('users')->logout();
|
||||
|
||||
return back()->withInput()->withErrors('Vui lòng kích hoạt tài khoản để tiếp tục sử dụng dịch vụ.');
|
||||
}
|
||||
if (auth('users')->user()->status == 0) {
|
||||
|
||||
auth('users')->logout();
|
||||
|
||||
return back()->withInput()->withErrors('Tài khoản bị khoá, Vui lòng liên hệ admin để được hỗ trợ.');
|
||||
}
|
||||
|
||||
$request->session()->regenerate();
|
||||
|
||||
$token = \auth('users')->user()->createToken('access_token', ['*'], Carbon::now()->addDay())->plainTextToken;
|
||||
|
||||
Cookie::queue('aut_accessToken', $token, 24 * 60);
|
||||
|
||||
$this->customerLogRepository->create([
|
||||
'customer_id' => auth('users')->id(),
|
||||
'client_ip' => $request->getClientIp(),
|
||||
'agent' => $request->userAgent(),
|
||||
'action' => 'Đăng nhập'
|
||||
]);
|
||||
|
||||
if (Cookie::has('prev_url') && Cookie::get('prev_url') !== route('home')) {
|
||||
Cookie::queue(Cookie::forget('prev_url'));
|
||||
return redirect(Cookie::get('prev_url'));
|
||||
}
|
||||
|
||||
return redirect()->route('home')->with('success', 'Đăng nhập tài khoản thành công!');
|
||||
}
|
||||
return back()->withInput()->withErrors('Thông tin đăng nhập không chính xác!');
|
||||
}
|
||||
|
||||
public function Logout()
|
||||
{
|
||||
$this->customerLogRepository->create([
|
||||
'customer_id' => auth('users')->id(),
|
||||
'client_ip' => \request()->getClientIp(),
|
||||
'agent' => \request()->userAgent(),
|
||||
'action' => 'Đăng xuất'
|
||||
]);
|
||||
|
||||
Cookie::queue(Cookie::forget('aut_accessToken'));
|
||||
|
||||
auth('users')->user()->tokens()->delete();
|
||||
|
||||
auth('users')->logout();
|
||||
|
||||
return redirect()->route('user.login')->with('success', 'Đăng xuất tài khoản thành công!');
|
||||
}
|
||||
|
||||
public function Information()
|
||||
{
|
||||
$title = "Thông tin tài khoản";
|
||||
$customer = auth('users')->user();
|
||||
|
||||
return view("frontend.user.account", compact('title', 'customer'));
|
||||
}
|
||||
|
||||
public function StoreInformation(UserStoreInformation $request)
|
||||
{
|
||||
$validated = $request->validated();
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$user = user();
|
||||
$dataToUpdate = collect($validated)->except(['_token', '_method', 'email'])->toArray();
|
||||
$path = 'uploads/customers/' . $user->id . '/documents';
|
||||
|
||||
$fileFields = ['front_cccd', 'back_cccd', 'avatar'];
|
||||
|
||||
foreach ($fileFields as $field) {
|
||||
if ($request->hasFile($field)) {
|
||||
$file = $request->file($field);
|
||||
$filename = $field . '_' . $user->id . '_' . time() . '.' . $file->getClientOriginalExtension();
|
||||
|
||||
foreach (Storage::disk('public')->files($path) as $existingFile) {
|
||||
if (Str::startsWith(basename($existingFile), $field . '_')) {
|
||||
Storage::disk('public')->delete($existingFile);
|
||||
}
|
||||
}
|
||||
|
||||
$file->storeAs($path, $filename, 'public');
|
||||
$dataToUpdate[$field] = $path . '/' . $filename;
|
||||
}
|
||||
}
|
||||
|
||||
Customer::find($user->id)->update($dataToUpdate);
|
||||
|
||||
DB::commit();
|
||||
return back()->with('success', 'Cập nhật thông tin tài khoản thành công');
|
||||
} catch (Exception $exception) {
|
||||
DB::rollBack();
|
||||
return back()->withInput()->withErrors(['update_error' => $exception->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function updateNotification(Request $request)
|
||||
{
|
||||
$user = auth('users')->user();
|
||||
|
||||
$request->validate([
|
||||
'email_marketing' => 'boolean',
|
||||
'receive_deposit' => 'boolean',
|
||||
]);
|
||||
|
||||
$user->update([
|
||||
'email_marketing' => $request->input('email_marketing', false),
|
||||
'receive_deposit' => $request->input('receive_deposit', false),
|
||||
]);
|
||||
|
||||
return response()->json(['success' => true]);
|
||||
}
|
||||
|
||||
|
||||
public function Email()
|
||||
{
|
||||
$title = "Lịch sử Email";
|
||||
return view("frontend.email", compact('title'));
|
||||
}
|
||||
|
||||
public function ViewEmail(Request $request)
|
||||
{
|
||||
$validator = Validator::make(
|
||||
$request->all(),
|
||||
[
|
||||
'id' => 'required|numeric|not__in:0'
|
||||
],
|
||||
[
|
||||
'id' => [
|
||||
'required' => 'Vui lòng chọn Email'
|
||||
]
|
||||
]
|
||||
);
|
||||
if ($validator->fails()) {
|
||||
return back()->withErrors($validator);
|
||||
}
|
||||
$template = Mail::find($request->id);
|
||||
if ($template) {
|
||||
$title = $template->subject;
|
||||
return view("emails.render", compact('title', 'template'));
|
||||
}
|
||||
return back()->withErrors("Yêu cầu không hợp lệ");
|
||||
}
|
||||
|
||||
public function ChangePassword()
|
||||
{
|
||||
$title = "Thay đổi mật khẩu";
|
||||
$breadcrumbs = $this->crums->add(__('Dashboard'), route('home'))->add(__('Bảo mật tài khoản'));
|
||||
return view("frontend.user.password", compact('title', 'breadcrumbs'));
|
||||
}
|
||||
|
||||
public function StoreChangePassword(Request $request)
|
||||
{
|
||||
$validator = Validator::make(
|
||||
$request->all(),
|
||||
[
|
||||
"password" => 'required',
|
||||
"new_password" => 'required',
|
||||
'confirm_password' => 'required'
|
||||
],
|
||||
[
|
||||
"password" => [
|
||||
'required' => 'Vui lòng nhập mật khẩu!'
|
||||
],
|
||||
"new_password" => [
|
||||
'required' => 'Vui lòng nhập mật khẩu mới'
|
||||
],
|
||||
'confirm_password' => [
|
||||
'required' => 'Vui lòng nhập lại mật khẩu mới'
|
||||
]
|
||||
]
|
||||
);
|
||||
if ($validator->fails()) {
|
||||
return back()->withInput()->withErrors($validator);
|
||||
}
|
||||
|
||||
if (!Hash::check($request->password, $request->user('users')->password)) {
|
||||
return back()->withInput()->withErrors("Mật khẩu cũ không chính xác");
|
||||
}
|
||||
|
||||
if ($request->new_password !== $request->confirm_password) {
|
||||
return back()->withInput()->withErrors("Nhập lại mật khẩu mới không chính xác");
|
||||
}
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
Customer::find($request->user('users')->id)->update([
|
||||
'password' => Hash::make($request->new_password)
|
||||
]);
|
||||
DB::commit();
|
||||
} catch (Exception $exception) {
|
||||
DB::rollBack();
|
||||
return back()->withInput()->withErrors($exception->getMessage());
|
||||
}
|
||||
|
||||
return back()->with('success', 'Thay đổi mật khẩu thành công!');
|
||||
}
|
||||
|
||||
public function Register()
|
||||
{
|
||||
return view("frontend.register", [
|
||||
'title' => 'Đăng ký tài khoản'
|
||||
]);
|
||||
}
|
||||
|
||||
public function RegisterStore(UserRegisterRequest $request)
|
||||
{
|
||||
try {
|
||||
$validator = $request->validated();
|
||||
DB::beginTransaction();
|
||||
$code = Helper::generateUniqueCode();
|
||||
$id = Customer::insertGetId([
|
||||
'firstname' => $validator['firstname'],
|
||||
'lastname' => $validator['lastname'],
|
||||
'email' => $validator['email'],
|
||||
'phone' => $validator['phone'],
|
||||
'avatar' => '/images/avatar.png',
|
||||
'email_hash' => $code,
|
||||
'status' => 2,
|
||||
'password' => Hash::make($validator['password']),
|
||||
'created_at' => Carbon::now(),
|
||||
'updated_at' => Carbon::now(),
|
||||
]);
|
||||
|
||||
$jobs = new CustomerRegister(1, $code, $validator['email'], $id);
|
||||
dispatch($jobs)->delay(now()->addSeconds(10));
|
||||
|
||||
DB::commit();
|
||||
|
||||
} catch (Exception $exception) {
|
||||
DB::rollBack();
|
||||
return back()->withInput()->withErrors($exception->getMessage());
|
||||
}
|
||||
return redirect()->route('user.login')->with('success', 'Đăng ký thành công. Vui lòng kiểm tra xác thực Email!');
|
||||
}
|
||||
|
||||
public function Verify(Request $request)
|
||||
{
|
||||
$validator = Validator::make(
|
||||
$request->all(),
|
||||
[
|
||||
'verify' => 'required',
|
||||
],
|
||||
['verify.required' => 'Yêu cầu không hợp lệ']
|
||||
);
|
||||
if ($validator->fails()) {
|
||||
return redirect()->route('user.login')->withErrors($validator);
|
||||
}
|
||||
$code = base64_decode(SignRSA::DecryptFunc($request->verify));
|
||||
|
||||
$customer = Customer::where('email_hash', 'LIKE', "%$code%")->first();
|
||||
|
||||
if (!$customer) {
|
||||
return redirect()->route('user.login')->withErrors('Yêu cầu không hợp lệ');
|
||||
}
|
||||
|
||||
if ($customer->status == 1 || $customer->status == 0) {
|
||||
return redirect()->route('user.login')->withErrors('Yêu cầu không hợp lệ');
|
||||
}
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$customer->update([
|
||||
'status' => 1,
|
||||
'email_verify' => Carbon::now()
|
||||
]);
|
||||
ModuleSnapshotLimit::create([
|
||||
'customer_id' => $customer->id,
|
||||
'limit' => 1,
|
||||
'used' => 0
|
||||
]);
|
||||
DB::commit();
|
||||
} catch (Exception $exception) {
|
||||
DB::rollBack();
|
||||
return redirect()->route('user.login')->withErrors($exception->getMessage());
|
||||
}
|
||||
return redirect()->route('user.login')->with('success', 'Kích hoạt tài khoản thành công');
|
||||
}
|
||||
|
||||
public function Reset()
|
||||
{
|
||||
$title = "Quên mật khẩu";
|
||||
|
||||
return view("frontend.reset", compact('title'));
|
||||
}
|
||||
|
||||
public function ResetStore(Request $request)
|
||||
{
|
||||
|
||||
$request->validate(
|
||||
[
|
||||
'email' => 'required|email'
|
||||
],
|
||||
[
|
||||
'email' => [
|
||||
'required' => 'Vui lòng nhập địa chỉ email!',
|
||||
'email' => 'Định dạng email không hợp lệ'
|
||||
]
|
||||
]
|
||||
);
|
||||
|
||||
$user = Customer::where('email', $request->email)->first();
|
||||
if (!$user) {
|
||||
return back()->withErrors('Không tìm thấy thông tin người dùng');
|
||||
}
|
||||
|
||||
$password = Str::random(10);
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$user->update([
|
||||
'password' => Hash::make($password)
|
||||
]);
|
||||
|
||||
$data = [
|
||||
'company_name' => setting('setting.company'),
|
||||
'company_logo' => setting('setting.logo'),
|
||||
'password' => $password,
|
||||
];
|
||||
|
||||
$jobs = new PasswordRecoverJob($data, $user);
|
||||
|
||||
dispatch($jobs)->delay(now()->addSeconds(10));
|
||||
DB::commit();
|
||||
} catch (Exception $exception) {
|
||||
DB::rollBack();
|
||||
return back()->withErrors($exception->getMessage());
|
||||
}
|
||||
return redirect()->route('user.login')->with('success', "Thiết lập đặt lại mật khẩu thành công. Vui lòng kiểm tra email và thực hiện xác thực theo hướng dẫn.");
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user